Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support LWT messages when using TASMESH #20392

Merged
merged 9 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion tasmota/my_user_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,9 @@
// Enabled by default on ESP32 and variants

// -- ESP-NOW -------------------------------------
//#define USE_TASMESH // Enable Tasmota Mesh using ESP-NOW (+11k code)
#define USE_TASMESH // Enable Tasmota Mesh using ESP-NOW (+11k code)
pipe01 marked this conversation as resolved.
Show resolved Hide resolved
#define TASMESH_HEARTBEAT 1 // If enabled, the broker will detect when nodes come online and offline and send Birth and LWT messages over MQTT correspondingly (0 = off, 1 = on)
pipe01 marked this conversation as resolved.
Show resolved Hide resolved
#define TASMESH_OFFLINE_DELAY 3 // Maximum number of seconds since the last heartbeat before the broker considers a node to be offline
pipe01 marked this conversation as resolved.
Show resolved Hide resolved

// -- OTA -----------------------------------------
//#define USE_ARDUINO_OTA // Add optional support for Arduino OTA with ESP8266 (+13k code)
Expand Down
9 changes: 8 additions & 1 deletion tasmota/tasmota_xdrv_driver/xdrv_57_1_tasmesh_support.ino
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ struct mesh_peer_t {
uint8_t MAC[6];
uint32_t lastMessageFromPeer; // Time of last message from peer
#ifdef ESP32
bool isAlive; // True if we have gotten a heartbeat recently
char topic[MESH_TOPICSZ];
#if TASMESH_HEARTBEAT
uint32_t lastHeartbeatFromPeer; // Time of last heartbeat from peer
#endif // TASMESH_HEARTBEAT
#endif //ESP32
};

Expand Down Expand Up @@ -164,7 +168,10 @@ enum MESH_Packet_Type { // Type of packet
PACKET_TYPE_REGISTER_NODE, // register a node with encrypted broker-MAC, announce mqtt topic to ESP32-proxy - broker will send time ASAP
PACKET_TYPE_REFRESH_NODE, // refresh node infos with encrypted broker-MAC, announce mqtt topic to ESP32-proxy - broker will send time slightly delayed
PACKET_TYPE_MQTT, // send regular mqtt messages, single or multipackets
PACKET_TYPE_WANTTOPIC // the broker has no topic for this peer/node
PACKET_TYPE_WANTTOPIC, // the broker has no topic for this peer/node
#if TASMESH_HEARTBEAT
PACKET_TYPE_HEARTBEAT // sent periodically from nodes to the broker to signal aliveness
#endif // TASMESH_HEARTBEAT
};

/*********************************************************************************************\
Expand Down
40 changes: 40 additions & 0 deletions tasmota/tasmota_xdrv_driver/xdrv_57_9_tasmesh.ino
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,25 @@ void MESHevery50MSecond(void) {
// AddLog(LOG_LEVEL_INFO, PSTR("MSH: %*_H), MESH.packetToConsume.front().chunkSize, (uint8_t *)&MESH.packetToConsume.front().payload);
}
break;
case PACKET_TYPE_HEARTBEAT:
#if TASMESH_HEARTBEAT
for (auto &_peer : MESH.peers){
if (memcmp(_peer.MAC, MESH.packetToConsume.front().sender, 6) == 0) {
_peer.lastHeartbeatFromPeer = millis();

if (!_peer.isAlive) {
_peer.isAlive = true;
char stopic[TOPSZ];
GetTopic_P(stopic, TELE, _peer.topic, S_LWT);
MqttPublishPayload(stopic, PSTR(MQTT_LWT_ONLINE));
}
break;
}
}
#else
break;
pipe01 marked this conversation as resolved.
Show resolved Hide resolved
#endif // TASMESH_HEARTBEAT

default:
AddLogBuffer(LOG_LEVEL_DEBUG, (uint8_t *)&MESH.packetToConsume.front(), MESH.packetToConsume.front().chunkSize +5);
break;
Expand Down Expand Up @@ -582,6 +601,17 @@ void MESHEverySecond(void) {
AddLog(LOG_LEVEL_INFO, PSTR("MSH: Multi packets in buffer %u"), MESH.multiPackets.size());
MESH.multiPackets.erase(MESH.multiPackets.begin());
}

#if TASMESH_HEARTBEAT
for (auto &_peer : MESH.peers){
if (_peer.isAlive && TimePassedSince(_peer.lastHeartbeatFromPeer) > TASMESH_OFFLINE_DELAY * 1000) {
_peer.isAlive = false;
char stopic[TOPSZ];
GetTopic_P(stopic, TELE, _peer.topic, S_LWT);
MqttPublishPayload(stopic, PSTR(MQTT_LWT_OFFLINE));
}
}
#endif TASMESH_HEARTBEAT
}

#else // ESP8266
Expand Down Expand Up @@ -649,6 +679,16 @@ void MESHEverySecond(void) {
MESHsetWifi(1);
WifiBegin(3, MESH.channel);
}

Jason2866 marked this conversation as resolved.
Show resolved Hide resolved
#if TASMESH_HEARTBEAT
MESH.sendPacket.counter++;
MESH.sendPacket.TTL = 2;
MESH.sendPacket.chunks = 0;
MESH.sendPacket.chunk = 0;
MESH.sendPacket.chunkSize = 0;
MESH.sendPacket.type = PACKET_TYPE_HEARTBEAT;
MESHsendPacket(&MESH.sendPacket);
#endif // TASMESH_HEARTBEAT
}
}

Expand Down