A simple library for creating ESP-NOW based Mesh network for ESP8266/ESP32.
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.