Skip to content

Commit 76b50c6

Browse files
committed
Fix examples
1 parent c6bdba7 commit 76b50c6

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

examples/advancedespnow/advancedespnow.cpp

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,31 @@ static uint8_t receiver[] = { 0x12, 0x34, 0x56, 0x78, 0x90, 0x12 };
1818
//#define DEST_ADDR ESPNOW_BROADCAST_ADDRESS
1919

2020
bool sent = true;
21-
bool received = false;
2221

2322
const 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-
3024
void 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

4437
void 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

examples/wifi_ap_and_espnow/wifi_ap_and_espnow.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
#include <Arduino.h>
2+
#if defined ESP32
23
#include <WiFi.h>
3-
#include <QuickEspNow.h>
44
#include <esp_wifi.h>
5+
#elif defined ESP8266
6+
#include <ESP8266WiFi.h>
7+
#define WIFI_MODE_STA WIFI_STA
8+
#define WIFI_MODE_AP WIFI_AP
9+
#else
10+
#error "Unsupported platform"
11+
#endif //ESP32
12+
#include <QuickEspNow.h>
513

614
static const String msg = "Hello esp-now from TTGO";
7-
static uint8_t addr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
8-
static uint8_t addr_t_display[] = { 0x08, 0x3a, 0xf2, 0x69, 0xc4, 0x04 };
9-
static uint8_t addr_ttgo[] = { 0xb4, 0xe6, 0x2d, 0x9d, 0xab, 0x15 };
10-
static uint8_t addr_ttgo_ap[] = { 0xb4, 0xe6, 0x2d, 0x9d, 0xab, 0x16 };
11-
static uint8_t addr_m5[] = { 0x4c, 0x75, 0x25, 0xac, 0xc8, 0x50 };
15+
16+
static uint8_t receiver[] = { 0x12, 0x34, 0x56, 0x78, 0x90, 0x12 };
17+
18+
#define DEST_ADDR receiver
19+
//#define DEST_ADDR ESPNOW_BROADCAST_ADDRESS
1220

1321
static const uint8_t CHANNEL = 1;
1422

@@ -61,12 +69,12 @@ void setup () {
6169

6270
void loop () {
6371
static time_t lastSend = 60000;
64-
static uint counter = 0;
72+
static unsigned int counter = 0;
6573

6674
if (millis () - lastSend >= 1000) {
6775
lastSend = millis ();
6876
String message = String (msg) + " " + String (counter++);
69-
if (!quickEspNow.send (addr_m5, (uint8_t*)message.c_str (), message.length ())) {
77+
if (!quickEspNow.send (DEST_ADDR, (uint8_t*)message.c_str (), message.length ())) {
7078
//Serial.printf (">>>>>>>>>> %ld: Message sent\n", micros());
7179
} else {
7280
Serial.printf (">>>>>>>>>> %ld: Message not sent\n", micros ());

examples/wifi_sta_and_espnow/wifi_sta_and_espnow.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
#include <Arduino.h>
2+
#if defined ESP32
23
#include <WiFi.h>
3-
#include <QuickEspNow.h>
44
#include <esp_wifi.h>
5+
#elif defined ESP8266
6+
#include <ESP8266WiFi.h>
7+
#define WIFI_MODE_STA WIFI_STA
8+
#else
9+
#error "Unsupported platform"
10+
#endif //ESP32
11+
#include <QuickEspNow.h>
512

613
static const String msg = "Hello esp-now!";
714

@@ -36,7 +43,7 @@ void setup () {
3643

3744
void loop() {
3845
static time_t lastSend = 60000;
39-
static uint counter = 0;
46+
static unsigned int counter = 0;
4047

4148
if (millis () - lastSend >= 1000) {
4249
lastSend = millis ();

0 commit comments

Comments
 (0)