ESP chip is not receiving UDP packets
Posted: Sun Mar 01, 2020 8:59 pm
I used the code below to start a udp server on my esp8266 HUZZAH and it seemed to work fine. I would send a packet using packet sender and it would write the content of the packet to the serial monitor. Then after coming back after about two days, it isn't working at all. I tried using different clients, different wifi networks and different esp chips and it just isn't working. I did get it working again by removing the line that declares a static IP address and letting it get one via DHCP. Does anyone see any issues with the code? Or have any ideas as to why it wouldn't be receiving packets when using the static IP address? My IP address lease time is the 24 hour Cisco default.
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
const char* ssid = "***";
const char* password = "***";
WiFiUDP UDPTestServer;
unsigned int UDPPort = 2807;
const int packetSize = 1;
byte packetBuffer[packetSize];
void setup() {
Serial.begin(115200);
delay(10);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
WiFi.config(IPAddress(192, 168, 1, 60), IPAddress(192, 168, 1, 1), IPAddress(255, 255, 255, 0));
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
UDPTestServer.begin(UDPPort);
}
int value = 0;
void loop() {
handleUDPServer();
delay(1);
}
void handleUDPServer() {
int cb = UDPTestServer.parsePacket();
if (cb) {
UDPTestServer.read(packetBuffer, packetSize);
int myData;
for(int i = 0; i < packetSize; i++) {
myData += (int)packetBuffer[i];
}
Serial.write(myData);
}
}
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
const char* ssid = "***";
const char* password = "***";
WiFiUDP UDPTestServer;
unsigned int UDPPort = 2807;
const int packetSize = 1;
byte packetBuffer[packetSize];
void setup() {
Serial.begin(115200);
delay(10);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
WiFi.config(IPAddress(192, 168, 1, 60), IPAddress(192, 168, 1, 1), IPAddress(255, 255, 255, 0));
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
UDPTestServer.begin(UDPPort);
}
int value = 0;
void loop() {
handleUDPServer();
delay(1);
}
void handleUDPServer() {
int cb = UDPTestServer.parsePacket();
if (cb) {
UDPTestServer.read(packetBuffer, packetSize);
int myData;
for(int i = 0; i < packetSize; i++) {
myData += (int)packetBuffer[i];
}
Serial.write(myData);
}
}