/* * Example for how to use SinricPro Temperaturesensor device: * - setup a temperature sensor device * - send temperature event to SinricPro server when temperature has changed * * DHT Sensor is connected to D5 on ESP8266 devices / GPIO5 on ESP32 devices * * DHT Library used in this example: https://github.com/markruys/arduino-DHT * * If you encounter any issues: * - check the readme.md at https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md * - ensure all dependent libraries are installed * - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#arduinoide * - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#dependencies * - open serial monitor and check whats happening * - check full user documentation at https://sinricpro.github.io/esp8266-esp32-sdk * - visit https://github.com/sinricpro/esp8266-esp32-sdk/issues and check for existing issues or open a new one** angepasst und bearbeitet von Daniel - 12.01.2021 */
// 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 "Zugang.h" //Zugangsdaten in separater Datei#include <Arduino.h>#ifdef ESP8266 #include <ESP8266WiFi.h>#endif #ifdef ESP32 #include <WiFi.h>#endif
#include "SinricPro.h"#include "SinricProTemperaturesensor.h"#include "DHT.h" // https://github.com/markruys/arduino-DHT
#include "SinricProDimSwitch.h"#include "SinricProContactsensor.h"
#define TEMP_SENSOR_ID "5ff88550dfd58f56bc43733d" // Should look like "5dc1564130xxxxxxxxxxxxxx"#define LIGHT_ID "5ff88266dfd58f56bc4372f6" // Should look like "5dc1564130xxxxxxxxxxxxxx"#define CONTACT_ID "5ffdd324dfd58f56bc43d47d" // Should look like "5dc1564130xxxxxxxxxxxxxx"#define BAUD_RATE 115200 // Change baudrate to your need (used for serial monitor)#define EVENT_WAIT_TIME 60000 // send event every 60 seconds#define LED_PIN D1 //Lichterbogen LED#define CONTACT_PIN D5 // PIN where contactsensor is connected to
#ifdef ESP8266 #define DHT_PIN D6#endif#ifdef ESP32 #define DHT_PIN 5#endif
DHT dht; // DHT sensor
bool deviceIsOn; // Temeprature sensor on/off statefloat temperature; // actual temperaturefloat humidity; // actual humidityfloat 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
// we use a struct to store all states and values for our lightstruct { bool powerState = false; int powerLevel = 0;} device_state;
bool onPowerState(const String &deviceId, bool &state) { Serial.printf("Temperaturesensor turned %s (via SinricPro) \r\n", state?"on":"off"); deviceIsOn = state; // turn on / off temperature sensor return true; // request handled properly}
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 }
if (temperature == lastTemperature || humidity == lastHumidity) return; // if no values changed do nothing...
SinricProTemperaturesensor &mySensor = SinricPro[TEMP_SENSOR_ID]; // get temperaturesensor device bool success = mySensor.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}
//Dim Switch Funktionenbool onPowerStateDimSwitch(const String &deviceId, bool &state) { Serial.printf("Device %s power turned %s \r\n", deviceId.c_str(), state?"on":"off"); device_state.powerState = state; digitalWrite(LED_PIN, state); return true; // request handled properly}
bool onPowerLevel(const String &deviceId, int &powerLevel) { device_state.powerLevel = powerLevel; Serial.printf("Device %s brightness level changed to %d\r\n", deviceId.c_str(), powerLevel); if(powerLevel > 98) { digitalWrite(LED_PIN, HIGH); } else if(powerLevel < 1) { digitalWrite(LED_PIN, LOW); } else { analogWrite(LED_PIN, powerLevel*2.5); } return true;}
bool onAdjustPowerLevel(const String &deviceId, int powerLevelDelta) { device_state.powerLevel += powerLevelDelta; Serial.printf("Device %s brightness level changed about %i to %d\r\n", deviceId.c_str(), powerLevelDelta, device_state.powerLevel*2.5); powerLevelDelta = device_state.powerLevel; analogWrite(LED_PIN, device_state.powerLevel*2.5); return true;}
//Fensterkontaktbool myPowerState = true; // assume device is turned onbool lastContactState = false;unsigned long lastChange = 0;
void handleContactsensor() { if (!myPowerState) return; // if device switched off...do nothing
unsigned long actualMillis = millis(); if (actualMillis - lastChange < 250) return; // debounce contact state transitions (same as debouncing a pushbutton)
bool actualContactState = digitalRead(CONTACT_PIN); // read actual state of contactsensor
if (actualContactState != lastContactState) { // if state has changed Serial.printf("Contactsensor is %s now\r\n", actualContactState?"open":"closed"); lastContactState = actualContactState; // update last known state lastChange = actualMillis; // update debounce time SinricProContactsensor &myContact = SinricPro[CONTACT_ID]; // get contact sensor device myContact.sendContactEvent(actualContactState); // send event with actual state }}bool onPowerStateContact(const String &deviceId, bool &state) { Serial.printf("Device %s turned %s (via SinricPro) \r\n", deviceId.c_str(), state?"on":"off"); myPowerState = state; return true; // request handled properly}
// setup function for WiFi connectionvoid 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]);}
// setup function for SinricProvoid setupSinricPro() { // add device to SinricPro (Temperatursensor) SinricProTemperaturesensor &mySensor = SinricPro[TEMP_SENSOR_ID]; mySensor.onPowerState(onPowerState);
// get a new Light device from SinricPro SinricProDimSwitch &DimSwitch = SinricPro[LIGHT_ID]; DimSwitch.onPowerState(onPowerStateDimSwitch); DimSwitch.onPowerLevel(onPowerLevel); DimSwitch.onAdjustPowerLevel(onAdjustPowerLevel);
//Fensterkontakt initialisieren SinricProContactsensor& myContact = SinricPro[CONTACT_ID]; myContact.onPowerState(onPowerStateContact);
// 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); SinricPro.restoreDeviceStates(true); // get latest known deviceState from server (is device turned on?)}
// main setup functionvoid setup() { Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n"); dht.setup(DHT_PIN);
//Spannungsversorgung für DHT pinMode(D8, OUTPUT); pinMode(D7, OUTPUT); digitalWrite(D7, LOW); //GND digitalWrite(D8, HIGH);//3,3V
//Ausgang für LED aktivieren pinMode(LED_PIN, OUTPUT);
//Fensterkontakt pinMode(CONTACT_PIN, INPUT_PULLUP); setupWiFi(); setupSinricPro();}
void loop() { handleContactsensor(); SinricPro.handle(); handleTemperaturesensor();}