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

Changed MQTT message template to fixed JSON payload, added temperature and humidity

This commit is contained in:
Pieter De Mil 2021-07-08 12:53:11 +02:00
parent ce58e673fc
commit d4dd03590e
2 changed files with 81 additions and 11 deletions

View File

@ -40,7 +40,6 @@ WiFiClient wificlient;
WiFiClientSecure wificlientsecure;
DHT dht(DHTPIN, DHTTYPE);
const int pin_portalbutton = 35;
const int pin_demobutton = 0;
const int pin_backlight = 4;
@ -56,10 +55,17 @@ int co2_warning;
int co2_critical;
int co2_blink;
String mqtt_topic;
bool mqtt_template_enabled;
String mqtt_template;
bool mqtt_user_pass_enabled;
String mqtt_username;
String mqtt_password;
bool mqtt_temp_hum_enabled;
String mqtt_topic_temperature;
bool mqtt_template_temp_hum_enabled;
String mqtt_template_temp;
String mqtt_topic_humidity;
String mqtt_template_hum;
bool add_units;
bool wifi_enabled;
bool mqtt_enabled;
@ -469,8 +475,15 @@ void setup() {
max_failures = WiFiSettings.integer("operame_max_failures", 0, 1000, 10, T.config_max_failures);
mqtt_topic = WiFiSettings.string("operame_mqtt_topic", WiFiSettings.hostname, T.config_mqtt_topic);
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_template_enabled = WiFiSettings.checkbox("operame_mqtt_template_enabled", false, T.config_mqtt_template_enabled);
// mqtt_template = WiFiSettings.string("operame_mqtt_template", "{} PPM", T.config_mqtt_template);
// WiFiSettings.info(T.config_template_info);
mqtt_temp_hum_enabled = WiFiSettings.checkbox("operame_mqtt_temp_hum", false, T.config_mqtt_temp_hum);
mqtt_topic_temperature = WiFiSettings.string("operame_mqtt_topic_temperature", WiFiSettings.hostname + "/t", T.config_mqtt_topic_temperature);
mqtt_topic_humidity = WiFiSettings.string("operame_mqtt_topic_humidity", WiFiSettings.hostname + "/h", T.config_mqtt_topic_humidity);
// mqtt_template_temp_hum_enabled = WiFiSettings.checkbox("operame_mqtt_template_temp_hum_enabled", false, T.config_mqtt_template_temp_hum_enabled);
// mqtt_template_temp = WiFiSettings.string("operame_mqtt_template_temp", "{} C", T.config_mqtt_template_temp);
// mqtt_template_hum = WiFiSettings.string("operame_mqtt_template_hum", "{} %R.H.", T.config_mqtt_template_hum);
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);
@ -587,9 +600,45 @@ void loop() {
every(mqtt_interval) {
if (co2 <= 0) break;
connect_mqtt();
String message = mqtt_template;
message.replace("{}", String(co2));
//CO2
String message;
const size_t capacity = JSON_OBJECT_SIZE(3);
DynamicJsonDocument doc(capacity);
doc["variable"] = "CO2";
doc["value"] = co2;
doc["unit"] = "ppm";
serializeJson(doc, message);
retain(mqtt_topic, message);
//temperature
if(isnan(t)) {
Serial.println("Failed to read from DHT sensor, so no MQTT publish");
}
else {
String message;
const size_t capacity = JSON_OBJECT_SIZE(3);
DynamicJsonDocument doc(capacity);
doc["variable"] = "temperature";
doc["value"] = t;
doc["unit"] = "C";
serializeJson(doc, message);
retain(mqtt_topic, message);
}
//humidity
if(isnan(h)) {
Serial.println("Failed to read from DHT sensor, so no MQTT publish");
}
else {
String message;
const size_t capacity = JSON_OBJECT_SIZE(3);
DynamicJsonDocument doc(capacity);
doc["variable"] = "humidity";
doc["value"] = h;
doc["unit"] = "%R.H.";
serializeJson(doc, message);
retain(mqtt_topic, message);
}
}
}

View File

@ -23,9 +23,16 @@ struct Texts {
*config_mqtt_port,
*config_max_failures,
*config_mqtt_topic,
*config_mqtt_topic_temperature,
*config_mqtt_topic_humidity,
*config_mqtt_interval,
*config_mqtt_template_enabled,
*config_mqtt_template,
*config_template_info,
*config_mqtt_temp_hum,
*config_mqtt_template_temp_hum_enabled,
*config_mqtt_template_temp,
*config_mqtt_template_hum,
*config_mqtt_user_pass,
*config_mqtt_username,
*config_mqtt_password,
@ -80,12 +87,19 @@ bool select(Texts& T, String language) {
T.config_mqtt_server = "Broker"; // probably should not be translated
T.config_mqtt_port = "Broker TCP port";
T.config_max_failures = "Number of failed connections before automatic restart";
T.config_mqtt_topic = "Topic"; // probably should not be translated
T.config_mqtt_topic = "Topic for CO2"; // probably should not be translated
T.config_mqtt_topic_temperature = "Topic for temperature (if available)"; // probably should not be translated
T.config_mqtt_topic_humidity = "Topic for humidity (if available)"; // probably should not be translated
T.config_mqtt_interval = "Publication interval [s]";
T.config_mqtt_template = "Message template";
T.config_mqtt_template_enabled = "Enable this if you want to use your own MQTT message template for CO2";
T.config_mqtt_template = "Message template for CO2";
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_mqtt_temp_hum = "Enable MQTT publish of temperature and humidity (if available)";
T.config_mqtt_template_temp_hum_enabled = "Enable this if you want to use your own MQTT message template for temperature/humidity";
T.config_mqtt_template_temp = "Message template for temperature";
T.config_mqtt_template_hum = "Message template for humidity";
T.config_rest = "Publish measurements via the HTTPS protocol";
T.config_rest_domain = "Domain";
T.config_rest_uri = "URI";
@ -157,12 +171,19 @@ bool select(Texts& T, String language) {
T.config_mqtt_server = "Broker"; // zo heet dat in MQTT
T.config_mqtt_port = "Broker TCP-poort";
T.config_max_failures = "Aantal verbindingsfouten voor automatische herstart";
T.config_mqtt_topic = "Topic"; // zo heet dat in MQTT
T.config_mqtt_topic = "Topic voor CO2"; // zo heet dat in MQTT
T.config_mqtt_topic_temperature = "Topic voor temperatuur (indien beschikbaar)"; // probably should not be translated
T.config_mqtt_topic_humidity = "Topic voor rel. luchtvochtigheid (indien beschikbaar)"; // probably should not be translated
T.config_mqtt_interval = "Publicatie-interval [s]";
T.config_mqtt_template = "Berichtsjabloon";
T.config_mqtt_template_enabled = "Activeer dit indien je jouw eigen MQTT berichtsjabloon voor CO2 wil gebruiken";
T.config_mqtt_template = "Berichtsjabloon voor CO2";
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_mqtt_temp_hum = "Activeer MQTT publicatie van temperatuur en rel. luchtvochtigheid (indien beschikbaar)";
T.config_mqtt_template_temp_hum_enabled = "Activeer dit indien je jouw eigen MQTT berichtsjabloon voor temperatuur/rel. luchtvochtigheid wil gebruiken";
T.config_mqtt_template_temp = "Berichtsjabloon voor temperatuur";
T.config_mqtt_template_hum = "Berichtsjabloon voor rel. luchtvochtigheid";
T.config_rest = "Metingen via het HTTPS-protocol versturen";
T.config_rest_domain = "Domein";
T.config_rest_uri = "URI";