私は現在、Arduino + WiFiShield を使用して、ブラウザーを備えたポータブル デバイスによってドア ロックをリモートで制御できるドア ロッカーを作成しています。しかし、私のプログラムにバグがあるようで、なぜそれが起こっているのかわかりません。
以下は私のバグです。
私のArduinoは、Webブラウザから送信された同じコマンドを複数回処理します。つまり、Web ブラウザーに表示されるボタンを押すと、Arduino は、Arduino に記述した対応する関数を 1 回だけでなく、複数回 (通常は 3 回、場合によっては 2 回だけ...) 実行しに行きます。
私のコードを例にとると、Web ブラウザーで 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);
}