Skip to content

Commit 6191ec8

Browse files
1 parent fcc7331 commit 6191ec8

File tree

5 files changed

+155
-90
lines changed

5 files changed

+155
-90
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/***THIS IS A VERY SIMPLE EXCAMPLE THAT DOENT CHECK THE MACK OF THE SENDER IT JUST READS THE VALUE ALSO WE ASSUME THAT ONLY INTEGERS ARE SEND***/
2+
3+
#include "QuickESPNow.h"
4+
5+
#define MAX_PEERS 2
6+
7+
#define ID 2
8+
#define SENDER 1
9+
#define RECEIVER1 2
10+
#define RECEIVER2 3
11+
#define TWOWAY 4
12+
13+
#define CHANNEL 0
14+
15+
16+
uint8_t MACS[MAX_PEERS +1 ][MAC_LENGTH] = {
17+
{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},//Sender MAC
18+
{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAB},//RECEIBER1
19+
{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAC},//RECEIVER2
20+
{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAD}//TWOWAY ESP
21+
};
22+
23+
volatile int received_value = -1;
24+
25+
//Use this parameters for esp32 board version after the 2.0.17
26+
//void OnDataRecv(const esp_now_recv_info_t *info, const uint8_t *incomingData, int len);
27+
28+
//Use this parameters for esp32 board that's from 2.0.17 and before
29+
void OnDataRecv(const uint8_t *mac_addr, const uint8_t *incomingData, int len);
30+
31+
QuickESPNow receiver_2(RECEIVER, MAX_PEERS, MACS[ID]);
32+
33+
void setup() {
34+
Serial.begin(115200);
35+
pinMode(led, OUTPUT);
36+
37+
receiver_2.begin();
38+
// delay(500);
39+
receiver_2.addPeer(SENDER, MACS[0], CHANNEL, WIFI_IF_STA);
40+
receiver_2.addPeer(TWOWAY, MACS[3], CHANNEL, WIFI_IF_STA);
41+
receiver_2.FAIL_CHECK();//Since it has print function inside theres no need to print our own
42+
}
43+
44+
void loop() {
45+
received_value = my_esp.read<int>();
46+
Serial.print("Received: ");
47+
Serial.println(received_value);
48+
49+
delay(100);
50+
}
51+
52+
53+
// void customrecv(const esp_now_recv_info_t *info, const uint8_t *incomingData, int len){
54+
// msg_struct receivedData;
55+
// memcpy(&receivedData, incomingData, sizeof(receivedData));
56+
57+
// int current;
58+
// memcpy(&current, &receivedData.data_array[0], sizeof(int));
59+
60+
// received_value = current;
61+
// }
62+
63+
void customrecv(const uint8_t *mac_addr, const uint8_t *incomingData, int len){
64+
msg_struct receivedData;
65+
memcpy(&receivedData, incomingData, sizeof(receivedData));
66+
67+
int current;
68+
memcpy(&current, &receivedData.data_array[0], sizeof(int));
69+
70+
received_value = current;
71+
}
Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
11
#include "QuickESPNow.h"
22

3-
#define ID 3
3+
#define MAX_PEERS 2
44

5-
int timer = 5;
6-
int led = 0;
5+
#define ID 1
6+
#define SENDER 1
7+
#define RECEIVER1 2
8+
#define RECEIVER2 3
9+
#define TWOWAY 4
10+
11+
#define CHANNEL 0
712

8-
uint8_t MACS[4][MAC_LENGTH] = {
9-
{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
10-
{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAB},
11-
{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAC},
12-
{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAD}
13-
};
1413

15-
uint8_t Senders_MAC[MAC_LENGTH] = {0x84, 0xfc, 0xe6, 0x08, 0xee, 0x7c};
14+
uint8_t MACS[MAX_PEERS +1 ][MAC_LENGTH] = {
15+
{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},//Sender MAC
16+
{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAB},//RECEIBER1
17+
{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAC},//RECEIVER2
18+
{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAD}//TWOWAY ESP
19+
};
1620

17-
QuickESPNow my_esp(RECEIVER, 1, MACS[ID]);
21+
QuickESPNow receiver_1(RECEIVER, MAX_PEERS, MACS[ID]);
1822

1923
void setup() {
2024
Serial.begin(115200);
2125
pinMode(led, OUTPUT);
2226

23-
my_esp.begin();
27+
receiver_1.begin();
2428
// delay(500);
25-
my_esp.addPeer(0, senders_MAC, 0, WIFI_IF_STA);
26-
my_esp.FAIL_CHECK();
27-
// my_esp.FAIL_CHECK();
29+
receiver_1.addPeer(SENDER, MACS[0], CHANNEL, WIFI_IF_STA);
30+
receiver_1.addPeer(TWOWAY, MACS[3], CHANNEL, WIFI_IF_STA);
31+
receiver_1.FAIL_CHECK();//Since it has print function inside theres no need to print our own
2832
}
2933

3034
void loop() {
31-
if(my_esp.available()){
32-
timer = my_esp.read<int>();
33-
}
34-
35-
if(timer > 0){
36-
digitalWrite(led, HIGH);
37-
sleep(timer);
38-
digitalWrite(led, LOW);
39-
timer = 0;
35+
int received_value;
36+
if(receiver_1.available()){
37+
received_value = my_esp.read<int>();
38+
Serial.print("Received: ");
39+
Serial.println(received_value);
4040
}
41+
delay(100);
4142
}
Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,52 @@
11
#include "QuickESPNow.h"
22

3+
#define MAX_PEERS 3
34

4-
uint8_t MACS[4][MAC_LENGTH] = {
5-
{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
6-
{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAB},
7-
{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAC},
8-
{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAD}
9-
};
5+
#define ID 0
6+
#define SENDER 1
7+
#define RECEIVER1 2
8+
#define RECEIVER2 3
9+
#define TWOWAY 4
10+
11+
#define CHANNEL 0
1012

11-
uint8_t Senders_MAC[MAC_LENGTH] = {0x84, 0xfc, 0xe6, 0x08, 0xee, 0x7c};
1213

13-
QuickESPNow my_esp(SENDER, 4, Senders_MAC);
14+
uint8_t MACS[MAX_PEERS +1 ][MAC_LENGTH] = {
15+
{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},//Sender MAC
16+
{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAB},//RECEIBER1
17+
{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAC},//RECEIVER2
18+
{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAD}//TWOWAY ESP
19+
};
20+
21+
QuickESPNow sender(SENDER, MAX_PEERS, MACS[ID]);
1422

1523
void setup() {
1624
Serial.begin(115200);
1725

1826
// delay(500);
19-
my_esp.begin();
20-
my_esp.addPeer(0, MACS[0], 0, WIFI_IF_STA);
21-
my_esp.addPeer(1, MACS[1], 0, WIFI_IF_STA);
22-
my_esp.addPeer(2, MACS[2], 0, WIFI_IF_STA);
23-
my_esp.addPeer(3, MACS[3], 0, WIFI_IF_STA);
24-
my_esp.FAIL_CHECK();
27+
sender.begin();
28+
sender.addPeer(RECEIVER1, MACS[1], CHANNEL, WIFI_IF_STA);//
29+
sender.addPeer(RECEIVER2, MACS[2], CHANNEL, WIFI_IF_STA);//
30+
sender.addPeer(RECEIVER2, MACS[3], CHANNEL, WIFI_IF_STA);//
31+
sender.FAIL_CHECK();//Since it has print function inside theres no need to print our own
2532
}
2633

2734
void loop() {
28-
my_esp.Send(0, 1);
29-
delay(1000);
30-
my_esp.Send(1, 1);
31-
delay(1000);
32-
my_esp.Send(2, 1);
33-
delay(1000);
34-
my_esp.Send(3, 1);
35+
my_esp.Send(RECEIVER1, 1);
3536
delay(1000);
36-
my_esp.Send(2, 1);
37+
my_esp.Send(RECEIVER2, 1);
3738
delay(1000);
38-
my_esp.Send(1, 1);
39+
my_esp.Send(TWOWAY, 1);
3940
delay(1000);
40-
my_esp.Send(0, 1);
41+
my_esp.Send(RECEIVER1, 1);
42+
my_esp.Send(RECEIVER2, 1);
43+
delay(200);
44+
my_esp.Send(TWOWAY, 1);
4145
delay(1000);
42-
my_esp.Send(3, 5);
43-
delay(5000);
46+
if(!my_esp.Send(RECEIVER1, 4);){
47+
my_esp.Send(TWOWAY, 8);
48+
}else{
49+
my_esp.Send(RECEIVER2, 9);
50+
}
51+
delay(500);
4452
}

src/QuickESPNow.cpp

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ void QuickESPNow::getEspMAC(uint8_t* MAC){
1616

1717

1818
void QuickESPNow::OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status){
19-
Serial.print("\r\nLast Packet Send Status:\t");
20-
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
19+
msg_recved = (status == ESP_NOW_SEND_SUCCESS);
2120
}
2221

2322
#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
@@ -35,6 +34,8 @@ void QuickESPNow::OnDataRecv(const uint8_t *mac_addr, const uint8_t *incomingDat
3534
#endif
3635
uint8_t QuickESPNow::Local_MAC[MAC_LENGTH];
3736

37+
volatile bool QuickESPNow::msg_recved = false;
38+
3839
Msg_Queue QuickESPNow::recieved_msgs;
3940
/***********************************************************************/
4041

@@ -112,10 +113,7 @@ void QuickESPNow::addPeer(int id, uint8_t* Peers_MAC, int Ch, wifi_interface_t m
112113

113114
// Add receiver as peer
114115
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
115-
Serial.println("[Error] Failed to add peer");
116116
return;
117-
}else{
118-
Serial.println("[SUCCESS] peer has been added succesfuly");
119117
}
120118
}
121119

@@ -148,10 +146,7 @@ void QuickESPNow::addPeer(int id, uint8_t* Peers_MAC, int Ch, wifi_interface_t m
148146

149147
// Add receiver as peer
150148
if (esp_now_add_peer(&peerInfo) != ESP_OK){
151-
Serial.println("Failed to add peer");
152149
return;
153-
}else{
154-
Serial.println("[SUCCESS] peer has been added succesfuly");
155150
}
156151
}
157152

@@ -163,10 +158,7 @@ void QuickESPNow::addPeer(int id, esp_now_peer_info_t* Peer){
163158

164159
// Add receiver as peer
165160
if (esp_now_add_peer(Peer) != ESP_OK){
166-
Serial.println("Failed to add peer");
167161
return;
168-
}else{
169-
Serial.println("[SUCCESS] peer has been added succesfuly");
170162
}
171163
}
172164

@@ -194,25 +186,16 @@ void QuickESPNow::begin(){
194186
break;
195187
}
196188

197-
198-
// Read the old MAC
199-
Serial.print("[OLD] ESP32 Board MAC Address: ");
200-
Serial.println(WiFi.macAddress());
201-
202189
delay(100);
203190

204191
// Set new MAC
205192
esp_wifi_set_mac(WIFI_IF_STA, this->Local_MAC);
206193

207194
delay(100);
208195

209-
Serial.print("\r[NEW] ESP32 Board MAC Address: ");
210-
Serial.println(WiFi.macAddress());
211-
212196
// Verify that the new MAC is set correctly
213197

214198
if (!WiFi.macAddress().equals(getMACtoSTRING(this->Local_MAC))) {
215-
Serial.println("\r[Error] Failed to change MAC");
216199
this->setup_errors[this->error_counter] = NEW_MAC_INITIALIZATION_ERROR;
217200
this->error_counter++;
218201
}

0 commit comments

Comments
 (0)