mirror of
https://github.com/richonguzman/LoRa_APRS_iGate.git
synced 2026-03-28 16:52:33 +01:00
test MQTT 1
This commit is contained in:
@@ -33,6 +33,7 @@ lib_deps =
|
||||
ayushsharma82/ElegantOTA @ 3.1.5
|
||||
bblanchon/ArduinoJson @ 6.21.3
|
||||
jgromes/RadioLib @ 7.1.0
|
||||
knolleary/PubSubClient @ 2.8
|
||||
mathieucarbou/AsyncTCP @ 3.2.5
|
||||
mathieucarbou/ESPAsyncWebServer @ 3.2.3
|
||||
mikalhart/TinyGPSPlus @ 1.0.3
|
||||
|
||||
@@ -90,7 +90,14 @@
|
||||
"remoteManagement": {
|
||||
"managers": "",
|
||||
"rfOnly": true
|
||||
},
|
||||
},
|
||||
"mqtt": {
|
||||
"active": false,
|
||||
"host": "",
|
||||
"login": "",
|
||||
"password": "",
|
||||
"port": 1883
|
||||
},
|
||||
"other": {
|
||||
"rememberStationTime": 30,
|
||||
"backupDigiMode": false,
|
||||
|
||||
@@ -1502,6 +1502,124 @@
|
||||
</div>
|
||||
<hr>
|
||||
|
||||
<div class="row my-5 d-flex align-items-top">
|
||||
<div class="col-lg-3 col-sm-12">
|
||||
<h5>
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="20"
|
||||
height="20"
|
||||
fill="currentColor"
|
||||
class="bi bi-database-fill"
|
||||
viewBox="0 0 16 16"
|
||||
>
|
||||
<path
|
||||
d="M3.904 1.777C4.978 1.289 6.427 1 8 1s3.022.289 4.096.777C13.125 2.245 14 2.993 14 4s-.875 1.755-1.904 2.223C11.022 6.711 9.573 7 8 7s-3.022-.289-4.096-.777C2.875 5.755 2 5.007 2 4s.875-1.755 1.904-2.223"
|
||||
/>
|
||||
<path
|
||||
d="M2 6.161V7c0 1.007.875 1.755 1.904 2.223C4.978 9.71 6.427 10 8 10s3.022-.289 4.096-.777C13.125 8.755 14 8.007 14 7v-.839c-.457.432-1.004.751-1.49.972C11.278 7.693 9.682 8 8 8s-3.278-.307-4.51-.867c-.486-.22-1.033-.54-1.49-.972"
|
||||
/>
|
||||
<path
|
||||
d="M2 9.161V10c0 1.007.875 1.755 1.904 2.223C4.978 12.711 6.427 13 8 13s3.022-.289 4.096-.777C13.125 11.755 14 11.007 14 10v-.839c-.457.432-1.004.751-1.49.972-1.232.56-2.828.867-4.51.867s-3.278-.307-4.51-.867c-.486-.22-1.033-.54-1.49-.972"
|
||||
/>
|
||||
<path
|
||||
d="M2 12.161V13c0 1.007.875 1.755 1.904 2.223C4.978 15.711 6.427 16 8 16s3.022-.289 4.096-.777C13.125 14.755 14 14.007 14 13v-.839c-.457.432-1.004.751-1.49.972-1.232.56-2.828.867-4.51.867s-3.278-.307-4.51-.867c-.486-.22-1.033-.54-1.49-.972"
|
||||
/>
|
||||
</svg>
|
||||
MQTT
|
||||
</h5>
|
||||
<small>Set your MQTT server</small>
|
||||
</div>
|
||||
<div class="col-lg-9 col-sm-12">
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
<div class="form-check form-switch">
|
||||
<input
|
||||
type="checkbox"
|
||||
name="mqtt.active"
|
||||
id="mqtt.active"
|
||||
class="form-check-input"
|
||||
/>
|
||||
<label
|
||||
for="mqtt.active"
|
||||
class="form-label"
|
||||
>Enable</label
|
||||
>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-12">
|
||||
<label for="mqtt.server" class="form-label"
|
||||
>Server</label
|
||||
>
|
||||
<input
|
||||
type="text"
|
||||
name="mqtt.server"
|
||||
id="mqtt.server"
|
||||
class="form-control"
|
||||
/>
|
||||
</div>
|
||||
<div class="col-12">
|
||||
<label for="mqtt.topic" class="form-label"
|
||||
>Topic</label
|
||||
>
|
||||
<input
|
||||
type="text"
|
||||
name="mqtt.topic"
|
||||
id="mqtt.topic"
|
||||
class="form-control"
|
||||
placeholder="aprs-igate"
|
||||
/>
|
||||
<div class="form-text">
|
||||
Default is <strong>aprs-igate</strong>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-12">
|
||||
<label for="mqtt.username" class="form-label"
|
||||
>Username</label
|
||||
>
|
||||
<input
|
||||
type="text"
|
||||
name="mqtt.username"
|
||||
id="mqtt.username"
|
||||
class="form-control"
|
||||
/>
|
||||
</div>
|
||||
<div class="col-12 mt-3">
|
||||
<label for="mqtt.password" class="form-label"
|
||||
>Password</label
|
||||
>
|
||||
<input
|
||||
type="password"
|
||||
name="mqtt.password"
|
||||
id="mqtt.password"
|
||||
class="form-control"
|
||||
/>
|
||||
</div>
|
||||
<div class="col-12 mt-3">
|
||||
<label
|
||||
for="mqtt.port"
|
||||
class="form-label"
|
||||
>Port</label
|
||||
>
|
||||
<input
|
||||
type="number"
|
||||
name="mqtt.port"
|
||||
id="mqtt.port"
|
||||
class="form-control"
|
||||
placeholder="1883"
|
||||
required=""
|
||||
step="1"
|
||||
min="0"
|
||||
/>
|
||||
<div class="form-text">
|
||||
Default is <strong>1883</strong>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<hr>
|
||||
|
||||
<div class="row my-5 d-flex align-items-top">
|
||||
<div class="col-lg-3 col-sm-12">
|
||||
<h5>
|
||||
|
||||
@@ -224,6 +224,14 @@ function loadSettings(settings) {
|
||||
// NTP
|
||||
document.getElementById("ntp.gmtCorrection").value = settings.ntp.gmtCorrection;
|
||||
|
||||
// MQTT
|
||||
document.getElementById("mqtt.active").checked = settings.mqtt.active;
|
||||
document.getElementById("mqtt.server").value = settings.mqtt.server;
|
||||
document.getElementById("mqtt.topic").value = settings.mqtt.topic;
|
||||
document.getElementById("mqtt.username").value = settings.mqtt.username;
|
||||
document.getElementById("mqtt.password").value = settings.mqtt.password;
|
||||
document.getElementById("mqtt.port").value = settings.mqtt.port;
|
||||
|
||||
// Experimental
|
||||
document.getElementById("other.backupDigiMode").checked = settings.other.backupDigiMode;
|
||||
|
||||
@@ -351,6 +359,32 @@ WebadminCheckbox.addEventListener("change", function () {
|
||||
WebadminPassword.disabled = !this.checked;
|
||||
});
|
||||
|
||||
const MqttCheckbox = document.querySelector(
|
||||
'input[name="mqtt.active"]'
|
||||
);
|
||||
const MqttServer = document.querySelector(
|
||||
'input[name="mqtt.server"]'
|
||||
);
|
||||
const MqttTopic = document.querySelector(
|
||||
'input[name="mqtt.topic"]'
|
||||
);
|
||||
const MqttUsername = document.querySelector(
|
||||
'input[name="mqtt.username"]'
|
||||
);
|
||||
const MqttPassword = document.querySelector(
|
||||
'input[name="mqtt.password"]'
|
||||
);
|
||||
const MqttPort = document.querySelector(
|
||||
'input[name="mqtt.port"]'
|
||||
);
|
||||
MqttCheckbox.addEventListener("change", function () {
|
||||
MqttServer.disabled = !this.checked;
|
||||
MqttTopic.disabled = !this.checked;
|
||||
MqttUsername.disabled = !this.checked;
|
||||
MqttPassword.disabled = !this.checked;
|
||||
MqttPort.disabled = !this.checked;
|
||||
});
|
||||
|
||||
document.querySelector(".new button").addEventListener("click", function () {
|
||||
const networksContainer = document.querySelector(".list-networks");
|
||||
|
||||
|
||||
@@ -149,6 +149,16 @@ public:
|
||||
bool rfOnly;
|
||||
};
|
||||
|
||||
class MQTT {
|
||||
public:
|
||||
bool active;
|
||||
String server;
|
||||
String topic;
|
||||
String username;
|
||||
String password;
|
||||
int port;
|
||||
};
|
||||
|
||||
class Configuration {
|
||||
public:
|
||||
String callsign;
|
||||
@@ -173,6 +183,7 @@ public:
|
||||
WEBADMIN webadmin;
|
||||
NTP ntp;
|
||||
REMOTE_MANAGEMENT remoteManagement;
|
||||
MQTT mqtt;
|
||||
|
||||
void init();
|
||||
void writeFile();
|
||||
|
||||
34
include/mqtt_utils.h
Normal file
34
include/mqtt_utils.h
Normal file
@@ -0,0 +1,34 @@
|
||||
/* Copyright (C) 2025 Ricardo Guzman - CA2RXU
|
||||
*
|
||||
* This file is part of LoRa APRS iGate.
|
||||
*
|
||||
* LoRa APRS iGate is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LoRa APRS iGate is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LoRa APRS iGate. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef MQTT_UTILS_H_
|
||||
#define MQTT_UTILS_H_
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
|
||||
namespace MQTT_Utils {
|
||||
|
||||
void sendToMqtt(const String& packet);
|
||||
void connect();
|
||||
void loop();
|
||||
void setup();
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -51,6 +51,7 @@ ___________________________________________________________________*/
|
||||
#include "syslog_utils.h"
|
||||
#include "power_utils.h"
|
||||
#include "sleep_utils.h"
|
||||
#include "mqtt_utils.h"
|
||||
#include "lora_utils.h"
|
||||
#include "wifi_utils.h"
|
||||
#include "digi_utils.h"
|
||||
@@ -66,9 +67,10 @@ ___________________________________________________________________*/
|
||||
#endif
|
||||
|
||||
|
||||
String versionDate = "2025-08-26";
|
||||
String versionDate = "2025-08-27";
|
||||
Configuration Config;
|
||||
WiFiClient espClient;
|
||||
WiFiClient aprsIsClient;
|
||||
WiFiClient mqttClient;
|
||||
#ifdef HAS_GPS
|
||||
HardwareSerial gpsSerial(1);
|
||||
TinyGPSPlus gps;
|
||||
@@ -118,6 +120,7 @@ void setup() {
|
||||
WX_Utils::setup();
|
||||
WEB_Utils::setup();
|
||||
TNC_Utils::setup();
|
||||
MQTT_Utils::setup();
|
||||
#ifdef HAS_A7670
|
||||
A7670_Utils::setup();
|
||||
#endif
|
||||
@@ -166,11 +169,13 @@ void loop() {
|
||||
if (Config.aprs_is.active && !modemLoggedToAPRSIS) A7670_Utils::APRS_IS_connect();
|
||||
#else
|
||||
WIFI_Utils::checkWiFi();
|
||||
if (Config.aprs_is.active && (WiFi.status() == WL_CONNECTED) && !espClient.connected()) APRS_IS_Utils::connect();
|
||||
if (Config.aprs_is.active && (WiFi.status() == WL_CONNECTED) && !aprsIsClient.connected()) APRS_IS_Utils::connect();
|
||||
if (Config.mqtt.active && (WiFi.status() == WL_CONNECTED) && !mqttClient.connected()) MQTT_Utils::connect();
|
||||
#endif
|
||||
|
||||
NTP_Utils::update();
|
||||
TNC_Utils::loop();
|
||||
MQTT_Utils::loop();
|
||||
|
||||
Utils::checkDisplayInterval();
|
||||
Utils::checkBeaconInterval();
|
||||
@@ -183,7 +188,7 @@ void loop() {
|
||||
}
|
||||
|
||||
if (packet != "") {
|
||||
if (Config.aprs_is.active) { // If APRSIS enabled
|
||||
if (Config.aprs_is.active) { // If APRSIS enabled
|
||||
APRS_IS_Utils::processLoRaPacket(packet); // Send received packet to APRSIS
|
||||
}
|
||||
|
||||
@@ -192,17 +197,12 @@ void loop() {
|
||||
DIGI_Utils::processLoRaPacket(packet); // Send received packet to Digi
|
||||
}
|
||||
|
||||
if (Config.tnc.enableServer) { // If TNC server enabled
|
||||
TNC_Utils::sendToClients(packet); // Send received packet to TNC KISS
|
||||
}
|
||||
if (Config.tnc.enableSerial) { // If Serial KISS enabled
|
||||
TNC_Utils::sendToSerial(packet); // Send received packet to Serial KISS
|
||||
}
|
||||
if (Config.tnc.enableServer) TNC_Utils::sendToClients(packet); // Send received packet to TNC KISS
|
||||
if (Config.tnc.enableSerial) TNC_Utils::sendToSerial(packet); // Send received packet to Serial KISS
|
||||
if (Config.mqtt.active) MQTT_Utils::sendToMqtt(packet); // Send received packet to MQTT
|
||||
}
|
||||
|
||||
if (Config.aprs_is.active) {
|
||||
APRS_IS_Utils::listenAPRSIS(); // listen received packet from APRSIS
|
||||
}
|
||||
if (Config.aprs_is.active) APRS_IS_Utils::listenAPRSIS(); // listen received packet from APRSIS
|
||||
|
||||
STATION_Utils::processOutputPacketBuffer();
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@
|
||||
|
||||
|
||||
extern Configuration Config;
|
||||
extern WiFiClient espClient;
|
||||
extern WiFiClient aprsIsClient;
|
||||
extern uint32_t lastScreenOn;
|
||||
extern String firstLine;
|
||||
extern String secondLine;
|
||||
@@ -53,17 +53,17 @@ bool passcodeValid = false;
|
||||
namespace APRS_IS_Utils {
|
||||
|
||||
void upload(const String& line) {
|
||||
espClient.print(line + "\r\n");
|
||||
aprsIsClient.print(line + "\r\n");
|
||||
}
|
||||
|
||||
void connect() {
|
||||
Serial.print("Connecting to APRS-IS ... ");
|
||||
uint8_t count = 0;
|
||||
while (!espClient.connect(Config.aprs_is.server.c_str(), Config.aprs_is.port) && count < 20) {
|
||||
while (!aprsIsClient.connect(Config.aprs_is.server.c_str(), Config.aprs_is.port) && count < 20) {
|
||||
Serial.println("Didn't connect with server...");
|
||||
delay(1000);
|
||||
espClient.stop();
|
||||
espClient.flush();
|
||||
aprsIsClient.stop();
|
||||
aprsIsClient.flush();
|
||||
Serial.println("Run client.stop");
|
||||
Serial.println("Trying to connect with Server: " + String(Config.aprs_is.server) + " AprsServerPort: " + String(Config.aprs_is.port));
|
||||
count++;
|
||||
@@ -110,7 +110,7 @@ namespace APRS_IS_Utils {
|
||||
aprsisState = "--";
|
||||
}
|
||||
#else
|
||||
if (espClient.connected()) {
|
||||
if (aprsIsClient.connected()) {
|
||||
aprsisState = "OK";
|
||||
} else {
|
||||
aprsisState = "--";
|
||||
@@ -192,7 +192,7 @@ namespace APRS_IS_Utils {
|
||||
}
|
||||
|
||||
void processLoRaPacket(const String& packet) {
|
||||
if (passcodeValid && (espClient.connected() || modemLoggedToAPRSIS)) {
|
||||
if (passcodeValid && (aprsIsClient.connected() || modemLoggedToAPRSIS)) {
|
||||
if (packet.indexOf("NOGATE") == -1 && packet.indexOf("RFONLY") == -1) {
|
||||
int firstColonIndex = packet.indexOf(":");
|
||||
if (firstColonIndex > 5 && firstColonIndex < (packet.length() - 1) && packet[firstColonIndex + 1] != '}' && packet.indexOf("TCPIP") == -1) {
|
||||
@@ -371,9 +371,9 @@ namespace APRS_IS_Utils {
|
||||
#ifdef HAS_A7670
|
||||
A7670_Utils::listenAPRSIS();
|
||||
#else
|
||||
if (espClient.connected()) {
|
||||
if (espClient.available()) {
|
||||
String aprsisPacket = espClient.readStringUntil('\r');
|
||||
if (aprsIsClient.connected()) {
|
||||
if (aprsIsClient.available()) {
|
||||
String aprsisPacket = aprsIsClient.readStringUntil('\r');
|
||||
aprsisPacket.trim(); // Serial.println(aprsisPacket);
|
||||
processAPRSISPacket(aprsisPacket);
|
||||
lastRxTime = millis();
|
||||
@@ -383,7 +383,7 @@ namespace APRS_IS_Utils {
|
||||
}
|
||||
|
||||
void firstConnection() {
|
||||
if (Config.aprs_is.active && (WiFi.status() == WL_CONNECTED) && !espClient.connected()) {
|
||||
if (Config.aprs_is.active && (WiFi.status() == WL_CONNECTED) && !aprsIsClient.connected()) {
|
||||
connect();
|
||||
while (!passcodeValid) {
|
||||
listenAPRSIS();
|
||||
|
||||
@@ -136,6 +136,13 @@ void Configuration::writeFile() {
|
||||
data["remoteManagement"]["managers"] = remoteManagement.managers;
|
||||
data["remoteManagement"]["rfOnly"] = remoteManagement.rfOnly;
|
||||
|
||||
data["mqtt"]["active"] = mqtt.active;
|
||||
data["mqtt"]["server"] = mqtt.server;
|
||||
data["mqtt"]["topic"] = mqtt.topic;
|
||||
data["mqtt"]["username"] = mqtt.username;
|
||||
data["mqtt"]["password"] = mqtt.password;
|
||||
data["mqtt"]["port"] = mqtt.port;
|
||||
|
||||
serializeJson(data, configFile);
|
||||
|
||||
configFile.close();
|
||||
@@ -264,6 +271,13 @@ bool Configuration::readFile() {
|
||||
remoteManagement.managers = data["remoteManagement"]["managers"] | "";
|
||||
remoteManagement.rfOnly = data["remoteManagement"]["rfOnly"] | true;
|
||||
|
||||
mqtt.active = data["mqtt"]["active"] | false;
|
||||
mqtt.server = data["mqtt"]["server"] | "";
|
||||
mqtt.topic = data["mqtt"]["topic"] | "aprs-igate";
|
||||
mqtt.username = data["mqtt"]["username"] | "";
|
||||
mqtt.password = data["mqtt"]["password"] | "";
|
||||
mqtt.port = data["mqtt"]["port"] | 1883;
|
||||
|
||||
if (wifiAPs.size() == 0) { // If we don't have any WiFi's from config we need to add "empty" SSID for AUTO AP
|
||||
WiFi_AP wifiap;
|
||||
wifiap.ssid = "";
|
||||
@@ -382,6 +396,13 @@ void Configuration::init() {
|
||||
remoteManagement.managers = "";
|
||||
remoteManagement.rfOnly = true;
|
||||
|
||||
mqtt.active = false;
|
||||
mqtt.server = "";
|
||||
mqtt.topic = "aprs-igate";
|
||||
mqtt.username = "";
|
||||
mqtt.password = "";
|
||||
mqtt.port = 1883;
|
||||
|
||||
Serial.println("All is Written!");
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,6 @@
|
||||
#endif
|
||||
|
||||
extern Configuration Config;
|
||||
extern WiFiClient espClient;
|
||||
extern HardwareSerial gpsSerial;
|
||||
extern TinyGPSPlus gps;
|
||||
String distance, iGateBeaconPacket, iGateLoRaBeaconPacket;
|
||||
|
||||
113
src/mqtt_utils.cpp
Normal file
113
src/mqtt_utils.cpp
Normal file
@@ -0,0 +1,113 @@
|
||||
/* Copyright (C) 2025 Ricardo Guzman - CA2RXU
|
||||
*
|
||||
* This file is part of LoRa APRS iGate.
|
||||
*
|
||||
* LoRa APRS iGate is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LoRa APRS iGate is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LoRa APRS iGate. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/*#include <WiFiClientSecure.h>
|
||||
#include <PubSubClient.h>
|
||||
#include "wifi_utils.h"
|
||||
#include "mqtt_utils.h"
|
||||
|
||||
|
||||
extern String mqttServer;
|
||||
extern int mqttPort;
|
||||
extern String mqttUser;
|
||||
extern String mqttPassword;
|
||||
extern uint8_t gatewayID;
|
||||
extern bool subscribed;
|
||||
extern String subscribeTopic;
|
||||
|
||||
|
||||
WiFiClientSecure secureClient;
|
||||
PubSubClient mqttClient(secureClient);
|
||||
|
||||
bool mqttConnected = false;*/
|
||||
|
||||
#include <WiFiClientSecure.h>
|
||||
#include <PubSubClient.h>
|
||||
#include "configuration.h"
|
||||
#include "station_utils.h"
|
||||
#include "mqtt_utils.h"
|
||||
|
||||
|
||||
extern Configuration Config;
|
||||
extern WiFiClient mqttClient;
|
||||
|
||||
PubSubClient pubSub;
|
||||
|
||||
|
||||
namespace MQTT_Utils {
|
||||
|
||||
void sendToMqtt(const String& packet) {
|
||||
if (!pubSub.connected()) {
|
||||
Serial.println("Can not send to MQTT because it is not connected");
|
||||
return;
|
||||
}
|
||||
const String cleanPacket = packet.substring(3);
|
||||
const String sender = cleanPacket.substring(0, cleanPacket.indexOf(">"));
|
||||
const String topic = String(Config.mqtt.topic + "/" + sender);
|
||||
|
||||
const bool result = pubSub.publish(topic.c_str(), cleanPacket.c_str());
|
||||
if (result) {
|
||||
Serial.print("Packet sent to MQTT topic "); Serial.println(topic);
|
||||
} else {
|
||||
Serial.println("Packet not sent to MQTT (check connection)");
|
||||
}
|
||||
}
|
||||
|
||||
void receivedFromMqtt(char* topic, byte* payload, unsigned int length) {
|
||||
Serial.print("Received from MQTT topic "); Serial.print(topic); Serial.print(": ");
|
||||
for (int i = 0; i < length; i++) {
|
||||
Serial.print((char)payload[i]);
|
||||
}
|
||||
Serial.println();
|
||||
STATION_Utils::addToOutputPacketBuffer(String(payload, length));
|
||||
}
|
||||
|
||||
void connect() {
|
||||
if (pubSub.connected()) return;
|
||||
if (Config.mqtt.server.isEmpty() || Config.mqtt.port <= 0) {
|
||||
Serial.println("Connect to MQTT server KO because no host or port given");
|
||||
return;
|
||||
}
|
||||
pubSub.setServer(Config.mqtt.server.c_str(), Config.mqtt.port);
|
||||
Serial.print("Trying to connect with MQTT Server: " + String(Config.mqtt.server) + " MqttServerPort: " + String(Config.mqtt.port));
|
||||
if (pubSub.connect(Config.callsign.c_str(), Config.mqtt.username.c_str(), Config.mqtt.password.c_str())) {
|
||||
Serial.println(" -> Connected !");
|
||||
const String subscribedTopic = Config.mqtt.topic + "/" + Config.callsign + "/#";
|
||||
if (!pubSub.subscribe(subscribedTopic.c_str())) {
|
||||
Serial.println("Subscribed to MQTT Failed");
|
||||
}
|
||||
Serial.print("Subscribed to MQTT topic : ");
|
||||
Serial.println(subscribedTopic);
|
||||
} else {
|
||||
Serial.println(" -> Not Connected (Retry in 10 secs)");
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (!Config.mqtt.active) return;
|
||||
if (!pubSub.connected()) return;
|
||||
pubSub.loop();
|
||||
}
|
||||
|
||||
void setup() {
|
||||
if (!Config.mqtt.active) return;
|
||||
pubSub.setClient(mqttClient);
|
||||
pubSub.setCallback(receivedFromMqtt);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -35,7 +35,6 @@
|
||||
|
||||
|
||||
extern Configuration Config;
|
||||
extern WiFiClient espClient;
|
||||
extern TinyGPSPlus gps;
|
||||
extern String versionDate;
|
||||
extern String firstLine;
|
||||
|
||||
@@ -240,6 +240,13 @@ namespace WEB_Utils {
|
||||
Config.remoteManagement.managers = request->getParam("remoteManagement.managers", true)->value();
|
||||
Config.remoteManagement.rfOnly = request->getParam("remoteManagement.rfOnly", true);
|
||||
|
||||
Config.mqtt.active = request->getParam("mqtt.active", true);
|
||||
Config.mqtt.server = request->getParam("mqtt.server", true)->value();
|
||||
Config.mqtt.topic = request->getParam("mqtt.topic", true)->value();
|
||||
Config.mqtt.username = request->getParam("mqtt.username", true)->value();
|
||||
Config.mqtt.password = request->getParam("mqtt.password", true)->value();
|
||||
Config.mqtt.port = request->getParam("mqtt.port", true)->value().toInt();
|
||||
|
||||
Config.writeFile();
|
||||
|
||||
AsyncWebServerResponse *response = request->beginResponse(302, "text/html", "");
|
||||
|
||||
Reference in New Issue
Block a user