Add mesh de-init, a few comments

Signed-off-by: Sara Damiano <sdamiano@stroudcenter.org>
This commit is contained in:
Sara Damiano 2022-04-11 15:38:37 -04:00
parent 92d264f4e0
commit 830ac3b320
3 changed files with 15 additions and 5 deletions

View File

@ -22,7 +22,7 @@ Add ``#define USE_TASMESH`` to your file ``user_config_override.h`` before compi
## Commands ## Commands
**WARNING: The MAC address used for ESP-NOW is the *Soft AP MAC*, not the WiFi MAC.** **WARNING: The MAC address used for ESP-NOW on the broker is the *Soft AP MAC*, not the WiFi MAC.**
*NOTE: The colons in the mac addresses of the commands are optional.* *NOTE: The colons in the mac addresses of the commands are optional.*

View File

@ -41,10 +41,13 @@
#define MESH_MAX_PACKETS 3 // (3) Max number of packets #define MESH_MAX_PACKETS 3 // (3) Max number of packets
#define MESH_REFRESH 50 // Number of ms #define MESH_REFRESH 50 // Number of ms
// The format of the vendor-specific action frame is as follows:
// ------------------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------------------
// | MAC Header | Category Code | Organization Identifier | Random Values | Vendor Specific Content | FCS | // | MAC Header | Category Code | Organization Identifier | Random Values | Vendor Specific Content | FCS |
// ------------------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------------------
// 24 bytes 1 byte 3 bytes 4 bytes 7~255 bytes 4 bytes // 24 bytes 1 byte 3 bytes 4 bytes 7~255 bytes 4 bytes
//
// The Vendor Specific Content contains vendor-specific fields as follows:
// ------------------------------------------------------------------------------- // -------------------------------------------------------------------------------
// | Element ID | Length | Organization Identifier | Type | Version | Body | // | Element ID | Length | Organization Identifier | Type | Version | Body |
// ------------------------------------------------------------------------------- // -------------------------------------------------------------------------------

View File

@ -288,7 +288,7 @@ bool MESHroleNode(void) {
} }
/** /**
* @brief Redirects the mqtt message on the node just before it would have been sended to * @brief Redirects the outgoing mqtt message on the node just before it would have been sent to
* the broker via ESP-NOW * the broker via ESP-NOW
* *
* @param _topic * @param _topic
@ -385,8 +385,9 @@ void MESHstartNode(int32_t _channel, uint8_t _role){ //we need a running broker
wifi_promiscuous_enable(0); wifi_promiscuous_enable(0);
WiFi.disconnect(); WiFi.disconnect();
MESHsetWifi(0); MESHsetWifi(0);
esp_err_t init_result = esp_now_init(); esp_now_deinit(); // in case it was already initialized but disconnected
if (esp_err_t() != ESP_OK) { int init_result = esp_now_init();
if (init_result != 0) {
AddLog(LOG_LEVEL_INFO, PSTR("MSH: Node init failed with error: %d"), init_result); AddLog(LOG_LEVEL_INFO, PSTR("MSH: Node init failed with error: %d"), init_result);
// try to re-launch wifi // try to re-launch wifi
MESH.role = ROLE_NONE; MESH.role = ROLE_NONE;
@ -422,9 +423,10 @@ void MESHstartBroker(void) { // Must be called after WiFi is initialized!!
WiFi.softAPmacAddress(MESH.broker); //set MESH.broker to the needed MAC WiFi.softAPmacAddress(MESH.broker); //set MESH.broker to the needed MAC
uint32_t _channel = WiFi.channel(); uint32_t _channel = WiFi.channel();
esp_now_deinit(); // in case it was already initialized by disconnected
esp_err_t init_result = esp_now_init(); esp_err_t init_result = esp_now_init();
if (esp_err_t() != ESP_OK) { if (esp_err_t() != ESP_OK) {
AddLog(LOG_LEVEL_INFO, PSTR("MSH: Broker init failed with error: %d"), init_result); AddLog(LOG_LEVEL_INFO, PSTR("MSH: Broker init failed with error: %s"), init_result);
return; return;
} }
@ -648,6 +650,7 @@ void MESHEverySecond(void) {
if (millis() - MESH.lastMessageFromBroker > 70000) { if (millis() - MESH.lastMessageFromBroker > 70000) {
AddLog(LOG_LEVEL_DEBUG, PSTR("MSH: Broker not seen for 70 secs, try to re-launch wifi")); AddLog(LOG_LEVEL_DEBUG, PSTR("MSH: Broker not seen for 70 secs, try to re-launch wifi"));
MESH.role = ROLE_NONE; MESH.role = ROLE_NONE;
MESHdeInit(); // if we don't deinit after losing connection, we will get an error trying to reinit later
MESHsetWifi(1); MESHsetWifi(1);
WifiBegin(3, MESH.channel); WifiBegin(3, MESH.channel);
} }
@ -735,12 +738,15 @@ void (* const MeshCommand[])(void) PROGMEM = {
&CmndMeshBroker, &CmndMeshNode, &CmndMeshPeer, &CmndMeshChannel, &CmndMeshInterval }; &CmndMeshBroker, &CmndMeshNode, &CmndMeshPeer, &CmndMeshChannel, &CmndMeshInterval };
void CmndMeshBroker(void) { void CmndMeshBroker(void) {
#ifdef ESP32 // only ESP32 currently supported as broker
MESH.channel = WiFi.channel(); // The Broker gets the channel from the router, no need to declare it with MESHCHANNEL (will be mandatory set it when ETH will be implemented) MESH.channel = WiFi.channel(); // The Broker gets the channel from the router, no need to declare it with MESHCHANNEL (will be mandatory set it when ETH will be implemented)
MESHstartBroker(); MESHstartBroker();
ResponseCmndNumber(MESH.channel); ResponseCmndNumber(MESH.channel);
#endif // ESP32
} }
void CmndMeshNode(void) { void CmndMeshNode(void) {
#ifndef ESP32 // only ESP8266 current supported as node
if (XdrvMailbox.data_len > 0) { if (XdrvMailbox.data_len > 0) {
MESHHexStringToBytes(XdrvMailbox.data, MESH.broker); MESHHexStringToBytes(XdrvMailbox.data, MESH.broker);
if (XdrvMailbox.index != 0) { XdrvMailbox.index = 1; } // Everything not 0 is a full node if (XdrvMailbox.index != 0) { XdrvMailbox.index = 1; } // Everything not 0 is a full node
@ -766,6 +772,7 @@ void CmndMeshNode(void) {
AddLog(LOG_LEVEL_INFO, PSTR("MSH: No Mesh Broker found using MAC %s"), XdrvMailbox.data); AddLog(LOG_LEVEL_INFO, PSTR("MSH: No Mesh Broker found using MAC %s"), XdrvMailbox.data);
} }
} }
#endif // ESP32
} }
void CmndMeshPeer(void) { void CmndMeshPeer(void) {