Recently I bought a SparkFun ESP8266 Wifi Shield. It requires the use of their own library and now I am struggling to receive UDP packages.
Here’s the code that I’m using:
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <SparkFunESP8266WiFi.h>
#include <WiFiUdp.h>
const char ssid[] = "ssid";
const char ps[] = "password";
WiFiUDP Udp;
IPAddress myIP;
char packetBuffer[255];
unsigned const int localPort = 20777;
void setup() {
Serial.begin(9600);
Serial.println("Starting setup...");
if(esp8266.begin()){
Serial.println("ESP ready to go");
} else {
Serial.println("Cannot communicate with esp shield");
}
int retVal;
retVal = esp8266.connect(ssid, ps);
if(retVal < 0){
Serial.print(F("Error connecting: "));
Serial.println(retVal);
} else {
myIP = esp8266.localIP();
Serial.print(F("The IP-address is: ")); Serial.println(myIP);
}
Udp.begin(localPort);
Serial.print("Listening on "); Serial.print(myIP); Serial.print(":"); Serial.println(localPort);
Serial.println("Waiting for incoming packages...");
}
void loop() {
int packetSize = Udp.parsePacket();
if (packetSize) {
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remoteIp = Udp.remoteIP();
Serial.print(remoteIp);
Serial.print(", port ");
Serial.println(Udp.remotePort());
// read the packet into packetBufffer
int len = Udp.read(packetBuffer, 255);
if (len > 0) {
packetBuffer[len] = 0;
}
Serial.println("Contents:");
Serial.println(packetBuffer);
}
}
When I boot up WireShark and check the traffic between the sender (local pc) and the receiver (WifiShield), I can see that I’m getting an error:
The provided code does not have any errors.