@@ -18,39 +18,31 @@ static uint8_t receiver[] = { 0x12, 0x34, 0x56, 0x78, 0x90, 0x12 };
1818// #define DEST_ADDR ESPNOW_BROADCAST_ADDRESS
1919
2020bool sent = true ;
21- bool received = false ;
2221
2322const unsigned int SEND_MSG_MSEC = 2000 ;
2423
25- // ------------ SET ACCORDING YOUR BOARD -----------
26- static const int LED_PIN = 5 ;
27- static const int LED_ON = LOW;
28- // -------------------------------------------------
29-
3024void dataSent (uint8_t * address, uint8_t status) {
3125 sent = true ;
32- // Serial.printf ("Message sent to " MACSTR ", status: %d\n", MAC2STR (address), status);
26+ Serial.printf (" Message sent to " MACSTR " , status: %d\n " , MAC2STR (address), status);
3327}
3428
35- void dataReceived (uint8_t * address, uint8_t * data, uint8_t len, signed int rssi) {
36- digitalWrite (LED_PIN, LED_ON);
29+ void dataReceived (uint8_t * address, uint8_t * data, uint8_t len, signed int rssi, bool broadcast) {
3730 Serial.print (" Received: " );
3831 Serial.printf (" %.*s\n " , len, data);
3932 Serial.printf (" RSSI: %d dBm\n " , rssi);
4033 Serial.printf (" From: " MACSTR " \n " , MAC2STR (address));
41- digitalWrite (LED_PIN, !LED_ON );
34+ Serial. printf ( " %s \n " , broadcast ? " Broadcast " : " Unicast " );
4235}
4336
4437void setup () {
4538 Serial.begin (115200 );
4639 WiFi.mode (WIFI_MODE_STA);
47- pinMode (LED_PIN, OUTPUT);
48- digitalWrite (LED_PIN, !LED_ON);
4940#if defined ESP32
5041 WiFi.disconnect (false , true );
5142#elif defined ESP8266
5243 WiFi.disconnect (false );
5344#endif // ESP32
45+
5446 Serial.printf (" Connected to %s in channel %d\n " , WiFi.SSID ().c_str (), WiFi.channel ());
5547 Serial.printf (" IP address: %s\n " , WiFi.localIP ().toString ().c_str ());
5648 Serial.printf (" MAC address: %s\n " , WiFi.macAddress ().c_str ());
@@ -63,14 +55,15 @@ void loop () {
6355 static time_t lastSend = 0 ;
6456 static unsigned int counter = 0 ;
6557
58+ // Sent flag is needed to wait for the message to be actually sent. Avoids messages dropping, maximizing throughput.
6659 if (sent && ((millis () - lastSend) > SEND_MSG_MSEC)) {
6760 lastSend = millis ();
68- String message = String (msg); // + " " + String (counter++);
61+ String message = String (msg) + " " + String (counter++);
6962 sent = false ;
7063 if (!quickEspNow.send (DEST_ADDR, (uint8_t *)message.c_str (), message.length ())) {
71- // Serial.printf (">>>>>>>>>> %ld: Message sent\n", micros() );
64+ Serial.printf (" >>>>>>>>>> Message sent\n " );
7265 } else {
73- Serial.printf (" >>>>>>>>>> %ld: Message not sent\n " , micros () );
66+ Serial.printf (" >>>>>>>>>> Message not sent\n " );
7467 sent = true ;
7568 }
7669
0 commit comments