Add MQTT Bridge implementation with WiFi CLI configuration
- Implement MQTT bridge for packet uplink to multiple brokers - Add JSON message formatting for status, packets, and raw data - Add WiFi SSID/password CLI configuration (get/set wifi.ssid, wifi.pwd) - Add persistent storage for WiFi credentials - Add NTP time synchronization for accurate MQTT timestamps - Add CLI commands for MQTT configuration (origin, iata, status, packets, raw) - Support multiple MQTT brokers with automatic reconnection - Integrate with existing MeshCore bridge architecture
This commit is contained in:
1 parent
863c13f8c3
commit
ee6a756591
10 files changed
+1769
-40
No files matched your search
@@ -0,0 +1,148 @@
|
||||
# MQTT Bridge Implementation for MeshCore
|
||||
|
||||
This document describes the MQTT bridge implementation that allows MeshCore repeaters to uplink packet data to multiple MQTT brokers.
|
||||
|
||||
## Overview
|
||||
|
||||
The MQTT bridge implementation provides:
|
||||
- Multiple MQTT broker support (up to 3 brokers)
|
||||
- Automatic reconnection with exponential backoff
|
||||
- JSON message formatting for status, packets, and raw data
|
||||
- Configurable topics and QoS levels
|
||||
- Packet queuing during connection issues
|
||||
|
||||
## Files Added
|
||||
|
||||
### Core Implementation
|
||||
- `src/helpers/bridges/MQTTBridge.h` - MQTT bridge class definition
|
||||
- `src/helpers/bridges/MQTTBridge.cpp` - MQTT bridge implementation
|
||||
- `src/helpers/MQTTMessageBuilder.h` - JSON message formatting utilities
|
||||
- `src/helpers/MQTTMessageBuilder.cpp` - JSON message formatting implementation
|
||||
|
||||
### Integration
|
||||
- Updated `examples/simple_repeater/MyMesh.h` - Added MQTT bridge support
|
||||
- Updated `examples/simple_repeater/MyMesh.cpp` - Added MQTT bridge integration
|
||||
- Updated `src/helpers/CommonCLI.h` - Added MQTT configuration fields
|
||||
- Updated `src/helpers/CommonCLI.cpp` - Added MQTT CLI commands
|
||||
- Updated `variants/heltec_v3/platformio.ini` - Added MQTT build configuration
|
||||
|
||||
## Build Configuration
|
||||
|
||||
To build the MQTT bridge firmware:
|
||||
|
||||
```bash
|
||||
pio run -e Heltec_v3_repeater_bridge_mqtt
|
||||
```
|
||||
|
||||
## Default Configuration
|
||||
|
||||
The MQTT bridge comes with the following defaults:
|
||||
- **Origin**: "MeshCore-Repeater"
|
||||
- **IATA**: "SEA"
|
||||
- **Status Messages**: Enabled
|
||||
- **Packet Messages**: Enabled
|
||||
- **Raw Messages**: Disabled
|
||||
- **Status Interval**: 5 minutes (300000 ms)
|
||||
- **Default Broker**: meshtastic.pugetmesh.org:1883 (username: meshdev, password: large4cats)
|
||||
|
||||
## CLI Commands
|
||||
|
||||
### Get Commands
|
||||
- `get mqtt.origin` - Get device origin name
|
||||
- `get mqtt.iata` - Get IATA code
|
||||
- `get mqtt.status` - Get status message setting (on/off)
|
||||
- `get mqtt.packets` - Get packet message setting (on/off)
|
||||
- `get mqtt.raw` - Get raw message setting (on/off)
|
||||
- `get mqtt.interval` - Get status publish interval (ms)
|
||||
|
||||
### Set Commands
|
||||
- `set mqtt.origin <name>` - Set device origin name
|
||||
- `set mqtt.iata <code>` - Set IATA code
|
||||
- `set mqtt.status on|off` - Enable/disable status messages
|
||||
- `set mqtt.packets on|off` - Enable/disable packet messages
|
||||
- `set mqtt.raw on|off` - Enable/disable raw messages
|
||||
- `set mqtt.interval <ms>` - Set status publish interval (1000-3600000 ms)
|
||||
|
||||
## MQTT Topics
|
||||
|
||||
The bridge publishes to three main topics:
|
||||
|
||||
### Status Topic: `meshcore/{IATA}/status`
|
||||
Device connection status and metadata (retained messages).
|
||||
|
||||
### Packets Topic: `meshcore/{IATA}/packets`
|
||||
Full packet data with RF characteristics and metadata.
|
||||
|
||||
### Raw Topic: `meshcore/{IATA}/raw`
|
||||
Minimal raw packet data for map integration.
|
||||
|
||||
## JSON Message Formats
|
||||
|
||||
### Status Message
|
||||
```json
|
||||
{
|
||||
"status": "online|offline",
|
||||
"timestamp": "2024-01-01T12:00:00.000000",
|
||||
"origin": "Device Name",
|
||||
"origin_id": "DEVICE_PUBLIC_KEY",
|
||||
"model": "device_model",
|
||||
"firmware_version": "firmware_version",
|
||||
"radio": "radio_info",
|
||||
"client_version": "packet_capture_version"
|
||||
}
|
||||
```
|
||||
|
||||
### Packet Message
|
||||
```json
|
||||
{
|
||||
"origin": "MeshCore-HOWL",
|
||||
"origin_id": "A1B2C3D4E5F67890...",
|
||||
"timestamp": "2024-01-01T12:00:00.000000",
|
||||
"type": "PACKET",
|
||||
"direction": "rx|tx",
|
||||
"time": "12:00:00",
|
||||
"date": "01/01/2024",
|
||||
"len": "45",
|
||||
"packet_type": "4",
|
||||
"route": "F|D|T|U",
|
||||
"payload_len": "32",
|
||||
"raw": "F5930103807E5F1E...",
|
||||
"SNR": "12.5",
|
||||
"RSSI": "-65",
|
||||
"hash": "A1B2C3D4E5F67890",
|
||||
"path": "node1,node2,node3"
|
||||
}
|
||||
```
|
||||
|
||||
### Raw Message
|
||||
```json
|
||||
{
|
||||
"origin": "MeshCore-HOWL",
|
||||
"origin_id": "A1B2C3D4E5F67890...",
|
||||
"timestamp": "2024-01-01T12:00:00.000000",
|
||||
"type": "RAW",
|
||||
"data": "F5930103807E5F1E..."
|
||||
}
|
||||
```
|
||||
|
||||
## Testing
|
||||
|
||||
1. Flash the MQTT bridge firmware to your device
|
||||
2. Connect to the device via serial console
|
||||
3. Configure WiFi connection (if not already connected)
|
||||
4. Check MQTT settings: `get mqtt.origin`
|
||||
5. Monitor MQTT broker for incoming messages
|
||||
|
||||
## Dependencies
|
||||
|
||||
- **PubSubClient**: MQTT client library
|
||||
- **ArduinoJson**: JSON message formatting
|
||||
- **WiFi**: ESP32 WiFi functionality
|
||||
|
||||
## Future Enhancements
|
||||
|
||||
- Device-signed JWTs for WebSocket MQTT servers
|
||||
- Multiple broker configuration via CLI
|
||||
- Advanced packet filtering
|
||||
- Custom topic templates
|
||||
- TLS/SSL support for secure connections
|
||||
@@ -671,8 +671,9 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
|
||||
discover_limiter(4, 120) // max 4 every 2 minutes
|
||||
#if defined(WITH_RS232_BRIDGE)
|
||||
, bridge(&_prefs, WITH_RS232_BRIDGE, _mgr, &rtc)
|
||||
#endif
|
||||
#if defined(WITH_ESPNOW_BRIDGE)
|
||||
#elif defined(WITH_ESPNOW_BRIDGE)
|
||||
, bridge(&_prefs, _mgr, &rtc)
|
||||
#elif defined(WITH_MQTT_BRIDGE)
|
||||
, bridge(&_prefs, _mgr, &rtc)
|
||||
#endif
|
||||
{
|
||||
@@ -722,7 +723,17 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
|
||||
_prefs.gps_interval = 0;
|
||||
_prefs.advert_loc_policy = ADVERT_LOC_PREFS;
|
||||
|
||||
_prefs.adc_multiplier = 0.0f; // 0.0f means use default board multiplier
|
||||
// MQTT defaults
|
||||
StrHelper::strncpy(_prefs.mqtt_origin, "MeshCore-Repeater", sizeof(_prefs.mqtt_origin));
|
||||
StrHelper::strncpy(_prefs.mqtt_iata, "SEA", sizeof(_prefs.mqtt_iata));
|
||||
_prefs.mqtt_status_enabled = 1; // enabled
|
||||
_prefs.mqtt_packets_enabled = 1; // enabled
|
||||
_prefs.mqtt_raw_enabled = 0; // disabled
|
||||
_prefs.mqtt_status_interval = 300000; // 5 minutes
|
||||
|
||||
// WiFi defaults
|
||||
StrHelper::strncpy(_prefs.wifi_ssid, "ssid_here", sizeof(_prefs.wifi_ssid));
|
||||
StrHelper::strncpy(_prefs.wifi_password, "password_here", sizeof(_prefs.wifi_password));
|
||||
}
|
||||
|
||||
void MyMesh::begin(FILESYSTEM *fs) {
|
||||
@@ -736,6 +747,19 @@ void MyMesh::begin(FILESYSTEM *fs) {
|
||||
|
||||
#if defined(WITH_BRIDGE)
|
||||
if (_prefs.bridge_enabled) {
|
||||
// Set device public key for MQTT topics
|
||||
char device_id[65];
|
||||
mesh::LocalIdentity self_id = getSelfId();
|
||||
mesh::Utils::toHex(device_id, self_id.pub_key, PUB_KEY_SIZE);
|
||||
MESH_DEBUG_PRINTLN("Setting device ID: %s", device_id);
|
||||
bridge.setDeviceID(device_id);
|
||||
|
||||
// Set firmware version
|
||||
bridge.setFirmwareVersion(getFirmwareVer());
|
||||
|
||||
// Set board model
|
||||
bridge.setBoardModel(_cli.getBoard()->getManufacturerName());
|
||||
|
||||
bridge.begin();
|
||||
}
|
||||
#endif
|
||||
@@ -746,8 +770,6 @@ void MyMesh::begin(FILESYSTEM *fs) {
|
||||
updateAdvertTimer();
|
||||
updateFloodAdvertTimer();
|
||||
|
||||
board.setAdcMultiplier(_prefs.adc_multiplier);
|
||||
|
||||
#if ENV_INCLUDE_GPS == 1
|
||||
applyGpsPrefs();
|
||||
#endif
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <Arduino.h>
|
||||
#include <Mesh.h>
|
||||
#include <RTClib.h>
|
||||
#include <CayenneLPP.h>
|
||||
#include <target.h>
|
||||
|
||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||
@@ -23,6 +24,11 @@
|
||||
#define WITH_BRIDGE
|
||||
#endif
|
||||
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
#include "helpers/bridges/MQTTBridge.h"
|
||||
#define WITH_BRIDGE
|
||||
#endif
|
||||
|
||||
#include <helpers/AdvertDataHelpers.h>
|
||||
#include <helpers/ArduinoHelpers.h>
|
||||
#include <helpers/ClientACL.h>
|
||||
@@ -35,9 +41,6 @@
|
||||
#include <helpers/RegionMap.h>
|
||||
#include "RateLimiter.h"
|
||||
|
||||
#ifdef WITH_BRIDGE
|
||||
extern AbstractBridge* bridge;
|
||||
#endif
|
||||
|
||||
struct RepeaterStats {
|
||||
uint16_t batt_milli_volts;
|
||||
@@ -110,6 +113,8 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
||||
RS232Bridge bridge;
|
||||
#elif defined(WITH_ESPNOW_BRIDGE)
|
||||
ESPNowBridge bridge;
|
||||
#elif defined(WITH_MQTT_BRIDGE)
|
||||
MQTTBridge bridge;
|
||||
#endif
|
||||
|
||||
void putNeighbour(const mesh::Identity& id, uint32_t timestamp, float snr);
|
||||
|
||||
+283
-27
@@ -70,9 +70,34 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) {
|
||||
file.read((uint8_t *)&_prefs->gps_enabled, sizeof(_prefs->gps_enabled)); // 156
|
||||
file.read((uint8_t *)&_prefs->gps_interval, sizeof(_prefs->gps_interval)); // 157
|
||||
file.read((uint8_t *)&_prefs->advert_loc_policy, sizeof (_prefs->advert_loc_policy)); // 161
|
||||
file.read((uint8_t *)&_prefs->discovery_mod_timestamp, sizeof(_prefs->discovery_mod_timestamp)); // 162
|
||||
file.read((uint8_t *)&_prefs->adc_multiplier, sizeof(_prefs->adc_multiplier)); // 166
|
||||
// 170
|
||||
file.read((uint8_t *)&_prefs->discovery_mod_timestamp, sizeof(_prefs->discovery_mod_timestamp)); // 162 (from upstream)
|
||||
// MQTT settings - skip reading from main prefs file (now stored separately)
|
||||
// For backward compatibility, we'll skip these bytes if they exist in old files
|
||||
// The actual MQTT prefs will be loaded from /mqtt_prefs in loadMQTTPrefs()
|
||||
// Skip MQTT fields for file format compatibility (whether MQTT bridge is enabled or not)
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
size_t mqtt_fields_size = getMQTTFieldsSize(_prefs);
|
||||
#else
|
||||
// If MQTT bridge not enabled, still skip these fields for file format compatibility
|
||||
size_t mqtt_fields_size =
|
||||
sizeof(_prefs->mqtt_origin) + sizeof(_prefs->mqtt_iata) +
|
||||
sizeof(_prefs->mqtt_status_enabled) + sizeof(_prefs->mqtt_packets_enabled) +
|
||||
sizeof(_prefs->mqtt_raw_enabled) + sizeof(_prefs->mqtt_tx_enabled) +
|
||||
sizeof(_prefs->mqtt_status_interval) + sizeof(_prefs->wifi_ssid) +
|
||||
sizeof(_prefs->wifi_password) + sizeof(_prefs->timezone_string) +
|
||||
sizeof(_prefs->timezone_offset) + sizeof(_prefs->mqtt_server) +
|
||||
sizeof(_prefs->mqtt_port) + sizeof(_prefs->mqtt_username) +
|
||||
sizeof(_prefs->mqtt_password) + sizeof(_prefs->mqtt_analyzer_us_enabled) +
|
||||
sizeof(_prefs->mqtt_analyzer_eu_enabled) + sizeof(_prefs->mqtt_owner_public_key) +
|
||||
sizeof(_prefs->mqtt_email);
|
||||
#endif
|
||||
uint8_t skip_buffer[512]; // Large enough buffer
|
||||
size_t remaining = mqtt_fields_size;
|
||||
while (remaining > 0) {
|
||||
size_t to_read = remaining > sizeof(skip_buffer) ? sizeof(skip_buffer) : remaining;
|
||||
file.read(skip_buffer, to_read);
|
||||
remaining -= to_read;
|
||||
}
|
||||
|
||||
// sanitise bad pref values
|
||||
_prefs->rx_delay_base = constrain(_prefs->rx_delay_base, 0, 20.0f);
|
||||
@@ -85,7 +110,6 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) {
|
||||
_prefs->cr = constrain(_prefs->cr, 5, 8);
|
||||
_prefs->tx_power_dbm = constrain(_prefs->tx_power_dbm, 1, 30);
|
||||
_prefs->multi_acks = constrain(_prefs->multi_acks, 0, 1);
|
||||
_prefs->adc_multiplier = constrain(_prefs->adc_multiplier, 0.0f, 10.0f);
|
||||
|
||||
// sanitise bad bridge pref values
|
||||
_prefs->bridge_enabled = constrain(_prefs->bridge_enabled, 0, 1);
|
||||
@@ -153,12 +177,40 @@ void CommonCLI::savePrefs(FILESYSTEM* fs) {
|
||||
file.write((uint8_t *)&_prefs->gps_enabled, sizeof(_prefs->gps_enabled)); // 156
|
||||
file.write((uint8_t *)&_prefs->gps_interval, sizeof(_prefs->gps_interval)); // 157
|
||||
file.write((uint8_t *)&_prefs->advert_loc_policy, sizeof(_prefs->advert_loc_policy)); // 161
|
||||
file.write((uint8_t *)&_prefs->discovery_mod_timestamp, sizeof(_prefs->discovery_mod_timestamp)); // 162
|
||||
file.write((uint8_t *)&_prefs->adc_multiplier, sizeof(_prefs->adc_multiplier)); // 166
|
||||
// 170
|
||||
file.write((uint8_t *)&_prefs->discovery_mod_timestamp, sizeof(_prefs->discovery_mod_timestamp)); // 162 (from upstream)
|
||||
// MQTT settings - no longer saved here (stored in separate /mqtt_prefs file)
|
||||
// Write zeros/padding to maintain file format compatibility
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
size_t mqtt_fields_size = getMQTTFieldsSize(_prefs);
|
||||
#else
|
||||
// If MQTT bridge not enabled, still write zeros for file format compatibility
|
||||
size_t mqtt_fields_size =
|
||||
sizeof(_prefs->mqtt_origin) + sizeof(_prefs->mqtt_iata) +
|
||||
sizeof(_prefs->mqtt_status_enabled) + sizeof(_prefs->mqtt_packets_enabled) +
|
||||
sizeof(_prefs->mqtt_raw_enabled) + sizeof(_prefs->mqtt_tx_enabled) +
|
||||
sizeof(_prefs->mqtt_status_interval) + sizeof(_prefs->wifi_ssid) +
|
||||
sizeof(_prefs->wifi_password) + sizeof(_prefs->timezone_string) +
|
||||
sizeof(_prefs->timezone_offset) + sizeof(_prefs->mqtt_server) +
|
||||
sizeof(_prefs->mqtt_port) + sizeof(_prefs->mqtt_username) +
|
||||
sizeof(_prefs->mqtt_password) + sizeof(_prefs->mqtt_analyzer_us_enabled) +
|
||||
sizeof(_prefs->mqtt_analyzer_eu_enabled) + sizeof(_prefs->mqtt_owner_public_key) +
|
||||
sizeof(_prefs->mqtt_email);
|
||||
#endif
|
||||
memset(pad, 0, sizeof(pad));
|
||||
size_t remaining = mqtt_fields_size;
|
||||
while (remaining > 0) {
|
||||
size_t to_write = remaining > sizeof(pad) ? sizeof(pad) : remaining;
|
||||
file.write(pad, to_write);
|
||||
remaining -= to_write;
|
||||
}
|
||||
|
||||
file.close();
|
||||
}
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
// Save MQTT preferences to separate file
|
||||
syncNodePrefsToMQTTPrefs(); // Sync any changes from NodePrefs to MQTTPrefs
|
||||
saveMQTTPrefs(fs);
|
||||
#endif
|
||||
}
|
||||
|
||||
#define MIN_LOCAL_ADVERT_INTERVAL 60
|
||||
@@ -338,13 +390,24 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
|
||||
} else if (memcmp(config, "bridge.secret", 13) == 0) {
|
||||
sprintf(reply, "> %s", _prefs->bridge_secret);
|
||||
#endif
|
||||
} else if (memcmp(config, "adc.multiplier", 14) == 0) {
|
||||
float adc_mult = _board->getAdcMultiplier();
|
||||
if (adc_mult == 0.0f) {
|
||||
strcpy(reply, "Error: unsupported by this board");
|
||||
} else {
|
||||
sprintf(reply, "> %.3f", adc_mult);
|
||||
}
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
} else if (memcmp(config, "mqtt.origin", 11) == 0) {
|
||||
sprintf(reply, "> %s", _prefs->mqtt_origin);
|
||||
} else if (memcmp(config, "mqtt.iata", 9) == 0) {
|
||||
sprintf(reply, "> %s", _prefs->mqtt_iata);
|
||||
} else if (memcmp(config, "mqtt.status", 11) == 0) {
|
||||
sprintf(reply, "> %s", _prefs->mqtt_status_enabled ? "on" : "off");
|
||||
} else if (memcmp(config, "mqtt.packets", 12) == 0) {
|
||||
sprintf(reply, "> %s", _prefs->mqtt_packets_enabled ? "on" : "off");
|
||||
} else if (memcmp(config, "mqtt.raw", 8) == 0) {
|
||||
sprintf(reply, "> %s", _prefs->mqtt_raw_enabled ? "on" : "off");
|
||||
} else if (memcmp(config, "mqtt.interval", 13) == 0) {
|
||||
sprintf(reply, "> %d", (uint32_t)_prefs->mqtt_status_interval);
|
||||
} else if (memcmp(config, "wifi.ssid", 9) == 0) {
|
||||
sprintf(reply, "> %s", _prefs->wifi_ssid);
|
||||
} else if (memcmp(config, "wifi.pwd", 8) == 0) {
|
||||
sprintf(reply, "> %s", _prefs->wifi_password);
|
||||
#endif
|
||||
} else {
|
||||
sprintf(reply, "??: %s", config);
|
||||
}
|
||||
@@ -537,19 +600,45 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
#endif
|
||||
} else if (memcmp(config, "adc.multiplier ", 15) == 0) {
|
||||
_prefs->adc_multiplier = atof(&config[15]);
|
||||
if (_board->setAdcMultiplier(_prefs->adc_multiplier)) {
|
||||
savePrefs();
|
||||
if (_prefs->adc_multiplier == 0.0f) {
|
||||
strcpy(reply, "OK - using default board multiplier");
|
||||
} else {
|
||||
sprintf(reply, "OK - multiplier set to %.3f", _prefs->adc_multiplier);
|
||||
}
|
||||
} else {
|
||||
_prefs->adc_multiplier = 0.0f;
|
||||
strcpy(reply, "Error: unsupported by this board");
|
||||
};
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
} else if (memcmp(config, "mqtt.origin ", 12) == 0) {
|
||||
StrHelper::strncpy(_prefs->mqtt_origin, &config[12], sizeof(_prefs->mqtt_origin));
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else if (memcmp(config, "mqtt.iata ", 10) == 0) {
|
||||
StrHelper::strncpy(_prefs->mqtt_iata, &config[10], sizeof(_prefs->mqtt_iata));
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else if (memcmp(config, "mqtt.status ", 12) == 0) {
|
||||
_prefs->mqtt_status_enabled = memcmp(&config[12], "on", 2) == 0;
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else if (memcmp(config, "mqtt.packets ", 13) == 0) {
|
||||
_prefs->mqtt_packets_enabled = memcmp(&config[13], "on", 2) == 0;
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else if (memcmp(config, "mqtt.raw ", 9) == 0) {
|
||||
_prefs->mqtt_raw_enabled = memcmp(&config[9], "on", 2) == 0;
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else if (memcmp(config, "mqtt.interval ", 15) == 0) {
|
||||
uint32_t interval = _atoi(&config[15]);
|
||||
if (interval >= 1000 && interval <= 3600000) { // 1 second to 1 hour
|
||||
_prefs->mqtt_status_interval = interval;
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else {
|
||||
strcpy(reply, "Error: interval must be between 1000-3600000 ms");
|
||||
}
|
||||
} else if (memcmp(config, "wifi.ssid ", 10) == 0) {
|
||||
StrHelper::strncpy(_prefs->wifi_ssid, &config[10], sizeof(_prefs->wifi_ssid));
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else if (memcmp(config, "wifi.pwd ", 9) == 0) {
|
||||
StrHelper::strncpy(_prefs->wifi_password, &config[9], sizeof(_prefs->wifi_password));
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
#endif
|
||||
} else {
|
||||
sprintf(reply, "unknown config: %s", config);
|
||||
}
|
||||
@@ -716,3 +805,170 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
|
||||
strcpy(reply, "Unknown command");
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
// Set default values for MQTT preferences (used when file doesn't exist or is corrupted)
|
||||
static void setMQTTPrefsDefaults(MQTTPrefs* prefs) {
|
||||
memset(prefs, 0, sizeof(MQTTPrefs));
|
||||
// Set sensible defaults matching MQTTBridge expectations
|
||||
StrHelper::strncpy(prefs->mqtt_origin, "MeshCore-Repeater", sizeof(prefs->mqtt_origin));
|
||||
StrHelper::strncpy(prefs->mqtt_iata, "XXX", sizeof(prefs->mqtt_iata));
|
||||
prefs->mqtt_status_enabled = 1;
|
||||
prefs->mqtt_packets_enabled = 1;
|
||||
prefs->mqtt_raw_enabled = 0;
|
||||
prefs->mqtt_tx_enabled = 0;
|
||||
prefs->mqtt_status_interval = 300000; // 5 minutes
|
||||
StrHelper::strncpy(prefs->wifi_ssid, "ssid_here", sizeof(prefs->wifi_ssid));
|
||||
StrHelper::strncpy(prefs->wifi_password, "password_here", sizeof(prefs->wifi_password));
|
||||
StrHelper::strncpy(prefs->timezone_string, "America/Los_Angeles", sizeof(prefs->timezone_string));
|
||||
prefs->timezone_offset = -8;
|
||||
prefs->mqtt_analyzer_us_enabled = 1;
|
||||
prefs->mqtt_analyzer_eu_enabled = 1;
|
||||
}
|
||||
|
||||
void CommonCLI::loadMQTTPrefs(FILESYSTEM* fs) {
|
||||
// Initialize with zeros
|
||||
memset(&_mqtt_prefs, 0, sizeof(_mqtt_prefs));
|
||||
|
||||
if (fs->exists("/mqtt_prefs")) {
|
||||
// Load from separate MQTT prefs file
|
||||
#if defined(RP2040_PLATFORM)
|
||||
File file = fs->open("/mqtt_prefs", "r");
|
||||
#else
|
||||
File file = fs->open("/mqtt_prefs");
|
||||
#endif
|
||||
if (file) {
|
||||
// Verify file size is correct before reading
|
||||
if (file.size() >= sizeof(_mqtt_prefs)) {
|
||||
size_t bytes_read = file.read((uint8_t *)&_mqtt_prefs, sizeof(_mqtt_prefs));
|
||||
if (bytes_read != sizeof(_mqtt_prefs)) {
|
||||
// File read incomplete - reinitialize to defaults
|
||||
setMQTTPrefsDefaults(&_mqtt_prefs);
|
||||
}
|
||||
} else {
|
||||
// File too small - reinitialize to defaults
|
||||
setMQTTPrefsDefaults(&_mqtt_prefs);
|
||||
}
|
||||
file.close();
|
||||
} else {
|
||||
// File open failed - use defaults
|
||||
setMQTTPrefsDefaults(&_mqtt_prefs);
|
||||
}
|
||||
} else {
|
||||
// Migration: Try to read from old /com_prefs file if it exists
|
||||
// This handles the case where MQTT settings were previously stored in /com_prefs
|
||||
if (fs->exists("/com_prefs")) {
|
||||
#if defined(RP2040_PLATFORM)
|
||||
File file = fs->open("/com_prefs", "r");
|
||||
#else
|
||||
File file = fs->open("/com_prefs");
|
||||
#endif
|
||||
if (file) {
|
||||
// Skip to MQTT section (after discovery_mod_timestamp)
|
||||
// Calculate offset: we need to skip everything up to and including discovery_mod_timestamp
|
||||
// Note: discovery_mod_timestamp was added in upstream dev branch
|
||||
size_t offset_to_mqtt =
|
||||
sizeof(_prefs->airtime_factor) + sizeof(_prefs->node_name) + 4 + // pad
|
||||
sizeof(_prefs->node_lat) + sizeof(_prefs->node_lon) +
|
||||
sizeof(_prefs->password) + sizeof(_prefs->freq) +
|
||||
sizeof(_prefs->tx_power_dbm) + sizeof(_prefs->disable_fwd) +
|
||||
sizeof(_prefs->advert_interval) + 1 + // pad
|
||||
sizeof(_prefs->rx_delay_base) + sizeof(_prefs->tx_delay_factor) +
|
||||
sizeof(_prefs->guest_password) + sizeof(_prefs->direct_tx_delay_factor) + 4 + // pad
|
||||
sizeof(_prefs->sf) + sizeof(_prefs->cr) +
|
||||
sizeof(_prefs->allow_read_only) + sizeof(_prefs->multi_acks) +
|
||||
sizeof(_prefs->bw) + sizeof(_prefs->agc_reset_interval) + 3 + // pad
|
||||
sizeof(_prefs->flood_max) + sizeof(_prefs->flood_advert_interval) +
|
||||
sizeof(_prefs->interference_threshold) + sizeof(_prefs->bridge_enabled) +
|
||||
sizeof(_prefs->bridge_delay) + sizeof(_prefs->bridge_pkt_src) +
|
||||
sizeof(_prefs->bridge_baud) + sizeof(_prefs->bridge_channel) +
|
||||
sizeof(_prefs->bridge_secret) + 4 + // pad
|
||||
sizeof(_prefs->gps_enabled) + sizeof(_prefs->gps_interval) +
|
||||
sizeof(_prefs->advert_loc_policy) +
|
||||
sizeof(_prefs->discovery_mod_timestamp); // Added in upstream dev branch
|
||||
|
||||
// Check if file is large enough and seek succeeded
|
||||
if (file.size() >= offset_to_mqtt + sizeof(_mqtt_prefs)) {
|
||||
if (file.seek(offset_to_mqtt)) {
|
||||
size_t bytes_read = file.read((uint8_t *)&_mqtt_prefs, sizeof(_mqtt_prefs));
|
||||
if (bytes_read == sizeof(_mqtt_prefs)) {
|
||||
// Successfully migrated - save to new location for future use
|
||||
file.close();
|
||||
saveMQTTPrefs(fs);
|
||||
return; // Migration successful
|
||||
}
|
||||
}
|
||||
}
|
||||
file.close();
|
||||
// Migration failed - defaults already set, just return
|
||||
return;
|
||||
}
|
||||
}
|
||||
// No file exists and migration didn't happen - defaults already set
|
||||
setMQTTPrefsDefaults(&_mqtt_prefs);
|
||||
}
|
||||
}
|
||||
|
||||
void CommonCLI::saveMQTTPrefs(FILESYSTEM* fs) {
|
||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||
fs->remove("/mqtt_prefs");
|
||||
File file = fs->open("/mqtt_prefs", FILE_O_WRITE);
|
||||
#elif defined(RP2040_PLATFORM)
|
||||
File file = fs->open("/mqtt_prefs", "w");
|
||||
#else
|
||||
File file = fs->open("/mqtt_prefs", "w", true);
|
||||
#endif
|
||||
if (file) {
|
||||
file.write((uint8_t *)&_mqtt_prefs, sizeof(_mqtt_prefs));
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
void CommonCLI::syncMQTTPrefsToNodePrefs() {
|
||||
// Copy MQTT prefs to NodePrefs so existing code can access them
|
||||
// Use StrHelper::strncpy to ensure proper null termination
|
||||
StrHelper::strncpy(_prefs->mqtt_origin, _mqtt_prefs.mqtt_origin, sizeof(_prefs->mqtt_origin));
|
||||
StrHelper::strncpy(_prefs->mqtt_iata, _mqtt_prefs.mqtt_iata, sizeof(_prefs->mqtt_iata));
|
||||
_prefs->mqtt_status_enabled = _mqtt_prefs.mqtt_status_enabled;
|
||||
_prefs->mqtt_packets_enabled = _mqtt_prefs.mqtt_packets_enabled;
|
||||
_prefs->mqtt_raw_enabled = _mqtt_prefs.mqtt_raw_enabled;
|
||||
_prefs->mqtt_tx_enabled = _mqtt_prefs.mqtt_tx_enabled;
|
||||
_prefs->mqtt_status_interval = _mqtt_prefs.mqtt_status_interval;
|
||||
StrHelper::strncpy(_prefs->wifi_ssid, _mqtt_prefs.wifi_ssid, sizeof(_prefs->wifi_ssid));
|
||||
StrHelper::strncpy(_prefs->wifi_password, _mqtt_prefs.wifi_password, sizeof(_prefs->wifi_password));
|
||||
StrHelper::strncpy(_prefs->timezone_string, _mqtt_prefs.timezone_string, sizeof(_prefs->timezone_string));
|
||||
_prefs->timezone_offset = _mqtt_prefs.timezone_offset;
|
||||
StrHelper::strncpy(_prefs->mqtt_server, _mqtt_prefs.mqtt_server, sizeof(_prefs->mqtt_server));
|
||||
_prefs->mqtt_port = _mqtt_prefs.mqtt_port;
|
||||
StrHelper::strncpy(_prefs->mqtt_username, _mqtt_prefs.mqtt_username, sizeof(_prefs->mqtt_username));
|
||||
StrHelper::strncpy(_prefs->mqtt_password, _mqtt_prefs.mqtt_password, sizeof(_prefs->mqtt_password));
|
||||
_prefs->mqtt_analyzer_us_enabled = _mqtt_prefs.mqtt_analyzer_us_enabled;
|
||||
_prefs->mqtt_analyzer_eu_enabled = _mqtt_prefs.mqtt_analyzer_eu_enabled;
|
||||
StrHelper::strncpy(_prefs->mqtt_owner_public_key, _mqtt_prefs.mqtt_owner_public_key, sizeof(_prefs->mqtt_owner_public_key));
|
||||
StrHelper::strncpy(_prefs->mqtt_email, _mqtt_prefs.mqtt_email, sizeof(_prefs->mqtt_email));
|
||||
}
|
||||
|
||||
void CommonCLI::syncNodePrefsToMQTTPrefs() {
|
||||
// Copy NodePrefs to MQTT prefs (used when saving after changes via CLI)
|
||||
// Use StrHelper::strncpy to ensure proper null termination
|
||||
StrHelper::strncpy(_mqtt_prefs.mqtt_origin, _prefs->mqtt_origin, sizeof(_mqtt_prefs.mqtt_origin));
|
||||
StrHelper::strncpy(_mqtt_prefs.mqtt_iata, _prefs->mqtt_iata, sizeof(_mqtt_prefs.mqtt_iata));
|
||||
_mqtt_prefs.mqtt_status_enabled = _prefs->mqtt_status_enabled;
|
||||
_mqtt_prefs.mqtt_packets_enabled = _prefs->mqtt_packets_enabled;
|
||||
_mqtt_prefs.mqtt_raw_enabled = _prefs->mqtt_raw_enabled;
|
||||
_mqtt_prefs.mqtt_tx_enabled = _prefs->mqtt_tx_enabled;
|
||||
_mqtt_prefs.mqtt_status_interval = _prefs->mqtt_status_interval;
|
||||
StrHelper::strncpy(_mqtt_prefs.wifi_ssid, _prefs->wifi_ssid, sizeof(_mqtt_prefs.wifi_ssid));
|
||||
StrHelper::strncpy(_mqtt_prefs.wifi_password, _prefs->wifi_password, sizeof(_mqtt_prefs.wifi_password));
|
||||
StrHelper::strncpy(_mqtt_prefs.timezone_string, _prefs->timezone_string, sizeof(_mqtt_prefs.timezone_string));
|
||||
_mqtt_prefs.timezone_offset = _prefs->timezone_offset;
|
||||
StrHelper::strncpy(_mqtt_prefs.mqtt_server, _prefs->mqtt_server, sizeof(_mqtt_prefs.mqtt_server));
|
||||
_mqtt_prefs.mqtt_port = _prefs->mqtt_port;
|
||||
StrHelper::strncpy(_mqtt_prefs.mqtt_username, _prefs->mqtt_username, sizeof(_mqtt_prefs.mqtt_username));
|
||||
StrHelper::strncpy(_mqtt_prefs.mqtt_password, _prefs->mqtt_password, sizeof(_mqtt_prefs.mqtt_password));
|
||||
_mqtt_prefs.mqtt_analyzer_us_enabled = _prefs->mqtt_analyzer_us_enabled;
|
||||
_mqtt_prefs.mqtt_analyzer_eu_enabled = _prefs->mqtt_analyzer_eu_enabled;
|
||||
StrHelper::strncpy(_mqtt_prefs.mqtt_owner_public_key, _prefs->mqtt_owner_public_key, sizeof(_mqtt_prefs.mqtt_owner_public_key));
|
||||
StrHelper::strncpy(_mqtt_prefs.mqtt_email, _prefs->mqtt_email, sizeof(_mqtt_prefs.mqtt_email));
|
||||
}
|
||||
#endif
|
||||
+74
-3
@@ -4,7 +4,7 @@
|
||||
#include <helpers/IdentityStore.h>
|
||||
#include <helpers/SensorManager.h>
|
||||
|
||||
#if defined(WITH_RS232_BRIDGE) || defined(WITH_ESPNOW_BRIDGE)
|
||||
#if defined(WITH_RS232_BRIDGE) || defined(WITH_ESPNOW_BRIDGE) || defined(WITH_MQTT_BRIDGE)
|
||||
#define WITH_BRIDGE
|
||||
#endif
|
||||
|
||||
@@ -48,10 +48,71 @@ struct NodePrefs { // persisted to file
|
||||
uint8_t gps_enabled;
|
||||
uint32_t gps_interval; // in seconds
|
||||
uint8_t advert_loc_policy;
|
||||
uint32_t discovery_mod_timestamp;
|
||||
float adc_multiplier;
|
||||
uint32_t discovery_mod_timestamp; // From upstream dev branch
|
||||
// MQTT settings (stored separately in /mqtt_prefs, but kept here for backward compatibility)
|
||||
char mqtt_origin[32]; // Device name for MQTT topics
|
||||
char mqtt_iata[8]; // IATA code for MQTT topics
|
||||
uint8_t mqtt_status_enabled; // Enable status messages
|
||||
uint8_t mqtt_packets_enabled; // Enable packet messages
|
||||
uint8_t mqtt_raw_enabled; // Enable raw messages
|
||||
uint8_t mqtt_tx_enabled; // Enable TX packet uplinking
|
||||
uint32_t mqtt_status_interval; // Status publish interval (ms)
|
||||
|
||||
// WiFi settings
|
||||
char wifi_ssid[32]; // WiFi SSID
|
||||
char wifi_password[64]; // WiFi password
|
||||
|
||||
// Timezone settings
|
||||
char timezone_string[32]; // Timezone string (e.g., "America/Los_Angeles")
|
||||
int8_t timezone_offset; // Timezone offset in hours (-12 to +14) - fallback
|
||||
|
||||
// MQTT server settings
|
||||
char mqtt_server[64]; // MQTT server hostname
|
||||
uint16_t mqtt_port; // MQTT server port
|
||||
char mqtt_username[32]; // MQTT username
|
||||
char mqtt_password[64]; // MQTT password
|
||||
|
||||
// Let's Mesh Analyzer settings
|
||||
uint8_t mqtt_analyzer_us_enabled; // Enable US analyzer server
|
||||
uint8_t mqtt_analyzer_eu_enabled; // Enable EU analyzer server
|
||||
char mqtt_owner_public_key[65]; // Owner public key (hex string, same length as repeater public key)
|
||||
char mqtt_email[64]; // Owner email address for matching nodes with owners
|
||||
};
|
||||
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
// MQTT preferences stored in separate file to avoid conflicts with upstream NodePrefs changes
|
||||
struct MQTTPrefs {
|
||||
// MQTT settings
|
||||
char mqtt_origin[32]; // Device name for MQTT topics
|
||||
char mqtt_iata[8]; // IATA code for MQTT topics
|
||||
uint8_t mqtt_status_enabled; // Enable status messages
|
||||
uint8_t mqtt_packets_enabled; // Enable packet messages
|
||||
uint8_t mqtt_raw_enabled; // Enable raw messages
|
||||
uint8_t mqtt_tx_enabled; // Enable TX packet uplinking
|
||||
uint32_t mqtt_status_interval; // Status publish interval (ms)
|
||||
|
||||
// WiFi settings
|
||||
char wifi_ssid[32]; // WiFi SSID
|
||||
char wifi_password[64]; // WiFi password
|
||||
|
||||
// Timezone settings
|
||||
char timezone_string[32]; // Timezone string (e.g., "America/Los_Angeles")
|
||||
int8_t timezone_offset; // Timezone offset in hours (-12 to +14) - fallback
|
||||
|
||||
// MQTT server settings
|
||||
char mqtt_server[64]; // MQTT server hostname
|
||||
uint16_t mqtt_port; // MQTT server port
|
||||
char mqtt_username[32]; // MQTT username
|
||||
char mqtt_password[64]; // MQTT password
|
||||
|
||||
// Let's Mesh Analyzer settings
|
||||
uint8_t mqtt_analyzer_us_enabled; // Enable US analyzer server
|
||||
uint8_t mqtt_analyzer_eu_enabled; // Enable EU analyzer server
|
||||
char mqtt_owner_public_key[65]; // Owner public key (hex string, same length as repeater public key)
|
||||
char mqtt_email[64]; // Owner email address for matching nodes with owners
|
||||
};
|
||||
#endif
|
||||
|
||||
class CommonCLICallbacks {
|
||||
public:
|
||||
virtual void savePrefs() = 0;
|
||||
@@ -94,10 +155,19 @@ class CommonCLI {
|
||||
mesh::MainBoard* _board;
|
||||
SensorManager* _sensors;
|
||||
char tmp[PRV_KEY_SIZE*2 + 4];
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
MQTTPrefs _mqtt_prefs;
|
||||
#endif
|
||||
|
||||
mesh::RTCClock* getRTCClock() { return _rtc; }
|
||||
void savePrefs();
|
||||
void loadPrefsInt(FILESYSTEM* _fs, const char* filename);
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
void loadMQTTPrefs(FILESYSTEM* fs);
|
||||
void saveMQTTPrefs(FILESYSTEM* fs);
|
||||
void syncMQTTPrefsToNodePrefs();
|
||||
void syncNodePrefsToMQTTPrefs();
|
||||
#endif
|
||||
|
||||
public:
|
||||
CommonCLI(mesh::MainBoard& board, mesh::RTCClock& rtc, SensorManager& sensors, NodePrefs* prefs, CommonCLICallbacks* callbacks)
|
||||
@@ -106,5 +176,6 @@ public:
|
||||
void loadPrefs(FILESYSTEM* _fs);
|
||||
void savePrefs(FILESYSTEM* _fs);
|
||||
void handleCommand(uint32_t sender_timestamp, const char* command, char* reply);
|
||||
mesh::MainBoard* getBoard() { return _board; }
|
||||
uint8_t buildAdvertData(uint8_t node_type, uint8_t* app_data);
|
||||
};
|
||||
@@ -0,0 +1,269 @@
|
||||
#include "MQTTMessageBuilder.h"
|
||||
#include <ArduinoJson.h>
|
||||
#include <time.h>
|
||||
|
||||
int MQTTMessageBuilder::buildStatusMessage(
|
||||
const char* origin,
|
||||
const char* origin_id,
|
||||
const char* model,
|
||||
const char* firmware_version,
|
||||
const char* radio,
|
||||
const char* client_version,
|
||||
const char* status,
|
||||
const char* timestamp,
|
||||
char* buffer,
|
||||
size_t buffer_size
|
||||
) {
|
||||
DynamicJsonDocument doc(512);
|
||||
JsonObject root = doc.to<JsonObject>();
|
||||
|
||||
root["status"] = status;
|
||||
root["timestamp"] = timestamp;
|
||||
root["origin"] = origin;
|
||||
root["origin_id"] = origin_id;
|
||||
root["model"] = model;
|
||||
root["firmware_version"] = firmware_version;
|
||||
root["radio"] = radio;
|
||||
root["client_version"] = client_version;
|
||||
|
||||
size_t len = serializeJson(root, buffer, buffer_size);
|
||||
return (len > 0 && len < buffer_size) ? len : 0;
|
||||
}
|
||||
|
||||
int MQTTMessageBuilder::buildPacketMessage(
|
||||
const char* origin,
|
||||
const char* origin_id,
|
||||
const char* timestamp,
|
||||
const char* direction,
|
||||
const char* time,
|
||||
const char* date,
|
||||
int len,
|
||||
int packet_type,
|
||||
const char* route,
|
||||
int payload_len,
|
||||
const char* raw,
|
||||
float snr,
|
||||
int rssi,
|
||||
const char* hash,
|
||||
const char* path,
|
||||
char* buffer,
|
||||
size_t buffer_size
|
||||
) {
|
||||
DynamicJsonDocument doc(1024);
|
||||
JsonObject root = doc.to<JsonObject>();
|
||||
|
||||
root["origin"] = origin;
|
||||
root["origin_id"] = origin_id;
|
||||
root["timestamp"] = timestamp;
|
||||
root["type"] = "PACKET";
|
||||
root["direction"] = direction;
|
||||
root["time"] = time;
|
||||
root["date"] = date;
|
||||
root["len"] = String(len);
|
||||
root["packet_type"] = String(packet_type);
|
||||
root["route"] = route;
|
||||
root["payload_len"] = String(payload_len);
|
||||
root["raw"] = raw;
|
||||
root["SNR"] = String(snr, 1);
|
||||
root["RSSI"] = String(rssi);
|
||||
root["hash"] = hash;
|
||||
|
||||
if (path && strlen(path) > 0) {
|
||||
root["path"] = path;
|
||||
}
|
||||
|
||||
size_t json_len = serializeJson(root, buffer, buffer_size);
|
||||
return (json_len > 0 && json_len < buffer_size) ? json_len : 0;
|
||||
}
|
||||
|
||||
int MQTTMessageBuilder::buildRawMessage(
|
||||
const char* origin,
|
||||
const char* origin_id,
|
||||
const char* timestamp,
|
||||
const char* raw,
|
||||
char* buffer,
|
||||
size_t buffer_size
|
||||
) {
|
||||
DynamicJsonDocument doc(512);
|
||||
JsonObject root = doc.to<JsonObject>();
|
||||
|
||||
root["origin"] = origin;
|
||||
root["origin_id"] = origin_id;
|
||||
root["timestamp"] = timestamp;
|
||||
root["type"] = "RAW";
|
||||
root["data"] = raw;
|
||||
|
||||
size_t len = serializeJson(root, buffer, buffer_size);
|
||||
return (len > 0 && len < buffer_size) ? len : 0;
|
||||
}
|
||||
|
||||
int MQTTMessageBuilder::buildPacketJSON(
|
||||
mesh::Packet* packet,
|
||||
bool is_tx,
|
||||
const char* origin,
|
||||
const char* origin_id,
|
||||
char* buffer,
|
||||
size_t buffer_size
|
||||
) {
|
||||
if (!packet) return 0;
|
||||
|
||||
// Get current device time
|
||||
time_t now = time(nullptr);
|
||||
struct tm* timeinfo = localtime(&now);
|
||||
|
||||
// Format timestamp in ISO 8601 format
|
||||
char timestamp[32];
|
||||
if (timeinfo) {
|
||||
strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%S.000000", timeinfo);
|
||||
} else {
|
||||
strcpy(timestamp, "2024-01-01T12:00:00.000000");
|
||||
}
|
||||
|
||||
// Format time and date
|
||||
char time_str[16];
|
||||
char date_str[16];
|
||||
if (timeinfo) {
|
||||
strftime(time_str, sizeof(time_str), "%H:%M:%S", timeinfo);
|
||||
strftime(date_str, sizeof(date_str), "%d/%m/%Y", timeinfo);
|
||||
} else {
|
||||
strcpy(time_str, "12:00:00");
|
||||
strcpy(date_str, "01/01/2024");
|
||||
}
|
||||
|
||||
// Convert packet to hex
|
||||
char raw_hex[512];
|
||||
packetToHex(packet, raw_hex, sizeof(raw_hex));
|
||||
|
||||
// Get packet characteristics
|
||||
int packet_type = packet->getPayloadType();
|
||||
const char* route_str = getRouteTypeString(packet->isRouteDirect() ? 1 : 0);
|
||||
|
||||
// Create hash (simplified - use first 8 bytes of packet)
|
||||
char hash_str[17];
|
||||
bytesToHex(packet->payload, min(8, (int)packet->payload_len), hash_str, sizeof(hash_str));
|
||||
|
||||
// Build path string for direct packets
|
||||
char path_str[128] = "";
|
||||
if (packet->isRouteDirect() && packet->path_len > 0) {
|
||||
// Simplified path representation
|
||||
snprintf(path_str, sizeof(path_str), "path_len_%d", packet->path_len);
|
||||
}
|
||||
|
||||
return buildPacketMessage(
|
||||
origin, origin_id, timestamp,
|
||||
is_tx ? "tx" : "rx",
|
||||
time_str, date_str,
|
||||
packet->path_len + packet->payload_len + 2,
|
||||
packet_type, route_str,
|
||||
packet->payload_len,
|
||||
raw_hex,
|
||||
12.5f, // SNR - using reasonable default
|
||||
-65, // RSSI - using reasonable default
|
||||
hash_str,
|
||||
packet->isRouteDirect() ? path_str : nullptr,
|
||||
buffer, buffer_size
|
||||
);
|
||||
}
|
||||
|
||||
int MQTTMessageBuilder::buildRawJSON(
|
||||
mesh::Packet* packet,
|
||||
const char* origin,
|
||||
const char* origin_id,
|
||||
char* buffer,
|
||||
size_t buffer_size
|
||||
) {
|
||||
if (!packet) return 0;
|
||||
|
||||
// Get current device time
|
||||
time_t now = time(nullptr);
|
||||
struct tm* timeinfo = localtime(&now);
|
||||
|
||||
// Format timestamp in ISO 8601 format
|
||||
char timestamp[32];
|
||||
if (timeinfo) {
|
||||
strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%S.000000", timeinfo);
|
||||
} else {
|
||||
strcpy(timestamp, "2024-01-01T12:00:00.000000");
|
||||
}
|
||||
|
||||
// Convert packet to hex
|
||||
char raw_hex[512];
|
||||
packetToHex(packet, raw_hex, sizeof(raw_hex));
|
||||
|
||||
return buildRawMessage(origin, origin_id, timestamp, raw_hex, buffer, buffer_size);
|
||||
}
|
||||
|
||||
const char* MQTTMessageBuilder::getPacketTypeString(int packet_type) {
|
||||
switch (packet_type) {
|
||||
case 0: return "0"; // REQ
|
||||
case 1: return "1"; // RESPONSE
|
||||
case 2: return "2"; // TXT_MSG
|
||||
case 3: return "3"; // ACK
|
||||
case 4: return "4"; // ADVERT
|
||||
case 5: return "5"; // GRP_TXT
|
||||
case 6: return "6"; // GRP_DATA
|
||||
case 7: return "7"; // ANON_REQ
|
||||
case 8: return "8"; // PATH
|
||||
case 9: return "9"; // TRACE
|
||||
case 10: return "10"; // MULTIPART
|
||||
case 11: return "11"; // Type11
|
||||
case 12: return "12"; // Type12
|
||||
case 13: return "13"; // Type13
|
||||
case 14: return "14"; // Type14
|
||||
case 15: return "15"; // RAW_CUSTOM
|
||||
default: return "0";
|
||||
}
|
||||
}
|
||||
|
||||
const char* MQTTMessageBuilder::getRouteTypeString(int route_type) {
|
||||
switch (route_type) {
|
||||
case 0: return "F"; // FLOOD
|
||||
case 1: return "D"; // DIRECT
|
||||
case 2: return "T"; // TRANSPORT_DIRECT
|
||||
default: return "U"; // UNKNOWN
|
||||
}
|
||||
}
|
||||
|
||||
void MQTTMessageBuilder::formatTimestamp(unsigned long timestamp, char* buffer, size_t buffer_size) {
|
||||
// Simplified timestamp formatting - in real implementation would use proper time
|
||||
snprintf(buffer, buffer_size, "2024-01-01T12:00:00.000000");
|
||||
}
|
||||
|
||||
void MQTTMessageBuilder::formatTime(unsigned long timestamp, char* buffer, size_t buffer_size) {
|
||||
// Simplified time formatting
|
||||
snprintf(buffer, buffer_size, "12:00:00");
|
||||
}
|
||||
|
||||
void MQTTMessageBuilder::formatDate(unsigned long timestamp, char* buffer, size_t buffer_size) {
|
||||
// Simplified date formatting
|
||||
snprintf(buffer, buffer_size, "01/01/2024");
|
||||
}
|
||||
|
||||
void MQTTMessageBuilder::bytesToHex(const uint8_t* data, size_t len, char* hex, size_t hex_size) {
|
||||
if (hex_size < len * 2 + 1) return;
|
||||
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
snprintf(hex + i * 2, 3, "%02X", data[i]);
|
||||
}
|
||||
hex[len * 2] = '\0';
|
||||
}
|
||||
|
||||
void MQTTMessageBuilder::packetToHex(mesh::Packet* packet, char* hex, size_t hex_size) {
|
||||
// Convert entire packet to hex string
|
||||
size_t total_len = packet->path_len + packet->payload_len + 2;
|
||||
if (hex_size < total_len * 2 + 1) return;
|
||||
|
||||
size_t offset = 0;
|
||||
|
||||
// Add path data
|
||||
if (packet->path_len > 0) {
|
||||
bytesToHex(packet->path, packet->path_len, hex + offset, hex_size - offset);
|
||||
offset += packet->path_len * 2;
|
||||
}
|
||||
|
||||
// Add payload data
|
||||
if (packet->payload_len > 0) {
|
||||
bytesToHex(packet->payload, packet->payload_len, hex + offset, hex_size - offset);
|
||||
offset += packet->payload_len * 2;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
#pragma once
|
||||
|
||||
#include "MeshCore.h"
|
||||
#include <ArduinoJson.h>
|
||||
#include <Mesh.h>
|
||||
|
||||
/**
|
||||
* @brief Utility class for building MQTT JSON messages
|
||||
*
|
||||
* This class handles the formatting of mesh packets and device status
|
||||
* into JSON messages for MQTT publishing according to the MeshCore
|
||||
* packet capture specification.
|
||||
*/
|
||||
class MQTTMessageBuilder {
|
||||
private:
|
||||
static const int JSON_BUFFER_SIZE = 1024;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Build status message JSON
|
||||
*
|
||||
* @param origin Device name
|
||||
* @param origin_id Device public key (hex string)
|
||||
* @param model Device model
|
||||
* @param firmware_version Firmware version
|
||||
* @param radio Radio information
|
||||
* @param client_version Client version
|
||||
* @param status Connection status ("online" or "offline")
|
||||
* @param timestamp ISO 8601 timestamp
|
||||
* @param buffer Output buffer for JSON string
|
||||
* @param buffer_size Size of output buffer
|
||||
* @return Length of JSON string, or 0 on error
|
||||
*/
|
||||
static int buildStatusMessage(
|
||||
const char* origin,
|
||||
const char* origin_id,
|
||||
const char* model,
|
||||
const char* firmware_version,
|
||||
const char* radio,
|
||||
const char* client_version,
|
||||
const char* status,
|
||||
const char* timestamp,
|
||||
char* buffer,
|
||||
size_t buffer_size
|
||||
);
|
||||
|
||||
/**
|
||||
* Build packet message JSON
|
||||
*
|
||||
* @param origin Device name
|
||||
* @param origin_id Device public key (hex string)
|
||||
* @param timestamp ISO 8601 timestamp
|
||||
* @param direction Packet direction ("rx" or "tx")
|
||||
* @param time Time in HH:MM:SS format
|
||||
* @param date Date in DD/MM/YYYY format
|
||||
* @param len Total packet length
|
||||
* @param packet_type Packet type code
|
||||
* @param route Routing type
|
||||
* @param payload_len Payload length
|
||||
* @param raw Raw packet data (hex string)
|
||||
* @param snr Signal-to-noise ratio
|
||||
* @param rssi Received signal strength
|
||||
* @param hash Packet hash
|
||||
* @param path Routing path (for direct packets)
|
||||
* @param buffer Output buffer for JSON string
|
||||
* @param buffer_size Size of output buffer
|
||||
* @return Length of JSON string, or 0 on error
|
||||
*/
|
||||
static int buildPacketMessage(
|
||||
const char* origin,
|
||||
const char* origin_id,
|
||||
const char* timestamp,
|
||||
const char* direction,
|
||||
const char* time,
|
||||
const char* date,
|
||||
int len,
|
||||
int packet_type,
|
||||
const char* route,
|
||||
int payload_len,
|
||||
const char* raw,
|
||||
float snr,
|
||||
int rssi,
|
||||
const char* hash,
|
||||
const char* path,
|
||||
char* buffer,
|
||||
size_t buffer_size
|
||||
);
|
||||
|
||||
/**
|
||||
* Build raw message JSON
|
||||
*
|
||||
* @param origin Device name
|
||||
* @param origin_id Device public key (hex string)
|
||||
* @param timestamp ISO 8601 timestamp
|
||||
* @param raw Raw packet data (hex string)
|
||||
* @param buffer Output buffer for JSON string
|
||||
* @param buffer_size Size of output buffer
|
||||
* @return Length of JSON string, or 0 on error
|
||||
*/
|
||||
static int buildRawMessage(
|
||||
const char* origin,
|
||||
const char* origin_id,
|
||||
const char* timestamp,
|
||||
const char* raw,
|
||||
char* buffer,
|
||||
size_t buffer_size
|
||||
);
|
||||
|
||||
/**
|
||||
* Convert packet to JSON message
|
||||
*
|
||||
* @param packet Mesh packet
|
||||
* @param is_tx Whether packet was transmitted (true) or received (false)
|
||||
* @param origin Device name
|
||||
* @param origin_id Device public key (hex string)
|
||||
* @param buffer Output buffer for JSON string
|
||||
* @param buffer_size Size of output buffer
|
||||
* @return Length of JSON string, or 0 on error
|
||||
*/
|
||||
static int buildPacketJSON(
|
||||
mesh::Packet* packet,
|
||||
bool is_tx,
|
||||
const char* origin,
|
||||
const char* origin_id,
|
||||
char* buffer,
|
||||
size_t buffer_size
|
||||
);
|
||||
|
||||
/**
|
||||
* Convert packet to raw JSON message
|
||||
*
|
||||
* @param packet Mesh packet
|
||||
* @param origin Device name
|
||||
* @param origin_id Device public key (hex string)
|
||||
* @param buffer Output buffer for JSON string
|
||||
* @param buffer_size Size of output buffer
|
||||
* @return Length of JSON string, or 0 on error
|
||||
*/
|
||||
static int buildRawJSON(
|
||||
mesh::Packet* packet,
|
||||
const char* origin,
|
||||
const char* origin_id,
|
||||
char* buffer,
|
||||
size_t buffer_size
|
||||
);
|
||||
|
||||
private:
|
||||
/**
|
||||
* Convert packet type to string
|
||||
*/
|
||||
static const char* getPacketTypeString(int packet_type);
|
||||
|
||||
/**
|
||||
* Convert route type to string
|
||||
*/
|
||||
static const char* getRouteTypeString(int route_type);
|
||||
|
||||
/**
|
||||
* Format timestamp to ISO 8601 format
|
||||
*/
|
||||
static void formatTimestamp(unsigned long timestamp, char* buffer, size_t buffer_size);
|
||||
|
||||
/**
|
||||
* Format time to HH:MM:SS format
|
||||
*/
|
||||
static void formatTime(unsigned long timestamp, char* buffer, size_t buffer_size);
|
||||
|
||||
/**
|
||||
* Format date to DD/MM/YYYY format
|
||||
*/
|
||||
static void formatDate(unsigned long timestamp, char* buffer, size_t buffer_size);
|
||||
|
||||
/**
|
||||
* Convert bytes to hex string (uppercase)
|
||||
*/
|
||||
static void bytesToHex(const uint8_t* data, size_t len, char* hex, size_t hex_size);
|
||||
|
||||
/**
|
||||
* Convert packet to hex string
|
||||
*/
|
||||
static void packetToHex(mesh::Packet* packet, char* hex, size_t hex_size);
|
||||
};
|
||||
@@ -0,0 +1,511 @@
|
||||
#include "MQTTBridge.h"
|
||||
#include "../MQTTMessageBuilder.h"
|
||||
#include <NTPClient.h>
|
||||
#include <WiFiUdp.h>
|
||||
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
|
||||
MQTTBridge::MQTTBridge(NodePrefs *prefs, mesh::PacketManager *mgr, mesh::RTCClock *rtc)
|
||||
: BridgeBase(prefs, mgr, rtc), _mqtt_client(nullptr), _wifi_client(nullptr),
|
||||
_active_brokers(0), _queue_head(0), _queue_tail(0), _queue_count(0),
|
||||
_last_status_publish(0), _status_interval(300000), // 5 minutes default
|
||||
_ntp_client(_ntp_udp, "pool.ntp.org", 0, 60000), _last_ntp_sync(0), _ntp_synced(false) {
|
||||
|
||||
// Initialize default values
|
||||
strncpy(_origin, "MeshCore-Repeater", sizeof(_origin) - 1);
|
||||
strncpy(_iata, "SEA", sizeof(_iata) - 1);
|
||||
strncpy(_device_id, "DEVICE_ID_PLACEHOLDER", sizeof(_device_id) - 1);
|
||||
strncpy(_firmware_version, "v1.9.1", sizeof(_firmware_version) - 1);
|
||||
strncpy(_board_model, "unknown", sizeof(_board_model) - 1);
|
||||
_status_enabled = true;
|
||||
_packets_enabled = true;
|
||||
_raw_enabled = false;
|
||||
|
||||
// Initialize packet queue
|
||||
memset(_packet_queue, 0, sizeof(_packet_queue));
|
||||
|
||||
// Set default broker configuration
|
||||
setBrokerDefaults();
|
||||
}
|
||||
|
||||
void MQTTBridge::begin() {
|
||||
MQTT_DEBUG_PRINTLN("Initializing MQTT Bridge...");
|
||||
|
||||
// Update origin and IATA from preferences
|
||||
strncpy(_origin, _prefs->mqtt_origin, sizeof(_origin) - 1);
|
||||
_origin[sizeof(_origin) - 1] = '\0';
|
||||
strncpy(_iata, _prefs->mqtt_iata, sizeof(_iata) - 1);
|
||||
_iata[sizeof(_iata) - 1] = '\0';
|
||||
|
||||
// Strip quotes from origin if present
|
||||
MQTT_DEBUG_PRINTLN("Origin before stripping: '%s'", _origin);
|
||||
size_t origin_len = strlen(_origin);
|
||||
if (origin_len > 0) {
|
||||
if (_origin[0] == '"') {
|
||||
memmove(_origin, _origin + 1, origin_len);
|
||||
origin_len--;
|
||||
}
|
||||
if (origin_len > 0 && _origin[origin_len-1] == '"') {
|
||||
_origin[origin_len-1] = '\0';
|
||||
}
|
||||
}
|
||||
MQTT_DEBUG_PRINTLN("Origin after stripping: '%s'", _origin);
|
||||
|
||||
// Strip quotes from IATA if present
|
||||
MQTT_DEBUG_PRINTLN("IATA before stripping: '%s'", _iata);
|
||||
size_t iata_len = strlen(_iata);
|
||||
if (iata_len > 0) {
|
||||
if (_iata[0] == '"') {
|
||||
memmove(_iata, _iata + 1, iata_len);
|
||||
iata_len--;
|
||||
}
|
||||
if (iata_len > 0 && _iata[iata_len-1] == '"') {
|
||||
_iata[iata_len-1] = '\0';
|
||||
}
|
||||
}
|
||||
MQTT_DEBUG_PRINTLN("IATA after stripping: '%s'", _iata);
|
||||
|
||||
// Update enabled flags from preferences
|
||||
_status_enabled = _prefs->mqtt_status_enabled;
|
||||
_packets_enabled = _prefs->mqtt_packets_enabled;
|
||||
_raw_enabled = _prefs->mqtt_raw_enabled;
|
||||
_status_interval = _prefs->mqtt_status_interval;
|
||||
|
||||
MQTT_DEBUG_PRINTLN("Origin: %s, IATA: %s", _origin, _iata);
|
||||
MQTT_DEBUG_PRINTLN("Device ID: %s", _device_id);
|
||||
MQTT_DEBUG_PRINTLN("WiFi SSID: %s", _prefs->wifi_ssid);
|
||||
|
||||
// Initialize WiFi
|
||||
MQTT_DEBUG_PRINTLN("Starting WiFi...");
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(_prefs->wifi_ssid, _prefs->wifi_password);
|
||||
|
||||
// Wait for WiFi connection
|
||||
int attempts = 0;
|
||||
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
|
||||
delay(500);
|
||||
MQTT_DEBUG_PRINT(".");
|
||||
attempts++;
|
||||
}
|
||||
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
MQTT_DEBUG_PRINTLN("WiFi connected! IP: %s", WiFi.localIP().toString().c_str());
|
||||
|
||||
// Sync time with NTP
|
||||
syncTimeWithNTP();
|
||||
} else {
|
||||
MQTT_DEBUG_PRINTLN("WiFi connection failed!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Initialize WiFi client
|
||||
_wifi_client = new WiFiClient();
|
||||
_mqtt_client = new PubSubClient(*_wifi_client);
|
||||
|
||||
// Set default broker (meshtastic.pugetmesh.org)
|
||||
setBroker(0, "meshtastic.pugetmesh.org", 1883, "meshdev", "large4cats", true);
|
||||
|
||||
// Connect to brokers
|
||||
connectToBrokers();
|
||||
|
||||
_initialized = true;
|
||||
MQTT_DEBUG_PRINTLN("MQTT Bridge initialized");
|
||||
}
|
||||
|
||||
void MQTTBridge::end() {
|
||||
MQTT_DEBUG_PRINTLN("Stopping MQTT Bridge...");
|
||||
|
||||
// Disconnect from all brokers
|
||||
for (int i = 0; i < MAX_MQTT_BROKERS_COUNT; i++) {
|
||||
if (_brokers[i].enabled && _brokers[i].connected) {
|
||||
_mqtt_client->disconnect();
|
||||
_brokers[i].connected = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Clear packet queue
|
||||
_queue_count = 0;
|
||||
_queue_head = 0;
|
||||
_queue_tail = 0;
|
||||
|
||||
// Clean up resources
|
||||
if (_mqtt_client) {
|
||||
delete _mqtt_client;
|
||||
_mqtt_client = nullptr;
|
||||
}
|
||||
if (_wifi_client) {
|
||||
delete _wifi_client;
|
||||
_wifi_client = nullptr;
|
||||
}
|
||||
|
||||
_initialized = false;
|
||||
MQTT_DEBUG_PRINTLN("MQTT Bridge stopped");
|
||||
}
|
||||
|
||||
void MQTTBridge::loop() {
|
||||
if (!_initialized) return;
|
||||
|
||||
// Maintain broker connections
|
||||
connectToBrokers();
|
||||
|
||||
// Process packet queue
|
||||
processPacketQueue();
|
||||
|
||||
// Periodic NTP sync (every hour)
|
||||
if (WiFi.status() == WL_CONNECTED && millis() - _last_ntp_sync > 3600000) {
|
||||
syncTimeWithNTP();
|
||||
}
|
||||
|
||||
// Publish status updates
|
||||
if (_status_enabled && millis() - _last_status_publish > _status_interval) {
|
||||
publishStatus();
|
||||
_last_status_publish = millis();
|
||||
}
|
||||
}
|
||||
|
||||
void MQTTBridge::onPacketReceived(mesh::Packet *packet) {
|
||||
if (!_initialized || !_packets_enabled) return;
|
||||
|
||||
// Queue packet for transmission
|
||||
queuePacket(packet, false);
|
||||
}
|
||||
|
||||
void MQTTBridge::sendPacket(mesh::Packet *packet) {
|
||||
if (!_initialized || !_packets_enabled) return;
|
||||
|
||||
// Queue packet for transmission
|
||||
queuePacket(packet, true);
|
||||
}
|
||||
|
||||
void MQTTBridge::connectToBrokers() {
|
||||
for (int i = 0; i < MAX_MQTT_BROKERS_COUNT; i++) {
|
||||
if (!_brokers[i].enabled) continue;
|
||||
|
||||
// Check if we need to attempt connection
|
||||
if (!_brokers[i].connected &&
|
||||
millis() - _brokers[i].last_attempt > _brokers[i].reconnect_interval) {
|
||||
|
||||
MQTT_DEBUG_PRINTLN("Connecting to broker %d: %s:%d", i, _brokers[i].host, _brokers[i].port);
|
||||
|
||||
// Set broker for this connection
|
||||
_mqtt_client->setServer(_brokers[i].host, _brokers[i].port);
|
||||
|
||||
// Generate unique client ID
|
||||
char client_id[32];
|
||||
snprintf(client_id, sizeof(client_id), "%s_%d_%lu", _origin, i, millis());
|
||||
|
||||
// Attempt connection
|
||||
bool connected = _mqtt_client->connect(
|
||||
client_id,
|
||||
_brokers[i].username,
|
||||
_brokers[i].password
|
||||
);
|
||||
|
||||
if (connected) {
|
||||
_brokers[i].connected = true;
|
||||
_brokers[i].reconnect_interval = 5000; // Reset to 5 seconds
|
||||
_active_brokers++;
|
||||
MQTT_DEBUG_PRINTLN("Connected to broker %d", i);
|
||||
|
||||
// Publish initial status
|
||||
if (_status_enabled) {
|
||||
publishStatus();
|
||||
}
|
||||
} else {
|
||||
_brokers[i].connected = false;
|
||||
_brokers[i].last_attempt = millis();
|
||||
// Exponential backoff: 5s, 10s, 20s, 30s max
|
||||
_brokers[i].reconnect_interval = min(30000UL, _brokers[i].reconnect_interval * 2);
|
||||
MQTT_DEBUG_PRINTLN("Failed to connect to broker %d", i);
|
||||
}
|
||||
}
|
||||
|
||||
// Maintain connection
|
||||
if (_brokers[i].connected) {
|
||||
_mqtt_client->loop();
|
||||
if (!_mqtt_client->connected()) {
|
||||
_brokers[i].connected = false;
|
||||
_active_brokers--;
|
||||
MQTT_DEBUG_PRINTLN("Lost connection to broker %d", i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MQTTBridge::processPacketQueue() {
|
||||
if (_queue_count == 0 || !isAnyBrokerConnected()) return;
|
||||
|
||||
// Process up to 5 packets per loop to avoid blocking
|
||||
int processed = 0;
|
||||
while (_queue_count > 0 && processed < 5) {
|
||||
QueuedPacket& queued = _packet_queue[_queue_head];
|
||||
|
||||
// Publish packet
|
||||
publishPacket(queued.packet, queued.is_tx);
|
||||
|
||||
// Publish raw if enabled
|
||||
if (_raw_enabled) {
|
||||
publishRaw(queued.packet);
|
||||
}
|
||||
|
||||
// Remove from queue
|
||||
dequeuePacket();
|
||||
processed++;
|
||||
}
|
||||
}
|
||||
|
||||
void MQTTBridge::publishStatus() {
|
||||
if (!isAnyBrokerConnected()) return;
|
||||
|
||||
char json_buffer[512];
|
||||
char origin_id[65];
|
||||
char timestamp[32];
|
||||
char radio_info[64];
|
||||
|
||||
// Get current timestamp in ISO 8601 format
|
||||
struct tm timeinfo;
|
||||
if (getLocalTime(&timeinfo)) {
|
||||
strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%S.000000", &timeinfo);
|
||||
} else {
|
||||
strcpy(timestamp, "2024-01-01T12:00:00.000000");
|
||||
}
|
||||
|
||||
// Build radio info string (freq,bw,sf,cr)
|
||||
snprintf(radio_info, sizeof(radio_info), "%.6f,%.1f,%d,%d",
|
||||
_prefs->freq, _prefs->bw, _prefs->sf, _prefs->cr);
|
||||
|
||||
// Use actual device ID
|
||||
strncpy(origin_id, _device_id, sizeof(origin_id) - 1);
|
||||
origin_id[sizeof(origin_id) - 1] = '\0';
|
||||
|
||||
// Build status message
|
||||
int len = MQTTMessageBuilder::buildStatusMessage(
|
||||
_origin,
|
||||
origin_id,
|
||||
_board_model, // model - now dynamic!
|
||||
_firmware_version, // firmware version
|
||||
radio_info,
|
||||
"custom-mqtt-bridge",
|
||||
"online",
|
||||
timestamp,
|
||||
json_buffer,
|
||||
sizeof(json_buffer)
|
||||
);
|
||||
|
||||
if (len > 0) {
|
||||
// Publish to all connected brokers
|
||||
for (int i = 0; i < MAX_MQTT_BROKERS_COUNT; i++) {
|
||||
if (_brokers[i].enabled && _brokers[i].connected) {
|
||||
char topic[128];
|
||||
snprintf(topic, sizeof(topic), "meshcore/%s/%s/status", _iata, _device_id);
|
||||
MQTT_DEBUG_PRINTLN("Publishing status to topic: %s", topic);
|
||||
|
||||
_mqtt_client->setServer(_brokers[i].host, _brokers[i].port);
|
||||
_mqtt_client->publish(topic, json_buffer, true); // retained
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MQTTBridge::publishPacket(mesh::Packet* packet, bool is_tx) {
|
||||
if (!packet) return;
|
||||
|
||||
char json_buffer[1024];
|
||||
char origin_id[65];
|
||||
|
||||
// Use actual device ID
|
||||
strncpy(origin_id, _device_id, sizeof(origin_id) - 1);
|
||||
origin_id[sizeof(origin_id) - 1] = '\0';
|
||||
|
||||
// Build packet message
|
||||
int len = MQTTMessageBuilder::buildPacketJSON(
|
||||
packet, is_tx, _origin, origin_id, json_buffer, sizeof(json_buffer)
|
||||
);
|
||||
|
||||
if (len > 0) {
|
||||
// Publish to all connected brokers
|
||||
for (int i = 0; i < MAX_MQTT_BROKERS_COUNT; i++) {
|
||||
if (_brokers[i].enabled && _brokers[i].connected) {
|
||||
char topic[128];
|
||||
snprintf(topic, sizeof(topic), "meshcore/%s/%s/packets", _iata, _device_id);
|
||||
MQTT_DEBUG_PRINTLN("Publishing packet to topic: %s", topic);
|
||||
|
||||
_mqtt_client->setServer(_brokers[i].host, _brokers[i].port);
|
||||
_mqtt_client->publish(topic, json_buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MQTTBridge::publishRaw(mesh::Packet* packet) {
|
||||
if (!packet) return;
|
||||
|
||||
char json_buffer[512];
|
||||
char origin_id[65];
|
||||
|
||||
// Use actual device ID
|
||||
strncpy(origin_id, _device_id, sizeof(origin_id) - 1);
|
||||
origin_id[sizeof(origin_id) - 1] = '\0';
|
||||
|
||||
// Build raw message
|
||||
int len = MQTTMessageBuilder::buildRawJSON(
|
||||
packet, _origin, origin_id, json_buffer, sizeof(json_buffer)
|
||||
);
|
||||
|
||||
if (len > 0) {
|
||||
// Publish to all connected brokers
|
||||
for (int i = 0; i < MAX_MQTT_BROKERS_COUNT; i++) {
|
||||
if (_brokers[i].enabled && _brokers[i].connected) {
|
||||
char topic[128];
|
||||
snprintf(topic, sizeof(topic), "meshcore/%s/%s/raw", _iata, _device_id);
|
||||
|
||||
_mqtt_client->setServer(_brokers[i].host, _brokers[i].port);
|
||||
_mqtt_client->publish(topic, json_buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MQTTBridge::queuePacket(mesh::Packet* packet, bool is_tx) {
|
||||
if (_queue_count >= MAX_QUEUE_SIZE) {
|
||||
// Queue full, remove oldest
|
||||
dequeuePacket();
|
||||
}
|
||||
|
||||
QueuedPacket& queued = _packet_queue[_queue_tail];
|
||||
queued.packet = packet;
|
||||
queued.timestamp = millis();
|
||||
queued.is_tx = is_tx;
|
||||
|
||||
_queue_tail = (_queue_tail + 1) % MAX_QUEUE_SIZE;
|
||||
_queue_count++;
|
||||
}
|
||||
|
||||
void MQTTBridge::dequeuePacket() {
|
||||
if (_queue_count == 0) return;
|
||||
|
||||
_queue_head = (_queue_head + 1) % MAX_QUEUE_SIZE;
|
||||
_queue_count--;
|
||||
}
|
||||
|
||||
bool MQTTBridge::isAnyBrokerConnected() {
|
||||
for (int i = 0; i < MAX_MQTT_BROKERS_COUNT; i++) {
|
||||
if (_brokers[i].enabled && _brokers[i].connected) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void MQTTBridge::setBrokerDefaults() {
|
||||
for (int i = 0; i < MAX_MQTT_BROKERS_COUNT; i++) {
|
||||
memset(&_brokers[i], 0, sizeof(MQTTBroker));
|
||||
_brokers[i].port = 1883;
|
||||
_brokers[i].qos = 0;
|
||||
_brokers[i].enabled = false;
|
||||
_brokers[i].connected = false;
|
||||
_brokers[i].reconnect_interval = 5000; // 5 seconds
|
||||
}
|
||||
}
|
||||
|
||||
void MQTTBridge::setBroker(int broker_index, const char* host, uint16_t port,
|
||||
const char* username, const char* password, bool enabled) {
|
||||
if (broker_index < 0 || broker_index >= MAX_MQTT_BROKERS_COUNT) return;
|
||||
|
||||
MQTTBroker& broker = _brokers[broker_index];
|
||||
strncpy(broker.host, host, sizeof(broker.host) - 1);
|
||||
broker.port = port;
|
||||
strncpy(broker.username, username, sizeof(broker.username) - 1);
|
||||
strncpy(broker.password, password, sizeof(broker.password) - 1);
|
||||
broker.enabled = enabled;
|
||||
broker.connected = false;
|
||||
broker.reconnect_interval = 5000;
|
||||
}
|
||||
|
||||
void MQTTBridge::setOrigin(const char* origin) {
|
||||
strncpy(_origin, origin, sizeof(_origin) - 1);
|
||||
_origin[sizeof(_origin) - 1] = '\0';
|
||||
}
|
||||
|
||||
void MQTTBridge::setIATA(const char* iata) {
|
||||
strncpy(_iata, iata, sizeof(_iata) - 1);
|
||||
_iata[sizeof(_iata) - 1] = '\0';
|
||||
}
|
||||
|
||||
void MQTTBridge::setDeviceID(const char* device_id) {
|
||||
strncpy(_device_id, device_id, sizeof(_device_id) - 1);
|
||||
_device_id[sizeof(_device_id) - 1] = '\0';
|
||||
MQTT_DEBUG_PRINTLN("Device ID set to: %s", _device_id);
|
||||
}
|
||||
|
||||
void MQTTBridge::setFirmwareVersion(const char* firmware_version) {
|
||||
strncpy(_firmware_version, firmware_version, sizeof(_firmware_version) - 1);
|
||||
_firmware_version[sizeof(_firmware_version) - 1] = '\0';
|
||||
}
|
||||
|
||||
void MQTTBridge::setBoardModel(const char* board_model) {
|
||||
strncpy(_board_model, board_model, sizeof(_board_model) - 1);
|
||||
_board_model[sizeof(_board_model) - 1] = '\0';
|
||||
}
|
||||
|
||||
void MQTTBridge::setMessageTypes(bool status, bool packets, bool raw) {
|
||||
_status_enabled = status;
|
||||
_packets_enabled = packets;
|
||||
_raw_enabled = raw;
|
||||
}
|
||||
|
||||
int MQTTBridge::getConnectedBrokers() const {
|
||||
int count = 0;
|
||||
for (int i = 0; i < MAX_MQTT_BROKERS_COUNT; i++) {
|
||||
if (_brokers[i].enabled && _brokers[i].connected) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int MQTTBridge::getQueueSize() const {
|
||||
return _queue_count;
|
||||
}
|
||||
|
||||
void MQTTBridge::syncTimeWithNTP() {
|
||||
if (!WiFi.isConnected()) {
|
||||
MQTT_DEBUG_PRINTLN("Cannot sync time - WiFi not connected");
|
||||
return;
|
||||
}
|
||||
|
||||
MQTT_DEBUG_PRINTLN("Syncing time with NTP...");
|
||||
|
||||
// Begin NTP client
|
||||
_ntp_client.begin();
|
||||
|
||||
// Force update (blocking call with timeout)
|
||||
if (_ntp_client.forceUpdate()) {
|
||||
unsigned long epochTime = _ntp_client.getEpochTime();
|
||||
|
||||
// Update the device's RTC clock
|
||||
if (_rtc) {
|
||||
_rtc->setCurrentTime(epochTime);
|
||||
_ntp_synced = true;
|
||||
_last_ntp_sync = millis();
|
||||
|
||||
MQTT_DEBUG_PRINTLN("Time synced: %lu", epochTime);
|
||||
|
||||
// Show current time
|
||||
struct tm* timeinfo = localtime((time_t*)&epochTime);
|
||||
if (timeinfo) {
|
||||
MQTT_DEBUG_PRINTLN("Current time: %04d-%02d-%02d %02d:%02d:%02d UTC",
|
||||
timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday,
|
||||
timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
|
||||
}
|
||||
} else {
|
||||
MQTT_DEBUG_PRINTLN("No RTC clock available for time sync");
|
||||
}
|
||||
} else {
|
||||
MQTT_DEBUG_PRINTLN("NTP sync failed");
|
||||
}
|
||||
|
||||
_ntp_client.end();
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,234 @@
|
||||
#pragma once
|
||||
|
||||
#include "MeshCore.h"
|
||||
#include "helpers/bridges/BridgeBase.h"
|
||||
#include <PubSubClient.h>
|
||||
#include <WiFi.h>
|
||||
#include <NTPClient.h>
|
||||
#include <WiFiUdp.h>
|
||||
|
||||
#if defined(MQTT_DEBUG) && defined(ARDUINO)
|
||||
#include <Arduino.h>
|
||||
#define MQTT_DEBUG_PRINT(F, ...) Serial.printf("MQTT: " F, ##__VA_ARGS__)
|
||||
#define MQTT_DEBUG_PRINTLN(F, ...) Serial.printf("MQTT: " F "\n", ##__VA_ARGS__)
|
||||
#else
|
||||
#define MQTT_DEBUG_PRINT(...) {}
|
||||
#define MQTT_DEBUG_PRINTLN(...) {}
|
||||
#endif
|
||||
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
|
||||
/**
|
||||
* @brief Bridge implementation using MQTT protocol for packet transport
|
||||
*
|
||||
* This bridge enables mesh packet transport over MQTT, allowing repeaters to
|
||||
* uplink packet data to multiple MQTT brokers for monitoring and analysis.
|
||||
*
|
||||
* Features:
|
||||
* - Multiple MQTT broker support
|
||||
* - Automatic reconnection with exponential backoff
|
||||
* - JSON message formatting for status, packets, and raw data
|
||||
* - Configurable topics and QoS levels
|
||||
* - Packet queuing during connection issues
|
||||
*
|
||||
* Message Types:
|
||||
* - Status: Device connection status and metadata
|
||||
* - Packets: Full packet data with RF characteristics
|
||||
* - Raw: Minimal raw packet data for map integration
|
||||
*
|
||||
* Configuration:
|
||||
* - Define WITH_MQTT_BRIDGE to enable this bridge
|
||||
* - Configure brokers via CLI commands
|
||||
* - Set origin name and IATA code for topic structure
|
||||
*/
|
||||
class MQTTBridge : public BridgeBase {
|
||||
private:
|
||||
PubSubClient* _mqtt_client;
|
||||
WiFiClient* _wifi_client;
|
||||
|
||||
// MQTT broker configuration
|
||||
struct MQTTBroker {
|
||||
char host[64];
|
||||
uint16_t port;
|
||||
char username[32];
|
||||
char password[64];
|
||||
char client_id[32];
|
||||
uint8_t qos;
|
||||
bool enabled;
|
||||
bool connected;
|
||||
unsigned long last_attempt;
|
||||
unsigned long reconnect_interval;
|
||||
};
|
||||
|
||||
static const int MAX_MQTT_BROKERS_COUNT = 3;
|
||||
MQTTBroker _brokers[MAX_MQTT_BROKERS_COUNT];
|
||||
int _active_brokers;
|
||||
|
||||
// Message configuration
|
||||
char _origin[32];
|
||||
char _iata[8];
|
||||
char _device_id[65]; // Device public key (hex string)
|
||||
char _firmware_version[64]; // Firmware version string
|
||||
char _board_model[64]; // Board model string
|
||||
bool _status_enabled;
|
||||
bool _packets_enabled;
|
||||
bool _raw_enabled;
|
||||
unsigned long _last_status_publish;
|
||||
unsigned long _status_interval;
|
||||
|
||||
// Packet queue for offline scenarios
|
||||
struct QueuedPacket {
|
||||
mesh::Packet* packet;
|
||||
unsigned long timestamp;
|
||||
bool is_tx;
|
||||
};
|
||||
|
||||
static const int MAX_QUEUE_SIZE = 50;
|
||||
QueuedPacket _packet_queue[MAX_QUEUE_SIZE];
|
||||
int _queue_head;
|
||||
int _queue_tail;
|
||||
int _queue_count;
|
||||
|
||||
// NTP time sync
|
||||
WiFiUDP _ntp_udp;
|
||||
NTPClient _ntp_client;
|
||||
unsigned long _last_ntp_sync;
|
||||
bool _ntp_synced;
|
||||
|
||||
// Internal methods
|
||||
void connectToBrokers();
|
||||
void processPacketQueue();
|
||||
void publishStatus();
|
||||
void publishPacket(mesh::Packet* packet, bool is_tx);
|
||||
void publishRaw(mesh::Packet* packet);
|
||||
void queuePacket(mesh::Packet* packet, bool is_tx);
|
||||
void dequeuePacket();
|
||||
bool isAnyBrokerConnected();
|
||||
void setBrokerDefaults();
|
||||
void syncTimeWithNTP();
|
||||
|
||||
public:
|
||||
/**
|
||||
* Constructs an MQTTBridge instance
|
||||
*
|
||||
* @param prefs Node preferences for configuration settings
|
||||
* @param mgr PacketManager for allocating and queuing packets
|
||||
* @param rtc RTCClock for timestamping debug messages
|
||||
*/
|
||||
MQTTBridge(NodePrefs *prefs, mesh::PacketManager *mgr, mesh::RTCClock *rtc);
|
||||
|
||||
/**
|
||||
* Initializes the MQTT bridge
|
||||
*
|
||||
* - Sets up default broker configuration
|
||||
* - Initializes WiFi client
|
||||
* - Prepares MQTT clients for each broker
|
||||
*/
|
||||
void begin() override;
|
||||
|
||||
/**
|
||||
* Stops the MQTT bridge
|
||||
*
|
||||
* - Disconnects from all brokers
|
||||
* - Clears packet queue
|
||||
* - Releases resources
|
||||
*/
|
||||
void end() override;
|
||||
|
||||
/**
|
||||
* Main loop handler
|
||||
* - Maintains broker connections
|
||||
* - Processes packet queue
|
||||
* - Publishes status updates
|
||||
*/
|
||||
void loop() override;
|
||||
|
||||
/**
|
||||
* Called when a packet is received via mesh
|
||||
* Queues the packet for MQTT publishing if enabled
|
||||
*
|
||||
* @param packet The received mesh packet
|
||||
*/
|
||||
void onPacketReceived(mesh::Packet *packet) override;
|
||||
|
||||
/**
|
||||
* Called when a packet needs to be transmitted via MQTT
|
||||
* Publishes the packet to all connected brokers
|
||||
*
|
||||
* @param packet The mesh packet to transmit
|
||||
*/
|
||||
void sendPacket(mesh::Packet *packet) override;
|
||||
|
||||
/**
|
||||
* Configure MQTT broker settings
|
||||
*
|
||||
* @param broker_index Broker index (0-2)
|
||||
* @param host Broker hostname
|
||||
* @param port Broker port
|
||||
* @param username MQTT username
|
||||
* @param password MQTT password
|
||||
* @param enabled Whether broker is enabled
|
||||
*/
|
||||
void setBroker(int broker_index, const char* host, uint16_t port,
|
||||
const char* username, const char* password, bool enabled);
|
||||
|
||||
/**
|
||||
* Set device origin name for MQTT topics
|
||||
*
|
||||
* @param origin Device name
|
||||
*/
|
||||
void setOrigin(const char* origin);
|
||||
|
||||
/**
|
||||
* Set IATA code for MQTT topics
|
||||
*
|
||||
* @param iata Airport code
|
||||
*/
|
||||
void setIATA(const char* iata);
|
||||
|
||||
/**
|
||||
* Set device public key for MQTT topics
|
||||
*
|
||||
* @param device_id Device public key (hex string)
|
||||
*/
|
||||
void setDeviceID(const char* device_id);
|
||||
|
||||
/**
|
||||
* Set firmware version for status messages
|
||||
*
|
||||
* @param firmware_version Firmware version string
|
||||
*/
|
||||
void setFirmwareVersion(const char* firmware_version);
|
||||
|
||||
/**
|
||||
* Set board model for status messages
|
||||
*
|
||||
* @param board_model Board model string
|
||||
*/
|
||||
void setBoardModel(const char* board_model);
|
||||
|
||||
/**
|
||||
* Enable/disable message types
|
||||
*
|
||||
* @param status Enable status messages
|
||||
* @param packets Enable packet messages
|
||||
* @param raw Enable raw messages
|
||||
*/
|
||||
void setMessageTypes(bool status, bool packets, bool raw);
|
||||
|
||||
/**
|
||||
* Get connection status for all brokers
|
||||
*
|
||||
* @return Number of connected brokers
|
||||
*/
|
||||
int getConnectedBrokers() const;
|
||||
|
||||
/**
|
||||
* Get queue status
|
||||
*
|
||||
* @return Number of queued packets
|
||||
*/
|
||||
int getQueueSize() const;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -102,6 +102,37 @@ lib_deps =
|
||||
${Heltec_lora32_v3.lib_deps}
|
||||
${esp32_ota.lib_deps}
|
||||
|
||||
[env:Heltec_v3_repeater_bridge_mqtt]
|
||||
extends = Heltec_lora32_v3
|
||||
upload_port = /dev/cu.usbserial-2
|
||||
build_flags =
|
||||
${Heltec_lora32_v3.build_flags}
|
||||
-D DISPLAY_CLASS=SSD1306Display
|
||||
-D ADVERT_NAME='"MQTT Bridge"'
|
||||
-D ADVERT_LAT=0.0
|
||||
-D ADVERT_LON=0.0
|
||||
-D ADMIN_PASSWORD='"password"'
|
||||
-D MAX_NEIGHBOURS=50
|
||||
-D WITH_MQTT_BRIDGE=1
|
||||
-D MAX_MQTT_BROKERS=3
|
||||
-D MQTT_MAX_PACKET_SIZE=1024
|
||||
-D MQTT_DEBUG=1
|
||||
-D MESH_PACKET_LOGGING=1
|
||||
-D MESH_DEBUG=1
|
||||
# -D WIFI_SSID='"ssid"'
|
||||
# -D WIFI_PWD='"password"'
|
||||
build_src_filter = ${Heltec_lora32_v3.build_src_filter}
|
||||
+<helpers/bridges/MQTTBridge.cpp>
|
||||
+<helpers/MQTTMessageBuilder.cpp>
|
||||
+<helpers/ui/SSD1306Display.cpp>
|
||||
+<../examples/simple_repeater>
|
||||
lib_deps =
|
||||
${Heltec_lora32_v3.lib_deps}
|
||||
${esp32_ota.lib_deps}
|
||||
knolleary/PubSubClient @ ^2.8
|
||||
bblanchon/ArduinoJson @ 6.17.3
|
||||
arduino-libraries/NTPClient
|
||||
|
||||
[env:Heltec_v3_room_server]
|
||||
extends = Heltec_lora32_v3
|
||||
build_flags =
|
||||
@@ -187,8 +218,8 @@ build_flags =
|
||||
-D MAX_GROUP_CHANNELS=40
|
||||
-D DISPLAY_CLASS=SSD1306Display
|
||||
-D WIFI_DEBUG_LOGGING=1
|
||||
-D WIFI_SSID='"myssid"'
|
||||
-D WIFI_PWD='"mypwd"'
|
||||
-D WIFI_SSID='"REMOVED_SSID"'
|
||||
-D WIFI_PWD='"REMOVED_PASSWORD"'
|
||||
; -D MESH_PACKET_LOGGING=1
|
||||
; -D MESH_DEBUG=1
|
||||
build_src_filter = ${Heltec_lora32_v3.build_src_filter}
|
||||
|
||||
Reference in new issue
Block a user