We are fairly new to Arduino and ESP8266. We know how to send and receive UDP packets from one ESP8266 to the other. However, we want to broadcast the UDP packets to all IP addresses on the network. Our IP address is 192.168.50.40, so after research, we figured out that our broadcast IP address is 192.168.50.255. However, when we send the UDP packet to this broadcast IP address, the other ESP8266 does not receive the packet. Here is our sketch:
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
const char* ssid = "xxxxxx";
const char* password = "xxxxxx";
WiFiUDP Udp;
unsigned int localUdpPort = 4211; // local port to listen on
char incomingPacket[255]; // buffer for incoming packets
IPAddress IP_Remote(192, 168, 50, 255);
void setup()
{
Serial.begin(115200);
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected");
Udp.begin(localUdpPort);
Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
}
void loop()
{
Udp.beginPacket(IP_Remote, 4210);
Udp.write("Hello");
Udp.endPacket();
}
Can you please help us figure out if we are broadcasting correctly? Thank you!
In Internet Protocol Version 4 (IPv4) networks, broadcast addresses are special values in the host-identification part of an IP address.[1] The all-ones value was established in RFC 919 as the standard broadcast address for networks that support broadcast. This method of using the all-ones address was first proposed by R. Gurwitz and R. Hinden in 1982 (IEN-212).[2] The later introduction of subnets and Classless Inter-Domain Routing changed this slightly, so that the all-ones host address of each subnet is that subnet's broadcast address.[3]
@PaulS
Hi, yes initially we researched from that wiki page itself. We learned and crosschecked how to figure out the broadcast address. That is how we got ours. We also confirmed that we are listening to the correct port. But the problem still persists.
I was able to get this to work fairly easily with three ESP8266 devices with a fairly simple code change:
// My ip definitions for reference
IPAddress ip(192, 168, 220, 1);
IPAddress broadcastIp(192,168,220,255);
IPAddress netmask(255, 255, 255, 0);
//
// And then later in the sketch where I want to send some data...
// Replaced the remoteIp with the broadcast IP.
// udp.beginPacket(remoteIp, port); // remote IP and port
udp.beginPacket(broadcastIp, port); // subnet Broadcast IP and port
udp.write(buf2, i2);
udp.endPacket();
I believe the problem is with the udp.write("hello") and not the IP part.
It has to be a byte or buffer. (however it will compile without a warning with a string)
Check the examples sketch > WifIUdpSendReceiveString for an example of how to do that.
Have you confirmed that your network supports broadcasting?
WiFi sort-of HAS to support broadcasts... I don't know if the ESP8266 code supports UDP receiving UDP broadcasts, though. (It almost certainly supports SENDING them, for use by BOOTP...)
I would use 255.255.255.255 as the broadcast IP address, though. It's much more "universally recognized" as a broadcast address.
I struggled a long time with this, but this worked for me. The UDP Broadcast is on the default IP of 255.255.255.255 and I chose port 2255. This code plus a python script to receive the data from my ESP32 is on GitHub ESP32 Project: UDP Broadcasting. It should work with an ESP8266 but just send your own data as it only has 1 ADC unlike the ESP32. See it in action.
#include <WiFi.h>
#include <AsyncUDP.h>
const char * ssid = "SSID";
const char * pass = "PASSWORD";
// I am using 4 Sliders the 5th one is not used
const int NUM_SLIDERS = 5;
// I am using a NODEMCU ESP 32S and pins SVP, SVN, P34, P35, (P32 (Not used))
const int analogInputs[NUM_SLIDERS] = {36, 39, 34, 35, 32};
int analogSliderValues[NUM_SLIDERS];
AsyncUDP udp;
void setup()
{
// Set all slider pins to INPUT
for (int i = 0; i < NUM_SLIDERS; i++) {
pinMode(analogInputs[i], INPUT);
}
// Get WiFi going
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("WiFi Failed");
while(1) {
delay(1000);
}
}
}
// Get slider pin values
void updateSliderValues() {
for (int i = 0; i < NUM_SLIDERS; i++) {
analogSliderValues[i] = analogRead(analogInputs[i]);
}
}
// TODO only send values if there is significant change in values to limit network traffick
// UDP Broadcast slider values
void sendSliderValues() {
String builtString = String("");
for (int i = 0; i < NUM_SLIDERS; i++) {
builtString += String((int)analogSliderValues[i]);
// Build the string to broadcast by seperating values using | except for last value
if (i < NUM_SLIDERS - 1) {
builtString += String("|");
}
}
// Send UDP Broadcast to 255.255.255.255 (default broadcast addr), Port 2255
udp.broadcastTo(builtString.c_str(), 2255);
}
void loop()
{
updateSliderValues();
sendSliderValues(); // Send data
delay(100);
}