我最近购买了Arduino WiFi Shield,并且我已将固件更新到最新版本。
我正在使用Webduino创建一种Web服务,这样我就可以查看pin的状态并将其值设置为HIGH或LOW。
在第一秒内一切正常,但是在几秒钟不活动后,我可以看到红色错误指示灯亮起,之后,当它恢复到网络的连接时,我无法再与屏蔽通信,我必须重启Arduino板。
我在论坛上查了很多帖子,我看到有更多人有同样的问题。他们中的许多人谈论与固件有关的问题,因此看起来有一个错误导致了这个问题。
有谁知道为什么会这样?你能指出我正确的方向吗?
你可以看到我附带的源代码,所以也许你可以看到错误。
#include <SPI.h>
#include <WiFi.h>
#include <Streaming.h>
#define WEBDUINO_SERIAL_DEBUGGING 1
#include <WebServer.h>
#include <sha1.h>
char ssid[] = "******";
char pass[] = "******";
IPAddress ip(192,168,1,210);
int status = WL_IDLE_STATUS;
unsigned long sessionId;
#define WEBDUINO_METHOD_NOT_ALLOWED_MESSAGE "<h1>405 Requested method is not supported for resource</h1>"
#define CREDENTIALS "********"
#define PREFIX ""
#define ANALOG_PINS_AVAILABLE 5
#define DIGITAL_PINS_AVAILABLE 9
WebServer webserver(PREFIX, 80);
void welcome(WebServer &server) {
P(welcomeMsg1) = "HTTP/1.0 200 OK" CRLF;
server.printP(welcomeMsg1);
P(welcomeMsg2) =
"Content-Type: text/html" CRLF
CRLF
"<h1>Welcome to the web service</h1>";
server.printP(welcomeMsg2);
}
void httpMethodNotAllowed(WebServer &server) {
P(methodNotAllowedMsg1) = "HTTP/1.0 405 Method Not Allowed" CRLF;
server.printP(methodNotAllowedMsg1);
P(methodNotAllowedMsg2) =
"Content-Type: text/html" CRLF
CRLF
WEBDUINO_METHOD_NOT_ALLOWED_MESSAGE;
server.printP(methodNotAllowedMsg2);
}
void pinValuesJSON(WebServer &server) {
char sessionIdHeader[21];
sprintf(sessionIdHeader, "SessionId: %lu" CRLF, sessionId);
server.httpSuccess("application/json; charset=utf-8", sessionIdHeader);
server << "{";
server << "\"d2\":" << digitalRead(2) << ",";
server << "\"d3\":" << digitalRead(3) << ",";
server << "\"d5\":" << digitalRead(5) << ",";
server << "\"d6\":" << digitalRead(6);
server << "}";
}
void write(char *pin, char *value) {
if (pin[0] == 'a') {
analogWrite(strtoul(pin + 1, NULL, 10), strtoul(value, NULL, 10));
} else if (pin[0] == 'd') {
digitalWrite(strtoul(pin + 1, NULL, 10), strtoul(value, NULL, 10));
}
}
void defaultCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) {
if (server.checkCredentials(CREDENTIALS)) {
if (type == WebServer::GET) {
welcome(server);
} else {
httpMethodNotAllowed(server);
}
} else {
server.httpUnauthorized();
}
}
void pinsCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) {
#define PINLEN 5
#define VALUELEN 5
if (server.checkCredentials(CREDENTIALS)) {
if (type == WebServer::GET) {
pinValuesJSON(server);
} else if (type == WebServer::PUT) {
bool repeat;
char pin[PINLEN];
char value[VALUELEN];
do {
repeat = server.readPOSTparam(pin, PINLEN, value, VALUELEN);
write(pin, value);
} while (repeat);
pinValuesJSON(server);
}
} else {
server.httpUnauthorized();
}
}
void setup() {
randomSeed(analogRead(0));
sessionId = random(999999999);
/* Pins
* 13: SCK
* 12: MISO
* 11: MOSI
* 10: SS for WiFi
* 9: L9 (yellow) => Informational LED
* 7: Handshake between shield and Arduino
* 4: SS for SD card */
/* When SD card peripheral is not being used, it's necessary to set
pin 4 as an output and write a high to it */
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
/* Socket pins */
pinMode(2, OUTPUT);
digitalWrite(2, LOW);
pinMode(3, OUTPUT);
digitalWrite(3, LOW);
pinMode(5, OUTPUT);
digitalWrite(5, LOW);
pinMode(6, OUTPUT);
digitalWrite(6, LOW);
WiFi.config(ip);
while (status != WL_CONNECTED) {
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
delay(4000);
}
webserver.setDefaultCommand(&defaultCmd);
webserver.addCommand("pins", &pinsCmd);
/* start the webserver */
webserver.begin();
digitalWrite(8, HIGH);
}
void loop() {
/* process incoming connections one at a time forever */
webserver.processConnection();
}