1+ /*
2+ * If you encounter any issues:
3+ * - check the readme.md at https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md
4+ * - ensure all dependent libraries are installed
5+ * - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#arduinoide
6+ * - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#dependencies
7+ * - open serial monitor and check whats happening
8+ * - check full user documentation at https://sinricpro.github.io/esp8266-esp32-sdk
9+ * - visit https://github.com/sinricpro/esp8266-esp32-sdk/issues and check for existing issues or open a new one
10+ */
11+
12+ // Uncomment the following line to enable serial debug output
13+ // #define ENABLE_DEBUG
14+
15+ #ifdef ENABLE_DEBUG
16+ #define DEBUG_ESP_PORT Serial
17+ #define NODEBUG_WEBSOCKETS
18+ #define NDEBUG
19+ #endif
20+
21+ #include < Arduino.h>
22+ #ifdef ESP8266
23+ #include < ESP8266WiFi.h>
24+ #endif
25+ #ifdef ESP32
26+ #include < WiFi.h>
27+ #endif
28+
29+ #include " SinricPro.h"
30+ #include " SinricProFan.h"
31+
32+ #define WIFI_SSID " YOUR-WIFI-SSID"
33+ #define WIFI_PASS " YOUR-WIFI-PASSWORD"
34+ #define APP_KEY " YOUR-APP-KEY" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
35+ #define APP_SECRET " YOUR-APP-SECRET" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"
36+ #define FAN_ID " YOUR-DEVICE-ID" // Should look like "5dc1564130xxxxxxxxxxxxxx"
37+ #define BAUD_RATE 9600 // Change baudrate to your need
38+
39+ // we use a struct to store all states and values for our fan
40+ // powerLevel is used for fan speed (0..100)
41+ struct {
42+ bool powerState = false ;
43+ int powerLevel = 0 ;
44+ } device_state;
45+
46+ bool onPowerState (const String &deviceId, bool &state) {
47+ Serial.printf (" Fan turned %s\r\n " , state?" on" :" off" );
48+ device_state.powerState = state;
49+ return true ; // request handled properly
50+ }
51+
52+ bool onPowerLevel (const String &deviceId, int &powerLevel) {
53+ device_state.powerLevel = powerLevel;
54+ Serial.printf (" Fan speed changed to %d\r\n " , device_state.powerLevel );
55+ return true ;
56+ }
57+
58+ bool onAdjustPowerLevel (const String &deviceId, int levelDelta) {
59+ device_state.powerLevel += levelDelta;
60+ Serial.printf (" Fan speed changed about %i to %d\r\n " , levelDelta, device_state.powerLevel );
61+ levelDelta = device_state.powerLevel ;
62+ return true ;
63+ }
64+
65+ void setupWiFi () {
66+ Serial.printf (" \r\n [Wifi]: Connecting" );
67+ WiFi.begin (WIFI_SSID, WIFI_PASS);
68+
69+ while (WiFi.status () != WL_CONNECTED) {
70+ Serial.printf (" ." );
71+ delay (250 );
72+ }
73+ Serial.printf (" connected!\r\n [WiFi]: IP-Address is %s\r\n " , WiFi.localIP ().toString ().c_str ());
74+ }
75+
76+ void setupSinricPro () {
77+ SinricProFan &myFan = SinricPro[FAN_ID];
78+
79+ // set callback function to device
80+ myFan.onPowerState (onPowerState);
81+ myFan.onPowerLevel (onPowerLevel);
82+ myFan.onAdjustPowerLevel (onAdjustPowerLevel);
83+
84+ // setup SinricPro
85+ SinricPro.onConnected ([](){ Serial.printf (" Connected to SinricPro\r\n " ); });
86+ SinricPro.onDisconnected ([](){ Serial.printf (" Disconnected from SinricPro\r\n " ); });
87+ SinricPro.begin (APP_KEY, APP_SECRET);
88+ }
89+
90+ // main setup function
91+ void setup () {
92+ Serial.begin (BAUD_RATE); Serial.printf (" \r\n\r\n " );
93+ setupWiFi ();
94+ setupSinricPro ();
95+ }
96+
97+ void loop () {
98+ SinricPro.handle ();
99+ }
0 commit comments