|
EnigmaIOT
0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
|
Go to the documentation of this file.
25 if (esp_now_init ()) {
31 esp_now_set_self_role (ESP_NOW_ROLE_CONTROLLER);
41 DEBUG_DBG (
"WIFI channel is %d", WiFi.channel ());
46 esp_now_set_self_role (ESP_NOW_ROLE_SLAVE);
50 esp_now_register_recv_cb (
reinterpret_cast<esp_now_recv_cb_t
>(
rx_cb));
51 esp_now_register_send_cb (
reinterpret_cast<esp_now_send_cb_t
>(
tx_cb));
72 DEBUG_DBG (
"Ready to send: true");
95 esp_now_peer_info_t peer;
98 wifi_second_chan_t secondCh;
99 esp_wifi_get_channel (&ch, &secondCh);
102 peer.ifidx = WIFI_IF_STA;
104 peer.ifidx = WIFI_IF_AP;
106 peer.encrypt =
false;
107 esp_err_t error = esp_now_add_peer (&peer);
108 DEBUG_DBG (
"Peer %s added on channel %u. Result 0x%X %s",
mac2str (da), ch, error, esp_err_to_name (error));
109 return error == ESP_OK;
116 DEBUG_INFO (
"-------------> ESP-NOW STOP");
117 esp_now_unregister_recv_cb ();
118 esp_now_unregister_send_cb ();
125 if (!da || !
data || !len) {
126 DEBUG_WARN (
"Parameters error");
131 DEBUG_WARN (
"Length error");
144 DEBUG_DBG (
"%d Comms messages queued. Type: 0x%02X Len: %d",
out_queue.
size (),
data[0], len);
147 DEBUG_WARN (
"Error queuing Comms message 0x%02X to %s",
data[0],
mac2str (da));
154 DEBUG_DBG (
"Comms message got from queue");
169 DEBUG_DBG (
"Comms message pop. Queue size %d",
out_queue.
size ());
187 DEBUG_DBG (
"Peer added");
192 DEBUG_DBG (
"Ready to send: false");
195 DEBUG_DBG (
"esp now send result = %s", esp_err_to_name(error));
197 error = esp_now_del_peer (message->
dstAddress);
198 DEBUG_DBG (
"Peer deleted. Result %s", esp_err_to_name(error));
236 vTaskDelay (1 / portTICK_PERIOD_MS);
EnigmaIOTRingBuffer< comms_queue_item_t > out_queue
peerType_t
Peer role on communication.
static const size_t ENIGMAIOT_ADDR_LEN
Address size. Mac address = 6 bytes.
bool addPeer(const uint8_t *da)
Adds a peer to esp-now peer list.
static const uint8_t MAX_MESSAGE_LENGTH
Maximum payload size on ESP-NOW.
int size()
Returns actual number of elements that buffer holds.
Definition for ESP-NOW hardware abstraction layer.
bool push(Telement *item)
Adds a new item to buffer, deleting older element if it is full.
void handle() override
Sends next message in the queue.
void begin(uint8_t *gateway, uint8_t channel=0, peerType_t peerType=COMM_NODE) override
Setup communication environment and establish the connection from node to gateway.
static void ICACHE_FLASH_ATTR tx_cb(uint8_t *mac_addr, uint8_t status)
Function that gets sending status.
bool empty()
Checks if buffer is empty.
bool pop()
Deletes older item from buffer, if buffer is not empty.
static void ICACHE_FLASH_ATTR rx_cb(uint8_t *mac_addr, uint8_t *data, uint8_t len)
Function that processes incoming messages and passes them to upper layer.
comms_queue_item_t * getCommsQueue()
uint8_t dstAddress[ENIGMAIOT_ADDR_LEN]
int32_t send(uint8_t *da, uint8_t *data, int len) override
Sends data to the other peer.
comms_hal_sent_data sentResult
Pointer to a function to be called to notify last sending status.
Telement * front()
Gets a pointer to older item in buffer, if buffer is not empty.
static const uint8_t COMMS_QUEUE_SIZE
void initComms(peerType_t peerType) override
Communication subsistem initialization.
void onDataSent(comms_hal_sent_data dataRcvd) override
Attach a callback function to be run after sending a message to receive its status.
static const uint8_t BROADCAST_ADDRESS[]
Broadcast address.
void(* comms_hal_rcvd_data)(uint8_t *address, uint8_t *data, uint8_t len)
void onDataRcvd(comms_hal_rcvd_data dataRcvd) override
Attach a callback function to be run on every received message.
comms_hal_rcvd_data dataRcvd
Pointer to a function to be called on every received message.
peerType_t _ownPeerType
Stores peer type, node or gateway.
Espnow_halClass Espnow_hal
Singleton instance of ESP-NOW class.
int32_t sendEspNowMessage(comms_queue_item_t *message)
char * mac2str(const uint8_t *mac, char *extBuffer)
Debug helper function that generates a string that represent a MAC address.
static void runHandle(void *param)
Static function that calls handle inside task.
void(* comms_hal_sent_data)(uint8_t *address, uint8_t status)
ESP-NOW communication system abstraction layer. To be used on ESP8266 or ESP32 platforms.
void stop() override
Terminates communication and closes all connectrions.
uint8_t gateway[COMMS_HAL_ADDR_LEN]
Gateway address.
uint8_t channel
Comms channel to be used.
static const uint8_t COMMS_HAL_ADDR_LEN
Address length for ESP-NOW. Correspond to mac address.
uint8_t payload[MAX_MESSAGE_LENGTH]