WIFI UDP no longer receives data after data is sent
Posted: Sat Apr 04, 2015 3:16 pm
I'm not entirely sure that this is a bug but I can't see any other explanation for the behavior. I am using the following code on which is connected to a python script which essentially acts as a terminal-esque way of sending and receiving data to the ESP. The code below works and I can have the light set on or off. The problem is that, after a message has been sent using sendMessage, the ESP will no longer receive packets. I can see from the serial output that the loop is still running and if I remove the call to sendMessage, it will continue turning the light on and off merrily. I wonder if I'm just missing something?
Code: Select all
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
WiFiClient client;
WiFiUDP port;
char packetBuffer[255];
unsigned int localPort = 9999;
IPAddress serverIP(192, 168, 1, 117);
void setup() {
Serial.begin(9600);
WiFi.begin("******", "********");
port.begin(9999);
pinMode(2, OUTPUT);
digitalWrite(2,LOW);
}
void loop() {
int packetSize = port.parsePacket();
Serial.println(packetSize);
if (packetSize) {
int len = port.read(packetBuffer, 255);
if (len > 0) packetBuffer[len] = 0;
Serial.println(packetBuffer);
handleMessage(String(packetBuffer));
}
delay(500);
}
void handleMessage(String data) {
if(data=="on"){
digitalWrite(2,HIGH);
Serial.println("ON");
sendMessage("light on");
}
if(data=="off"){
digitalWrite(2,LOW);
Serial.write("OFF");
sendMessage("light off");
}
}
void sendMessage(char data[]) {
port.beginPacket(serverIP,localPort);
port.write(data);
port.endPacket();
}