Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define FASTLED_ALLOW_INTERRUPTS 0
- #include "FastLED.h"
- #include <Arduino.h>
- #include <ESP8266WiFi.h>
- #include <ESP8266WiFiMulti.h>
- #include <WebSocketsClient.h> // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries
- #include <ArduinoJson.h> // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries
- #include <StreamString.h>
- #define DATA_PIN 3
- #define LED_TYPE WS2812
- #define COLOR_ORDER GRB
- #define NUM_LEDS 30 // Change this to reflect the number of LEDs you have
- #define BRIGHTNESS 128 // Set Brightness here
- CRGB leds[NUM_LEDS];
- #define SECONDS_PER_PALETTE 15
- ESP8266WiFiMulti WiFiMulti;
- WebSocketsClient webSocket;
- WiFiClient client;
- #define MyApiKey "xxxx" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard
- #define MySSID "xxxxx" // TODO: Change to your Wifi network SSID
- #define MyWifiPassword "xxxx" // TODO: Change to your Wifi network password
- #define HEARTBEAT_INTERVAL 300000 // 5 Minutes
- uint64_t heartbeatTimestamp = 0;
- bool isConnected = false;
- void setPowerStateOnServer(String deviceId, String value);
- void setTargetTemperatureOnServer(String deviceId, String value, String scale);
- extern const TProgmemRGBGradientPalettePtr gGradientPalettes[];
- extern const uint8_t gGradientPaletteCount;
- uint8_t gCurrentPaletteNumber = 0;
- CRGBPalette16 gCurrentPalette( CRGB::Black);
- CRGBPalette16 gTargetPalette( gGradientPalettes[0] );
- const uint8_t gGradientPaletteCount =
- sizeof( gGradientPalettes) / sizeof( TProgmemRGBGradientPalettePtr );
- const TProgmemRGBGradientPalettePtr gGradientPalettes[] = {
- Sunset_Real_gp,
- es_rivendell_15_gp,
- es_ocean_breeze_036_gp,
- rgi_15_gp,
- retro2_16_gp,
- Analogous_1_gp,
- es_pinksplash_08_gp,
- Coral_reef_gp,
- es_ocean_breeze_068_gp,
- es_pinksplash_07_gp,
- es_vintage_01_gp,
- departure_gp,
- es_landscape_64_gp,
- es_landscape_33_gp,
- rainbowsherbet_gp,
- gr65_hult_gp,
- gr64_hult_gp,
- GMT_drywet_gp,
- ib_jul01_gp,
- es_vintage_57_gp,
- ib15_gp,
- Fuschia_7_gp,
- es_emerald_dragon_08_gp,
- lava_gp,
- fire_gp,
- Colorfull_gp,
- Magenta_Evening_gp,
- Pink_Purple_gp,
- es_autumn_19_gp,
- BlacK_Blue_Magenta_White_gp,
- BlacK_Magenta_Red_gp,
- BlacK_Red_Magenta_Yellow_gp,
- Blue_Cyan_Yellow_gp };
- DEFINE_GRADIENT_PALETTE( ib_jul01_gp ) {
- 0, 194, 1, 1,
- 94, 1, 29, 18,
- 132, 57,131, 28,
- 255, 113, 1, 1};
- DEFINE_GRADIENT_PALETTE( es_vintage_57_gp ) {
- 0, 2, 1, 1,
- 53, 18, 1, 0,
- 104, 69, 29, 1,
- 153, 167,135, 10,
- 255, 46, 56, 4};
- DEFINE_GRADIENT_PALETTE( es_vintage_01_gp ) {
- 0, 4, 1, 1,
- 51, 16, 0, 1,
- 76, 97,104, 3,
- 101, 255,131, 19,
- 127, 67, 9, 4,
- 153, 16, 0, 1,
- 229, 4, 1, 1,
- 255, 4, 1, 1};
- DEFINE_GRADIENT_PALETTE( es_rivendell_15_gp ) {
- 0, 1, 14, 5,
- 101, 16, 36, 14,
- 165, 56, 68, 30,
- 242, 150,156, 99,
- 255, 150,156, 99};
- DEFINE_GRADIENT_PALETTE( rgi_15_gp ) {
- 0, 4, 1, 31,
- 31, 55, 1, 16,
- 63, 197, 3, 7,
- 95, 59, 2, 17,
- 127, 6, 2, 34,
- 159, 39, 6, 33,
- 191, 112, 13, 32,
- 223, 56, 9, 35,
- 255, 22, 6, 38};
- DEFINE_GRADIENT_PALETTE( retro2_16_gp ) {
- 0, 188,135, 1,
- 255, 46, 7, 1};
- DEFINE_GRADIENT_PALETTE( Analogous_1_gp ) {
- 0, 3, 0,255,
- 63, 23, 0,255,
- 127, 67, 0,255,
- 191, 142, 0, 45,
- 255, 255, 0, 0};
- DEFINE_GRADIENT_PALETTE( es_pinksplash_08_gp ) {
- 0, 126, 11,255,
- 127, 197, 1, 22,
- 175, 210,157,172,
- 221, 157, 3,112,
- 255, 157, 3,112};
- DEFINE_GRADIENT_PALETTE( es_pinksplash_07_gp ) {
- 0, 229, 1, 1,
- 61, 242, 4, 63,
- 101, 255, 12,255,
- 127, 249, 81,252,
- 153, 255, 11,235,
- 193, 244, 5, 68,
- 255, 232, 1, 5};
- DEFINE_GRADIENT_PALETTE( Coral_reef_gp ) {
- 0, 40,199,197,
- 50, 10,152,155,
- 96, 1,111,120,
- 96, 43,127,162,
- 139, 10, 73,111,
- 255, 1, 34, 71};
- DEFINE_GRADIENT_PALETTE( es_ocean_breeze_068_gp ) {
- 0, 100,156,153,
- 51, 1, 99,137,
- 101, 1, 68, 84,
- 104, 35,142,168,
- 178, 0, 63,117,
- 255, 1, 10, 10};
- DEFINE_GRADIENT_PALETTE( es_ocean_breeze_036_gp ) {
- 0, 1, 6, 7,
- 89, 1, 99,111,
- 153, 144,209,255,
- 255, 0, 73, 82};
- DEFINE_GRADIENT_PALETTE( departure_gp ) {
- 0, 8, 3, 0,
- 42, 23, 7, 0,
- 63, 75, 38, 6,
- 84, 169, 99, 38,
- 106, 213,169,119,
- 116, 255,255,255,
- 138, 135,255,138,
- 148, 22,255, 24,
- 170, 0,255, 0,
- 191, 0,136, 0,
- 212, 0, 55, 0,
- 255, 0, 55, 0};
- DEFINE_GRADIENT_PALETTE( es_landscape_64_gp ) {
- 0, 0, 0, 0,
- 37, 2, 25, 1,
- 76, 15,115, 5,
- 127, 79,213, 1,
- 128, 126,211, 47,
- 130, 188,209,247,
- 153, 144,182,205,
- 204, 59,117,250,
- 255, 1, 37,192};
- DEFINE_GRADIENT_PALETTE( es_landscape_33_gp ) {
- 0, 1, 5, 0,
- 19, 32, 23, 1,
- 38, 161, 55, 1,
- 63, 229,144, 1,
- 66, 39,142, 74,
- 255, 1, 4, 1};
- DEFINE_GRADIENT_PALETTE( rainbowsherbet_gp ) {
- 0, 255, 33, 4,
- 43, 255, 68, 25,
- 86, 255, 7, 25,
- 127, 255, 82,103,
- 170, 255,255,242,
- 209, 42,255, 22,
- 255, 87,255, 65};
- DEFINE_GRADIENT_PALETTE( gr65_hult_gp ) {
- 0, 247,176,247,
- 48, 255,136,255,
- 89, 220, 29,226,
- 160, 7, 82,178,
- 216, 1,124,109,
- 255, 1,124,109};
- DEFINE_GRADIENT_PALETTE( gr64_hult_gp ) {
- 0, 1,124,109,
- 66, 1, 93, 79,
- 104, 52, 65, 1,
- 130, 115,127, 1,
- 150, 52, 65, 1,
- 201, 1, 86, 72,
- 239, 0, 55, 45,
- 255, 0, 55, 45};
- DEFINE_GRADIENT_PALETTE( GMT_drywet_gp ) {
- 0, 47, 30, 2,
- 42, 213,147, 24,
- 84, 103,219, 52,
- 127, 3,219,207,
- 170, 1, 48,214,
- 212, 1, 1,111,
- 255, 1, 7, 33};
- DEFINE_GRADIENT_PALETTE( ib15_gp ) {
- 0, 113, 91,147,
- 72, 157, 88, 78,
- 89, 208, 85, 33,
- 107, 255, 29, 11,
- 141, 137, 31, 39,
- 255, 59, 33, 89};
- DEFINE_GRADIENT_PALETTE( Fuschia_7_gp ) {
- 0, 43, 3,153,
- 63, 100, 4,103,
- 127, 188, 5, 66,
- 191, 161, 11,115,
- 255, 135, 20,182};
- DEFINE_GRADIENT_PALETTE( es_emerald_dragon_08_gp ) {
- 0, 97,255, 1,
- 101, 47,133, 1,
- 178, 13, 43, 1,
- 255, 2, 10, 1};
- DEFINE_GRADIENT_PALETTE( lava_gp ) {
- 0, 0, 0, 0,
- 46, 18, 0, 0,
- 96, 113, 0, 0,
- 108, 142, 3, 1,
- 119, 175, 17, 1,
- 146, 213, 44, 2,
- 174, 255, 82, 4,
- 188, 255,115, 4,
- 202, 255,156, 4,
- 218, 255,203, 4,
- 234, 255,255, 4,
- 244, 255,255, 71,
- 255, 255,255,255};
- DEFINE_GRADIENT_PALETTE( fire_gp ) {
- 0, 1, 1, 0,
- 76, 32, 5, 0,
- 146, 192, 24, 0,
- 197, 220,105, 5,
- 240, 252,255, 31,
- 250, 252,255,111,
- 255, 255,255,255};
- DEFINE_GRADIENT_PALETTE( Colorfull_gp ) {
- 0, 10, 85, 5,
- 25, 29,109, 18,
- 60, 59,138, 42,
- 93, 83, 99, 52,
- 106, 110, 66, 64,
- 109, 123, 49, 65,
- 113, 139, 35, 66,
- 116, 192,117, 98,
- 124, 255,255,137,
- 168, 100,180,155,
- 255, 22,121,174};
- DEFINE_GRADIENT_PALETTE( Magenta_Evening_gp ) {
- 0, 71, 27, 39,
- 31, 130, 11, 51,
- 63, 213, 2, 64,
- 70, 232, 1, 66,
- 76, 252, 1, 69,
- 108, 123, 2, 51,
- 255, 46, 9, 35};
- DEFINE_GRADIENT_PALETTE( Pink_Purple_gp ) {
- 0, 19, 2, 39,
- 25, 26, 4, 45,
- 51, 33, 6, 52,
- 76, 68, 62,125,
- 102, 118,187,240,
- 109, 163,215,247,
- 114, 217,244,255,
- 122, 159,149,221,
- 149, 113, 78,188,
- 183, 128, 57,155,
- 255, 146, 40,123};
- DEFINE_GRADIENT_PALETTE( Sunset_Real_gp ) {
- 0, 120, 0, 0,
- 22, 179, 22, 0,
- 51, 255,104, 0,
- 85, 167, 22, 18,
- 135, 100, 0,103,
- 198, 16, 0,130,
- 255, 0, 0,160};
- DEFINE_GRADIENT_PALETTE( es_autumn_19_gp ) {
- 0, 26, 1, 1,
- 51, 67, 4, 1,
- 84, 118, 14, 1,
- 104, 137,152, 52,
- 112, 113, 65, 1,
- 122, 133,149, 59,
- 124, 137,152, 52,
- 135, 113, 65, 1,
- 142, 139,154, 46,
- 163, 113, 13, 1,
- 204, 55, 3, 1,
- 249, 17, 1, 1,
- 255, 17, 1, 1};
- DEFINE_GRADIENT_PALETTE( BlacK_Blue_Magenta_White_gp ) {
- 0, 0, 0, 0,
- 42, 0, 0, 45,
- 84, 0, 0,255,
- 127, 42, 0,255,
- 170, 255, 0,255,
- 212, 255, 55,255,
- 255, 255,255,255};
- DEFINE_GRADIENT_PALETTE( BlacK_Magenta_Red_gp ) {
- 0, 0, 0, 0,
- 63, 42, 0, 45,
- 127, 255, 0,255,
- 191, 255, 0, 45,
- 255, 255, 0, 0};
- DEFINE_GRADIENT_PALETTE( BlacK_Red_Magenta_Yellow_gp ) {
- 0, 0, 0, 0,
- 42, 42, 0, 0,
- 84, 255, 0, 0,
- 127, 255, 0, 45,
- 170, 255, 0,255,
- 212, 255, 55, 45,
- 255, 255,255, 0};
- DEFINE_GRADIENT_PALETTE( Blue_Cyan_Yellow_gp ) {
- 0, 0, 0,255,
- 63, 0, 55,255,
- 127, 0,255,255,
- 191, 42,255, 45,
- 255, 255,255, 0};
- void colorwaves( CRGB* ledarray, uint16_t numleds, CRGBPalette16& palette)
- {
- static uint16_t sPseudotime = 0;
- static uint16_t sLastMillis = 0;
- static uint16_t sHue16 = 0;
- uint8_t sat8 = beatsin88( 87, 220, 250);
- uint8_t brightdepth = beatsin88( 341, 96, 224);
- uint16_t brightnessthetainc16 = beatsin88( 203, (25 * 256), (40 * 256));
- uint8_t msmultiplier = beatsin88(147, 23, 60);
- uint16_t hue16 = sHue16;//gHue * 256;
- uint16_t hueinc16 = beatsin88(113, 300, 1500);
- uint16_t ms = millis();
- uint16_t deltams = ms - sLastMillis ;
- sLastMillis = ms;
- sPseudotime += deltams * msmultiplier;
- sHue16 += deltams * beatsin88( 400, 5,9);
- uint16_t brightnesstheta16 = sPseudotime;
- for( uint16_t i = 0 ; i < numleds; i++) {
- hue16 += hueinc16;
- uint8_t hue8 = hue16 / 256;
- uint16_t h16_128 = hue16 >> 7;
- if( h16_128 & 0x100) {
- hue8 = 255 - (h16_128 >> 1);
- } else {
- hue8 = h16_128 >> 1;
- }
- brightnesstheta16 += brightnessthetainc16;
- uint16_t b16 = sin16( brightnesstheta16 ) + 32768;
- uint16_t bri16 = (uint32_t)((uint32_t)b16 * (uint32_t)b16) / 65536;
- uint8_t bri8 = (uint32_t)(((uint32_t)bri16) * brightdepth) / 65536;
- bri8 += (255 - brightdepth);
- uint8_t index = hue8;
- //index = triwave8( index);
- index = scale8( index, 240);
- CRGB newcolor = ColorFromPalette( palette, index, bri8);
- uint16_t pixelnumber = i;
- pixelnumber = (numleds-1) - pixelnumber;
- nblend( ledarray[pixelnumber], newcolor, 128);
- }
- }
- void palettetest( CRGB* ledarray, uint16_t numleds, const CRGBPalette16& gCurrentPalette)
- {
- static uint8_t startindex = 0;
- startindex--;
- fill_palette( ledarray, numleds, startindex, (256 / NUM_LEDS) + 1, gCurrentPalette, 255, LINEARBLEND);
- }
- void turnOn(String deviceId) {
- if (deviceId == "5b8d6027f332cf2dc424f95d") // Device ID of first device
- {
- Serial.print("Turn on device id: ");
- Serial.println(deviceId);
- EVERY_N_SECONDS( SECONDS_PER_PALETTE ) {
- gCurrentPaletteNumber = addmod8( gCurrentPaletteNumber, 1, gGradientPaletteCount);
- gTargetPalette = gGradientPalettes[ gCurrentPaletteNumber ];
- }
- EVERY_N_MILLISECONDS(40) {
- nblendPaletteTowardPalette( gCurrentPalette, gTargetPalette, 16);
- }
- colorwaves( leds, NUM_LEDS, gCurrentPalette);
- FastLED.show();
- FastLED.delay(20);
- }
- else {
- Serial.print("Turn on for unknown device id: ");
- Serial.println(deviceId);
- }
- }
- void turnOff(String deviceId) {
- if (deviceId == "5b8d6027f332cf2dc424f95d") // Device ID of first device
- {
- Serial.print("Turn off Device ID: ");
- Serial.println(deviceId);
- fill_solid(leds, NUM_LEDS, CRGB::Black);
- FastLED.show();
- }
- else {
- Serial.print("Turn off for unknown device id: ");
- Serial.println(deviceId);
- }
- }
- void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
- switch(type) {
- case WStype_DISCONNECTED:
- isConnected = false;
- Serial.printf("[WSc] Webservice disconnected from sinric.com!\n");
- break;
- case WStype_CONNECTED: {
- isConnected = true;
- Serial.printf("[WSc] Service connected to sinric.com at url: %s\n", payload);
- Serial.printf("Waiting for commands from sinric.com ...\n");
- }
- break;
- case WStype_TEXT: {
- Serial.printf("[WSc] get text: %s\n", payload);
- // Example payloads
- // For Switch or Light device types
- // {"deviceId": xxxx, "action": "setPowerState", value: "ON"} // https://developer.amazon.com/docs/device-apis/alexa-powercontroller.html
- // For Light device type
- // Look at the light example in github
- DynamicJsonBuffer jsonBuffer;
- JsonObject& json = jsonBuffer.parseObject((char*)payload);
- String deviceId = json ["deviceId"];
- String action = json ["action"];
- if(action == "setPowerState") { // Switch or Light
- String value = json ["value"];
- if(value == "ON") {
- turnOn(deviceId);
- } else {
- turnOff(deviceId);
- }
- }
- else if (action == "SetTargetTemperature") {
- String deviceId = json ["deviceId"];
- String action = json ["action"];
- String value = json ["value"];
- }
- else if (action == "test") {
- Serial.println("[WSc] received test command from sinric.com");
- }
- }
- break;
- case WStype_BIN:
- Serial.printf("[WSc] get binary length: %u\n", length);
- break;
- }
- }
- void setup() {
- Serial.begin(115200);
- WiFiMulti.addAP(MySSID, MyWifiPassword);
- Serial.println();
- Serial.print("Connecting to Wifi: ");
- Serial.println(MySSID);
- // Waiting for Wifi connect
- while(WiFiMulti.run() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- if(WiFiMulti.run() == WL_CONNECTED) {
- Serial.println("");
- Serial.print("WiFi connected. ");
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS)
- .setDither(BRIGHTNESS < 255);
- FastLED.setBrightness(BRIGHTNESS);
- }
- // server address, port and URL
- webSocket.begin("iot.sinric.com", 80, "/");
- // event handler
- webSocket.onEvent(webSocketEvent);
- webSocket.setAuthorization("apikey", MyApiKey);
- // try again every 5000ms if connection has failed
- webSocket.setReconnectInterval(5000); // If you see 'class WebSocketsClient' has no member named 'setReconnectInterval' error update arduinoWebSockets
- FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
- }
- void loop() {
- webSocket.loop();
- if(isConnected) {
- uint64_t now = millis();
- // Send heartbeat in order to avoid disconnections during ISP resetting IPs over night. Thanks @MacSass
- if((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) {
- heartbeatTimestamp = now;
- webSocket.sendTXT("H");
- }
- }
- }
- // If you are going to use a push button to on/off the switch manually, use this function to update the status on the server
- // so it will reflect on Alexa app.
- // eg: setPowerStateOnServer("deviceid", "ON")
- void setPowerStateOnServer(String deviceId, String value) {
- DynamicJsonBuffer jsonBuffer;
- JsonObject& root = jsonBuffer.createObject();
- root["deviceId"] = deviceId;
- root["action"] = "setPowerState";
- root["value"] = value;
- StreamString databuf;
- root.printTo(databuf);
- webSocket.sendTXT(databuf);
- }
- //eg: setPowerStateOnServer("deviceid", "CELSIUS", "25.0")
- void setTargetTemperatureOnServer(String deviceId, String value, String scale) {
- DynamicJsonBuffer jsonBuffer;
- JsonObject& root = jsonBuffer.createObject();
- root["action"] = "SetTargetTemperature";
- root["deviceId"] = deviceId;
- JsonObject& valueObj = root.createNestedObject("value");
- JsonObject& targetSetpoint = valueObj.createNestedObject("targetSetpoint");
- targetSetpoint["value"] = value;
- targetSetpoint["scale"] = scale;
- StreamString databuf;
- root.printTo(databuf);
- webSocket.sendTXT(databuf);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement