我现在正在使用 Arduino + WiFiShield 创建一个门锁,它可以通过带有浏览器的便携式设备远程控制门锁。但似乎我的程序有一个错误,我不知道它为什么会发生。
以下是我的错误。
我的 Arduino 多次执行从 Web 浏览器发送的相同命令。换句话说,如果我按下出现在网络浏览器上的按钮,我的 Arduino 将执行我在 Arduino 中编写的相应功能,不仅一次而且多次(通常是 3 次,有时只有两次......)。
以我的代码为例,如果我在网络浏览器上按下 FORWARD 按钮,我的 Arduino 将运行子函数“closedoor();” 3次。串行监视器返回日志如下:
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);
}