Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
c1fe139756 | |||
4f58a04eae | |||
2ed860b78b | |||
425e5035c9 | |||
7a1b67102e |
@ -116,6 +116,12 @@ myNet.setApSetting("SSID", "PASSWORD");
|
||||
myNet.begin();
|
||||
```
|
||||
|
||||
### ESP-NOW Mesh network deinitialization
|
||||
|
||||
```cpp
|
||||
myNet.stop();
|
||||
```
|
||||
|
||||
### Sends broadcast message to all nodes
|
||||
|
||||
```cpp
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=ZHNetwork
|
||||
version=1.01
|
||||
version=1.12
|
||||
author=Alexey Zholtikov
|
||||
maintainer=Alexey Zholtikov
|
||||
sentence=ESP-NOW based Mesh network for ESP8266/ESP32
|
||||
|
@ -1,31 +1,17 @@
|
||||
#include "ZHNetwork.h"
|
||||
|
||||
routing_vector_t routingVector;
|
||||
incoming_queue_t queueForIncomingData;
|
||||
outgoing_queue_t queueForOutgoingData;
|
||||
waiting_queue_t queueForRoutingVectorWaiting;
|
||||
routing_vector_t ZHNetwork::routingVector;
|
||||
incoming_queue_t ZHNetwork::queueForIncomingData;
|
||||
outgoing_queue_t ZHNetwork::queueForOutgoingData;
|
||||
waiting_queue_t ZHNetwork::queueForRoutingVectorWaiting;
|
||||
|
||||
const char *firmware PROGMEM{"1.0"};
|
||||
const uint8_t broadcastMAC[6]{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
|
||||
bool criticalProcessSemaphore{false};
|
||||
bool sentMessageSemaphore{false};
|
||||
bool confirmReceivingSemaphore{false};
|
||||
bool confirmReceiving{false};
|
||||
uint8_t localMAC[6]{0};
|
||||
uint8_t numberOfAttemptsToSend{1};
|
||||
uint16_t lastMessageID[10]{0};
|
||||
uint64_t lastMessageSentTime{0};
|
||||
|
||||
work_mode_t workMode_{ESP_NOW};
|
||||
char netName_[20]{0};
|
||||
char apSsid_[32]{"ESP-NOW NODE"};
|
||||
char apPassword_[64]{0};
|
||||
char staSsid_[32]{0};
|
||||
char staPassword_[64]{0};
|
||||
uint8_t maxNumberOfAttempts_{3};
|
||||
uint8_t maxWaitingTimeBetweenTransmissions_{50};
|
||||
uint16_t maxTimeForRoutingInfoWaiting_{500};
|
||||
bool ZHNetwork::criticalProcessSemaphore{false};
|
||||
bool ZHNetwork::sentMessageSemaphore{false};
|
||||
bool ZHNetwork::confirmReceivingSemaphore{false};
|
||||
bool ZHNetwork::confirmReceiving{false};
|
||||
char ZHNetwork::netName_[20]{0};
|
||||
uint8_t ZHNetwork::localMAC[6]{0};
|
||||
uint16_t ZHNetwork::lastMessageID[10]{0};
|
||||
|
||||
ZHNetwork &ZHNetwork::setOnBroadcastReceivingCallback(on_message_t onBroadcastReceivingCallback)
|
||||
{
|
||||
@ -62,23 +48,20 @@ error_code_t ZHNetwork::setNetName(const char *netName)
|
||||
{
|
||||
if (strlen(netName) < 1 || strlen(netName) > 20)
|
||||
return ERROR;
|
||||
memset(&netName_, 0, strlen(netName));
|
||||
strcpy(netName_, netName);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
String ZHNetwork::getNetName()
|
||||
{
|
||||
return String(netName_);
|
||||
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;
|
||||
memset(&staSsid_, 0, strlen(ssid));
|
||||
strcpy(staSsid_, ssid);
|
||||
memset(&staPassword_, 0, strlen(password));
|
||||
strcpy(staPassword_, password);
|
||||
return SUCCESS;
|
||||
}
|
||||
@ -87,9 +70,7 @@ 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;
|
||||
memset(&apSsid_, 0, strlen(ssid));
|
||||
strcpy(apSsid_, ssid);
|
||||
memset(&apPassword_, 0, strlen(password));
|
||||
strcpy(apPassword_, password);
|
||||
return SUCCESS;
|
||||
}
|
||||
@ -135,6 +116,15 @@ error_code_t ZHNetwork::begin()
|
||||
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)
|
||||
{
|
||||
broadcastMessage(data, broadcastMAC, BROADCAST);
|
||||
@ -193,7 +183,6 @@ void ZHNetwork::maintenance()
|
||||
}
|
||||
}
|
||||
waiting_data_t waitingData;
|
||||
esp_memset(&waitingData, 0, sizeof(waiting_data_t));
|
||||
waitingData.time = millis();
|
||||
memcpy(&waitingData.intermediateTargetMAC, &outgoingData.intermediateTargetMAC, 6);
|
||||
memcpy(&waitingData.transmittedData, &outgoingData.transmittedData, sizeof(transmitted_data_t));
|
||||
@ -413,7 +402,6 @@ void ZHNetwork::maintenance()
|
||||
{
|
||||
queueForRoutingVectorWaiting.pop();
|
||||
outgoing_data_t outgoingData;
|
||||
esp_memset(&outgoingData, 0, sizeof(outgoing_data_t));
|
||||
memcpy(&outgoingData.transmittedData, &waitingData.transmittedData, sizeof(transmitted_data_t));
|
||||
memcpy(&outgoingData.intermediateTargetMAC, &routingTable.intermediateTargetMAC, 6);
|
||||
queueForOutgoingData.push(outgoingData);
|
||||
@ -601,7 +589,6 @@ void IRAM_ATTR ZHNetwork::onDataReceive(uint8_t *mac, uint8_t *data, uint8_t len
|
||||
void ZHNetwork::broadcastMessage(const char *data, const uint8_t *target, message_type_t type)
|
||||
{
|
||||
outgoing_data_t outgoingData;
|
||||
esp_memset(&outgoingData, 0, sizeof(outgoing_data_t));
|
||||
outgoingData.transmittedData.messageType = type;
|
||||
outgoingData.transmittedData.messageID = ((uint16_t)random(32767) << 8) | (uint16_t)random(32767);
|
||||
memcpy(&outgoingData.transmittedData.netName, &netName_, 20);
|
||||
@ -636,7 +623,6 @@ void ZHNetwork::broadcastMessage(const char *data, const uint8_t *target, messag
|
||||
void ZHNetwork::unicastMessage(const char *data, const uint8_t *target, const uint8_t *sender, message_type_t type)
|
||||
{
|
||||
outgoing_data_t outgoingData;
|
||||
esp_memset(&outgoingData, 0, sizeof(outgoing_data_t));
|
||||
outgoingData.transmittedData.messageType = type;
|
||||
outgoingData.transmittedData.messageID = ((uint16_t)random(32767) << 8) | (uint16_t)random(32767);
|
||||
memcpy(&outgoingData.transmittedData.netName, &netName_, 20);
|
||||
|
@ -15,46 +15,39 @@
|
||||
|
||||
// #define PRINT_LOG // Uncomment to display to serial port the full operation log.
|
||||
|
||||
#if defined(ESP8266)
|
||||
#define esp_memset memset // Just for remove the compiler notice for ESP8266 at "memset". I don't know why this is happening...
|
||||
#endif
|
||||
#if defined(ESP32)
|
||||
#define esp_memset memset
|
||||
#endif
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t messageType;
|
||||
uint16_t messageID;
|
||||
char netName[20];
|
||||
uint8_t originalTargetMAC[6];
|
||||
uint8_t originalSenderMAC[6];
|
||||
char message[200];
|
||||
uint8_t messageType{0};
|
||||
uint16_t messageID{0};
|
||||
char netName[20]{0};
|
||||
uint8_t originalTargetMAC[6]{0};
|
||||
uint8_t originalSenderMAC[6]{0};
|
||||
char message[200]{0};
|
||||
} transmitted_data_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t intermediateTargetMAC[6];
|
||||
uint8_t intermediateTargetMAC[6]{0};
|
||||
transmitted_data_t transmittedData;
|
||||
} outgoing_data_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t intermediateSenderMAC[6];
|
||||
uint8_t intermediateSenderMAC[6]{0};
|
||||
transmitted_data_t transmittedData;
|
||||
} incoming_data_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint64_t time;
|
||||
uint8_t intermediateTargetMAC[6];
|
||||
uint64_t time{0};
|
||||
uint8_t intermediateTargetMAC[6]{0};
|
||||
transmitted_data_t transmittedData;
|
||||
} waiting_data_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t originalTargetMAC[6];
|
||||
uint8_t intermediateTargetMAC[6];
|
||||
uint8_t originalTargetMAC[6]{0};
|
||||
uint8_t intermediateTargetMAC[6]{0};
|
||||
} routing_table_t;
|
||||
|
||||
typedef enum
|
||||
@ -104,6 +97,7 @@ public:
|
||||
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 sendUnicastMessage(const char *data, const uint8_t *target, const bool confirm = false);
|
||||
@ -126,6 +120,32 @@ public:
|
||||
uint16_t getMaxWaitingTimeForRoutingInfo(void);
|
||||
|
||||
private:
|
||||
static routing_vector_t routingVector;
|
||||
static incoming_queue_t queueForIncomingData;
|
||||
static outgoing_queue_t queueForOutgoingData;
|
||||
static waiting_queue_t queueForRoutingVectorWaiting;
|
||||
|
||||
static bool criticalProcessSemaphore;
|
||||
static bool sentMessageSemaphore;
|
||||
static bool confirmReceivingSemaphore;
|
||||
static bool confirmReceiving;
|
||||
static uint8_t localMAC[6];
|
||||
static uint16_t lastMessageID[10];
|
||||
static char netName_[20];
|
||||
|
||||
const char *firmware{"1.12"};
|
||||
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 maxWaitingTimeBetweenTransmissions_{50};
|
||||
uint8_t numberOfAttemptsToSend{1};
|
||||
uint16_t maxTimeForRoutingInfoWaiting_{500};
|
||||
uint64_t lastMessageSentTime{0};
|
||||
|
||||
#if defined(ESP8266)
|
||||
static void onDataSent(uint8_t *mac, uint8_t status);
|
||||
static void onDataReceive(uint8_t *mac, uint8_t *data, uint8_t length);
|
||||
|
Reference in New Issue
Block a user