Created
February 16, 2019 11:20
-
-
Save mqu/f8d4f6877703bb39676d68733801871d to your computer and use it in GitHub Desktop.
ESP8266 / Arduino UDP Multicast example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <WiFiUdp.h> | |
#include <ESP8266WebServer.h> | |
// simple POC : send hello world string over UDP-multicast address every 2 seconds. | |
// a very simple way to connect IOT to local network without need of any server. | |
// author : Marc Quinton / feb 2019 | |
// keywords : esp8266, arduino, multicast, IOT, example, POC | |
WiFiUDP udp; | |
const int port = 8266; | |
// IPAddress broadcast; | |
IPAddress broadcast=IPAddress(224, 0, 1, 3); | |
const char* ssid = "ssid"; | |
const char* password = "password"; | |
void setup(void) | |
{ | |
Serial.begin(115200); | |
Serial.println("Startup"); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
Serial.println(""); | |
// Wait for connection | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.print("Connected to "); | |
Serial.println(ssid); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
Serial.print("broadcast address : "); | |
Serial.print(broadcast); | |
Serial.print(":"); | |
Serial.println(port); | |
} | |
void sendUDP(String string) { | |
Serial.print("sendUDP : "); | |
Serial.println(string); | |
// convert string to char array | |
char msg[255]; | |
string.toCharArray(msg,255); | |
udp.beginPacketMulticast(broadcast, port, WiFi.localIP()); | |
udp.write(msg); | |
udp.endPacket(); | |
} | |
void loop(void) | |
{ | |
sendUDP((String)"hello world"); | |
delay(2000); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
require 'ipaddr' | |
MULTICAST_ADDR = "224.0.1.3" | |
PORT = 8266 | |
ip = IPAddr.new(MULTICAST_ADDR).hton + IPAddr.new("0.0.0.0").hton | |
sock = UDPSocket.new | |
sock.setsockopt(Socket::IPPROTO_IP, Socket::IP_ADD_MEMBERSHIP, ip) | |
sock.bind(Socket::INADDR_ANY, PORT) | |
loop do | |
msg, info = sock.recvfrom(1024) | |
# puts "MSG: #{msg} from #{info[2]} (#{info[3]})/#{info[1]} len #{msg.size}" | |
puts msg | |
end |
this is example i think so its no matter
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very naughty, using a multicast address in the well-known service scope. Use the locally scoped address range 239.0.0.0 to 239.255.255.255