Arduino从web浏览器多次执行同一命令(使用WiServer.h)
我现在正在使用Arduino+WiFiShield创建一个门锁柜,它可以通过带有浏览器的便携式设备远程控制门锁。但我的程序似乎有一个bug,我不知道为什么会这样 下面是我的错误 我的Arduino多次执行从web浏览器发送的同一命令。 换句话说,如果我按下web浏览器上出现的按钮,我的Arduino将转到 执行我在Arduino中编写的相应函数,不仅执行一次,而且执行多次(通常执行3次,有时仅执行两次…) 以我的代码为例,如果我按下web浏览器上的“前进”按钮,我的Arduino 将运行子函数“closedoor();”3次。串行监视器是 归还日志如下:Arduino从web浏览器多次执行同一命令(使用WiServer.h),arduino,Arduino,我现在正在使用Arduino+WiFiShield创建一个门锁柜,它可以通过带有浏览器的便携式设备远程控制门锁。但我的程序似乎有一个bug,我不知道为什么会这样 下面是我的错误 我的Arduino多次执行从web浏览器发送的同一命令。 换句话说,如果我按下web浏览器上出现的按钮,我的Arduino将转到 执行我在Arduino中编写的相应函数,不仅执行一次,而且执行多次(通常执行3次,有时仅执行两次…) 以我的代码为例,如果我按下web浏览器上的“前进”按钮,我的Arduino 将运行子函数“
Server connected
Processing request for /?V=C
STOP :
closingdoor :
TX 329 bytes
STOP :
closingdoor :
TX 329 bytes
STOP :
closingdoor :
TX 174 bytes
Server connection closed
Server connected
Processing request for /favicon.ico
TX 19 bytes
Server connection closed
如果我取消对“Serial.println(URL);”的注释,我将从串行监视器获得以下日志:
Server connected
Processing request for /?V=C
/?V=C
STOP :
closingdoor :
TX 329 bytes
/?V=C
STOP :
closingdoor :
TX 329 bytes
/?V=C
STOP :
closingdoor :
TX 174 bytes
Server connection closed
Server connected
Processing request for /favicon.ico
/favicon.ico
TX 19 bytes
Server connection closed
如果有人知道什么,请给我一个建议。我真的需要弄清楚
下面是我的代码。请看一看
#include <WiServer.h>
const int LMD18245_Brake = 4; // Pin 4 of Motoduino
const int LMD18245_Direction = 5; // Pin 5 of Motoduino
// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,1,5}; // IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,1}; // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
char ssid[] = {"Johnny"}; // max 32 bytes
unsigned char security_type = 2; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"xxxxxxxx"}; // max 64 characters
byte sampledata=50;
char link[]="http://motoduino.com/"; //link data
// WEP 128-bit keys
prog_uchar wep_keys[] PROGMEM = {
0x61, 0x62, 0x63, 0x64, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 0
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3
};
// setup the wireless mode; infrastructure - connect to AP; adhoc - connect to another WiFi device
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;
// End of wireless configuration parameters ----------------------------------------
boolean mainpage()
{
WiServer.print("<html><head></head>");
WiServer.print("<body>");
WiServer.print("<table border= 0>");
WiServer.print("<tr>");
WiServer.print("<th> </th>");
WiServer.print("<th>");
WiServer.print("<form method=get>");
WiServer.print("<input type=hidden name=V value=C /><br />");
WiServer.print("<input type=submit value=FORWARD>");
WiServer.print("</form>");
WiServer.print("</th><th> </th>");
WiServer.print("</tr>");
WiServer.print("<tr><th>");
WiServer.print("<form method=get >");
WiServer.print("<input type=hidden name=V value=O /><br />");
WiServer.print("<input type=submit value=LEFT>");
WiServer.print("</form>");
WiServer.print("</th><th>");
WiServer.print("<form method=get >");
WiServer.print("<input type=hidden name=V value=S /><br />");
WiServer.print("<input type=submit value=STOP>");
WiServer.print("</form>");
WiServer.print("</th><th> ");
WiServer.print("<form method=get >");
WiServer.print("<input type=hidden name=V value=R /><br />");
WiServer.print("<input type=submit value=RIGHT>");
WiServer.print("</form>");
WiServer.print("</th></tr> <tr> <th> </th> <th> ");
WiServer.print("<form method=get >");
WiServer.print("<input type=hidden name=V value=B /><br />");
WiServer.print("<input type=submit value=BACKWARD>");
WiServer.print("</form>");
WiServer.print(" </th> <th> </th> </tr>");
WiServer.print("</table>");
WiServer.print("<br/>");
WiServer.print("<font color=#888888 size=1>Project_Test</font><font size=3>");
WiServer.print("<br /></font><font size=3> Johnny</font><br />");
WiServer.print("</body>");
WiServer.print("</html>");
return true;
}
boolean controlpage(char* URL)
{
// Serial.println(URL);
if (strcmp(URL, "/") == 0)
{
mainpage();
return true;
}
else
{
if(URL[1] == '?')
{
if((URL[2] == 'V') && (URL[3] == '='))
{
switch(URL[4])
{
case 'C':
closedoor();
break;
case 'O':
opendoor();
break;
case 'S':
stopmode();
break;
default:
//Do nothing
break;
}
mainpage();
return true;
}
mainpage();
return true;
}
}
}
void opendoor()
{
digitalWrite( LMD18245_Brake, 0);
digitalWrite( LMD18245_Direction, 1);
delay(200);
stopmode();
Serial.println("openingdoor : ");
}
void closedoor()
{
digitalWrite( LMD18245_Brake, 0);
digitalWrite( LMD18245_Direction, 0);
delay(200);
stopmode();
Serial.println("closingdoor : ");
}
void stopmode()
{
digitalWrite( LMD18245_Brake, 1);
digitalWrite( LMD18245_Direction, 0);
Serial.println("STOP : ");
}
void setup() {
Serial.begin(9600);
// set all color leds as output pins
pinMode(LMD18245_Brake, OUTPUT);
pinMode(LMD18245_Direction, OUTPUT);
WiServer.init(controlpage);
digitalWrite( LMD18245_Brake, 1);
// Enable Serial output and ask WiServer to generate log messages (optional)
WiServer.enableVerboseMode(true);
}
void loop(){
// Run WiServer
WiServer.server_task();
delay(10);
}
#包括
常数int LMD18245_制动器=4;//Motoduino的针脚4
常数int LMD18245_方向=5;//Motoduino的针脚5
//无线配置参数----------------------------------------
无符号字符本地_ip[]={192168,1,5};//WiShield的IP地址
无符号字符网关_ip[]={192168,1,1};//路由器或网关IP地址
无符号字符子网_掩码[]={255255,0};//本地网络的子网掩码
char ssid[]={“Johnny”};//最大32字节
无符号字符安全类型=2;//0-开放;1-WEP;2-WPA;3-WPA2
//WPA/WPA2密码短语
const prog_char security_密码短语[]PROGMEM={“xxxxxxxx”};//最多64个字符
字节采样数据=50;
字符链接[]=”http://motoduino.com/"; //链接数据
//WEP 128位密钥
prog_uchar wep_keys[]PROGMEM={
0x61、0x62、0x63、0x64、0x65、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、//键0
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//键1
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//键2
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00//键3
};
//设置无线模式;基础设施-连接到AP;临时-连接到另一个WiFi设备
#定义无线模式\u INFRA 1
#定义无线模式2
无符号字符无线模式=无线模式下;
无符号字符ssid_len;
未签名字符安全性\u密码短语\u len;
//无线配置参数结束----------------------------------------
布尔主页()
{
WiServer.print(“”);
WiServer.print(“”);
WiServer.print(“”);
WiServer.print(“”);
WiServer.print(“”);
WiServer.print(“”);
WiServer.print(“”);
WiServer.print(“
”);
WiServer.print(“”);
WiServer.print(“”);
WiServer.print(“”);
WiServer.print(“”);
WiServer.print(“”);
WiServer.print(“”);
WiServer.print(“
”);
WiServer.print(“”);
WiServer.print(“”);
WiServer.print(“”);
WiServer.print(“”);
WiServer.print(“
”);
WiServer.print(“”);
WiServer.print(“”);
WiServer.print(“”);
WiServer.print(“”);
WiServer.print(“
”);
WiServer.print(“”);
WiServer.print(“”);
WiServer.print(“”);
WiServer.print(“”);
WiServer.print(“
”);
WiServer.print(“”);
WiServer.print(“”);
WiServer.print(“”);
WiServer.print(“”);
WiServer.print(“
”);
打印(“项目测试”);
WiServer.print(“
约翰尼
”);
WiServer.print(“”);
WiServer.print(“”);
返回true;
}
布尔控制页(字符*URL)
{
//Serial.println(URL);
如果(strcmp(URL,“/”==0)
{
主页();
返回true;
}
其他的
{
如果(URL[1]=='?')
{
如果((URL[2]=='V')&&(URL[3]=='V'))
{
开关(URL[4])
{
案例“C”:
闭门();
打破
案例“O”:
开门();
打破
案例S:
停止模式();
打破
违约:
//无所事事
打破
}
主页();
返回true;
}
主页();
返回true;
}
}
}
void opendoor()
{
数字写入(LMD18245_-Brake,0);
数字写入(LMD18245--U方向,1);
延迟(200);
停止模式();
Serial.println(“openingdoor:”);
}
无效闭门()
{
数字写入(LMD18245_-Brake,0);
数字写入(LMD18245_方向,0);
延迟(200);
停止模式();
Serial.println(“关门:”);
}
void stopmode()
{
数字写入(LMD18245掼U型制动器,1);
数字写入(LMD18245_方向,0);
Serial.println(“停止:”);
}
无效设置(){
Serial.begin(9600);
//将所有彩色LED设置为输出引脚
pinMode(LMD18245)制动,输出;
pinMode(LMD18245)U方向,输出);
初始化(控制页);
数字写入(LMD18245掼U型制动器,1);
//启用串行输出并要求WiServer生成日志消息(可选)
启用VerboseMode(true);
}
void循环(){
//运行WiServer
WiServer.server_task();
延迟(10);
}
这是因为创建wishield库时atmega328(或类似设备)上的缓冲区很小
atmega328的RAM非常有限,因此它们不得不使用一个小的缓冲区。
如果你“grep”的大小在图书馆,我相信这是李
boolean Server::sendInProgress() {
return false; // FIX ME
}