Skip to content

Commit 2ee34a7

Browse files
committed
FastLED Light Example
1 parent b154b5b commit 2ee34a7

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
#define FASTLED_ESP8266_DMA
30+
#define FASTLED_ESP8266_RAW_PIN_ORDER
31+
#include <FastLED.h>
32+
33+
#include "SinricPro.h"
34+
#include "SinricProLight.h"
35+
36+
#define WIFI_SSID "YOUR-WIFI-SSID"
37+
#define WIFI_PASS "YOUR-WIFI-PASSWORD"
38+
#define APP_KEY "YOUR-APP-KEY" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
39+
#define APP_SECRET "YOUR-APP-SECRET" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"
40+
#define LIGHT_ID "YOUR-DEVICE-ID" // Should look like "5dc1564130xxxxxxxxxxxxxx"
41+
#define BAUD_RATE 9600 // Change baudrate to your need
42+
43+
#define NUM_LEDS 1 // how much LEDs are on the stripe
44+
#define LED_PIN 3 // LED stripe is connected to PIN 3
45+
46+
bool powerState;
47+
int globalBrightness = 100;
48+
CRGB leds[NUM_LEDS];
49+
50+
bool onPowerState(const String &deviceId, bool &state) {
51+
powerState = state;
52+
if (state) {
53+
FastLED.setBrightness(map(globalBrightness, 0, 100, 0, 255));
54+
} else {
55+
FastLED.setBrightness(0);
56+
}
57+
FastLED.show();
58+
return true; // request handled properly
59+
}
60+
61+
bool onBrightness(const String &deviceId, int &brightness) {
62+
globalBrightness = brightness;
63+
FastLED.setBrightness(map(brightness, 0, 100, 0, 255));
64+
FastLED.show();
65+
return true;
66+
}
67+
68+
bool onAdjustBrightness(const String &deviceId, int brightnessDelta) {
69+
globalBrightness += brightnessDelta;
70+
brightnessDelta = globalBrightness;
71+
FastLED.setBrightness(map(globalBrightness, 0, 100, 0, 255));
72+
FastLED.show();
73+
return true;
74+
}
75+
76+
bool onColor(const String &deviceId, byte &r, byte &g, byte &b) {
77+
fill_solid(leds, NUM_LEDS, CRGB(r, g, b));
78+
FastLED.show();
79+
return true;
80+
}
81+
82+
void setupFastLED() {
83+
FastLED.addLeds<WS2812B, LED_PIN, RGB>(leds, NUM_LEDS);
84+
FastLED.setBrightness(map(globalBrightness, 0, 100, 0, 255));
85+
fill_solid(leds, NUM_LEDS, CRGB::White);
86+
FastLED.show();
87+
}
88+
89+
void setupWiFi() {
90+
Serial.printf("\r\n[Wifi]: Connecting");
91+
WiFi.begin(WIFI_SSID, WIFI_PASS);
92+
93+
while (WiFi.status() != WL_CONNECTED) {
94+
Serial.printf(".");
95+
delay(250);
96+
}
97+
IPAddress localIP = WiFi.localIP();
98+
Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", localIP.toString().c_str());
99+
}
100+
101+
void setupSinricPro() {
102+
// get a new Light device from SinricPro
103+
SinricProLight &myLight = SinricPro[LIGHT_ID];
104+
105+
// set callback function to device
106+
myLight.onPowerState(onPowerState);
107+
myLight.onBrightness(onBrightness);
108+
myLight.onAdjustBrightness(onAdjustBrightness);
109+
myLight.onColor(onColor);
110+
111+
// setup SinricPro
112+
SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); });
113+
SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); });
114+
SinricPro.restoreDeviceStates(true);
115+
SinricPro.begin(APP_KEY, APP_SECRET);
116+
}
117+
118+
// main setup function
119+
void setup() {
120+
Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");
121+
setupFastLED();
122+
setupWiFi();
123+
setupSinricPro();
124+
}
125+
126+
void loop() {
127+
SinricPro.handle();
128+
}

0 commit comments

Comments
 (0)