-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathEspNowBroadcast.ino
73 lines (59 loc) · 2.04 KB
/
EspNowBroadcast.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
#include <WifiEspNowBroadcast.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#endif
static const int BUTTON_PIN = 0; // "flash" button on NodeMCU, Witty Cloud, etc
static const int LED_PIN = 2; // ESP-12F blue LED
int ledState = HIGH;
void processRx(const uint8_t mac[6], const uint8_t* buf, size_t count, void* cbarg) {
Serial.printf("Message from %02X:%02X:%02X:%02X:%02X:%02X\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
for (int i = 0; i < count; ++i) {
Serial.print(static_cast<char>(buf[i]));
}
Serial.println();
digitalWrite(LED_PIN, ledState);
ledState = 1 - ledState;
}
void setup() {
Serial.begin(115200);
Serial.println();
WiFi.persistent(false);
bool ok = WifiEspNowBroadcast.begin("ESPNOW", 3);
if (!ok) {
Serial.println("WifiEspNowBroadcast.begin() failed");
ESP.restart();
}
WifiEspNowBroadcast.onReceive(processRx, nullptr);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, ledState);
Serial.print("MAC address of this node is ");
Serial.println(WiFi.softAPmacAddress());
Serial.println("Press the button to send a message");
}
void sendMessage() {
char msg[60];
int len = snprintf(msg, sizeof(msg), "hello ESP-NOW from %s at %lu", WiFi.softAPmacAddress().c_str(), millis());
WifiEspNowBroadcast.send(reinterpret_cast<const uint8_t*>(msg), len);
Serial.println("Sending message");
Serial.println(msg);
Serial.print("Recipients:");
const int MAX_PEERS = 20;
WifiEspNowPeerInfo peers[MAX_PEERS];
int nPeers = std::min(WifiEspNow.listPeers(peers, MAX_PEERS), MAX_PEERS);
for (int i = 0; i < nPeers; ++i) {
Serial.printf(" %02X:%02X:%02X:%02X:%02X:%02X", peers[i].mac[0], peers[i].mac[1], peers[i].mac[2], peers[i].mac[3], peers[i].mac[4], peers[i].mac[5]);
}
Serial.println();
}
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) { // button is pressed
sendMessage();
while (digitalRead(BUTTON_PIN) == LOW) // wait for button release
;
}
WifiEspNowBroadcast.loop();
delay(10);
}