Add MQTT neighbors functionality for PSRAM builds

- Introduced commands for periodic neighbors publishing: `get mqtt.neighbors`, `set mqtt.neighbors on|off`, `get mqtt.neighbors.interval`, and `set mqtt.neighbors.interval <hours>`.
- Updated MQTT topics to include a new neighbors topic for cached zero-hop repeater neighbors.
- Added JSON message format for neighbors, including fields for SNR, last-heard age, and status.
- Enhanced CLI documentation to reflect new commands and settings.
- Implemented neighbor discovery logic in MyMesh class, including handling responses and publishing neighbor data.
- Adjusted MQTTBridge to manage neighbors JSON buffer and publish logic.

This update enhances the MQTT implementation by allowing devices to share neighbor information, improving network awareness and connectivity.
This commit is contained in:
agessaman committed 2026-07-10 22:39:06 -07:00
1 parent ae045395f9
commit 6b3c543cba
11 files changed
+525 -5

No files matched your search

+6
View File
@@ -156,6 +156,12 @@ struct MQTTPrefs {
uint16_t alert_min_interval_min; // min minutes between same-fault alerts, default 60
char alert_hashtag[24]; // readback for `get alert.hashtag`
char alert_region[31]; // optional region override; empty = default_scope
// Neighbors publishing (PSRAM boards only). Appended at the end of the observer
// tail so a shorter /mqtt_prefs payload from earlier firmware still loads with
// these defaulting off (see applyMQTTDefaults); keeps the format at VERSION 1.
uint8_t mqtt_neighbors_enabled; // Periodic neighbors/scopes MQTT publish (PSRAM boards only)
uint32_t mqtt_neighbors_interval; // Neighbors publish interval (ms), default 24h, min 12h
};
// /mqtt_prefs is written with an 8-byte header so the format is self-describing.
+31
View File
@@ -246,6 +246,26 @@ bool CommonCLI::handleObserverSetCmd(uint32_t sender_timestamp, const char* conf
} else {
strcpy(reply, "Error: interval must be between 1-60 minutes");
}
#if defined(BOARD_HAS_PSRAM) && defined(MAX_NEIGHBOURS) && MAX_NEIGHBOURS > 0
} else if (memcmp(config, "mqtt.neighbors ", 14) == 0) {
_mqtt_prefs.mqtt_neighbors_enabled = memcmp(&config[14], "on", 2) == 0;
savePrefs();
strcpy(reply, "OK");
} else if (memcmp(config, "mqtt.neighbors.interval ", 24) == 0) {
uint32_t hours = _atoi(&config[24]);
if (hours >= 12 && hours <= 8760) {
_mqtt_prefs.mqtt_neighbors_interval = hours * 3600000UL;
savePrefs();
sprintf(reply, "OK - neighbors interval set to %u hours (%lu ms)", hours,
(unsigned long)_mqtt_prefs.mqtt_neighbors_interval);
} else {
strcpy(reply, "Error: neighbors interval must be between 12-8760 hours");
}
#elif defined(WITH_MQTT_BRIDGE)
} else if (memcmp(config, "mqtt.neighbors ", 14) == 0 ||
memcmp(config, "mqtt.neighbors.interval ", 24) == 0) {
strcpy(reply, "Err - not supported (requires PSRAM)");
#endif
} else if (memcmp(config, "mqtt.ntp ", 9) == 0) {
const char* host = &config[9];
while (*host == ' ') host++;
@@ -709,6 +729,17 @@ bool CommonCLI::handleObserverGetCmd(uint32_t sender_timestamp, const char* conf
} else if (memcmp(config, "mqtt.interval", 13) == 0) {
uint32_t minutes = (_mqtt_prefs.mqtt_status_interval + 29999) / 60000;
sprintf(reply, "> %u minutes (%lu ms)", minutes, (unsigned long)_mqtt_prefs.mqtt_status_interval);
#if defined(BOARD_HAS_PSRAM) && defined(MAX_NEIGHBOURS) && MAX_NEIGHBOURS > 0
} else if (memcmp(config, "mqtt.neighbors.interval", 23) == 0) {
uint32_t hours = (_mqtt_prefs.mqtt_neighbors_interval + 3599999) / 3600000;
sprintf(reply, "> %u hours (%lu ms)", hours, (unsigned long)_mqtt_prefs.mqtt_neighbors_interval);
} else if (memcmp(config, "mqtt.neighbors", 14) == 0) {
sprintf(reply, "> %s", _mqtt_prefs.mqtt_neighbors_enabled ? "on" : "off");
#elif defined(WITH_MQTT_BRIDGE)
} else if (memcmp(config, "mqtt.neighbors", 14) == 0 ||
memcmp(config, "mqtt.neighbors.interval", 23) == 0) {
strcpy(reply, "Err - not supported (requires PSRAM)");
#endif
} else if (memcmp(config, "mqtt.ntp.diag", 13) == 0 && (config[13] == '\0' || config[13] == ' ')) {
#ifdef ESP_PLATFORM
// Connectivity probe across all configured NTP servers; never updates the clock.
+2
View File
@@ -68,6 +68,8 @@ static inline void applyMQTTDefaults(MQTTPrefs* prefs) {
prefs->mqtt_tx_enabled = 2;
prefs->mqtt_rx_enabled = 1;
prefs->mqtt_status_interval = 300000;
prefs->mqtt_neighbors_enabled = 0;
prefs->mqtt_neighbors_interval = 24UL * 3600UL * 1000UL;
prefs->wifi_power_save = 1;
mqttDefaultSlotPreset(prefs->mqtt_slot_preset[0], sizeof(prefs->mqtt_slot_preset[0]),
+35
View File
@@ -227,6 +227,41 @@ int MQTTMessageBuilder::buildRawMessage(
return (len > 0 && len < buffer_size) ? len : 0;
}
int MQTTMessageBuilder::buildNeighborsMessage(
JsonDocument& doc,
const char* origin,
const char* origin_id,
const char* timestamp,
const char* self_scopes,
const NeighborsMessageEntry* neighbors,
int neighbor_count,
char* buffer,
size_t buffer_size
) {
doc.clear();
JsonObject root = doc.to<JsonObject>();
root["timestamp"] = timestamp;
root["origin"] = origin;
root["origin_id"] = origin_id;
JsonObject self = root.createNestedObject("self");
self["scopes"] = self_scopes ? self_scopes : "";
JsonArray arr = root.createNestedArray("neighbors");
for (int i = 0; i < neighbor_count; i++) {
JsonObject nb = arr.createNestedObject();
nb["pubkey"] = neighbors[i].pubkey_hex;
nb["snr"] = neighbors[i].snr;
nb["heard_secs_ago"] = neighbors[i].heard_secs_ago;
nb["scopes"] = neighbors[i].scopes ? neighbors[i].scopes : "";
nb["status"] = neighbors[i].status;
}
size_t len = serializeJson(root, buffer, buffer_size);
return (len > 0 && len < buffer_size) ? len : 0;
}
int MQTTMessageBuilder::buildPacketJSON(
JsonDocument& doc,
mesh::Packet* packet,
+23
View File
@@ -153,6 +153,29 @@ public:
size_t buffer_size
);
struct NeighborsMessageEntry {
const char* pubkey_hex;
float snr;
uint32_t heard_secs_ago;
const char* scopes;
const char* status;
};
/**
* Build neighbors table JSON for meshcore/{iata}/{device}/neighbors topic.
*/
static int buildNeighborsMessage(
JsonDocument& doc,
const char* origin,
const char* origin_id,
const char* timestamp,
const char* self_scopes,
const NeighborsMessageEntry* neighbors,
int neighbor_count,
char* buffer,
size_t buffer_size
);
/**
* Convert packet to JSON message
*
+71 -3
View File
@@ -506,6 +506,11 @@ MQTTBridge::MQTTBridge(NodePrefs *prefs, MQTTPrefs *obs, mesh::PacketManager *mg
_last_raw_data = (uint8_t*)psram_malloc(LAST_RAW_DATA_SIZE);
_publish_json_buffer = (char*)psram_malloc(PUBLISH_JSON_BUFFER_SIZE);
_status_json_buffer = (char*)psram_malloc(STATUS_JSON_BUFFER_SIZE);
#if defined(WITH_MQTT_NEIGHBORS)
_neighbors_json_buffer = (char*)psram_malloc(NEIGHBORS_JSON_BUFFER_SIZE);
_neighbors_publish_pending = false;
_neighbors_publish_len = 0;
#endif
#else
memset(_last_raw_data, 0, sizeof(_last_raw_data));
#endif
@@ -804,6 +809,11 @@ void MQTTBridge::end() {
psram_free(_last_raw_data); _last_raw_data = nullptr;
psram_free(_publish_json_buffer); _publish_json_buffer = nullptr;
psram_free(_status_json_buffer); _status_json_buffer = nullptr;
#if defined(WITH_MQTT_NEIGHBORS)
psram_free(_neighbors_json_buffer); _neighbors_json_buffer = nullptr;
_neighbors_publish_pending = false;
_neighbors_publish_len = 0;
#endif
#endif
// JSON documents are now StaticJsonDocument inline members — no heap allocation to free.
@@ -1023,6 +1033,17 @@ void MQTTBridge::mqttTaskLoop() {
// Process packet queue
processPacketQueue();
#if defined(WITH_MQTT_NEIGHBORS)
if (_neighbors_publish_pending) {
_neighbors_publish_pending = false;
if (publishNeighbors()) {
MQTT_DEBUG_PRINTLN("Neighbors published");
} else {
MQTT_DEBUG_PRINTLN("Neighbors publish failed");
}
}
#endif
#ifdef WITH_SNMP
// SNMP agent loop — process incoming UDP requests
if (_snmp_agent) {
@@ -1820,7 +1841,10 @@ bool MQTTBridge::publishToAllSlots(const char* topic, const char* payload, bool
// Presets use hardcoded topic logic; custom slots support user-defined templates.
// ---------------------------------------------------------------------------
bool MQTTBridge::substituteTopicTemplate(const char* tmpl, MQTTMessageType type, int slot_index, char* buf, size_t buf_size) {
const char* type_str = (type == MSG_STATUS) ? "status" : (type == MSG_PACKETS) ? "packets" : "raw";
const char* type_str = (type == MSG_STATUS) ? "status"
: (type == MSG_PACKETS) ? "packets"
: (type == MSG_RAW) ? "raw"
: "neighbors";
const char* token = _obs->mqtt_slot_token[slot_index];
size_t out = 0;
@@ -1878,7 +1902,10 @@ bool MQTTBridge::buildTopicForSlot(int index, MQTTMessageType type, char* topic_
}
// MQTT_TOPIC_MESHCORE (default for all other presets)
if (!isIATAValid()) return false;
const char* type_str = (type == MSG_STATUS) ? "status" : (type == MSG_PACKETS) ? "packets" : "raw";
const char* type_str = (type == MSG_STATUS) ? "status"
: (type == MSG_PACKETS) ? "packets"
: (type == MSG_RAW) ? "raw"
: "neighbors";
snprintf(topic_buf, buf_size, "meshcore/%s/%s/%s", _iata, _device_id, type_str);
return true;
}
@@ -1889,7 +1916,10 @@ bool MQTTBridge::buildTopicForSlot(int index, MQTTMessageType type, char* topic_
}
// Default: meshcore format
if (!isIATAValid()) return false;
const char* type_str = (type == MSG_STATUS) ? "status" : (type == MSG_PACKETS) ? "packets" : "raw";
const char* type_str = (type == MSG_STATUS) ? "status"
: (type == MSG_PACKETS) ? "packets"
: (type == MSG_RAW) ? "raw"
: "neighbors";
snprintf(topic_buf, buf_size, "meshcore/%s/%s/%s", _iata, _device_id, type_str);
return true;
}
@@ -2716,6 +2746,44 @@ bool MQTTBridge::publishStatus() {
return false;
}
#if defined(WITH_MQTT_NEIGHBORS)
void MQTTBridge::requestPublishNeighbors(const char* json, size_t len) {
if (!_neighbors_json_buffer || !json || len == 0) return;
if (len >= NEIGHBORS_JSON_BUFFER_SIZE) {
len = NEIGHBORS_JSON_BUFFER_SIZE - 1;
}
memcpy(_neighbors_json_buffer, json, len);
_neighbors_json_buffer[len] = '\0';
_neighbors_publish_len = len;
_neighbors_publish_pending = true;
}
bool MQTTBridge::publishNeighbors() {
if (!_neighbors_json_buffer || _neighbors_publish_len == 0) {
return false;
}
if (!_cached_has_connected_slots) {
return false;
}
refreshOriginFromPrefs();
bool published = false;
char topic[128];
for (int i = 0; i < RUNTIME_MQTT_SLOTS; i++) {
if (_slots[i].enabled && _slots[i].client && _slots[i].connected) {
if (buildTopicForSlot(i, MSG_NEIGHBORS, topic, sizeof(topic))) {
bool use_retain = _slots[i].preset ? _slots[i].preset->allow_retain : false;
if (publishToSlot(i, topic, _neighbors_json_buffer, use_retain, 1)) {
published = true;
}
}
}
}
return published;
}
#endif
bool MQTTBridge::publishPacket(mesh::Packet* packet, bool is_tx,
const uint8_t* raw_data, int raw_len,
float snr, float rssi) {
+18 -1
View File
@@ -35,6 +35,10 @@ class MeshSNMPAgent; // Forward declaration
#ifdef WITH_MQTT_BRIDGE
#if defined(BOARD_HAS_PSRAM) && defined(MAX_NEIGHBOURS) && MAX_NEIGHBOURS > 0
#define WITH_MQTT_NEIGHBORS 1
#endif
/**
* @brief Bridge implementation using MQTT protocol for packet transport
*
@@ -257,6 +261,11 @@ private:
// class object so these allocations don't interleave with large TLS buffers at startup.
static const size_t PUBLISH_JSON_BUFFER_SIZE = 2048;
static const size_t STATUS_JSON_BUFFER_SIZE = 768;
#if defined(WITH_MQTT_NEIGHBORS)
char* _neighbors_json_buffer;
size_t _neighbors_publish_len;
bool _neighbors_publish_pending;
#endif
#if defined(BOARD_HAS_PSRAM)
char* _publish_json_buffer;
char* _status_json_buffer;
@@ -318,7 +327,7 @@ private:
mesh::MillisecondClock* _ms; // For uptime
// Topic building
enum MQTTMessageType { MSG_STATUS, MSG_PACKETS, MSG_RAW };
enum MQTTMessageType { MSG_STATUS, MSG_PACKETS, MSG_RAW, MSG_NEIGHBORS };
bool buildTopicForSlot(int index, MQTTMessageType type, char* topic_buf, size_t buf_size);
bool substituteTopicTemplate(const char* tmpl, MQTTMessageType type, int slot_index, char* buf, size_t buf_size);
@@ -349,6 +358,9 @@ private:
void processPacketQueue();
bool publishStatus(); // Returns true if status was successfully published
#if defined(WITH_MQTT_NEIGHBORS)
bool publishNeighbors();
#endif
bool handleWiFiConnection(unsigned long now);
// FreeRTOS task function (runs on Core 0)
@@ -477,6 +489,11 @@ public:
static void formatSlotDiagReply(char* buf, size_t bufsize, int slot_index);
static uint8_t getLastWifiDisconnectReason();
static unsigned long getLastWifiDisconnectTime();
#if defined(WITH_MQTT_NEIGHBORS)
void requestPublishNeighbors(const char* json, size_t len);
static const size_t NEIGHBORS_JSON_BUFFER_SIZE = 10240;
#endif
static const char* wifiReasonStr(uint8_t reason);
static const char* tlsErrorStr(int32_t err);