Advertisement
Guest User

Untitled

a guest
Sep 6th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.87 KB | None | 0 0
  1. #define FASTLED_ALLOW_INTERRUPTS 0
  2. #include "FastLED.h"
  3. #include <Arduino.h>
  4. #include <ESP8266WiFi.h>
  5. #include <ESP8266WiFiMulti.h>
  6. #include <WebSocketsClient.h> //  https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries
  7. #include <ArduinoJson.h> // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries
  8. #include <StreamString.h>
  9.  
  10. #define DATA_PIN    3
  11. #define LED_TYPE    WS2812
  12. #define COLOR_ORDER GRB
  13. #define NUM_LEDS    30       // Change this to reflect the number of LEDs you have
  14. #define BRIGHTNESS  128       // Set Brightness here
  15.  
  16. CRGB leds[NUM_LEDS];
  17.  
  18. #define SECONDS_PER_PALETTE 15
  19.  
  20.  
  21. ESP8266WiFiMulti WiFiMulti;
  22. WebSocketsClient webSocket;
  23. WiFiClient client;
  24.  
  25. #define MyApiKey "xxxx" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard
  26. #define MySSID "xxxxx" // TODO: Change to your Wifi network SSID
  27. #define MyWifiPassword "xxxx" // TODO: Change to your Wifi network password
  28.  
  29. #define HEARTBEAT_INTERVAL 300000 // 5 Minutes
  30.  
  31. uint64_t heartbeatTimestamp = 0;
  32. bool isConnected = false;
  33.  
  34. void setPowerStateOnServer(String deviceId, String value);
  35. void setTargetTemperatureOnServer(String deviceId, String value, String scale);
  36.  
  37. extern const TProgmemRGBGradientPalettePtr gGradientPalettes[];
  38. extern const uint8_t gGradientPaletteCount;
  39.  
  40. uint8_t gCurrentPaletteNumber = 0;
  41.  
  42. CRGBPalette16 gCurrentPalette( CRGB::Black);
  43. CRGBPalette16 gTargetPalette( gGradientPalettes[0] );
  44.  
  45. const uint8_t gGradientPaletteCount =
  46. sizeof( gGradientPalettes) / sizeof( TProgmemRGBGradientPalettePtr );
  47.  
  48. const TProgmemRGBGradientPalettePtr gGradientPalettes[] = {
  49.   Sunset_Real_gp,
  50.   es_rivendell_15_gp,
  51.   es_ocean_breeze_036_gp,
  52.   rgi_15_gp,
  53.   retro2_16_gp,
  54.   Analogous_1_gp,
  55.   es_pinksplash_08_gp,
  56.   Coral_reef_gp,
  57.   es_ocean_breeze_068_gp,
  58.   es_pinksplash_07_gp,
  59.   es_vintage_01_gp,
  60.   departure_gp,
  61.   es_landscape_64_gp,
  62.   es_landscape_33_gp,
  63.   rainbowsherbet_gp,
  64.   gr65_hult_gp,
  65.   gr64_hult_gp,
  66.   GMT_drywet_gp,
  67.   ib_jul01_gp,
  68.   es_vintage_57_gp,
  69.   ib15_gp,
  70.   Fuschia_7_gp,
  71.   es_emerald_dragon_08_gp,
  72.   lava_gp,
  73.   fire_gp,
  74.   Colorfull_gp,
  75.   Magenta_Evening_gp,
  76.   Pink_Purple_gp,
  77.   es_autumn_19_gp,
  78.   BlacK_Blue_Magenta_White_gp,
  79.   BlacK_Magenta_Red_gp,
  80.   BlacK_Red_Magenta_Yellow_gp,
  81.   Blue_Cyan_Yellow_gp };
  82.  
  83. DEFINE_GRADIENT_PALETTE( ib_jul01_gp ) {
  84.     0, 194,  1,  1,
  85.    94,   1, 29, 18,
  86.   132,  57,131, 28,
  87.   255, 113,  1,  1};
  88.  
  89. DEFINE_GRADIENT_PALETTE( es_vintage_57_gp ) {
  90.     0,   2,  1,  1,
  91.    53,  18,  1,  0,
  92.   104,  69, 29,  1,
  93.   153, 167,135, 10,
  94.   255,  46, 56,  4};
  95.  
  96.  
  97.  
  98. DEFINE_GRADIENT_PALETTE( es_vintage_01_gp ) {
  99.     0,   4,  1,  1,
  100.    51,  16,  0,  1,
  101.    76,  97,104,  3,
  102.   101, 255,131, 19,
  103.   127,  67,  9,  4,
  104.   153,  16,  0,  1,
  105.   229,   4,  1,  1,
  106.   255,   4,  1,  1};
  107.  
  108. DEFINE_GRADIENT_PALETTE( es_rivendell_15_gp ) {
  109.     0,   1, 14,  5,
  110.   101,  16, 36, 14,
  111.   165,  56, 68, 30,
  112.   242, 150,156, 99,
  113.   255, 150,156, 99};
  114.  
  115. DEFINE_GRADIENT_PALETTE( rgi_15_gp ) {
  116.     0,   4,  1, 31,
  117.    31,  55,  1, 16,
  118.    63, 197,  3,  7,
  119.    95,  59,  2, 17,
  120.   127,   6,  2, 34,
  121.   159,  39,  6, 33,
  122.   191, 112, 13, 32,
  123.   223,  56,  9, 35,
  124.   255,  22,  6, 38};
  125.  
  126. DEFINE_GRADIENT_PALETTE( retro2_16_gp ) {
  127.     0, 188,135,  1,
  128.   255,  46,  7,  1};
  129.  
  130. DEFINE_GRADIENT_PALETTE( Analogous_1_gp ) {
  131.     0,   3,  0,255,
  132.    63,  23,  0,255,
  133.   127,  67,  0,255,
  134.   191, 142,  0, 45,
  135.   255, 255,  0,  0};
  136.  
  137. DEFINE_GRADIENT_PALETTE( es_pinksplash_08_gp ) {
  138.     0, 126, 11,255,
  139.   127, 197,  1, 22,
  140.   175, 210,157,172,
  141.   221, 157,  3,112,
  142.   255, 157,  3,112};
  143.  
  144. DEFINE_GRADIENT_PALETTE( es_pinksplash_07_gp ) {
  145.     0, 229,  1,  1,
  146.    61, 242,  4, 63,
  147.   101, 255, 12,255,
  148.   127, 249, 81,252,
  149.   153, 255, 11,235,
  150.   193, 244,  5, 68,
  151.   255, 232,  1,  5};
  152.  
  153. DEFINE_GRADIENT_PALETTE( Coral_reef_gp ) {
  154.     0,  40,199,197,
  155.    50,  10,152,155,
  156.    96,   1,111,120,
  157.    96,  43,127,162,
  158.   139,  10, 73,111,
  159.   255,   1, 34, 71};
  160.  
  161. DEFINE_GRADIENT_PALETTE( es_ocean_breeze_068_gp ) {
  162.     0, 100,156,153,
  163.    51,   1, 99,137,
  164.   101,   1, 68, 84,
  165.   104,  35,142,168,
  166.   178,   0, 63,117,
  167.   255,   1, 10, 10};
  168.  
  169. DEFINE_GRADIENT_PALETTE( es_ocean_breeze_036_gp ) {
  170.     0,   1,  6,  7,
  171.    89,   1, 99,111,
  172.   153, 144,209,255,
  173.   255,   0, 73, 82};
  174.  
  175. DEFINE_GRADIENT_PALETTE( departure_gp ) {
  176.     0,   8,  3,  0,
  177.    42,  23,  7,  0,
  178.    63,  75, 38,  6,
  179.    84, 169, 99, 38,
  180.   106, 213,169,119,
  181.   116, 255,255,255,
  182.   138, 135,255,138,
  183.   148,  22,255, 24,
  184.   170,   0,255,  0,
  185.   191,   0,136,  0,
  186.   212,   0, 55,  0,
  187.   255,   0, 55,  0};
  188.  
  189. DEFINE_GRADIENT_PALETTE( es_landscape_64_gp ) {
  190.     0,   0,  0,  0,
  191.    37,   2, 25,  1,
  192.    76,  15,115,  5,
  193.   127,  79,213,  1,
  194.   128, 126,211, 47,
  195.   130, 188,209,247,
  196.   153, 144,182,205,
  197.   204,  59,117,250,
  198.   255,   1, 37,192};
  199.  
  200. DEFINE_GRADIENT_PALETTE( es_landscape_33_gp ) {
  201.     0,   1,  5,  0,
  202.    19,  32, 23,  1,
  203.    38, 161, 55,  1,
  204.    63, 229,144,  1,
  205.    66,  39,142, 74,
  206.   255,   1,  4,  1};
  207.  
  208. DEFINE_GRADIENT_PALETTE( rainbowsherbet_gp ) {
  209.     0, 255, 33,  4,
  210.    43, 255, 68, 25,
  211.    86, 255,  7, 25,
  212.   127, 255, 82,103,
  213.   170, 255,255,242,
  214.   209,  42,255, 22,
  215.   255,  87,255, 65};
  216.  
  217.  
  218. DEFINE_GRADIENT_PALETTE( gr65_hult_gp ) {
  219.     0, 247,176,247,
  220.    48, 255,136,255,
  221.    89, 220, 29,226,
  222.   160,   7, 82,178,
  223.   216,   1,124,109,
  224.   255,   1,124,109};
  225.  
  226. DEFINE_GRADIENT_PALETTE( gr64_hult_gp ) {
  227.     0,   1,124,109,
  228.    66,   1, 93, 79,
  229.   104,  52, 65,  1,
  230.   130, 115,127,  1,
  231.   150,  52, 65,  1,
  232.   201,   1, 86, 72,
  233.   239,   0, 55, 45,
  234.   255,   0, 55, 45};
  235.  
  236. DEFINE_GRADIENT_PALETTE( GMT_drywet_gp ) {
  237.     0,  47, 30,  2,
  238.    42, 213,147, 24,
  239.    84, 103,219, 52,
  240.   127,   3,219,207,
  241.   170,   1, 48,214,
  242.   212,   1,  1,111,
  243.   255,   1,  7, 33};
  244.  
  245. DEFINE_GRADIENT_PALETTE( ib15_gp ) {
  246.     0, 113, 91,147,
  247.    72, 157, 88, 78,
  248.    89, 208, 85, 33,
  249.   107, 255, 29, 11,
  250.   141, 137, 31, 39,
  251.   255,  59, 33, 89};
  252.  
  253. DEFINE_GRADIENT_PALETTE( Fuschia_7_gp ) {
  254.     0,  43,  3,153,
  255.    63, 100,  4,103,
  256.   127, 188,  5, 66,
  257.   191, 161, 11,115,
  258.   255, 135, 20,182};
  259.  
  260. DEFINE_GRADIENT_PALETTE( es_emerald_dragon_08_gp ) {
  261.     0,  97,255,  1,
  262.   101,  47,133,  1,
  263.   178,  13, 43,  1,
  264.   255,   2, 10,  1};
  265.  
  266. DEFINE_GRADIENT_PALETTE( lava_gp ) {
  267.     0,   0,  0,  0,
  268.    46,  18,  0,  0,
  269.    96, 113,  0,  0,
  270.   108, 142,  3,  1,
  271.   119, 175, 17,  1,
  272.   146, 213, 44,  2,
  273.   174, 255, 82,  4,
  274.   188, 255,115,  4,
  275.   202, 255,156,  4,
  276.   218, 255,203,  4,
  277.   234, 255,255,  4,
  278.   244, 255,255, 71,
  279.   255, 255,255,255};
  280.  
  281. DEFINE_GRADIENT_PALETTE( fire_gp ) {
  282.     0,   1,  1,  0,
  283.    76,  32,  5,  0,
  284.   146, 192, 24,  0,
  285.   197, 220,105,  5,
  286.   240, 252,255, 31,
  287.   250, 252,255,111,
  288.   255, 255,255,255};
  289.  
  290. DEFINE_GRADIENT_PALETTE( Colorfull_gp ) {
  291.     0,  10, 85,  5,
  292.    25,  29,109, 18,
  293.    60,  59,138, 42,
  294.    93,  83, 99, 52,
  295.   106, 110, 66, 64,
  296.   109, 123, 49, 65,
  297.   113, 139, 35, 66,
  298.   116, 192,117, 98,
  299.   124, 255,255,137,
  300.   168, 100,180,155,
  301.   255,  22,121,174};
  302.  
  303. DEFINE_GRADIENT_PALETTE( Magenta_Evening_gp ) {
  304.     0,  71, 27, 39,
  305.    31, 130, 11, 51,
  306.    63, 213,  2, 64,
  307.    70, 232,  1, 66,
  308.    76, 252,  1, 69,
  309.   108, 123,  2, 51,
  310.   255,  46,  9, 35};
  311.  
  312. DEFINE_GRADIENT_PALETTE( Pink_Purple_gp ) {
  313.     0,  19,  2, 39,
  314.    25,  26,  4, 45,
  315.    51,  33,  6, 52,
  316.    76,  68, 62,125,
  317.   102, 118,187,240,
  318.   109, 163,215,247,
  319.   114, 217,244,255,
  320.   122, 159,149,221,
  321.   149, 113, 78,188,
  322.   183, 128, 57,155,
  323.   255, 146, 40,123};
  324.  
  325. DEFINE_GRADIENT_PALETTE( Sunset_Real_gp ) {
  326.     0, 120,  0,  0,
  327.    22, 179, 22,  0,
  328.    51, 255,104,  0,
  329.    85, 167, 22, 18,
  330.   135, 100,  0,103,
  331.   198,  16,  0,130,
  332.   255,   0,  0,160};
  333.  
  334. DEFINE_GRADIENT_PALETTE( es_autumn_19_gp ) {
  335.     0,  26,  1,  1,
  336.    51,  67,  4,  1,
  337.    84, 118, 14,  1,
  338.   104, 137,152, 52,
  339.   112, 113, 65,  1,
  340.   122, 133,149, 59,
  341.   124, 137,152, 52,
  342.   135, 113, 65,  1,
  343.   142, 139,154, 46,
  344.   163, 113, 13,  1,
  345.   204,  55,  3,  1,
  346.   249,  17,  1,  1,
  347.   255,  17,  1,  1};
  348.  
  349. DEFINE_GRADIENT_PALETTE( BlacK_Blue_Magenta_White_gp ) {
  350.     0,   0,  0,  0,
  351.    42,   0,  0, 45,
  352.    84,   0,  0,255,
  353.   127,  42,  0,255,
  354.   170, 255,  0,255,
  355.   212, 255, 55,255,
  356.   255, 255,255,255};
  357.  
  358. DEFINE_GRADIENT_PALETTE( BlacK_Magenta_Red_gp ) {
  359.     0,   0,  0,  0,
  360.    63,  42,  0, 45,
  361.   127, 255,  0,255,
  362.   191, 255,  0, 45,
  363.   255, 255,  0,  0};
  364.  
  365. DEFINE_GRADIENT_PALETTE( BlacK_Red_Magenta_Yellow_gp ) {
  366.     0,   0,  0,  0,
  367.    42,  42,  0,  0,
  368.    84, 255,  0,  0,
  369.   127, 255,  0, 45,
  370.   170, 255,  0,255,
  371.   212, 255, 55, 45,
  372.   255, 255,255,  0};
  373.  
  374. DEFINE_GRADIENT_PALETTE( Blue_Cyan_Yellow_gp ) {
  375.     0,   0,  0,255,
  376.    63,   0, 55,255,
  377.   127,   0,255,255,
  378.   191,  42,255, 45,
  379.   255, 255,255,  0};
  380.  
  381. void colorwaves( CRGB* ledarray, uint16_t numleds, CRGBPalette16& palette)
  382. {
  383.   static uint16_t sPseudotime = 0;
  384.   static uint16_t sLastMillis = 0;
  385.   static uint16_t sHue16 = 0;
  386.  
  387.   uint8_t sat8 = beatsin88( 87, 220, 250);
  388.   uint8_t brightdepth = beatsin88( 341, 96, 224);
  389.   uint16_t brightnessthetainc16 = beatsin88( 203, (25 * 256), (40 * 256));
  390.   uint8_t msmultiplier = beatsin88(147, 23, 60);
  391.  
  392.   uint16_t hue16 = sHue16;//gHue * 256;
  393.   uint16_t hueinc16 = beatsin88(113, 300, 1500);
  394.  
  395.   uint16_t ms = millis();
  396.   uint16_t deltams = ms - sLastMillis ;
  397.   sLastMillis  = ms;
  398.   sPseudotime += deltams * msmultiplier;
  399.   sHue16 += deltams * beatsin88( 400, 5,9);
  400.   uint16_t brightnesstheta16 = sPseudotime;
  401.  
  402.   for( uint16_t i = 0 ; i < numleds; i++) {
  403.     hue16 += hueinc16;
  404.     uint8_t hue8 = hue16 / 256;
  405.     uint16_t h16_128 = hue16 >> 7;
  406.     if( h16_128 & 0x100) {
  407.       hue8 = 255 - (h16_128 >> 1);
  408.     } else {
  409.       hue8 = h16_128 >> 1;
  410.     }
  411.  
  412.     brightnesstheta16  += brightnessthetainc16;
  413.     uint16_t b16 = sin16( brightnesstheta16  ) + 32768;
  414.  
  415.     uint16_t bri16 = (uint32_t)((uint32_t)b16 * (uint32_t)b16) / 65536;
  416.     uint8_t bri8 = (uint32_t)(((uint32_t)bri16) * brightdepth) / 65536;
  417.     bri8 += (255 - brightdepth);
  418.    
  419.     uint8_t index = hue8;
  420.     //index = triwave8( index);
  421.     index = scale8( index, 240);
  422.  
  423.     CRGB newcolor = ColorFromPalette( palette, index, bri8);
  424.  
  425.     uint16_t pixelnumber = i;
  426.     pixelnumber = (numleds-1) - pixelnumber;
  427.    
  428.     nblend( ledarray[pixelnumber], newcolor, 128);
  429.   }
  430. }
  431.  
  432. void palettetest( CRGB* ledarray, uint16_t numleds, const CRGBPalette16& gCurrentPalette)
  433. {
  434.   static uint8_t startindex = 0;
  435.   startindex--;
  436.   fill_palette( ledarray, numleds, startindex, (256 / NUM_LEDS) + 1, gCurrentPalette, 255, LINEARBLEND);
  437. }
  438.  
  439.  
  440. void turnOn(String deviceId) {
  441.   if (deviceId == "5b8d6027f332cf2dc424f95d") // Device ID of first device
  442.   {  
  443.     Serial.print("Turn on device id: ");
  444.     Serial.println(deviceId);
  445.  
  446.  EVERY_N_SECONDS( SECONDS_PER_PALETTE ) {
  447.     gCurrentPaletteNumber = addmod8( gCurrentPaletteNumber, 1, gGradientPaletteCount);
  448.     gTargetPalette = gGradientPalettes[ gCurrentPaletteNumber ];
  449.   }
  450.  
  451.   EVERY_N_MILLISECONDS(40) {
  452.     nblendPaletteTowardPalette( gCurrentPalette, gTargetPalette, 16);
  453.   }
  454.  
  455.   colorwaves( leds, NUM_LEDS, gCurrentPalette);
  456.  
  457.   FastLED.show();
  458.   FastLED.delay(20);
  459.  
  460.   }
  461.   else {
  462.     Serial.print("Turn on for unknown device id: ");
  463.     Serial.println(deviceId);    
  464.   }    
  465. }
  466.  
  467. void turnOff(String deviceId) {
  468.    if (deviceId == "5b8d6027f332cf2dc424f95d") // Device ID of first device
  469.    {  
  470.      Serial.print("Turn off Device ID: ");
  471.      Serial.println(deviceId);
  472.   fill_solid(leds, NUM_LEDS, CRGB::Black);
  473.   FastLED.show();
  474.    }
  475.    
  476.   else {
  477.      Serial.print("Turn off for unknown device id: ");
  478.      Serial.println(deviceId);    
  479.   }
  480. }
  481.  
  482. void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
  483.   switch(type) {
  484.     case WStype_DISCONNECTED:
  485.       isConnected = false;    
  486.       Serial.printf("[WSc] Webservice disconnected from sinric.com!\n");
  487.       break;
  488.     case WStype_CONNECTED: {
  489.       isConnected = true;
  490.       Serial.printf("[WSc] Service connected to sinric.com at url: %s\n", payload);
  491.       Serial.printf("Waiting for commands from sinric.com ...\n");        
  492.       }
  493.       break;
  494.     case WStype_TEXT: {
  495.         Serial.printf("[WSc] get text: %s\n", payload);
  496.         // Example payloads
  497.  
  498.         // For Switch or Light device types
  499.         // {"deviceId": xxxx, "action": "setPowerState", value: "ON"} // https://developer.amazon.com/docs/device-apis/alexa-powercontroller.html
  500.  
  501.         // For Light device type
  502.         // Look at the light example in github
  503.          
  504.         DynamicJsonBuffer jsonBuffer;
  505.         JsonObject& json = jsonBuffer.parseObject((char*)payload);
  506.         String deviceId = json ["deviceId"];    
  507.         String action = json ["action"];
  508.        
  509.         if(action == "setPowerState") { // Switch or Light
  510.             String value = json ["value"];
  511.             if(value == "ON") {
  512.                 turnOn(deviceId);
  513.             } else {
  514.                 turnOff(deviceId);
  515.             }
  516.         }
  517.         else if (action == "SetTargetTemperature") {
  518.             String deviceId = json ["deviceId"];    
  519.             String action = json ["action"];
  520.             String value = json ["value"];
  521.         }
  522.         else if (action == "test") {
  523.             Serial.println("[WSc] received test command from sinric.com");
  524.         }
  525.       }
  526.       break;
  527.     case WStype_BIN:
  528.       Serial.printf("[WSc] get binary length: %u\n", length);
  529.       break;
  530.   }
  531. }
  532.  
  533. void setup() {
  534.   Serial.begin(115200);
  535.  
  536.   WiFiMulti.addAP(MySSID, MyWifiPassword);
  537.   Serial.println();
  538.   Serial.print("Connecting to Wifi: ");
  539.   Serial.println(MySSID);  
  540.  
  541.   // Waiting for Wifi connect
  542.   while(WiFiMulti.run() != WL_CONNECTED) {
  543.     delay(500);
  544.     Serial.print(".");
  545.   }
  546.   if(WiFiMulti.run() == WL_CONNECTED) {
  547.     Serial.println("");
  548.     Serial.print("WiFi connected. ");
  549.     Serial.print("IP address: ");
  550.     Serial.println(WiFi.localIP());
  551.  
  552.   FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS)
  553.   .setDither(BRIGHTNESS < 255);
  554.   FastLED.setBrightness(BRIGHTNESS);
  555.   }
  556.  
  557.   // server address, port and URL
  558.   webSocket.begin("iot.sinric.com", 80, "/");
  559.  
  560.   // event handler
  561.   webSocket.onEvent(webSocketEvent);
  562.   webSocket.setAuthorization("apikey", MyApiKey);
  563.  
  564.   // try again every 5000ms if connection has failed
  565.   webSocket.setReconnectInterval(5000);   // If you see 'class WebSocketsClient' has no member named 'setReconnectInterval' error update arduinoWebSockets
  566.  
  567.   FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
  568. }
  569.  
  570. void loop() {
  571.   webSocket.loop();
  572.  
  573.   if(isConnected) {
  574.       uint64_t now = millis();
  575.      
  576.       // Send heartbeat in order to avoid disconnections during ISP resetting IPs over night. Thanks @MacSass
  577.       if((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) {
  578.           heartbeatTimestamp = now;
  579.           webSocket.sendTXT("H");          
  580.       }
  581.   }  
  582. }
  583.  
  584. // 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
  585. // so it will reflect on Alexa app.
  586. // eg: setPowerStateOnServer("deviceid", "ON")
  587. void setPowerStateOnServer(String deviceId, String value) {
  588.   DynamicJsonBuffer jsonBuffer;
  589.   JsonObject& root = jsonBuffer.createObject();
  590.   root["deviceId"] = deviceId;
  591.   root["action"] = "setPowerState";
  592.   root["value"] = value;
  593.   StreamString databuf;
  594.   root.printTo(databuf);
  595.  
  596.   webSocket.sendTXT(databuf);
  597. }
  598.  
  599. //eg: setPowerStateOnServer("deviceid", "CELSIUS", "25.0")
  600. void setTargetTemperatureOnServer(String deviceId, String value, String scale) {
  601.   DynamicJsonBuffer jsonBuffer;
  602.   JsonObject& root = jsonBuffer.createObject();
  603.   root["action"] = "SetTargetTemperature";
  604.   root["deviceId"] = deviceId;
  605.  
  606.   JsonObject& valueObj = root.createNestedObject("value");
  607.   JsonObject& targetSetpoint = valueObj.createNestedObject("targetSetpoint");
  608.   targetSetpoint["value"] = value;
  609.   targetSetpoint["scale"] = scale;
  610.    
  611.   StreamString databuf;
  612.   root.printTo(databuf);
  613.  
  614.   webSocket.sendTXT(databuf);
  615. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement