A simple library for creating ESP-NOW based Mesh network for ESP8266/ESP32.
The library is deprecated and is no longer supported. The new version can be viewed at https://github.com/aZholtikov/zh_network.
- The maximum size of transmitted data is 200 bytes.
- Encrypted and unencrypted messages. Simple XOR crypting.
- All nodes are not visible to the network scanner.
- Not required a pre-pairings for data transfer.
- Broadcast or unicast data transmissions.
- There are no periodic/synchronous messages on the network. All devices are in "silent mode" and do not "hum" into the air.
- Each node has its own independent routing table, updated only as needed.
- Each node will receive/send a message if it "sees" at least one device on the network.
- The number of devices on the network and the area of use is not limited (hypothetically). :-)
- Program 2 receivers and 1 transmitter (specify the MAC of the 1st receiver in the transmitter code).
- Connect the 1st receiver to the computer. Switch on serial port monitor. Turn transmitter on. Receiver will start receiving data.
- Move transmitter as far away from receiver as possible until receiver is able to receive data (reduce tx power and shield module if necessary).
- Turn on the 2nd receiver and place it between the 1st receiver and transmitter (preferably in the middle). The 1st receiver will resume data reception (with relaying through the 2nd receiver). P.S. You can use a transmitter instead of the 2nd receiver - makes no difference.
- Voila. ;-)
- P.S. Uncomment #define PRINT_LOG in ZHNetwork.h for display to serial port the full operation log.
- Possibility uses WiFi AP or STA modes at the same time with ESP-NOW using the standard libraries.
- For correct work at ESP-NOW + STA mode your WiFi router must be set on channel 1 and set gateway mode.
Note. Possibility uses one callback function for recieved unicast and broadcast messages.
myNet.setOnBroadcastReceivingCallback(onBroadcastReceiving);
void onBroadcastReceiving(const char *data, const uint8_t *sender)
{
// Do something when receiving a broadcast message.
}
Note. Possibility uses one callback function for recieved unicast and broadcast messages.
myNet.setOnUnicastReceivingCallback(onUnicastReceiving);
void onUnicastReceiving(const char *data, const uint8_t *sender)
{
// Do something when receiving a unicast message.
}
Note. Called only at broadcast or unicast with confirm message. Status will always true at sending broadcast message.
myNet.setOnConfirmReceivingCallback(onConfirmReceiving);
void onConfirmReceiving(const uint8_t *target, const uint16_t id, const bool status)
{
// Do something when receiving a delivery/undelivery confirm message.
}
1-20 characters.
Note. If network name not set node will work with all ESP-NOW networks. If set node will work with only one network.
myNet.begin("ZHNetwork");
myNet.begin("ZHNetwork", true); // Gateway mode.
Returns message ID.
myNet.sendBroadcastMessage("Hello world!");
Returns message ID.
myNet.sendUnicastMessage("Hello world!", target); // Without confirm.
myNet.sendUnicastMessage("Hello world!", target, true); // With confirm.
Attention! Must be uncluded in loop.
myNet.maintenance();
myNet.getNodeMac();
myNet.getFirmwareVersion();
myNet.macToString(mac);
uint8_t mac[6]
myNet.stringToMac(string, mac);
1-20 characters.
myNet.setCryptKey("VERY_LONG_CRYPT_KEY");
1-10. 3 default value.
myNet.setMaxNumberOfAttempts(3);
myNet.getMaxNumberOfAttempts();
50-250 ms. 50 default value.
myNet.setMaxWaitingTimeBetweenTransmissions(50);
myNet.getMaxWaitingTimeBetweenTransmissions();
500-5000 ms. 500 default value.
myNet.setMaxWaitingTimeForRoutingInfo(500);
myNet.getMaxWaitingTimeForRoutingInfo();
#include "ZHNetwork.h"
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 uint16_t id, const bool status);
ZHNetwork myNet;
uint64_t messageLastTime{0};
uint16_t messageTimerDelay{5000};
const uint8_t target[6]{0xA8, 0x48, 0xFA, 0xDC, 0x5B, 0xFA};
void setup()
{
Serial.begin(115200);
Serial.println();
myNet.begin("ZHNetwork");
// myNet.setCryptKey("VERY_LONG_CRYPT_KEY"); // If encryption is used, the key must be set same of all another ESP-NOW devices in network.
myNet.setOnBroadcastReceivingCallback(onBroadcastReceiving);
myNet.setOnUnicastReceivingCallback(onUnicastReceiving);
myNet.setOnConfirmReceivingCallback(onConfirmReceiving);
Serial.print("MAC: ");
Serial.print(myNet.getNodeMac());
Serial.print(". Firmware version: ");
Serial.print(myNet.getFirmwareVersion());
Serial.println(".");
}
void loop()
{
if ((millis() - messageLastTime) > messageTimerDelay)
{
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.");
messageLastTime = millis();
}
myNet.maintenance();
}
void onBroadcastReceiving(const char *data, const uint8_t *sender)
{
Serial.print("Broadcast message from MAC ");
Serial.print(myNet.macToString(sender));
Serial.println(" received.");
Serial.print("Message: ");
Serial.println(data);
}
void onUnicastReceiving(const char *data, const uint8_t *sender)
{
Serial.print("Unicast message from MAC ");
Serial.print(myNet.macToString(sender));
Serial.println(" received.");
Serial.print("Message: ");
Serial.println(data);
}
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.");
}
Any feedback via e-mail would be appreciated. Or... Buy me a coffee.