I am working on a local network control application using UDP to trigger various lights, reading sensors, etc. I have noticed that my UDP connection will fail to function after a period of 12-24 hours. I only call UDP.begin()
once in setup. But I’m using static IP so I don’t expect to have the same issues raised here with DHCP and leases.
I am currently running similar code on an Arduino Leonardo and Ethernet Shield, which has been running smoothly for months.
The way this code works is by sending a UDP command over port 8888 from a device such as another photon or phone or PC application. The format for this command is ll;1 to turn on the light and ll;0 to turn off the light. This is boiled down, so I usually have more going on, but I trimmed it down to this and still see the issue arise.
Please let me know if there is anything wrong in my implementation or if I need to make special considerations for my application. I am hoping not to need to reset the UDP port every interval of time. And since this is a receive only function with only occasional transactions, I will not be able to get an error code to trigger a reset as suggested here.
I am also using an external antenna and have a strong WiFi connection to the router, so I don’t believe this would be the issue.
Final note: running Photon with 0.5.0 firmware rev.
//boilded down UDP script
STARTUP(WiFi.selectAntenna(ANT_EXTERNAL));
unsigned int localPort = 8888; // local port to listen on
const int UDP_TX_PACKET_MAX_SIZE = 512;
UDP Udp;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
String pBStr = "";
const uint8_t LEDswitchpin = D7;
void setup() {
IPAddress myAddress(10, 0, 0, 157);
IPAddress netmask(255,255,255,0);
IPAddress gateway(10, 0, 0, 1);
IPAddress dns(8,8,8,8);
WiFi.setStaticIP(myAddress, netmask, gateway, dns);
pinMode(LEDswitchpin, OUTPUT);
WiFi.useStaticIP();
Udp.begin(localPort);
}
char *p;
char *str;
void loop() {
int packetSize = Udp.parsePacket();
if(packetSize)
{
IPAddress remote = Udp.remoteIP();
// read the packet into packetBufffer
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
p = packetBuffer;
String dvc = String(strtok_r(p, ";", &p));
uint8_t devstate;
if (dvc == "ll")
{
while ((str = strtok_r(p, ";", &p)) != NULL) // delimiter is the semicolon
{
pBStr = String(str);
int pBStr_len = pBStr.length();
String op = pBStr.substring(0,1);
if (op == "1") {
devstate = 1; //ON
}
else if (op == "0") {
devstate = 0; //OFF
}
}
if (devstate == 1) {
LEDturnON();
}
else if (devstate == 0) {
LEDturnOFF();
}
}
pBStr = "";
memset(packetBuffer,'\0',UDP_TX_PACKET_MAX_SIZE);
} //END OF DECODING COMMAND
}
void LEDturnON() {
digitalWrite(LEDswitchpin, HIGH);
}
void LEDturnOFF() {
digitalWrite(LEDswitchpin, LOW);
}
Thanks for any help.