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
45
operame.ino
45
operame.ino
@ -1,4 +1,5 @@
|
||||
#include <WiFi.h>
|
||||
#include <WiFiClient.h>
|
||||
#include <WiFiClientSecure.h>
|
||||
#include <MQTT.h>
|
||||
#include <SPIFFS.h>
|
||||
@ -35,7 +36,8 @@ HardwareSerial hwserial1(1);
|
||||
TFT_eSPI display;
|
||||
TFT_eSprite sprite(&display);
|
||||
MHZ19 mhz;
|
||||
WiFiClientSecure wificlient;
|
||||
WiFiClient wificlient;
|
||||
WiFiClientSecure wificlientsecure;
|
||||
DHT dht(DHTPIN, DHTTYPE);
|
||||
|
||||
|
||||
@ -55,6 +57,9 @@ int co2_critical;
|
||||
int co2_blink;
|
||||
String mqtt_topic;
|
||||
String mqtt_template;
|
||||
bool mqtt_user_pass_enabled;
|
||||
String mqtt_username;
|
||||
String mqtt_password;
|
||||
bool add_units;
|
||||
bool wifi_enabled;
|
||||
bool mqtt_enabled;
|
||||
@ -270,11 +275,22 @@ void connect_mqtt() {
|
||||
if (mqtt.connected()) return; // already/still connected
|
||||
|
||||
static int failures = 0;
|
||||
if (mqtt.connect(WiFiSettings.hostname.c_str())) {
|
||||
failures = 0;
|
||||
} else {
|
||||
failures++;
|
||||
if (failures >= max_failures) panic(T.error_mqtt);
|
||||
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())) {
|
||||
failures = 0;
|
||||
} else {
|
||||
failures++;
|
||||
if (failures >= max_failures) panic(T.error_mqtt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -383,7 +399,7 @@ void set_zero() {
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
Serial.println("Operame start");
|
||||
Serial.println("Operame / www.controlCO2.space start");
|
||||
|
||||
digitalWrite(pin_backlight, HIGH);
|
||||
display.init();
|
||||
@ -455,6 +471,9 @@ void setup() {
|
||||
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);
|
||||
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");
|
||||
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 (rest_cert_enabled) wificlient.setCACert(rest_cert.c_str());
|
||||
if (rest_cert_enabled) wificlientsecure.setCACert(rest_cert.c_str());
|
||||
|
||||
if (ota_enabled) setup_ota();
|
||||
}
|
||||
@ -530,7 +549,7 @@ void loop() {
|
||||
static float h;
|
||||
static float t;
|
||||
|
||||
every(5000) {
|
||||
every(60000) {
|
||||
// Read CO2, humidity and temperature
|
||||
co2 = get_co2();
|
||||
h = dht.readHumidity();
|
||||
@ -575,8 +594,8 @@ void loop() {
|
||||
}
|
||||
|
||||
if (rest_enabled) {
|
||||
while(wificlient.available()){
|
||||
String line = wificlient.readStringUntil('\r');
|
||||
while(wificlientsecure.available()){
|
||||
String line = wificlientsecure.readStringUntil('\r');
|
||||
Serial.print(line);
|
||||
}
|
||||
|
||||
@ -588,8 +607,8 @@ void loop() {
|
||||
message["co2"] = co2;
|
||||
message["id"] = rest_resource_id.c_str();
|
||||
|
||||
if (wificlient.connected() || wificlient.connect(&rest_domain[0], rest_port)) {
|
||||
post_rest_message(message, wificlient);
|
||||
if (wificlientsecure.connected() || wificlientsecure.connect(&rest_domain[0], rest_port)) {
|
||||
post_rest_message(message, wificlientsecure);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -26,6 +26,9 @@ struct Texts {
|
||||
*config_mqtt_interval,
|
||||
*config_mqtt_template,
|
||||
*config_template_info,
|
||||
*config_mqtt_user_pass,
|
||||
*config_mqtt_username,
|
||||
*config_mqtt_password,
|
||||
*config_rest,
|
||||
*config_rest_domain,
|
||||
*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_interval = "Publication interval [s]";
|
||||
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_domain = "Domain";
|
||||
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_interval = "Publicatie-interval [s]";
|
||||
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_domain = "Domein";
|
||||
T.config_rest_uri = "URI";
|
||||
|
Loading…
Reference in New Issue
Block a user