I’ve got a simple sketch that runs on an Arduino Uno with the 5100 Ethernet header board and it works ie it reliably prints out the UDP messages that come about every 10 seconds. Here it is…
/* This sketch listens for UDP packets and prints them out on the Serial output.
* It works fine running on the Arduino Uno with the 5100 Ethernet board.
* Specifically, it prints out all the Owl Intuition UDP messages without any problem.
*/
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#define buffer_size 360
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 160); // Give the Arduino an IP address
unsigned int localPort = 22600; // local port to listen on
char packetBuffer[buffer_size]; //buffer to hold incoming packet,
EthernetUDP Udp;
void setup() {
// start the Ethernet and UDP
Serial.begin(115200);
Ethernet.begin(mac,ip);
Udp.begin(localPort);
}
void loop() {
int packetSize = Udp.parsePacket();
if(packetSize){ // The packet has some size
Udp.read(packetBuffer,packetSize); //buffer_size);
for(int i=0; i < packetSize; i++) Serial.print(char(packetBuffer[i]));
Serial.println();
}
}
However, when I try to do it using WiFi on an ESP8266 I get nothing. It connects to the WiFi OK, but then I get nothing. The sketch for that is here and is very similar…
/* This sketch listens for UDP packets and prints them out on the Serial output.
* It doesn't work running on an ESP8266 WiFi card.
* Specifically, it connects to WiFi OK but never prints out any UDP messages.
*/
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#define buffer_size 360
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 199); // Give the Arduino an IP address
IPAddress gw(192, 168, 0,1);
IPAddress sn(255, 255, 255, 0);
IPAddress pd(8, 8, 8, 8);
IPAddress sd(8, 8, 8, 4);
const char* ssid = "myssid";
const char* password = "mypassword";
unsigned int localPort = 22600; // local port to listen on
char packetBuffer[buffer_size]; //buffer to hold incoming packet,
WiFiUDP Udp;
void setup() {
Serial.begin(115200);
WiFiBegin();
Udp.begin(localPort);
}
void loop() {
int packetSize = Udp.parsePacket();
if(packetSize){ // The packet has some size
Udp.read(packetBuffer,packetSize);
for(int i=0; i < packetSize; i++) Serial.print(char(packetBuffer[i]));
Serial.println();
}
}
void WiFiBegin(){
delay(200);
// Connect to the WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.config(ip, gw, sn, pd, sd);
WiFi.begin(ssid, password) ;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" Connected.");
}
I should add that the messages are going out on the WiFi as I have a python program running on my Android tablet that is seeing them.
I’m not a professional coder so most likely I have missed something out so if anyone can help I’d be grateful.
Cheers