通过wi-fi的Arduino伺服控制不';行不通
嗨,我正在使用arduino uno和wifi屏蔽。我正试图创建使用wifi屏蔽连接到伺服电机通过网络服务器。老实说,我真的不知道我在做什么。该代码能够控制伺服电机一次,整个系统关闭。 有人能检查一下我的代码,看看出了什么问题吗?如果可以,我希望代码能够控制伺服多次而不停止通过wi-fi的Arduino伺服控制不';行不通,arduino,android-wifi,Arduino,Android Wifi,嗨,我正在使用arduino uno和wifi屏蔽。我正试图创建使用wifi屏蔽连接到伺服电机通过网络服务器。老实说,我真的不知道我在做什么。该代码能够控制伺服电机一次,整个系统关闭。 有人能检查一下我的代码,看看出了什么问题吗?如果可以,我希望代码能够控制伺服多次而不停止 #include <SPI.h> #include <WiFi.h> #include <Servo.h> char ssid[] = "wifi";
#include <SPI.h>
#include <WiFi.h>
#include <Servo.h>
char ssid[] = "wifi"; // your network SSID (name)
char pass[] = "asdfghjkl"; // your network password
int keyIndex = 0;
// your network key Index number (needed only for WEP)
Servo myservo;
int status = WL_IDLE_STATUS;
WiFiServer server(80);
void setup() {
Serial.begin(9600); // initialize serial communication
myservo.attach(9);
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
while (true); // don't continue
}
String fv = WiFi.firmwareVersion();
if (fv != "1.1.0") {
Serial.println("Please upgrade the firmware");
}
// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(1000);
}
server.begin(); // start the web server on port 80
printWifiStatus(); // you're connected now, so print out the status
}
void loop() {
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("new client"); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
client.print("Click <a href=\"/H\">here</a> turn the Open the door <br>");
client.print("Click <a href=\"/L\">here</a> turn the Close the door <br>");
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
} else { // if you got a newline, then clear currentLine:
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H")) {
// scale it to use it with the servo (value between 0 and 180)
myservo.write(800); // sets the servo position according to the scaled value
delay(15); // GET /H turns the LED on
}
if (currentLine.endsWith("GET /L")) {
// scale it to use it with the servo (value between 0 and 180)
myservo.write(2500); // sets the servo position according to the scaled value
delay(15); // GET /L turns the LED off
}
}
}
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
// print where to go in a browser:
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
}
#包括
#包括
#包括
字符ssid[]=“wifi”//您的网络SSID(名称)
char pass[]=“asdfghjkl”;//您的网络密码
int-keyIndex=0;
//您的网络密钥索引号(仅WEP需要)
伺服myservo;
int状态=WL\U空闲\U状态;
WiFiServer服务器(80);
无效设置(){
Serial.begin(9600);//初始化串行通信
附件(9);
//检查是否存在屏蔽:
如果(WiFi.status()=无线无屏蔽){
Serial.println(“不存在WiFi屏蔽”);
while(true);//不要继续
}
字符串fv=WiFi.firmwareVersion();
如果(fv!=“1.1.0”){
Serial.println(“请升级固件”);
}
//尝试连接到Wifi网络:
while(状态!=WL\U已连接){
Serial.print(“正在尝试连接到名为:”)的网络;
Serial.println(ssid);//打印网络名称(ssid);
//连接到WPA/WPA2网络。如果使用open或WEP网络,请更改此行:
状态=WiFi.begin(ssid,通过);
//等待10秒钟以进行连接:
延迟(1000);
}
server.begin();//在端口80上启动web服务器
printWifiStatus();//您现在已连接,请打印状态
}
void循环(){
WiFiClient client=server.available();//侦听传入的客户端
如果(客户){//如果你有客户,
Serial.println(“新客户端”);//从串行端口打印消息
String currentLine=“;//生成一个字符串以保存来自客户端的传入数据
while(client.connected()){//在连接客户端时循环
如果(client.available()){//如果要从客户端读取字节,
char c=client.read();//读取一个字节,然后
Serial.write(c);//在串行监视器上打印出来
如果(c=='\n'){//如果字节是换行符
//如果当前行为空,则一行中有两个换行符。
//客户端HTTP请求到此结束,请发送响应:
如果(currentLine.length()==0){
//HTTP标头始终以响应代码开头(例如HTTP/1.1 200 OK)
//和一个内容类型,以便客户端知道接下来会发生什么,然后是一个空行:
client.println(“HTTP/1.1200ok”);
client.println(“内容类型:text/html”);
client.println();
//HTTP响应的内容如下所示:
client.print(“单击“打开门”
”);
client.print(“单击旋转门关闭
”;
//HTTP响应以另一个空行结束:
client.println();
//打破while循环:
打破
}否则{//如果有换行符,请清除currentLine:
currentLine=“”;
}
}else如果(c!='\r'){//如果您得到的不是回车字符,而是其他字符,
currentLine+=c;//将其添加到currentLine的末尾
}
//检查客户端请求是“GET/H”还是“GET/L”:
if(currentLine.endsWith(“GET/H”)){
//缩放以与伺服一起使用(值介于0和180之间)
myservo.write(800);//根据缩放值设置伺服位置
延迟(15);//GET/H打开LED
}
if(currentLine.endsWith(“GET/L”)){
//缩放以与伺服一起使用(值介于0和180之间)
myservo.write(2500);//根据缩放值设置伺服位置
延迟(15);//GET/L关闭LED
}
}
}
}
}
void printWifiStatus(){
//打印您连接到的网络的SSID:
序列号。打印(“SSID:”);
Serial.println(WiFi.SSID());
//打印您的WiFi屏蔽的IP地址:
ip地址ip=WiFi.localIP();
串行打印(“IP地址:”);
序列号println(ip);
//打印接收到的信号强度:
长rssi=WiFi.rssi();
串行打印(“信号强度(RSSI):”;
串行打印(rssi);
Serial.println(“dBm”);
//在浏览器中打印要转到的位置:
Serial.print(“要查看此页面的运行情况,请打开浏览器至http://”);
序列号println(ip);
}
欢迎,请考虑让你的整个帖子根本不让它变得大胆。你的问题到底是什么?Arduino是否耗电过多而关闭?或者这是一个软件问题,它不接受更多的请求?谢谢回复!!我不认为是电源问题,很可能是编码问题。我基于arduino库中的wifi控制LED构建此代码。在我运行编码后,伺服电机将激活一次,然后整个wifi屏蔽将自动关闭。使我无法连接到服务器。我需要刷新整个系统,以便它再次工作。欢迎,请考虑让你的整个帖子黑体基本上不让它大胆。你到底是什么问题?Arduino是否耗电过多而关闭?或者这是一个软件问题,它不接受更多的请求?谢谢回复!!我不认为是电源问题,很可能是编码问题。我基于arduino库中的wifi控制LED构建此代码。在我运行编码后,伺服电机将激活一次,然后整个wifi屏蔽将自动关闭。使我无法连接到服务器。我需要刷新整个系统