Code Monkey home page Code Monkey logo

Comments (3)

sivar2311 avatar sivar2311 commented on August 17, 2024

SinricProThermostat-Class supports the sendTemperatureEvent-Function.
Please see full documentation: https://sinricpro.github.io/esp8266-esp32-sdk/class_sinric_pro_thermostat.html#a9545808dacd9efc40a05f16e09d79b4e

from esp8266-esp32-sdk.

sivar2311 avatar sivar2311 commented on August 17, 2024

Closed because of inactivity

from esp8266-esp32-sdk.

huseman21 avatar huseman21 commented on August 17, 2024

Because the example code has nothing in it to handle the temp sensor. Below I have harshly added the code to allow reading a dht sensor connected on pin D1.

// 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>
#ifdef ESP8266
#include <ESP8266WiFi.h>
#endif
#ifdef ESP32
#include <WiFi.h>
#endif

#include "SinricPro.h"
#include "SinricProThermostat.h"
#include "DHT.h" // https://github.com/markruys/arduino-DHT
#include "DHTesp.h"

#define WIFI_SSID "8888888"
#define WIFI_PASS "88888888"
#define APP_KEY "8888888" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
#define APP_SECRET "888888888" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"
#define THERMOSTAT_ID "888888888" // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define BAUD_RATE 115200 // Change baudrate to your need
#define EVENT_WAIT_TIME 60000 // send event every 60 seconds
#define DHT_PIN 5

DHT dht; // DHT sensor

float globalTemperature;
bool globalPowerState;

float temperature; // actual temperature
float humidity; // actual humidity
float lastTemperature; // last known temperature (for compare)
float lastHumidity; // last known humidity (for compare)
unsigned long lastEvent = (-EVENT_WAIT_TIME); // last time event has been sent

bool onPowerState(const String &deviceId, bool &state) {
Serial.printf("Thermostat %s turned %s\r\n", deviceId.c_str(), state?"on":"off");
globalPowerState = state;
return true; // request handled properly
}

bool onTargetTemperature(const String &deviceId, float &temperature) {
Serial.printf("Thermostat %s set temperature to %f\r\n", deviceId.c_str(), temperature);
globalTemperature = temperature;
return true;
}

bool onAdjustTargetTemperature(const String & deviceId, float &temperatureDelta) {
globalTemperature += temperatureDelta; // calculate absolut temperature
Serial.printf("Thermostat %s changed temperature about %f to %f", deviceId.c_str(), temperatureDelta, globalTemperature);
temperatureDelta = globalTemperature; // return absolut temperature
return true;
}

bool onThermostatMode(const String &deviceId, String &mode) {
Serial.printf("Thermostat %s set to mode %s\r\n", deviceId.c_str(), mode.c_str());
return true;

}
bool sendTemperatureEvent ( float temperature,float humidity = -1, String cause = "PERIODIC_POLL" );

void setupWiFi() {
Serial.printf("\r\n[Wifi]: Connecting");
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];
SinricProThermostat &myThermostat = SinricPro[THERMOSTAT_ID];
myThermostat.onPowerState(onPowerState);
myThermostat.onTargetTemperature(onTargetTemperature);
myThermostat.onAdjustTargetTemperature(onAdjustTargetTemperature);
myThermostat.onThermostatMode(onThermostatMode);
// setup SinricPro
SinricPro.onConnected({ Serial.printf("Connected to SinricPro\r\n"); });
SinricPro.onDisconnected({ Serial.printf("Disconnected from SinricPro\r\n"); });
SinricPro.begin(APP_KEY, APP_SECRET);
}

void handleTemperaturesensor() {
// if (deviceIsOn == false) return; // device is off...do nothing

unsigned long actualMillis = millis();
if (actualMillis - lastEvent < EVENT_WAIT_TIME) return; //only check every EVENT_WAIT_TIME milliseconds
//temperature = dht.getTemperature(); // get actual temperature in °C
temperature = dht.getTemperature() * 1.8f + 32; // get actual temperature in °F
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
}
SinricProThermostat &myThermostat = SinricPro[THERMOSTAT_ID];
bool success = myThermostat.sendTemperatureEvent(temperature, humidity); // send event
if (success) { // if event was sent successfuly, print temperature and humidity to serial
Serial.printf("Temperature: %2.1f Celsius\tHumidity: %2.1f%%\r\n", temperature, humidity);
} else { // if sending event failed, print error message
Serial.printf("Something went wrong...could not send Event to server!\r\n");
}
lastTemperature = temperature; // save actual temperature for next compare
lastHumidity = humidity; // save actual humidity for next compare
lastEvent = actualMillis; // save actual time for next compare
}

void setup() {
Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");
dht.setup(DHT_PIN);
setupWiFi();
setupSinricPro();
}

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

from esp8266-esp32-sdk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.