Hi, I'm going crazy with this problem :~
Basically, I'm "polling" my Arduino with ethernet shield from an ajax webpage. Every a few seconds a PHP script sends an UDP packet to Arduino, and it answers with the status of a LED. I can also press a button on the page to make the LED switch on or off.
When I click on the button WHILE the script is already asking Arduino for the LED state, everything crashes and I have to reset Arduino to make it work again.
I suspect that this is somehow related to this bug: Google Code Archive - Long-term storage for Google Code Project Hosting.
But I tried to download the latest nightly build of the IDE -- and also to compile it from the current master on Github, but the problem persists.
This is my complete source code:
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
/* #define UDP_TX_PACKET_MAX_SIZE 64; */
int ledPin = 7; // ORANGE
int lightStatus1 = 3; // YELLOW
int val;
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 220);
unsigned int localPort = 8888; // local port to listen on
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(lightStatus1, INPUT);
digitalWrite(ledPin, HIGH);
Ethernet.begin(mac,ip);
Udp.begin(localPort);
Serial.begin(9600);
}
void loop() {
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if(packetSize)
{
memset(packetBuffer,0,sizeof(packetBuffer)); // reset the buffer
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i =0; i < 4; i++)
{
Serial.print(remote[i], DEC);
if (i < 3)
{
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp.remotePort());
// read the packet into packetBufffer
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
Serial.println("NEW PACKET");
Serial.println(packetBuffer);
if(auth()){
if ( (getValue(packetBuffer, ',', 2) == "A:1" && digitalRead(lightStatus1) == LOW) || (getValue(packetBuffer, ',', 2) == "A:0" && digitalRead(lightStatus1) == HIGH) ) {
digitalWrite(ledPin, LOW);
delay(50);
digitalWrite(ledPin, HIGH);
}
// send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write("A:");
Udp.write(digitalRead(lightStatus1) == LOW ? "0" : "1");
Udp.endPacket();
}
}
delay(10);
}
boolean auth(){
if(getValue(packetBuffer, ',', 0) !="MYUSER" || getValue(packetBuffer, ',', 1) != "MYPASS"){
Serial.println("AUTH ERROR");
return false;
}
Serial.println("AUTH OK");
return true;
}
/* UTILITIES */
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length()-1;
for(int i=0; i<=maxIndex && found<=index; i++){
if(data.charAt(i)==separator || i==maxIndex){
found++;
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}