mirror of
https://github.com/revspace/operame
synced 2024-10-31 21:47:30 +00:00
Merge pull request #4 from controlco2/feature/mqtt_username_password
Fix MQTT
This commit is contained in:
commit
ce58e673fc
35
operame.ino
35
operame.ino
@ -1,4 +1,5 @@
|
|||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
|
#include <WiFiClient.h>
|
||||||
#include <WiFiClientSecure.h>
|
#include <WiFiClientSecure.h>
|
||||||
#include <MQTT.h>
|
#include <MQTT.h>
|
||||||
#include <SPIFFS.h>
|
#include <SPIFFS.h>
|
||||||
@ -35,7 +36,8 @@ HardwareSerial hwserial1(1);
|
|||||||
TFT_eSPI display;
|
TFT_eSPI display;
|
||||||
TFT_eSprite sprite(&display);
|
TFT_eSprite sprite(&display);
|
||||||
MHZ19 mhz;
|
MHZ19 mhz;
|
||||||
WiFiClientSecure wificlient;
|
WiFiClient wificlient;
|
||||||
|
WiFiClientSecure wificlientsecure;
|
||||||
DHT dht(DHTPIN, DHTTYPE);
|
DHT dht(DHTPIN, DHTTYPE);
|
||||||
|
|
||||||
|
|
||||||
@ -55,6 +57,9 @@ int co2_critical;
|
|||||||
int co2_blink;
|
int co2_blink;
|
||||||
String mqtt_topic;
|
String mqtt_topic;
|
||||||
String mqtt_template;
|
String mqtt_template;
|
||||||
|
bool mqtt_user_pass_enabled;
|
||||||
|
String mqtt_username;
|
||||||
|
String mqtt_password;
|
||||||
bool add_units;
|
bool add_units;
|
||||||
bool wifi_enabled;
|
bool wifi_enabled;
|
||||||
bool mqtt_enabled;
|
bool mqtt_enabled;
|
||||||
@ -270,12 +275,23 @@ 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_user_pass_enabled ) {
|
||||||
|
if (mqtt.connect(WiFiSettings.hostname.c_str(), mqtt_username.c_str(), mqtt_password.c_str())) {
|
||||||
|
failures = 0;
|
||||||
|
display_big("MQTT connect");
|
||||||
|
} else {
|
||||||
|
failures++;
|
||||||
|
if (failures >= max_failures) panic(T.error_mqtt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
if (mqtt.connect(WiFiSettings.hostname.c_str())) {
|
if (mqtt.connect(WiFiSettings.hostname.c_str())) {
|
||||||
failures = 0;
|
failures = 0;
|
||||||
} else {
|
} else {
|
||||||
failures++;
|
failures++;
|
||||||
if (failures >= max_failures) panic(T.error_mqtt);
|
if (failures >= max_failures) panic(T.error_mqtt);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void flush(Stream& s, int limit = 20) {
|
void flush(Stream& s, int limit = 20) {
|
||||||
@ -383,7 +399,7 @@ void set_zero() {
|
|||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
Serial.println("Operame start");
|
Serial.println("Operame / www.controlCO2.space start");
|
||||||
|
|
||||||
digitalWrite(pin_backlight, HIGH);
|
digitalWrite(pin_backlight, HIGH);
|
||||||
display.init();
|
display.init();
|
||||||
@ -455,6 +471,9 @@ void setup() {
|
|||||||
mqtt_interval = 1000UL * WiFiSettings.integer("operame_mqtt_interval", 10, 3600, 60, T.config_mqtt_interval);
|
mqtt_interval = 1000UL * WiFiSettings.integer("operame_mqtt_interval", 10, 3600, 60, T.config_mqtt_interval);
|
||||||
mqtt_template = WiFiSettings.string("operame_mqtt_template", "{} PPM", T.config_mqtt_template);
|
mqtt_template = WiFiSettings.string("operame_mqtt_template", "{} PPM", T.config_mqtt_template);
|
||||||
WiFiSettings.info(T.config_template_info);
|
WiFiSettings.info(T.config_template_info);
|
||||||
|
mqtt_user_pass_enabled = WiFiSettings.checkbox("operame_mqtt_user_pass", false, T.config_mqtt_user_pass);
|
||||||
|
mqtt_username = WiFiSettings.string("operame_mqtt_username", 64, "", T.config_mqtt_username);
|
||||||
|
mqtt_password = WiFiSettings.string("operame_mqtt_password", 64, "", T.config_mqtt_password);
|
||||||
|
|
||||||
WiFiSettings.heading("REST");
|
WiFiSettings.heading("REST");
|
||||||
rest_enabled = WiFiSettings.checkbox("operame_rest", false, T.config_rest) && wifi_enabled;
|
rest_enabled = WiFiSettings.checkbox("operame_rest", false, T.config_rest) && wifi_enabled;
|
||||||
@ -506,7 +525,7 @@ void setup() {
|
|||||||
|
|
||||||
if (mqtt_enabled) mqtt.begin(server.c_str(), port, wificlient);
|
if (mqtt_enabled) mqtt.begin(server.c_str(), port, wificlient);
|
||||||
|
|
||||||
if (rest_cert_enabled) wificlient.setCACert(rest_cert.c_str());
|
if (rest_cert_enabled) wificlientsecure.setCACert(rest_cert.c_str());
|
||||||
|
|
||||||
if (ota_enabled) setup_ota();
|
if (ota_enabled) setup_ota();
|
||||||
}
|
}
|
||||||
@ -530,7 +549,7 @@ void loop() {
|
|||||||
static float h;
|
static float h;
|
||||||
static float t;
|
static float t;
|
||||||
|
|
||||||
every(5000) {
|
every(60000) {
|
||||||
// Read CO2, humidity and temperature
|
// Read CO2, humidity and temperature
|
||||||
co2 = get_co2();
|
co2 = get_co2();
|
||||||
h = dht.readHumidity();
|
h = dht.readHumidity();
|
||||||
@ -575,8 +594,8 @@ void loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (rest_enabled) {
|
if (rest_enabled) {
|
||||||
while(wificlient.available()){
|
while(wificlientsecure.available()){
|
||||||
String line = wificlient.readStringUntil('\r');
|
String line = wificlientsecure.readStringUntil('\r');
|
||||||
Serial.print(line);
|
Serial.print(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -588,8 +607,8 @@ void loop() {
|
|||||||
message["co2"] = co2;
|
message["co2"] = co2;
|
||||||
message["id"] = rest_resource_id.c_str();
|
message["id"] = rest_resource_id.c_str();
|
||||||
|
|
||||||
if (wificlient.connected() || wificlient.connect(&rest_domain[0], rest_port)) {
|
if (wificlientsecure.connected() || wificlientsecure.connect(&rest_domain[0], rest_port)) {
|
||||||
post_rest_message(message, wificlient);
|
post_rest_message(message, wificlientsecure);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,9 @@ struct Texts {
|
|||||||
*config_mqtt_interval,
|
*config_mqtt_interval,
|
||||||
*config_mqtt_template,
|
*config_mqtt_template,
|
||||||
*config_template_info,
|
*config_template_info,
|
||||||
|
*config_mqtt_user_pass,
|
||||||
|
*config_mqtt_username,
|
||||||
|
*config_mqtt_password,
|
||||||
*config_rest,
|
*config_rest,
|
||||||
*config_rest_domain,
|
*config_rest_domain,
|
||||||
*config_rest_uri,
|
*config_rest_uri,
|
||||||
@ -80,6 +83,9 @@ bool select(Texts& T, String language) {
|
|||||||
T.config_mqtt_topic = "Topic"; // probably should not be translated
|
T.config_mqtt_topic = "Topic"; // probably should not be translated
|
||||||
T.config_mqtt_interval = "Publication interval [s]";
|
T.config_mqtt_interval = "Publication interval [s]";
|
||||||
T.config_mqtt_template = "Message template";
|
T.config_mqtt_template = "Message template";
|
||||||
|
T.config_mqtt_user_pass = "Enable username and password for MQTT";
|
||||||
|
T.config_mqtt_username = "Username MQTT";
|
||||||
|
T.config_mqtt_password = "Password MQTT";
|
||||||
T.config_rest = "Publish measurements via the HTTPS protocol";
|
T.config_rest = "Publish measurements via the HTTPS protocol";
|
||||||
T.config_rest_domain = "Domain";
|
T.config_rest_domain = "Domain";
|
||||||
T.config_rest_uri = "URI";
|
T.config_rest_uri = "URI";
|
||||||
@ -154,6 +160,9 @@ bool select(Texts& T, String language) {
|
|||||||
T.config_mqtt_topic = "Topic"; // zo heet dat in MQTT
|
T.config_mqtt_topic = "Topic"; // zo heet dat in MQTT
|
||||||
T.config_mqtt_interval = "Publicatie-interval [s]";
|
T.config_mqtt_interval = "Publicatie-interval [s]";
|
||||||
T.config_mqtt_template = "Berichtsjabloon";
|
T.config_mqtt_template = "Berichtsjabloon";
|
||||||
|
T.config_mqtt_user_pass = "Activeer gebruikersnaam en paswoord voor MQTT";
|
||||||
|
T.config_mqtt_username = "Gebruikersnaam MQTT";
|
||||||
|
T.config_mqtt_password = "Paswoord MQTT";
|
||||||
T.config_rest = "Metingen via het HTTPS-protocol versturen";
|
T.config_rest = "Metingen via het HTTPS-protocol versturen";
|
||||||
T.config_rest_domain = "Domein";
|
T.config_rest_domain = "Domein";
|
||||||
T.config_rest_uri = "URI";
|
T.config_rest_uri = "URI";
|
||||||
|
Loading…
Reference in New Issue
Block a user