Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
32f7c013b8 | |||
62acd2a386 | |||
506acf1fc0 |
25
README.md
25
README.md
@ -5,10 +5,10 @@ A simple library for creating ESP-NOW based Mesh network for ESP8266/ESP32.
|
||||
## Features
|
||||
|
||||
1. The maximum size of transmitted data is 200 bytes. Currently only unencrypted messages.
|
||||
2. All nodes are not visible to the network scanner (for the ESP_NOW mode only).
|
||||
2. All nodes are not visible to the network scanner.
|
||||
3. Not required a pre-pairings for data transfer.
|
||||
4. Broadcast or unicast data transmission.
|
||||
5. There are no periodic/synchronous messages on the network. All devices are in "silent mode" and do not "hum" into the air (for the ESP_NOW mode only).
|
||||
5. There are no periodic/synchronous messages on the network. All devices are in "silent mode" and do not "hum" into the air.
|
||||
6. Each node has its own independent routing table, updated only as needed.
|
||||
7. Each node will receive/send a message if it "sees" at least one device on the network.
|
||||
8. The number of devices on the network and the area of use is not limited (hypothetically). :-)
|
||||
@ -25,7 +25,7 @@ A simple library for creating ESP-NOW based Mesh network for ESP8266/ESP32.
|
||||
## Notes
|
||||
|
||||
1. Possibility uses WiFi AP or STA modes at the same time with ESP-NOW using the standard libraries.
|
||||
2. For correct work at ESP-NOW + STA mode your WiFi router must be set on channel 1.
|
||||
2. For correct work at ESP-NOW + STA mode your WiFi router must be set on channel 1 and set gateway mode.
|
||||
|
||||
## Function descriptions
|
||||
|
||||
@ -59,7 +59,7 @@ Note. Called only at broadcast or unicast with confirm message. Status will alwa
|
||||
|
||||
```cpp
|
||||
myNet.setOnConfirmReceivingCallback(onConfirmReceiving);
|
||||
void onConfirmReceiving(const uint8_t *target, const bool status)
|
||||
void onConfirmReceiving(const uint8_t *target, const uint16_t id, const bool status)
|
||||
{
|
||||
// Do something when receiving a delivery/undelivery confirm message.
|
||||
}
|
||||
@ -73,16 +73,21 @@ Note. If network name not set node will work with all ESP-NOW networks. If set n
|
||||
|
||||
```cpp
|
||||
myNet.begin("ZHNetwork");
|
||||
myNet.begin("ZHNetwork", true); // Gateway mode.
|
||||
```
|
||||
|
||||
### Sends broadcast message to all nodes
|
||||
|
||||
Returns message ID.
|
||||
|
||||
```cpp
|
||||
myNet.sendBroadcastMessage("Hello world!");
|
||||
```
|
||||
|
||||
### Sends unicast message to node
|
||||
|
||||
Returns message ID.
|
||||
|
||||
```cpp
|
||||
myNet.sendUnicastMessage("Hello world!", target); // Without confirm.
|
||||
myNet.sendUnicastMessage("Hello world!", target, true); // With confirm.
|
||||
@ -170,7 +175,7 @@ myNet.getMaxWaitingTimeForRoutingInfo();
|
||||
|
||||
void onBroadcastReceiving(const char *data, const uint8_t *sender);
|
||||
void onUnicastReceiving(const char *data, const uint8_t *sender);
|
||||
void onConfirmReceiving(const uint8_t *target, const bool status);
|
||||
void onConfirmReceiving(const uint8_t *target, const uint16_t id, const bool status);
|
||||
|
||||
ZHNetwork myNet;
|
||||
|
||||
@ -199,14 +204,18 @@ void loop()
|
||||
{
|
||||
Serial.println("Broadcast message sended.");
|
||||
myNet.sendBroadcastMessage("Hello world!");
|
||||
|
||||
Serial.print("Unicast message to MAC ");
|
||||
Serial.print(myNet.macToString(target));
|
||||
Serial.println(" sended.");
|
||||
myNet.sendUnicastMessage("Hello world!", target);
|
||||
|
||||
Serial.print("Unicast with confirm message to MAC ");
|
||||
Serial.print(myNet.macToString(target));
|
||||
Serial.print(" ID ");
|
||||
Serial.print(myNet.sendUnicastMessage("Hello world!", target, true));
|
||||
Serial.println(" sended.");
|
||||
myNet.sendUnicastMessage("Hello world!", target, true);
|
||||
|
||||
messageLastTime = millis();
|
||||
}
|
||||
myNet.maintenance();
|
||||
@ -229,10 +238,12 @@ void onUnicastReceiving(const char *data, const uint8_t *sender)
|
||||
Serial.println(data);
|
||||
}
|
||||
|
||||
void onConfirmReceiving(const uint8_t *target, const bool status)
|
||||
void onConfirmReceiving(const uint8_t *target, const uint16_t id, const bool status)
|
||||
{
|
||||
Serial.print("Message to MAC ");
|
||||
Serial.print(myNet.macToString(target));
|
||||
Serial.print(" ID ");
|
||||
Serial.print(id);
|
||||
Serial.println(status ? " delivered." : " undelivered.");
|
||||
}
|
||||
```
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "ZHNetwork.h"
|
||||
|
||||
void onConfirmReceiving(const uint8_t *target, const bool status);
|
||||
void onConfirmReceiving(const uint8_t *target, const uint16_t id, const bool status);
|
||||
|
||||
ZHNetwork myNet;
|
||||
|
||||
@ -27,22 +27,28 @@ void loop()
|
||||
{
|
||||
Serial.println("Broadcast message sended.");
|
||||
myNet.sendBroadcastMessage("Hello world!");
|
||||
|
||||
Serial.print("Unicast message to MAC ");
|
||||
Serial.print(myNet.macToString(target));
|
||||
Serial.println(" sended.");
|
||||
myNet.sendUnicastMessage("Hello world!", target);
|
||||
|
||||
Serial.print("Unicast with confirm message to MAC ");
|
||||
Serial.print(myNet.macToString(target));
|
||||
Serial.print(" ID ");
|
||||
Serial.print(myNet.sendUnicastMessage("Hello world!", target, true));
|
||||
Serial.println(" sended.");
|
||||
myNet.sendUnicastMessage("Hello world!", target, true);
|
||||
|
||||
messageLastTime = millis();
|
||||
}
|
||||
myNet.maintenance();
|
||||
}
|
||||
|
||||
void onConfirmReceiving(const uint8_t *target, const bool status)
|
||||
void onConfirmReceiving(const uint8_t *target, const uint16_t id, const bool status)
|
||||
{
|
||||
Serial.print("Message to MAC ");
|
||||
Serial.print(myNet.macToString(target));
|
||||
Serial.print(" ID ");
|
||||
Serial.print(id);
|
||||
Serial.println(status ? " delivered." : " undelivered.");
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
name=ZHNetwork
|
||||
version=1.3
|
||||
version=1.32
|
||||
author=Alexey Zholtikov
|
||||
maintainer=Alexey Zholtikov
|
||||
sentence=ESP-NOW based Mesh network for ESP8266/ESP32
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include "ZHNetwork.h"
|
||||
|
||||
routing_vector_t ZHNetwork::routingVector;
|
||||
confirmation_vector_t ZHNetwork::confirmationVector;
|
||||
incoming_queue_t ZHNetwork::queueForIncomingData;
|
||||
outgoing_queue_t ZHNetwork::queueForOutgoingData;
|
||||
waiting_queue_t ZHNetwork::queueForRoutingVectorWaiting;
|
||||
@ -31,7 +32,7 @@ ZHNetwork &ZHNetwork::setOnConfirmReceivingCallback(on_confirm_t onConfirmReceiv
|
||||
return *this;
|
||||
}
|
||||
|
||||
error_code_t ZHNetwork::begin(const char *netName)
|
||||
error_code_t ZHNetwork::begin(const char *netName, const bool gateway)
|
||||
{
|
||||
randomSeed(analogRead(0));
|
||||
if (strlen(netName) > 1 && strlen(netName) < 20)
|
||||
@ -39,28 +40,28 @@ error_code_t ZHNetwork::begin(const char *netName)
|
||||
#ifdef PRINT_LOG
|
||||
Serial.begin(115200);
|
||||
#endif
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.mode(gateway ? WIFI_AP_STA : WIFI_STA);
|
||||
esp_now_init();
|
||||
#if defined(ESP8266)
|
||||
wifi_get_macaddr(STATION_IF, localMAC);
|
||||
wifi_get_macaddr(gateway ? SOFTAP_IF : STATION_IF, localMAC);
|
||||
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
|
||||
#endif
|
||||
#if defined(ESP32)
|
||||
esp_wifi_get_mac((wifi_interface_t)ESP_IF_WIFI_STA, localMAC);
|
||||
esp_wifi_get_mac(gateway ? (wifi_interface_t)ESP_IF_WIFI_AP : (wifi_interface_t)ESP_IF_WIFI_STA, localMAC);
|
||||
#endif
|
||||
esp_now_register_send_cb(onDataSent);
|
||||
esp_now_register_recv_cb(onDataReceive);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
void ZHNetwork::sendBroadcastMessage(const char *data)
|
||||
uint16_t ZHNetwork::sendBroadcastMessage(const char *data)
|
||||
{
|
||||
broadcastMessage(data, broadcastMAC, BROADCAST);
|
||||
return broadcastMessage(data, broadcastMAC, BROADCAST);
|
||||
}
|
||||
|
||||
void ZHNetwork::sendUnicastMessage(const char *data, const uint8_t *target, const bool confirm)
|
||||
uint16_t ZHNetwork::sendUnicastMessage(const char *data, const uint8_t *target, const bool confirm)
|
||||
{
|
||||
unicastMessage(data, target, localMAC, confirm ? UNICAST_WITH_CONFIRM : UNICAST);
|
||||
return unicastMessage(data, target, localMAC, confirm ? UNICAST_WITH_CONFIRM : UNICAST);
|
||||
}
|
||||
|
||||
void ZHNetwork::maintenance()
|
||||
@ -80,7 +81,15 @@ void ZHNetwork::maintenance()
|
||||
esp_now_del_peer(outgoingData.intermediateTargetMAC);
|
||||
#endif
|
||||
if (onConfirmReceivingCallback && macToString(outgoingData.transmittedData.originalSenderMAC) == macToString(localMAC) && outgoingData.transmittedData.messageType == BROADCAST)
|
||||
onConfirmReceivingCallback(outgoingData.transmittedData.originalTargetMAC, true);
|
||||
onConfirmReceivingCallback(outgoingData.transmittedData.originalTargetMAC, outgoingData.transmittedData.messageID, true);
|
||||
if (macToString(outgoingData.transmittedData.originalSenderMAC) == macToString(localMAC) && outgoingData.transmittedData.messageType == UNICAST_WITH_CONFIRM)
|
||||
{
|
||||
confirmation_waiting_data_t confirmationData;
|
||||
confirmationData.time = millis();
|
||||
memcpy(&confirmationData.targetMAC, &outgoingData.transmittedData.originalTargetMAC, 6);
|
||||
memcpy(&confirmationData.messageID, &outgoingData.transmittedData.messageID, 2);
|
||||
confirmationVector.push_back(confirmationData);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -218,7 +227,11 @@ void ZHNetwork::maintenance()
|
||||
{
|
||||
if (onUnicastReceivingCallback)
|
||||
onUnicastReceivingCallback(incomingData.transmittedData.message, incomingData.transmittedData.originalSenderMAC);
|
||||
unicastMessage("", incomingData.transmittedData.originalSenderMAC, localMAC, DELIVERY_CONFIRM_RESPONSE);
|
||||
confirmation_id_t id;
|
||||
memcpy(&id.messageID, &incomingData.transmittedData.messageID, 2);
|
||||
char temp[sizeof(transmitted_data_t::message)];
|
||||
memcpy(&temp, &id, sizeof(transmitted_data_t::message));
|
||||
unicastMessage(temp, incomingData.transmittedData.originalSenderMAC, localMAC, DELIVERY_CONFIRM_RESPONSE);
|
||||
}
|
||||
else
|
||||
unicastMessage(incomingData.transmittedData.message, incomingData.transmittedData.originalTargetMAC, incomingData.transmittedData.originalSenderMAC, UNICAST_WITH_CONFIRM);
|
||||
@ -236,7 +249,17 @@ void ZHNetwork::maintenance()
|
||||
if (macToString(incomingData.transmittedData.originalTargetMAC) == macToString(localMAC))
|
||||
{
|
||||
if (onConfirmReceivingCallback)
|
||||
onConfirmReceivingCallback(incomingData.transmittedData.originalSenderMAC, true);
|
||||
{
|
||||
confirmation_id_t id;
|
||||
memcpy(&id.messageID, &incomingData.transmittedData.message, 2);
|
||||
for (uint16_t i{0}; i < confirmationVector.size(); ++i)
|
||||
{
|
||||
confirmation_waiting_data_t confirmationData = confirmationVector[i];
|
||||
if (confirmationData.messageID == id.messageID)
|
||||
confirmationVector.erase(confirmationVector.begin() + i);
|
||||
}
|
||||
onConfirmReceivingCallback(incomingData.transmittedData.originalSenderMAC, id.messageID, true);
|
||||
}
|
||||
}
|
||||
else
|
||||
unicastMessage(incomingData.transmittedData.message, incomingData.transmittedData.originalTargetMAC, incomingData.transmittedData.originalSenderMAC, DELIVERY_CONFIRM_RESPONSE);
|
||||
@ -374,7 +397,21 @@ void ZHNetwork::maintenance()
|
||||
#endif
|
||||
if (waitingData.transmittedData.messageType == UNICAST_WITH_CONFIRM && macToString(waitingData.transmittedData.originalSenderMAC) == macToString(localMAC))
|
||||
if (onConfirmReceivingCallback)
|
||||
onConfirmReceivingCallback(waitingData.transmittedData.originalTargetMAC, false);
|
||||
onConfirmReceivingCallback(waitingData.transmittedData.originalTargetMAC, waitingData.transmittedData.messageID, false);
|
||||
}
|
||||
}
|
||||
if (confirmationVector.size())
|
||||
{
|
||||
for (uint16_t i{0}; i < confirmationVector.size(); ++i)
|
||||
{
|
||||
confirmation_waiting_data_t confirmationData = confirmationVector[i];
|
||||
if ((millis() - confirmationData.time) > 1000)
|
||||
{
|
||||
confirmationVector.erase(confirmationVector.begin() + i);
|
||||
broadcastMessage("", confirmationData.targetMAC, SEARCH_REQUEST);
|
||||
if (onConfirmReceivingCallback)
|
||||
onConfirmReceivingCallback(confirmationData.targetMAC, confirmationData.messageID, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -505,7 +542,7 @@ void IRAM_ATTR ZHNetwork::onDataReceive(uint8_t *mac, uint8_t *data, uint8_t len
|
||||
criticalProcessSemaphore = false;
|
||||
}
|
||||
|
||||
void ZHNetwork::broadcastMessage(const char *data, const uint8_t *target, message_type_t type)
|
||||
uint16_t ZHNetwork::broadcastMessage(const char *data, const uint8_t *target, message_type_t type)
|
||||
{
|
||||
outgoing_data_t outgoingData;
|
||||
outgoingData.transmittedData.messageType = type;
|
||||
@ -537,9 +574,10 @@ void ZHNetwork::broadcastMessage(const char *data, const uint8_t *target, messag
|
||||
Serial.print(macToString(outgoingData.transmittedData.originalTargetMAC));
|
||||
Serial.println(F(" added to queue."));
|
||||
#endif
|
||||
return outgoingData.transmittedData.messageID;
|
||||
}
|
||||
|
||||
void ZHNetwork::unicastMessage(const char *data, const uint8_t *target, const uint8_t *sender, message_type_t type)
|
||||
uint16_t ZHNetwork::unicastMessage(const char *data, const uint8_t *target, const uint8_t *sender, message_type_t type)
|
||||
{
|
||||
outgoing_data_t outgoingData;
|
||||
outgoingData.transmittedData.messageType = type;
|
||||
@ -583,7 +621,7 @@ void ZHNetwork::unicastMessage(const char *data, const uint8_t *target, const ui
|
||||
Serial.print(macToString(outgoingData.intermediateTargetMAC));
|
||||
Serial.println(F(" added to queue."));
|
||||
#endif
|
||||
return;
|
||||
return outgoingData.transmittedData.messageID;
|
||||
}
|
||||
}
|
||||
memcpy(&outgoingData.intermediateTargetMAC, target, 6);
|
||||
@ -616,4 +654,5 @@ void ZHNetwork::unicastMessage(const char *data, const uint8_t *target, const ui
|
||||
Serial.print(macToString(outgoingData.intermediateTargetMAC));
|
||||
Serial.println(F(" added to queue."));
|
||||
#endif
|
||||
return outgoingData.transmittedData.messageID;
|
||||
}
|
@ -50,6 +50,19 @@ typedef struct
|
||||
uint8_t intermediateTargetMAC[6]{0};
|
||||
} routing_table_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint16_t messageID{0};
|
||||
char empty[198]{0}; // Just only to prevent compiler warnings.
|
||||
} confirmation_id_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint64_t time{0};
|
||||
uint8_t targetMAC[6]{0};
|
||||
uint16_t messageID{0};
|
||||
} confirmation_waiting_data_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
BROADCAST = 1,
|
||||
@ -67,8 +80,9 @@ typedef enum // Just for further development.
|
||||
} error_code_t;
|
||||
|
||||
typedef std::function<void(const char *, const uint8_t *)> on_message_t;
|
||||
typedef std::function<void(const uint8_t *, const bool)> on_confirm_t;
|
||||
typedef std::function<void(const uint8_t *, const uint16_t, const bool)> on_confirm_t;
|
||||
typedef std::vector<routing_table_t> routing_vector_t;
|
||||
typedef std::vector<confirmation_waiting_data_t> confirmation_vector_t;
|
||||
typedef std::queue<outgoing_data_t> outgoing_queue_t;
|
||||
typedef std::queue<incoming_data_t> incoming_queue_t;
|
||||
typedef std::queue<waiting_data_t> waiting_queue_t;
|
||||
@ -80,10 +94,10 @@ public:
|
||||
ZHNetwork &setOnUnicastReceivingCallback(on_message_t onUnicastReceivingCallback);
|
||||
ZHNetwork &setOnConfirmReceivingCallback(on_confirm_t onConfirmReceivingCallback);
|
||||
|
||||
error_code_t begin(const char *netName = "");
|
||||
error_code_t begin(const char *netName = "", const bool gateway = false);
|
||||
|
||||
void sendBroadcastMessage(const char *data);
|
||||
void sendUnicastMessage(const char *data, const uint8_t *target, const bool confirm = false);
|
||||
uint16_t sendBroadcastMessage(const char *data);
|
||||
uint16_t sendUnicastMessage(const char *data, const uint8_t *target, const bool confirm = false);
|
||||
|
||||
void maintenance(void);
|
||||
|
||||
@ -103,6 +117,7 @@ public:
|
||||
|
||||
private:
|
||||
static routing_vector_t routingVector;
|
||||
static confirmation_vector_t confirmationVector;
|
||||
static incoming_queue_t queueForIncomingData;
|
||||
static outgoing_queue_t queueForOutgoingData;
|
||||
static waiting_queue_t queueForRoutingVectorWaiting;
|
||||
@ -115,7 +130,7 @@ private:
|
||||
static uint16_t lastMessageID[10];
|
||||
static char netName_[20];
|
||||
|
||||
const char *firmware{"1.3"};
|
||||
const char *firmware{"1.32"};
|
||||
const uint8_t broadcastMAC[6]{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
uint8_t maxNumberOfAttempts_{3};
|
||||
uint8_t maxWaitingTimeBetweenTransmissions_{50};
|
||||
@ -131,8 +146,8 @@ private:
|
||||
static void onDataSent(const uint8_t *mac, esp_now_send_status_t status);
|
||||
static void onDataReceive(const uint8_t *mac, const uint8_t *data, int length);
|
||||
#endif
|
||||
void broadcastMessage(const char *data, const uint8_t *target, message_type_t type);
|
||||
void unicastMessage(const char *data, const uint8_t *target, const uint8_t *sender, message_type_t type);
|
||||
uint16_t broadcastMessage(const char *data, const uint8_t *target, message_type_t type);
|
||||
uint16_t unicastMessage(const char *data, const uint8_t *target, const uint8_t *sender, message_type_t type);
|
||||
on_message_t onBroadcastReceivingCallback;
|
||||
on_message_t onUnicastReceivingCallback;
|
||||
on_confirm_t onConfirmReceivingCallback;
|
||||
|
Reference in New Issue
Block a user