From 99198638ca8a5d52cf97dc99b4eeeac888391efc Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Fri, 15 Jun 2018 21:58:11 +0200 Subject: [PATCH] add mqtt --- src/main.cpp | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 0f34c49..c5457be 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,7 +14,6 @@ extern "C" { #include #include -#define MQTT_MAX_PACKET_SIZE 512 #include #include @@ -31,9 +30,10 @@ const char* server = "ingress.opensensemap.org"; //const char* ssid = "Freifunk"; //const char* password = ""; +#define MQTT_MAX_PACKET_SIZE 512 const char* mqttserver = "home.xd0.de"; const char* mqttusername = "esp-weatherstation"; -const char* mqttpassword = "password"; +const char* mqttpassword = "password4"; constexpr unsigned int postingInterval = 60000; //Uploadintervall in Millisekunden constexpr unsigned int dhcp_interval = 60*60*1000; @@ -147,6 +147,8 @@ void ICACHE_FLASH_ATTR sendValues() { sd.pressure /= 100; bmp.getTemperature(&(sd.temp2)); + sd.voltage = ESP.getVcc()/1024.0; + int sds_error = sds.read(&(sd.p25), &(sd.p10)); if(!sds_error) { // Serial.println("SDS011 updated."); @@ -156,7 +158,6 @@ void ICACHE_FLASH_ATTR sendValues() { // sds.sleep(); - sd.cpm = 0; sd.radioactivity = -1; sd.cpm = geigeraverage.getAverage()*10; float constexpr own_cpm = OWN_BACKGROUND_CPS * 60; @@ -172,43 +173,53 @@ void ICACHE_FLASH_ATTR sendValues() { DynamicJsonBuffer jsonBuffer; JsonArray& array = jsonBuffer.createArray(); + DynamicJsonBuffer jsonBuffer2; + JsonObject& root = jsonBuffer.createObject(); JsonObject& temperatureObject = array.createNestedObject(); temperatureObject["sensor"] = SENSOR1_ID; temperatureObject["value"] = sd.temperature; + root["temperature"] = sd.temperature; JsonObject& humidityObject = array.createNestedObject(); humidityObject["sensor"] = SENSOR2_ID; humidityObject["value"] = sd.humidity; + root["humidity"] = sd.humidity; JsonObject& pressureObject = array.createNestedObject(); pressureObject["sensor"] = SENSOR3_ID; pressureObject["value"] = sd.pressure; + root["pressure"] = sd.pressure; if (!sds_error) { JsonObject& pm10Object = array.createNestedObject(); pm10Object["sensor"] = SENSOR4_ID; pm10Object["value"] = sd.p10; + root["pm10"] = sd.p10; JsonObject& pm25Object = array.createNestedObject(); pm25Object["sensor"] = SENSOR5_ID; pm25Object["value"] = sd.p25; - + root["pm2.5"] = sd.p25; } if (sd.cpm > 0) { JsonObject& cpmObject = array.createNestedObject(); cpmObject["sensor"] = SENSOR6_ID; cpmObject["value"] = sd.radioactivity; + root["cpm"] = sd.cpm; + root["radioactivity"] = sd.radioactivity; } JsonObject& voltageObject = array.createNestedObject(); voltageObject["sensor"] = SENSOR7_ID; - voltageObject["value"] = ESP.getVcc()/1024.0; + voltageObject["value"] = sd.voltage; + root["voltage"] = sd.voltage; JsonObject& rssiObject = array.createNestedObject(); rssiObject["sensor"] = SENSOR8_ID; rssiObject["value"] = sd.rssi; + root["rssi"] = sd.rssi; char buffer[500]; @@ -305,7 +316,7 @@ void ICACHE_FLASH_ATTR sendValues() { } WiFiClientSecure net; - MQTTClient mqttclient; + MQTTClient mqttclient(MQTT_MAX_PACKET_SIZE); mqttclient.begin(mqttserver, 8883, net); int tries = 0; @@ -323,8 +334,8 @@ void ICACHE_FLASH_ATTR sendValues() { } mqttclient.loop(); - Serial.println(buffer); - if (mqttclient.publish("sensor/esp-weatherstation/json", buffer, sizeof(buffer))) { + root.printTo(buffer, sizeof(buffer)); + if (mqttclient.publish("sensor/esp-weatherstation/01/json", buffer, strlen(buffer))) { Serial.println("mqtt done"); } else { Serial.println("mqtt failed");