-
Notifications
You must be signed in to change notification settings - Fork 1
/
ESP32_WiFi_Broadcasting.ino
61 lines (49 loc) · 1.75 KB
/
ESP32_WiFi_Broadcasting.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
#include <WiFi.h>
#include <WiFiUdp.h>
const char ssid[] = "MyUDPBroadcasting_WiFi"; // SSID
const char pass[] = "aiueoabc"; // password
const int localPort = 8888; // Port from esp
const int remotePort = 9818; // Port to device (Set this on device to listen)
const IPAddress ip(192, 168, 4, 1); // IP (Use as gateway, too)
const IPAddress subnet(255, 255, 255, 0); // subnet
static const char *RemoteIpadr = "255.255.255.255"; // ToBroadCast
WiFiUDP udp;
// WiFi Initialize Function
void wifiSetup() {
Serial.println("WiFiSetup");
WiFi.softAP(ssid, pass); // SSID password Setting
delay(100); // wait and let esp32 to set SSID and password
WiFi.softAPConfig(ip, ip, subnet); // set ip, gateway, subnet
Serial.print("AP IP address: ");
IPAddress myIP = WiFi.softAPIP();
Serial.println(myIP);
Serial.println("Starting UDP");
udp.begin(localPort);
Serial.print("Local port: ");
Serial.println(localPort);
}
unsigned long stime;
void setup() {
Serial.begin(115200);
wifiSetup();
stime = millis();
}
void loop() {
// put your main code here, to run repeatedly:
int example_number = 0120;
String message = "PutWhatYouWantToSend_like: " + String(example_number);
Serial.print("This is what you send to remote port => ");
Serial.println(message);
int msgLength = message.length() + 1;
char char_array[msgLength];
message.toCharArray(char_array, msgLength);
if (millis() - stime > 100) { // send once in 100 msecs
udp.beginPacket(RemoteIpadr, remotePort);
for (int i = 0; i < msgLength; i++) {
char onechar = char_array[i];
udp.write(int(onechar));
}
udp.endPacket();
stime = millis();
}
}