This repository has been archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsyncUDPMulticastServer.ino
133 lines (101 loc) · 3.34 KB
/
AsyncUDPMulticastServer.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/****************************************************************************************************************************
AsyncUDPMulticastServer.ino
AsyncUDP_RP2040W is a library for the RP2040W with CYW43439 WiFi
Based on and modified from ESPAsyncUDP (https://github.com/me-no-dev/ESPAsyncUDP)
Built by Khoi Hoang https://github.com/khoih-prog/AsyncUDP_RP2040W
*****************************************************************************************************************************/
#if !( defined(ARDUINO_RASPBERRY_PI_PICO_W) )
#error For RASPBERRY_PI_PICO_W only
#endif
#include <WiFi.h>
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include <AsyncUDP_RP2040W.h> // https://github.com/khoih-prog/AsyncUDP_RP2040W
char ssid[] = "your_ssid"; // your network SSID (name)
char pass[] = "12345678"; // your network password (use for WPA, or use as key for WEP), length must be 8+
AsyncUDP udp;
int status = WL_IDLE_STATUS;
#define BUF_SZ 32
char buf[BUF_SZ];
void parsePacket(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();
snprintf(buf, sizeof(buf) - 1, "Got %u bytes of data", packet.length());
//reply to the client
// size_t write(const uint8_t *data, size_t len);
packet.write((uint8_t*) buf, strlen(buf));
}
void printWifiStatus()
{
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("Local IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
void setup()
{
Serial.begin(115200);
while (!Serial && millis() < 5000);
Serial.print("\nStart AsyncUDPMulticastServer on ");
Serial.println(BOARD_NAME);
Serial.println(ASYNC_UDP_RP2040W_VERSION);
///////////////////////////////////
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE)
{
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
Serial.print(F("Connecting to SSID: "));
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(1000);
// attempt to connect to WiFi network
while ( status != WL_CONNECTED)
{
delay(500);
// Connect to WPA/WPA2 network
status = WiFi.status();
}
printWifiStatus();
///////////////////////////////////
if (udp.listenMulticast(IPAddress(239, 1, 2, 3), 1234))
{
Serial.print("UDP Listening on IP: ");
Serial.println(WiFi.localIP());
udp.onPacket([](AsyncUDPPacket packet)
{
parsePacket(packet);
});
//Send multicast
udp.print("Hello!");
}
}
void loop()
{
delay(1000);
//Send multicast
udp.print("Anyone here?");
}