0

I am using a WeMos Wifi ESP8266 with a DHT11 sensor to measure temperature and humidity in my house and send them to SinricPro. Alexa then reads from here and uses them to answer to the question "what is the temperature inside?".

My issue is that values are read as floats and when connected to a debug console they are printed with decimal values, but on Sinric they look like rounded (so for example I see 25.3° on the debug console but then I see 23.0° on Sinric). Why?

This is the code running on the WeMos:

// Uncomment the following line to enable serial debug output
//#define ENABLE_DEBUG

#ifdef ENABLE_DEBUG #define DEBUG_ESP_PORT Serial #define NODEBUG_WEBSOCKETS #define NDEBUG #endif

#include <Arduino.h> #include <ESP8266WiFi.h> #include <SinricPro.h> #include <SinricProTemperaturesensor.h> #include <DHT.h>

#define WIFI_SSID "My-WiFi-SSD" // Your WiFI SSID name #define WIFI_PASS "My-password" // Your WiFi Password. #define APP_KEY "0123456789" //Get it from Portal -> Secrets #define APP_SECRET "9876543210" //Get it from Portal -> Secrets #define TEMP_SENSOR_ID "abcdefg" //Get it from Portal -> Devices) #define BAUD_RATE 115200 // Change baudrate to your need (used for serial monitor) #define EVENT_WAIT_TIME 60000 // send event every 60 seconds

// DHT Sensor #define DHT_PIN D4

DHT dht; // DHT sensor

float temperature; // actual temperature float humidity; // actual humidity float lastTemperature; // last known temperature (for compare) float lastHumidity; // last known humidity (for compare)

void handleTemperaturesensor() { if (SinricPro.isConnected() == false) { Serial.printf("Not connected to Sinric Pro...!\r\n"); return; }

static unsigned long last_millis; unsigned long current_millis = millis(); if (last_millis && current_millis - last_millis < EVENT_WAIT_TIME) return; last_millis = current_millis;

temperature = dht.getTemperature(); // get actual temperature in °C humidity = dht.getHumidity(); // get actual humidity

if (isnan(temperature) || isnan(humidity)) { // reading failed... Serial.printf("DHT reading failed!\r\n"); // print error message return; // try again next time }

Serial.printf("Temperature: %2.1f Celsius\tHumidity: %2.1f%%\r\n", temperature, humidity);

if (temperature == lastTemperature && humidity == lastHumidity) { Serial.printf("Temperature did not changed. do nothing...!\r\n"); return; }

SinricProTemperaturesensor &mySensor = SinricPro[TEMP_SENSOR_ID]; // get temperaturesensor device bool success = mySensor.sendTemperatureEvent(temperature, humidity); // send event if (success) {
Serial.printf("Sent!\r\n"); } else { Serial.printf("Something went wrong...could not send Event to server!\r\n"); // Enable ENABLE_DEBUG to see why }

lastTemperature = temperature; // save actual temperature for next compare lastHumidity = humidity; // save actual humidity for next compare }

// setup function for WiFi connection void setupWiFi() { Serial.printf("\r\n[Wifi]: Connecting");

WiFi.setSleepMode(WIFI_NONE_SLEEP); WiFi.setAutoReconnect(true);

WiFi.begin(WIFI_SSID, WIFI_PASS);

while (WiFi.status() != WL_CONNECTED) { Serial.printf("."); delay(250); } IPAddress localIP = WiFi.localIP(); Serial.printf("connected!\r\n[WiFi]: IP-Address is %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]); }

void setupSinricPro() { SinricProTemperaturesensor &mySensor = SinricPro[TEMP_SENSOR_ID];

// setup SinricPro SinricPro.onConnected({ Serial.printf("Connected to SinricPro\r\n"); }); SinricPro.onDisconnected({ Serial.printf("Disconnected from SinricPro\r\n"); }); //SinricPro.restoreDeviceStates(true); // Uncomment to restore the last known state from the server. SinricPro.begin(APP_KEY, APP_SECRET);
}

// main setup function void setup() { Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n"); dht.setup(DHT_PIN);

setupWiFi(); setupSinricPro(); }

void loop() { SinricPro.handle(); handleTemperaturesensor(); }

And this is what I see on Sinric, where you can see values where the decimal is .0: sinric dashboard

Deffo
  • 109
  • 6

0 Answers0