Skip to content

Commit

Permalink
feature (MQTT): added selection for MQTT data format.
Browse files Browse the repository at this point in the history
the user can choose between JSON (default) and influx
  • Loading branch information
Mario Lukas committed Feb 16, 2021
1 parent 4d1d5c0 commit caddeef
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CO2-Ampel_Plus/CO2-Ampel_Plus.ino
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
CO2-Ampel (v2.0.1)
CO2-Ampel (v2.1.0)
Testmodus:
1. Den Switch-Taster beim Einschalten gedrueckt halten.
Expand Down
8 changes: 7 additions & 1 deletion CO2-Ampel_Plus/Config.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef CONFIG_H
#define CONFIG_H

#define VERSION "v2.0.1"
#define VERSION "v2.1.0"

#define DEBUG_LOG 0 // 1 = Enable debug log

Expand Down Expand Up @@ -47,6 +47,12 @@
#define MQTT_SENSOR_HUM 890
#define MQTT_SENSOR_LUX 891

/* MQTT Data Format.
* 0 - JSON
* 1 - InfluxDB Line Format
*/
#define MQTT_FORMAT 0

//--- Buzzer configuration ---
#define BUZZER_ENABLED true
#define BUZZER_VOLUME 255 / 128 // 1-255
Expand Down
1 change: 1 addition & 0 deletions CO2-Ampel_Plus/DeviceConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ void config_set_factory_defaults() {
TEMPERATURE_OFFSET,
MQTT_USERNAME,
MQTT_PASSWORD,
MQTT_FORMAT,
LIGHT_ENABLED,
BUZZER_ENABLED};
config_store.write(_default_config);
Expand Down
1 change: 1 addition & 0 deletions CO2-Ampel_Plus/DeviceConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ typedef struct {
float temperature_offset;
char mqtt_username[20];
char mqtt_password[20];
int mqtt_format;
bool light_enabled;
bool buzzer_enabled;
} device_config_t;
Expand Down
56 changes: 41 additions & 15 deletions CO2-Ampel_Plus/MQTTManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,56 @@ void mqtt_send_value(int co2, float temp, int hum, int lux) {
if (mqttClient.connected()) {
device_config_t cfg = config_get_values();
char mqttTopic[128];
sprintf(mqttTopic, "%s/%s", cfg.mqtt_topic, cfg.ampel_name);
char mqttMessage[512];
char tempMessage[20];
sprintf(tempMessage, "%d.%02d", (int)temp, (int)(temp*100)%100);

if (cfg.mqtt_format == 0){ // sending data in JSON Format to specified topic...
sprintf(mqttTopic, "%s/%s", cfg.mqtt_topic, cfg.ampel_name);

#if DEBUG_LOG > 0
Serial.print("TempMQTTMessage: ");
Serial.println(tempMessage);
#endif
sprintf(mqttMessage,
"{\"co2\":\"%i\",\"temp\":\" %s \",\"hum\":\"%i\",\"lux\":\"%i\"}",
co2, tempMessage, hum, lux);
if (mqttClient.publish(mqttTopic, mqttMessage)) {
Serial.println("Data publication successfull.");
sprintf(mqttMessage,
"{\"co2\":\"%i\",\"temp\":\" %s \",\"hum\":\"%i\",\"lux\":\"%i\"}",
co2, tempMessage, hum, lux);
if (mqttClient.publish(mqttTopic, mqttMessage)) {
Serial.println("Data publication successfull.");
#if DEBUG_LOG > 0
Serial.print("Message: ");
Serial.println(mqttMessage);
Serial.print("Topic: ");
Serial.print(mqttTopic);
Serial.print("Message: ");
Serial.println(mqttMessage);
Serial.print("Topic: ");
Serial.print(mqttTopic);
#endif
} else {
Serial.println(
"Data publication failed, either connection lost or message too "
"large.");
};
} else {
Serial.println(
"Data publication failed, either connection lost or message too "
"large.");
};

} else { // sending data in influxdb format

sprintf(mqttMessage, "co2ampel,name=%s co2=%i,temp=%s,hum=%i,lux=%i", cfg.ampel_name, co2, tempMessage, hum, lux );
if (mqttClient.publish(cfg.mqtt_topic, mqttMessage)) {
Serial.println("Data publication successfull.");

#if DEBUG_LOG > 0
Serial.print("Message: ");
Serial.println(mqttMessage);
Serial.print("Topic: ");
Serial.print(mqttTopic);
#endif

} else {
Serial.println(
"Data publication failed, either connection lost or message too "
"large.");
};

}


} else {
Serial.println("Data publication failed, client is not connected. Trying to reconnect.");
mqtt_connect();
Expand Down
15 changes: 15 additions & 0 deletions CO2-Ampel_Plus/NetworkManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ void wifi_handle_client() {
}
}

if ((requestParser.getField("format").length() > 0)) {
cfg.mqtt_format = requestParser.getField("format").toInt();
}

/**
* Reboot if required.
*/
Expand Down Expand Up @@ -382,6 +386,17 @@ void wifi_handle_client() {
client.print("<option value=\"false\" selected>Disabled</option>");
};
client.print("</select>");
client.print("<br><br>");
client.print("<label for=format>Format</label>");
client.print("<select id=format name=format size=2>");
if (cfg.mqtt_format == 0) {
client.print("<option value=\"0\" selected>JSON</option>");
client.print("<option value=\"1\">Influx</option>");
} else {
client.print("<option value=\"0\">JSON</option>");
client.print("<option value=\"1\" selected>Influx</option>");
};
client.print("</select>");

// client.print("<div class=\"btnbox\"><button
// onclick=\"window.location.href='/selftest'\"
Expand Down

0 comments on commit caddeef

Please sign in to comment.