Version 1.3

Removed a lot of unnecessary functions.
This commit is contained in:
Alexey Zholtikov 2022-12-22 18:27:17 +03:00
parent 1d5198e0ab
commit b328ad916c
6 changed files with 17 additions and 219 deletions

View File

@ -22,6 +22,11 @@ A simple library for creating ESP-NOW based Mesh network for ESP8266/ESP32.
5. Voila. ;-) 5. Voila. ;-)
6. P.S. Uncomment #define PRINT_LOG in ZHNetwork.h for display to serial port the full operation log. 6. P.S. Uncomment #define PRINT_LOG in ZHNetwork.h for display to serial port the full operation log.
## 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.
## Function descriptions ## Function descriptions
### Sets the callback function for processing a received broadcast message ### Sets the callback function for processing a received broadcast message
@ -60,66 +65,14 @@ void onConfirmReceiving(const uint8_t *target, const bool status)
} }
``` ```
### Sets one of the three possibility operating modes ### ESP-NOW Mesh network initialization
* ESP_NOW. Default mode. ESP-NOW Mesh network only.
* ESP_NOW_AP. ESP-NOW Mesh network + access point.
* ESP_NOW_STA. ESP-NOW Mesh network + connect to your WiFi router.
Attention! For correct work on ESP_NOW_STA mode your WiFi router must be set on channel 1.
```cpp
myNet.setWorkMode(ESP_NOW);
```
### Gets used operating mode
```cpp
myNet.getWorkMode();
```
### Sets ESP-NOW Mesh network name
1-20 characters. 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. Note. If network name not set node will work with all ESP-NOW networks. If set node will work with only one network.
```cpp ```cpp
myNet.setNetName("ZHNetwork"); myNet.begin("ZHNetwork");
```
### Gets used ESP-NOW Mesh network name
```cpp
myNet.getNetName();
```
### Sets WiFi ssid and password for ESP_NOW_STA mode
Note. Must be called before Mesh network initialization.
```cpp
myNet.setStaSetting("SSID", "PASSWORD");
```
### Sets access point ssid and password for ESP_NOW_AP mode
Note. Must be called before Mesh network initialization.
```cpp
myNet.setApSetting("SSID", "PASSWORD");
```
### ESP-NOW Mesh network initialization
```cpp
myNet.begin();
```
### ESP-NOW Mesh network deinitialization
```cpp
myNet.stop();
``` ```
### Sends broadcast message to all nodes ### Sends broadcast message to all nodes
@ -149,12 +102,6 @@ myNet.maintenance();
myNet.getNodeMac(); myNet.getNodeMac();
``` ```
### Gets node IP address
```cpp
myNet.getNodeIp();
```
### Gets version of this library ### Gets version of this library
```cpp ```cpp
@ -235,27 +182,12 @@ void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
Serial.println(); Serial.println();
// *** ESP-NOW mode only. myNet.begin("ZHNetwork");
myNet.setWorkMode(ESP_NOW);
// *** Or ESP-NOW + access point mode.
// myNet.setWorkMode(ESP_NOW_AP);
// myNet.setApSetting("ESP NODE TEST", "12345678");
// *** Or ESP-NOW + connect to your router mode.
// myNet.setWorkMode(ESP_NOW_STA);
// myNet.setStaSetting("SSID", "PASSWORD");
// ***
myNet.setNetName("ZHNetwork"); // Optional.
myNet.setMaxNumberOfAttempts(3); // Optional.
myNet.setMaxWaitingTimeBetweenTransmissions(50); // Optional.
myNet.setMaxWaitingTimeForRoutingInfo(500); // Optional.
myNet.begin();
myNet.setOnBroadcastReceivingCallback(onBroadcastReceiving); myNet.setOnBroadcastReceivingCallback(onBroadcastReceiving);
myNet.setOnUnicastReceivingCallback(onUnicastReceiving); myNet.setOnUnicastReceivingCallback(onUnicastReceiving);
myNet.setOnConfirmReceivingCallback(onConfirmReceiving); myNet.setOnConfirmReceivingCallback(onConfirmReceiving);
Serial.print("MAC: "); Serial.print("MAC: ");
Serial.print(myNet.getNodeMac()); Serial.print(myNet.getNodeMac());
Serial.print(". IP: ");
Serial.print(myNet.getNodeIp());
Serial.print(". Firmware version: "); Serial.print(". Firmware version: ");
Serial.print(myNet.getFirmwareVersion()); Serial.print(myNet.getFirmwareVersion());
Serial.println("."); Serial.println(".");

View File

@ -9,26 +9,11 @@ void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
Serial.println(); Serial.println();
// *** ESP-NOW mode only. myNet.begin("ZHNetwork");
myNet.setWorkMode(ESP_NOW);
// *** Or ESP-NOW + access point mode.
// myNet.setWorkMode(ESP_NOW_AP);
// myNet.setApSetting("ESP NODE TEST", "12345678");
// *** Or ESP-NOW + connect to your router mode.
// myNet.setWorkMode(ESP_NOW_STA);
// myNet.setStaSetting("SSID", "PASSWORD");
// ***
myNet.setNetName("ZHNetwork"); // Optional.
myNet.setMaxNumberOfAttempts(3); // Optional.
myNet.setMaxWaitingTimeBetweenTransmissions(50); // Optional.
myNet.setMaxWaitingTimeForRoutingInfo(500); // Optional.
myNet.begin();
myNet.setOnBroadcastReceivingCallback(onBroadcastReceiving); myNet.setOnBroadcastReceivingCallback(onBroadcastReceiving);
myNet.setOnUnicastReceivingCallback(onUnicastReceiving); myNet.setOnUnicastReceivingCallback(onUnicastReceiving);
Serial.print("MAC: "); Serial.print("MAC: ");
Serial.print(myNet.getNodeMac()); Serial.print(myNet.getNodeMac());
Serial.print(". IP: ");
Serial.print(myNet.getNodeIp());
Serial.print(". Firmware version: "); Serial.print(". Firmware version: ");
Serial.print(myNet.getFirmwareVersion()); Serial.print(myNet.getFirmwareVersion());
Serial.println("."); Serial.println(".");

View File

@ -12,25 +12,10 @@ void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
Serial.println(); Serial.println();
// *** ESP-NOW mode only. myNet.begin("ZHNetwork");
myNet.setWorkMode(ESP_NOW);
// *** Or ESP-NOW + access point mode.
// myNet.setWorkMode(ESP_NOW_AP);
// myNet.setApSetting("ESP NODE TEST", "12345678");
// *** Or ESP-NOW + connect to your router mode.
// myNet.setWorkMode(ESP_NOW_STA);
// myNet.setStaSetting("SSID", "PASSWORD");
// ***
myNet.setNetName("ZHNetwork"); // Optional.
myNet.setMaxNumberOfAttempts(3); // Optional.
myNet.setMaxWaitingTimeBetweenTransmissions(50); // Optional.
myNet.setMaxWaitingTimeForRoutingInfo(500); // Optional.
myNet.begin();
myNet.setOnConfirmReceivingCallback(onConfirmReceiving); myNet.setOnConfirmReceivingCallback(onConfirmReceiving);
Serial.print("MAC: "); Serial.print("MAC: ");
Serial.print(myNet.getNodeMac()); Serial.print(myNet.getNodeMac());
Serial.print(". IP: ");
Serial.print(myNet.getNodeIp());
Serial.print(". Firmware version: "); Serial.print(". Firmware version: ");
Serial.print(myNet.getFirmwareVersion()); Serial.print(myNet.getFirmwareVersion());
Serial.println("."); Serial.println(".");

View File

@ -1,5 +1,5 @@
name=ZHNetwork name=ZHNetwork
version=1.12 version=1.3
author=Alexey Zholtikov author=Alexey Zholtikov
maintainer=Alexey Zholtikov maintainer=Alexey Zholtikov
sentence=ESP-NOW based Mesh network for ESP8266/ESP32 sentence=ESP-NOW based Mesh network for ESP8266/ESP32

View File

@ -31,78 +31,15 @@ ZHNetwork &ZHNetwork::setOnConfirmReceivingCallback(on_confirm_t onConfirmReceiv
return *this; return *this;
} }
error_code_t ZHNetwork::setWorkMode(const work_mode_t workMode) error_code_t ZHNetwork::begin(const char *netName)
{
if (workMode < ESP_NOW || workMode > ESP_NOW_STA)
return ERROR;
workMode_ = workMode;
return SUCCESS;
}
work_mode_t ZHNetwork::getWorkMode()
{
return workMode_;
}
error_code_t ZHNetwork::setNetName(const char *netName)
{
if (strlen(netName) < 1 || strlen(netName) > 20)
return ERROR;
strcpy(netName_, netName);
return SUCCESS;
}
String ZHNetwork::getNetName()
{
return netName_;
}
error_code_t ZHNetwork::setStaSetting(const char *ssid, const char *password)
{
if (strlen(ssid) < 1 || strlen(ssid) > 32 || strlen(password) > 64)
return ERROR;
strcpy(staSsid_, ssid);
strcpy(staPassword_, password);
return SUCCESS;
}
error_code_t ZHNetwork::setApSetting(const char *ssid, const char *password)
{
if (strlen(ssid) < 1 || strlen(ssid) > 32 || strlen(password) < 8 || strlen(password) > 64)
return ERROR;
strcpy(apSsid_, ssid);
strcpy(apPassword_, password);
return SUCCESS;
}
error_code_t ZHNetwork::begin()
{ {
randomSeed(analogRead(0)); randomSeed(analogRead(0));
if (strlen(netName) > 1 && strlen(netName) < 20)
strcpy(netName_, netName);
#ifdef PRINT_LOG #ifdef PRINT_LOG
Serial.begin(115200); Serial.begin(115200);
#endif #endif
switch (workMode_)
{
case ESP_NOW:
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
break;
case ESP_NOW_AP:
WiFi.mode(WIFI_AP_STA);
WiFi.softAP(apSsid_, apPassword_);
break;
case ESP_NOW_STA:
WiFi.mode(WIFI_STA);
WiFi.begin(staSsid_, staPassword_);
while (WiFi.status() != WL_CONNECTED)
{
if (WiFi.status() == WL_NO_SSID_AVAIL || WiFi.status() == WL_CONNECT_FAILED)
return ERROR;
delay(500);
}
break;
default:
break;
}
esp_now_init(); esp_now_init();
#if defined(ESP8266) #if defined(ESP8266)
wifi_get_macaddr(STATION_IF, localMAC); wifi_get_macaddr(STATION_IF, localMAC);
@ -116,15 +53,6 @@ error_code_t ZHNetwork::begin()
return SUCCESS; return SUCCESS;
} }
error_code_t ZHNetwork::stop()
{
WiFi.mode(WIFI_OFF);
esp_now_deinit();
esp_now_unregister_recv_cb();
esp_now_unregister_send_cb();
return SUCCESS;
}
void ZHNetwork::sendBroadcastMessage(const char *data) void ZHNetwork::sendBroadcastMessage(const char *data)
{ {
broadcastMessage(data, broadcastMAC, BROADCAST); broadcastMessage(data, broadcastMAC, BROADCAST);
@ -456,15 +384,6 @@ String ZHNetwork::getNodeMac()
return macToString(localMAC); return macToString(localMAC);
} }
IPAddress ZHNetwork::getNodeIp()
{
if (workMode_ == ESP_NOW_AP)
return WiFi.softAPIP();
if (workMode_ == ESP_NOW_STA)
return WiFi.localIP();
return IPAddress(0, 0, 0, 0);
}
String ZHNetwork::getFirmwareVersion() String ZHNetwork::getFirmwareVersion()
{ {
return firmware; return firmware;

View File

@ -50,13 +50,6 @@ typedef struct
uint8_t intermediateTargetMAC[6]{0}; uint8_t intermediateTargetMAC[6]{0};
} routing_table_t; } routing_table_t;
typedef enum
{
ESP_NOW = 1,
ESP_NOW_AP,
ESP_NOW_STA
} work_mode_t;
typedef enum typedef enum
{ {
BROADCAST = 1, BROADCAST = 1,
@ -87,17 +80,7 @@ public:
ZHNetwork &setOnUnicastReceivingCallback(on_message_t onUnicastReceivingCallback); ZHNetwork &setOnUnicastReceivingCallback(on_message_t onUnicastReceivingCallback);
ZHNetwork &setOnConfirmReceivingCallback(on_confirm_t onConfirmReceivingCallback); ZHNetwork &setOnConfirmReceivingCallback(on_confirm_t onConfirmReceivingCallback);
error_code_t setWorkMode(const work_mode_t workMode); error_code_t begin(const char *netName = "");
work_mode_t getWorkMode(void);
error_code_t setNetName(const char *netName);
String getNetName(void);
error_code_t setStaSetting(const char *ssid, const char *password);
error_code_t setApSetting(const char *ssid, const char *password);
error_code_t begin(void);
error_code_t stop(void);
void sendBroadcastMessage(const char *data); void sendBroadcastMessage(const char *data);
void sendUnicastMessage(const char *data, const uint8_t *target, const bool confirm = false); void sendUnicastMessage(const char *data, const uint8_t *target, const bool confirm = false);
@ -105,7 +88,6 @@ public:
void maintenance(void); void maintenance(void);
String getNodeMac(void); String getNodeMac(void);
IPAddress getNodeIp(void);
String getFirmwareVersion(void); String getFirmwareVersion(void);
String readErrorCode(error_code_t code); // Just for further development. String readErrorCode(error_code_t code); // Just for further development.
@ -133,13 +115,8 @@ private:
static uint16_t lastMessageID[10]; static uint16_t lastMessageID[10];
static char netName_[20]; static char netName_[20];
const char *firmware{"1.12"}; const char *firmware{"1.3"};
const uint8_t broadcastMAC[6]{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; const uint8_t broadcastMAC[6]{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
work_mode_t workMode_{ESP_NOW};
char apSsid_[32]{"ESP-NOW NODE"};
char apPassword_[64]{0};
char staSsid_[32]{0};
char staPassword_[64]{0};
uint8_t maxNumberOfAttempts_{3}; uint8_t maxNumberOfAttempts_{3};
uint8_t maxWaitingTimeBetweenTransmissions_{50}; uint8_t maxWaitingTimeBetweenTransmissions_{50};
uint8_t numberOfAttemptsToSend{1}; uint8_t numberOfAttemptsToSend{1};