1
0
mirror of https://github.com/revspace/operame synced 2024-10-31 21:47:30 +00:00

Allow username password in MQTT

Added 2 fields in Wifi settings (mqtt_username and mqtt_password) to allow posting to mqtt server with authentication
This commit is contained in:
bmellink 2021-01-06 15:43:06 +01:00
parent 7fb7682cfe
commit 44f018e3b0

View File

@ -6,7 +6,7 @@
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
#include <SPI.h> #include <SPI.h>
#include <TFT_eSPI.h> #include <TFT_eSPI.h>
#include <logo.h> #include "logo.h"
#include <list> #include <list>
enum Driver { AQC, MHZ }; enum Driver { AQC, MHZ };
@ -36,6 +36,8 @@ String mqtt_template;
bool add_units; bool add_units;
bool wifi_enabled; bool wifi_enabled;
bool mqtt_enabled; bool mqtt_enabled;
String mqtt_username;
String mqtt_password;
int max_failures; int max_failures;
void retain(const String& topic, const String& message) { void retain(const String& topic, const String& message) {
@ -172,9 +174,11 @@ void connect_mqtt() {
if (mqtt.connected()) return; // already/still connected if (mqtt.connected()) return; // already/still connected
static int failures = 0; static int failures = 0;
if (mqtt.connect(WiFiSettings.hostname.c_str())) { if (mqtt.connect(WiFiSettings.hostname.c_str(), mqtt_username.c_str(), mqtt_password.c_str(), false)) {
failures = 0; failures = 0;
Serial.println("MQTT connected");
} else { } else {
Serial.println("MQTT connect error");
failures++; failures++;
if (failures >= max_failures) panic("MQTT onbereikbaar"); if (failures >= max_failures) panic("MQTT onbereikbaar");
} }
@ -309,6 +313,8 @@ void setup() {
WiFiSettings.heading("MQTT"); WiFiSettings.heading("MQTT");
mqtt_enabled = WiFiSettings.checkbox("operame_mqtt", false, "Metingen via het MQTT-protocol versturen") && wifi_enabled; mqtt_enabled = WiFiSettings.checkbox("operame_mqtt", false, "Metingen via het MQTT-protocol versturen") && wifi_enabled;
String server = WiFiSettings.string("mqtt_server", 64, "", "Broker"); String server = WiFiSettings.string("mqtt_server", 64, "", "Broker");
mqtt_username = WiFiSettings.string("mqtt_username", 64, "", "Gebruikersnaam");
mqtt_password = WiFiSettings.string("mqtt_password", 64, "", "Wachtwoord");
int port = WiFiSettings.integer("mqtt_port", 0, 65535, 1883, "Broker TCP-poort"); int port = WiFiSettings.integer("mqtt_port", 0, 65535, 1883, "Broker TCP-poort");
max_failures = WiFiSettings.integer("operame_max_failures", 0, 1000, 10, "Aantal verbindingsfouten voor automatische herstart"); max_failures = WiFiSettings.integer("operame_max_failures", 0, 1000, 10, "Aantal verbindingsfouten voor automatische herstart");
mqtt_topic = WiFiSettings.string("operame_mqtt_topic", WiFiSettings.hostname, "Topic"); mqtt_topic = WiFiSettings.string("operame_mqtt_topic", WiFiSettings.hostname, "Topic");