4 #error "ESP32 constant is not defined."
12 #include "freertos/FreeRTOS.h"
13 #include "freertos/semphr.h"
14 #include "freertos/timers.h"
15 #include "nvs_flash.h"
16 #include "esp_event_loop.h"
17 #include "tcpip_adapter.h"
20 #include "esp_system.h"
22 #include "rom/ets_sys.h"
24 #include "esp_wifi_types.h"
26 static const char *TAG =
"espnow";
30 #if CONFIG_STATION_MODE
31 #define ESPNOW_WIFI_MODE WIFI_MODE_STA
32 #define ESPNOW_WIFI_IF ESP_IF_WIFI_STA
34 #define ESPNOW_WIFI_MODE WIFI_MODE_AP
35 #define ESPNOW_WIFI_IF ESP_IF_WIFI_AP
38 #define ESPNOW_MAX_PACKET 250
39 #define ESPNOW_QUEUE_SIZE 6
41 static uint8_t espnow_broadcast_mac[ESP_NOW_ETH_ALEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
42 #define IS_BROADCAST_ADDR(addr) (memcmp(addr, espnow_broadcast_mac, ESP_NOW_ETH_ALEN) == 0)
45 uint8_t mac_addr[ESP_NOW_ETH_ALEN];
46 esp_now_send_status_t status;
50 uint8_t mac_addr[ESP_NOW_ETH_ALEN];
56 ESPNOW_DATA_BROADCAST,
61 static uint8_t last_mac[ESP_NOW_ETH_ALEN];
62 static TaskHandle_t pjon_task_h = NULL;
63 static xQueueHandle espnow_recv_queue = NULL;
65 static void espnow_send_cb(
const uint8_t *mac_addr, esp_now_send_status_t status)
69 xTaskNotifyGive(pjon_task_h);
70 if(mac_addr == NULL) {
71 ESP_LOGE(TAG,
"Send cb arg error");
76 static void espnow_recv_cb(
const uint8_t *mac_addr,
const uint8_t *
data,
int len)
79 if(mac_addr == NULL ||
data == NULL || len <= 0) {
80 ESP_LOGE(TAG,
"Receive cb arg error");
83 memcpy(packet.mac_addr, mac_addr, ESP_NOW_ETH_ALEN);
84 packet.data = (uint8_t *)malloc(len);
85 if(packet.data == NULL) {
86 ESP_LOGE(TAG,
"Malloc receive data fail");
89 memcpy(packet.data,
data, len);
90 packet.data_len = len;
92 if(xQueueSend(espnow_recv_queue, &packet, 0) != pdTRUE) {
93 ESP_LOGW(TAG,
"Send receive queue fail");
100 uint8_t _magic_header[4];
101 uint8_t _channel = 14;
102 uint8_t _esp_pmk[16];
105 void add_node_mac(uint8_t mac_addr[ESP_NOW_ETH_ALEN])
107 ESP_ERROR_CHECK(add_peer(mac_addr));
110 esp_err_t add_peer(uint8_t mac_addr[ESP_NOW_ETH_ALEN])
112 if(esp_now_is_peer_exist(mac_addr)) {
116 esp_now_peer_info_t *peer =
117 (esp_now_peer_info_t *)malloc(
sizeof(esp_now_peer_info_t));
119 ESP_LOGE(TAG,
"Malloc peer information fail");
120 vSemaphoreDelete(espnow_recv_queue);
124 memset(peer, 0,
sizeof(esp_now_peer_info_t));
125 peer->channel = _channel;
126 peer->ifidx = ESPNOW_WIFI_IF;
127 if(IS_BROADCAST_ADDR(mac_addr)) {
128 peer->encrypt =
false;
134 memcpy(peer->peer_addr, mac_addr, ESP_NOW_ETH_ALEN);
135 ESP_ERROR_CHECK(esp_now_add_peer(peer));
140 bool begin(uint8_t channel, uint8_t *espnow_pmk)
142 esp_err_t ret = nvs_flash_init();
144 ret == ESP_ERR_NVS_NO_FREE_PAGES
148 ESP_ERROR_CHECK(nvs_flash_erase());
149 ret = nvs_flash_init();
151 ESP_ERROR_CHECK(ret);
152 pjon_task_h = xTaskGetCurrentTaskHandle();
154 memcpy(_esp_pmk, espnow_pmk, 16);
155 if(espnow_recv_queue != NULL) {
160 if(espnow_recv_queue == NULL) {
161 ESP_LOGE(TAG,
"Create mutex fail");
164 tcpip_adapter_init();
165 wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
166 ESP_ERROR_CHECK(esp_wifi_init(&cfg));
167 ESP_ERROR_CHECK(esp_wifi_set_country(&wifi_country));
168 ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
169 ESP_ERROR_CHECK(esp_wifi_set_mode(ESPNOW_WIFI_MODE));
174 ESP_ERROR_CHECK(esp_wifi_start());
175 ESP_ERROR_CHECK(esp_wifi_set_promiscuous(
true));
176 ESP_ERROR_CHECK(esp_wifi_set_channel(_channel, WIFI_SECOND_CHAN_NONE));
178 ESP_ERROR_CHECK(esp_now_init());
179 ESP_ERROR_CHECK(esp_now_register_send_cb(espnow_send_cb));
180 ESP_ERROR_CHECK(esp_now_register_recv_cb(espnow_recv_cb));
182 ESP_ERROR_CHECK(esp_now_set_pmk(_esp_pmk));
184 add_peer(espnow_broadcast_mac);
188 uint16_t receive_frame(uint8_t *
data, uint16_t max_length)
192 if(xQueueReceive(espnow_recv_queue, &packet, 0) == pdTRUE) {
193 if(packet.data_len >= 4) {
194 uint8_t len = packet.data_len - 4;
196 (packet.data[0] ^ len) != _magic_header[0] ||
197 (packet.data[1] ^ len) != _magic_header[1] ||
198 (packet.data[packet.data_len - 2] ^ len) != _magic_header[2] ||
199 (packet.data[packet.data_len - 1] ^ len) != _magic_header[3]
201 ESP_LOGE(TAG,
"magic mismatch");
205 if(len > max_length) {
207 ESP_LOGE(TAG,
"buffer overflow - %d bytes but max is %d", len, max_length);
210 memcpy(
data, packet.data + 2, len);
213 memcpy(last_mac, packet.mac_addr, ESP_NOW_ETH_ALEN);
216 ESP_LOGE(TAG,
"packet < 4 received");
227 uint8_t dest_mac[ESP_NOW_ETH_ALEN]
230 uint8_t packet[ESPNOW_MAX_PACKET];
231 if(length + 4 > ESPNOW_MAX_PACKET) {
232 ESP_LOGE(TAG,
"Packet send error - too long :%d", length + 4);
235 uint8_t len = length;
236 packet[0] = _magic_header[0] ^ len;
237 packet[1] = _magic_header[1] ^ len;
238 memcpy(packet + 2,
data, len);
239 packet[len + 2] = _magic_header[2] ^ len;
240 packet[len + 3] = _magic_header[3] ^ len;
241 if(esp_now_send(dest_mac, packet, len + 4) != ESP_OK) {
242 ESP_LOGE(TAG,
"Send error");
244 ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
248 void send_response(uint8_t response)
250 send_frame(&response, 1, last_mac);
253 void send_frame(uint8_t *
data, uint16_t length)
256 send_frame(
data, length, espnow_broadcast_mac);
259 void set_magic_header(uint8_t *magic_header)
261 memcpy(_magic_header, magic_header, 4);
264 void get_sender(uint8_t *ip)
266 memcpy(ip, last_mac, ESP_NOW_ETH_ALEN);