Last active
July 4, 2019 13:25
-
-
Save Romonaga/1302be81ddfb84eabed2c55d1bd4a42d to your computer and use it in GitHub Desktop.
ESP2866 Raspberry PI UDP Multicast Examples
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> | |
const char* ssid = "<YOURSSID>"; | |
const char* password = "<YOURPASSWORD>"; | |
void setup() | |
{ | |
Serial.begin(115200); | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void loop() | |
{ | |
Serial.println("Setting Up To Receive"); | |
WiFiUDP udp; | |
IPAddress address(239, 255, 255, 3); | |
udp.beginMulticast(WiFi.localIP(), address, 2013); | |
while (true) | |
{ | |
int packetSize = udp.parsePacket(); | |
if (packetSize > 0) | |
{ | |
char buffer[packetSize]; | |
udp.read(buffer, packetSize); | |
buffer[packetSize - 1] = '\0'; | |
Serial.println(buffer); | |
} | |
} | |
} |
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> | |
const char* ssid = "<YOURSSID>"; | |
const char* password = "<YOURPASSWORD>"; | |
void setup() | |
{ | |
Serial.begin(115200); | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void loop() | |
{ | |
Serial.println("Sending packet"); | |
WiFiUDP udp; | |
IPAddress address(239, 255, 255, 3); | |
char send[] = "Hello Bob!"; | |
udp.beginPacketMulticast(address, 2013, WiFi.localIP()); | |
udp.write(send, strlen(send)); | |
udp.endPacket(); | |
delay(2000); //Don't flood remote service | |
} |
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
QT += core network | |
QT -= gui | |
CONFIG += c++11 | |
TARGET = QTUDPReceiver | |
CONFIG += console | |
CONFIG -= app_bundle | |
TEMPLATE = app | |
SOURCES += rcvmain.cpp | |
# The following define makes your compiler emit warnings if you use | |
# any feature of Qt which as been marked deprecated (the exact warnings | |
# depend on your compiler). Please consult the documentation of the | |
# deprecated API in order to know how to port your code away from it. | |
DEFINES += QT_DEPRECATED_WARNINGS | |
# You can also make your code fail to compile if you use deprecated APIs. | |
# In order to do so, uncomment the following line. | |
# You can also select to disable deprecated APIs only up to a certain version of Qt. | |
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 |
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
QT += core network | |
QT -= gui | |
CONFIG += c++11 | |
TARGET = QTUDPSender | |
CONFIG += console | |
CONFIG -= app_bundle | |
TEMPLATE = app | |
SOURCES += sndmain.cpp | |
# The following define makes your compiler emit warnings if you use | |
# any feature of Qt which as been marked deprecated (the exact warnings | |
# depend on your compiler). Please consult the documentation of the | |
# deprecated API in order to know how to port your code away from it. | |
DEFINES += QT_DEPRECATED_WARNINGS | |
# You can also make your code fail to compile if you use deprecated APIs. | |
# In order to do so, uncomment the following line. | |
# You can also select to disable deprecated APIs only up to a certain version of Qt. | |
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 |
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 <QCoreApplication> | |
#include <QtNetwork> | |
#include <iostream> | |
#include <iomanip> | |
int main(int argc, char *argv[]) | |
{ | |
QCoreApplication a(argc, argv); | |
QUdpSocket udpSocket4; | |
QHostAddress groupAddress4(QStringLiteral("239.255.255.3")); | |
if(false == udpSocket4.bind(QHostAddress::AnyIPv4, 2013, QUdpSocket::ShareAddress)) | |
{ | |
std::cout << "Socket Bind Failed" << std::endl; | |
exit(12); | |
} | |
udpSocket4.joinMulticastGroup(groupAddress4); | |
udpSocket4.setSocketOption(QAbstractSocket::MulticastTtlOption, 5); | |
while(true) | |
{ | |
QByteArray datagram; | |
while(true == udpSocket4.hasPendingDatagrams()) | |
{ | |
datagram.resize(int(udpSocket4.pendingDatagramSize())); | |
udpSocket4.readDatagram(datagram.data(), datagram.size()); | |
std::cout << datagram.constData() << std::endl; | |
} | |
} | |
return a.exec(); | |
} |
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 <QCoreApplication> | |
#include <QtNetwork> | |
int main(int argc, char *argv[]) | |
{ | |
int messageNo = 0; | |
QCoreApplication a(argc, argv); | |
QHostAddress groupAddress4(QStringLiteral("239.255.255.3")); | |
QUdpSocket udpSocket4; | |
udpSocket4.bind(QHostAddress(QStringLiteral("<YOURIPADDRESS>")), 0); | |
udpSocket4.setSocketOption(QAbstractSocket::MulticastTtlOption, 5); | |
while(true) | |
{ | |
messageNo++; | |
QByteArray datagram = "Multicast message " + QByteArray::number(messageNo); | |
udpSocket4.writeDatagram(datagram, groupAddress4, 2013); | |
} | |
return a.exec(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
If you are here then you must be having troubles figuring put how to Get UDP multicast to work. i hope these examples save you time, as i wish I had them when I was implementing this on the ESP8266.