/* * 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** Bearbeitet von Daniel 07.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 <Arduino.h>#ifdef ESP8266 #include <ESP8266WiFi.h>#endif #ifdef ESP32 #include <WiFi.h>#endif
#include "SinricPro.h"#include "SinricProDimSwitch.h"
#define WIFI_SSID "xxx" #define WIFI_PASS "xxx"#define APP_KEY "xxx" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"#define APP_SECRET "xxx" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"#define LIGHT_ID "xxx" // Should look like "5dc1564130xxxxxxxxxxxxxx"#define BAUD_RATE 115200 // Change baudrate to your need
#define LED_PIN D1
// 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("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); 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;}
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() { // get a new Light device from SinricPro SinricProDimSwitch &DimSwitch = SinricPro[LIGHT_ID]; // set callback function to device DimSwitch.onPowerState(onPowerState); DimSwitch.onPowerLevel(onPowerLevel); DimSwitch.onAdjustPowerLevel(onAdjustPowerLevel);
// 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);}
// main setup functionvoid setup() { Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n"); setupWiFi(); setupSinricPro();
pinMode(LED_PIN, OUTPUT);}
void loop() { SinricPro.handle();}