1919#endif
2020
2121#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 " SinricProLight.h"
31-
32- #define WIFI_SSID " YOUR-WIFI-SSID"
33- #define WIFI_PASS " YOUR-WIFI-PASS"
34- #define APP_KEY " YOUR-APPKEY" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
35- #define APP_SECRET " YOUR-APPSECRET" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"
36- #define LIGHT_ID " YOUR-DEVICEID" // Should look like "5dc1564130xxxxxxxxxxxxxx"
37- #define BAUD_RATE 9600 // Change baudrate to your need for serial log
38-
39- #define BLUE_PIN D5 // PIN for BLUE Mosfet - change this to your need (D5 = GPIO14 on ESP8266)
40- #define RED_PIN D6 // PIN for RED Mosfet - change this to your need (D6 = GPIO12 on ESP8266)
41- #define GREEN_PIN D7 // PIN for GREEN Mosfet - change this to your need (D7 = GPIO13 on ESP8266)
42-
43- struct { // Stores current device state with following initial values:
44- bool powerState = false ; // initial state is off
45- int brightness = 100 ; // initial brightness is set to 100
46- struct {
47- byte r = 255 ; // color
48- byte g = 255 ; // is set
49- byte b = 255 ; // to white
50- } color;
51- } device_state;
52-
53- // setStripe: sets the mosfets values corresponding to values stored in device_state
22+ #include < ESP8266WiFi.h>
23+ #include < SinricPro.h>
24+ #include < SinricProLight.h>
25+
26+ #include < map>
27+
28+ #define WIFI_SSID " YOUR-WIFI-SSID"
29+ #define WIFI_PASS " YOUR-WIFI-PASS"
30+ #define APP_KEY " YOUR-APPKEY" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
31+ #define APP_SECRET " YOUR-APPSECRET" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"
32+ #define LIGHT_ID " YOUR-DEVICEID" // Should look like "5dc1564130xxxxxxxxxxxxxx"
33+ #define BAUD_RATE 9600 // Change baudrate to your need for serial log
34+
35+ #define BLUE_PIN D5 // PIN for BLUE Mosfet - change this to your need (D5 = GPIO14 on ESP8266)
36+ #define RED_PIN D6 // PIN for RED Mosfet - change this to your need (D6 = GPIO12 on ESP8266)
37+ #define GREEN_PIN D7 // PIN for GREEN Mosfet - change this to your need (D7 = GPIO13 on ESP8266)
38+
39+ struct Color {
40+ uint8_t r;
41+ uint8_t g;
42+ uint8_t b;
43+ };
44+
45+ // Colortemperature lookup table
46+ using ColorTemperatures = std::map<uint16_t , Color>;
47+ ColorTemperatures colorTemperatures{
48+ // {Temperature value, {color r, g, b}}
49+ {2000 , {255 , 138 , 18 }},
50+ {2200 , {255 , 147 , 44 }},
51+ {2700 , {255 , 169 , 87 }},
52+ {3000 , {255 , 180 , 107 }},
53+ {4000 , {255 , 209 , 163 }},
54+ {5000 , {255 , 228 , 206 }},
55+ {5500 , {255 , 236 , 224 }},
56+ {6000 , {255 , 243 , 239 }},
57+ {6500 , {255 , 249 , 253 }},
58+ {7000 , {245 , 243 , 255 }},
59+ {7500 , {235 , 238 , 255 }},
60+ {9000 , {214 , 225 , 255 }}};
61+
62+ struct DeviceState { // Stores current device state with following initial values:
63+ bool powerState = false ; // initial state is off
64+ Color color = colorTemperatures[9000 ]; // color is set to white (9000k)
65+ int colorTemperature = 9000 ; // color temperature is set to 9000k
66+ int brightness = 100 ; // brightness is set to 100
67+ } device_state;
68+
69+ SinricProLight& myLight = SinricPro[LIGHT_ID]; // SinricProLight device
70+
5471void setStripe () {
55- int rValue = map (device_state.color .r * device_state.brightness , 0 , 255 * 100 , 0 , 1023 ); // calculate red value and map between 0 and 1023 for analogWrite
56- int gValue = map (device_state.color .g * device_state.brightness , 0 , 255 * 100 , 0 , 1023 ); // calculate green value and map between 0 and 1023 for analogWrite
57- int bValue = map (device_state.color .b * device_state.brightness , 0 , 255 * 100 , 0 , 1023 ); // calculate blue value and map between 0 and 1023 for analogWrite
58-
59- if (device_state.powerState == false ) { // turn off?
60- digitalWrite (RED_PIN, LOW); // set
61- digitalWrite (GREEN_PIN, LOW); // mosfets
62- digitalWrite (BLUE_PIN, LOW); // low
63- } else {
64- analogWrite (RED_PIN, rValue); // write red value to pin
65- analogWrite (GREEN_PIN, gValue ); // write green value to pin
66- analogWrite (BLUE_PIN, bValue); // write blue value to pin
67- }
72+ int rValue = map (device_state.color .r * device_state.brightness , 0 , 255 * 100 , 0 , 1023 ); // calculate red value and map between 0 and 1023 for analogWrite
73+ int gValue = map (device_state.color .g * device_state.brightness , 0 , 255 * 100 , 0 , 1023 ); // calculate green value and map between 0 and 1023 for analogWrite
74+ int bValue = map (device_state.color .b * device_state.brightness , 0 , 255 * 100 , 0 , 1023 ); // calculate blue value and map between 0 and 1023 for analogWrite
75+
76+ if (device_state.powerState == false ) { // turn off?
77+ digitalWrite (RED_PIN, LOW); // set
78+ digitalWrite (GREEN_PIN, LOW); // mosfets
79+ digitalWrite (BLUE_PIN, LOW); // low
80+ } else {
81+ analogWrite (RED_PIN, rValue); // write red value to pin
82+ analogWrite (GREEN_PIN, gValue ); // write green value to pin
83+ analogWrite (BLUE_PIN, bValue); // write blue value to pin
84+ }
6885}
6986
70- bool onPowerState (const String & deviceId, bool & state) {
71- device_state.powerState = state; // store the new power state
72- setStripe (); // update the mosfets
73- return true ;
87+ bool onPowerState (const String& deviceId, bool & state) {
88+ device_state.powerState = state; // store the new power state
89+ setStripe (); // update the mosfets
90+ return true ;
7491}
7592
76- bool onBrightness (const String & deviceId, int & brightness) {
77- device_state.brightness = brightness; // store new brightness level
78- setStripe (); // update the mosfets
79- return true ;
93+ bool onBrightness (const String& deviceId, int & brightness) {
94+ device_state.brightness = brightness; // store new brightness level
95+ setStripe (); // update the mosfets
96+ return true ;
8097}
8198
82- bool onAdjustBrightness (const String & deviceId, int & brightnessDelta) {
83- device_state.brightness += brightnessDelta; // calculate and store new absolute brightness
84- brightnessDelta = device_state.brightness ; // return absolute brightness
85- setStripe (); // update the mosfets
86- return true ;
99+ bool onAdjustBrightness (const String& deviceId, int & brightnessDelta) {
100+ device_state.brightness += brightnessDelta; // calculate and store new absolute brightness
101+ brightnessDelta = device_state.brightness ; // return absolute brightness
102+ setStripe (); // update the mosfets
103+ return true ;
87104}
88105
89- bool onColor (const String & deviceId, byte & r, byte & g, byte & b) {
90- device_state.color .r = r; // store new red value
91- device_state.color .g = g; // store new green value
92- device_state.color .b = b; // store new blue value
93- setStripe (); // update the mosfets
94- return true ;
106+ bool onColor (const String& deviceId, byte& r, byte& g, byte& b) {
107+ device_state.color .r = r; // store new red value
108+ device_state.color .g = g; // store new green value
109+ device_state.color .b = b; // store new blue value
110+ setStripe (); // update the mosfets
111+ return true ;
95112}
96113
97- void setup () {
98- pinMode (RED_PIN, OUTPUT); // set red-mosfet pin as output
99- pinMode (GREEN_PIN, OUTPUT); // set green-mosfet pin as output
100- pinMode (BLUE_PIN, OUTPUT); // set blue-mosfet pin as output
114+ bool onColorTemperature (const String& deviceId, int & colorTemperature) {
115+ device_state.color = colorTemperatures[colorTemperature]; // set rgb values from corresponding colortemperauture
116+ device_state.colorTemperature = colorTemperature; // store the current color temperature
117+ setStripe (); // update the mosfets
118+ return true ;
119+ }
101120
102- Serial.begin (BAUD_RATE); // setup serial
121+ bool onIncreaseColorTemperature (const String& devceId, int & colorTemperature) {
122+ auto current = colorTemperatures.find (device_state.colorTemperature ); // get current entry from colorTemperature map
123+ auto next = std::next (current); // get next element
124+ if (next == colorTemperatures.end ()) next = current; // prevent past last element
125+ device_state.color = next->second ; // set color
126+ device_state.colorTemperature = next->first ; // set colorTemperature
127+ colorTemperature = device_state.colorTemperature ; // return new colorTemperature
128+ setStripe ();
129+ return true ;
130+ }
131+
132+ bool onDecreaseColorTemperature (const String& devceId, int & colorTemperature) {
133+ auto current = colorTemperatures.find (device_state.colorTemperature ); // get current entry from colorTemperature map
134+ auto next = std::prev (current); // get previous element
135+ if (next == colorTemperatures.end ()) next = current; // prevent before first element
136+ device_state.color = next->second ; // set color
137+ device_state.colorTemperature = next->first ; // set colorTemperature
138+ colorTemperature = device_state.colorTemperature ; // return new colorTemperature
139+ setStripe ();
140+ return true ;
141+ }
103142
104- WiFi.begin (WIFI_SSID, WIFI_PASS); // connect wifi
143+ void setupWiFi () {
144+ Serial.printf (" WiFi: connecting" );
145+ WiFi.begin (WIFI_SSID, WIFI_PASS);
146+ while (WiFi.status () != WL_CONNECTED) {
147+ Serial.printf (" ." );
148+ delay (250 );
149+ }
150+ Serial.printf (" connected\r\n IP is %s\r\n " , WiFi.localIP ().toString ().c_str ());
151+ }
152+
153+ void setupSinricPro () {
154+ myLight.onPowerState (onPowerState); // assign onPowerState callback
155+ myLight.onBrightness (onBrightness); // assign onBrightness callback
156+ myLight.onAdjustBrightness (onAdjustBrightness); // assign onAdjustBrightness callback
157+ myLight.onColor (onColor); // assign onColor callback
158+ myLight.onColorTemperature (onColorTemperature); // assign onColorTemperature callback
159+ myLight.onDecreaseColorTemperature (onDecreaseColorTemperature); // assign onDecreaseColorTemperature callback
160+ myLight.onIncreaseColorTemperature (onIncreaseColorTemperature); // assign onIncreaseColorTemperature callback
161+
162+ SinricPro.begin (APP_KEY, APP_SECRET); // start SinricPro
163+ }
164+
165+ void setup () {
166+ Serial.begin (BAUD_RATE); // setup serial
105167
106- SinricProLight &myLight = SinricPro[LIGHT_ID]; // create light device
107- myLight.onPowerState (onPowerState); // set PowerState callback
108- myLight.onBrightness (onBrightness); // set Brightness callback
109- myLight.onAdjustBrightness (onAdjustBrightness); // set AdjustBrightness callback
110- myLight.onColor (onColor); // set Color callback
168+ pinMode (RED_PIN, OUTPUT); // set red-mosfet pin as output
169+ pinMode (GREEN_PIN, OUTPUT); // set green-mosfet pin as output
170+ pinMode (BLUE_PIN, OUTPUT); // set blue-mosfet pin as output
111171
112- SinricPro.begin (APP_KEY, APP_SECRET); // start SinricPro
172+ setupWiFi (); // connect wifi
173+ setupSinricPro (); // setup SinricPro
113174}
114175
115176void loop () {
116- SinricPro.handle (); // handle SinricPro communication
177+ SinricPro.handle (); // handle SinricPro communication
117178}
0 commit comments