UDP Packets appear in wireshark and UDPlistener
This commit is contained in:
parent
c49c511d75
commit
0991343a55
67
UDP.ino
67
UDP.ino
|
@ -1,52 +1,33 @@
|
||||||
|
const char* ssid = "CaveBot";
|
||||||
|
const char* password = "&nHM%D2!$]Qg[VUv";
|
||||||
|
|
||||||
#include "WiFi.h"
|
#include "WiFi.h"
|
||||||
#include "AsyncUDP.h"
|
#include "AsyncUDP.h"
|
||||||
|
|
||||||
const char * ssid = "CaveBot";
|
const IPAddress multicastIP = IPAddress(239, 1, 2, 3);
|
||||||
const char * password = "Eating0-Untaxed0-Pod6-Jokester8";
|
int port = 1234;
|
||||||
|
|
||||||
AsyncUDP udp;
|
AsyncUDP udp;
|
||||||
|
|
||||||
void UdpInit()
|
void UdpInit() {
|
||||||
{
|
//Serial.begin(115200);
|
||||||
//Serial.begin(115200);
|
ConnectToWiFi();
|
||||||
WiFi.mode(WIFI_STA);
|
//udp.connect(multicastIP, port);
|
||||||
WiFi.begin(ssid, password);
|
|
||||||
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
|
|
||||||
Serial.println("WiFi Failed");
|
|
||||||
while(1) {
|
|
||||||
delay(1000);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(udp.listenMulticast(IPAddress(239,1,2,3), 1234)) {
|
|
||||||
Serial.print("UDP Listening on IP: ");
|
|
||||||
Serial.println(WiFi.localIP());
|
|
||||||
udp.onPacket([](AsyncUDPPacket packet) {
|
|
||||||
Serial.print("UDP Packet Type: ");
|
|
||||||
Serial.print(packet.isBroadcast()?"Broadcast":packet.isMulticast()?"Multicast":"Unicast");
|
|
||||||
Serial.print(", From: ");
|
|
||||||
Serial.print(packet.remoteIP());
|
|
||||||
Serial.print(":");
|
|
||||||
Serial.print(packet.remotePort());
|
|
||||||
Serial.print(", To: ");
|
|
||||||
Serial.print(packet.localIP());
|
|
||||||
Serial.print(":");
|
|
||||||
Serial.print(packet.localPort());
|
|
||||||
Serial.print(", Length: ");
|
|
||||||
Serial.print(packet.length());
|
|
||||||
Serial.print(", Data: ");
|
|
||||||
Serial.write(packet.data(), packet.length());
|
|
||||||
Serial.println();
|
|
||||||
//reply to the client
|
|
||||||
packet.printf("Got %u bytes of data", packet.length());
|
|
||||||
});
|
|
||||||
//Send multicast
|
|
||||||
udp.print("Hello!");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void UdpLoop()
|
void UdpLoop() {
|
||||||
{
|
byte data[8] = {1, 2, 3, 4, 5, 6, 7, 8};
|
||||||
delay(1000);
|
udp.writeTo(data, sizeof(data), multicastIP, port);
|
||||||
//Send multicast
|
}
|
||||||
udp.print("Anyone here?");
|
|
||||||
|
|
||||||
|
void ConnectToWiFi() {
|
||||||
|
WiFi.mode(WIFI_STA);
|
||||||
|
WiFi.begin(ssid, password);
|
||||||
|
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
|
||||||
|
Serial.println("WiFi Failed");
|
||||||
|
while (1) {
|
||||||
|
delay(1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue