-
Notifications
You must be signed in to change notification settings - Fork 9
/
espnowBroadcast.cpp
132 lines (115 loc) · 3.08 KB
/
espnowBroadcast.cpp
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//#define DEBUG_PRINTS
#ifdef ESP32
#include <esp_now.h>
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#include <Esp.h>
#include <espnow.h>
#define ESP_OK 0
#endif
#include "espnowBroadcast.h"
const unsigned char broadcast_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
bool init_done = false;
void(*espnowCB)(const uint8_t *, int, const uint8_t *) = NULL;
#ifdef ESP32
void esp_msg_recv_cb(const uint8_t *mac_addr, const uint8_t *data, int len)
#else
void esp_msg_recv_cb(u8 *mac_addr, u8 *data, u8 len)
#endif
{
#ifdef DEBUG_PRINTS
char macStr[18];
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
Serial.print("Last Packet Recv from: "); Serial.println(macStr);
#endif
//Serial.print(".");
if ( espnowCB != NULL ) {
espnowCB(data, len, mac_addr);
}
}
#ifdef DEBUG_PRINTS
bool sending = false;
long send_ts = 0;
#endif
#ifdef ESP32
static void msg_send_cb(const uint8_t* mac, esp_now_send_status_t sendStatus)
{
#ifdef DEBUG_PRINTS
Serial.print("^");
Serial.println(sendStatus);
#endif
}
#else
static void msg_send_cb(u8* mac_addr, u8 status)
{
#ifdef DEBUG_PRINTS
sending = false;
Serial.print("^");
Serial.print(status);
Serial.print(" > elapsed: ");
Serial.println(micros() - send_ts);
#endif
}
#endif
void espnowBroadcast_begin(int channel) {
// takes too much time - now it's external
//WiFi.mode(WIFI_STA);
//WiFi.disconnect();
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Set up callback
esp_now_register_recv_cb(esp_msg_recv_cb);
esp_now_register_send_cb(msg_send_cb);
#ifdef ESP32
static esp_now_peer_info_t slave;
memset(&slave, 0, sizeof(slave));
for (int ii = 0; ii < 6; ++ii) {
slave.peer_addr[ii] = (uint8_t)0xff;
}
slave.channel = channel; // pick a channel
slave.encrypt = 0; // no encryption
const esp_now_peer_info_t *peer = &slave;
const uint8_t *peer_addr = slave.peer_addr;
esp_now_add_peer(peer);
#else
esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
esp_now_add_peer((u8*)broadcast_mac, ESP_NOW_ROLE_SLAVE, channel, NULL, 0);
#endif
init_done = true;
}
void espnowBroadcast_send(const uint8_t *d, int len){
if (init_done == false) {
#ifdef DEBUG_PRINTS
Serial.println("espnowBroadcast not initialized");
#endif
return;
}
#ifdef ESP32
esp_now_send(broadcast_mac, (uint8_t*)(d), len);
#else
#ifdef DEBUG_PRINTS
//Serial.print("*");
if (sending) {
Serial.print("Error - we did't receive sent callback!, last sent was: ");
Serial.println(micros() - send_ts);
//delay(3);
}
sending = true;
send_ts = micros();
#endif
int result = esp_now_send((u8*)broadcast_mac, (u8*)(d), len);
if (result != ESP_OK) {
#ifdef DEBUG_PRINTS
Serial.print("Error sending the data: ");
Serial.println(result);
#endif
}
#endif
}
void espnowBroadcast_cb(void(*cb)(const uint8_t *, int, const uint8_t *)) {
espnowCB = cb;
}