· 5 years ago · Dec 25, 2020, 04:58 PM
1/*
2Alexa controlled plant light
3Besed on Sinric example https://sinric.com/
4https://github.com/kakopappa/sinric
5*/
6
7#include <Arduino.h>
8#include <ESP8266WiFi.h>
9#include <ESP8266WiFiMulti.h>
10#include <WebSocketsClient.h> // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries
11#include <ArduinoJson.h> // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries (use the correct version)
12#include <StreamString.h>
13
14ESP8266WiFiMulti WiFiMulti;
15WebSocketsClient webSocket;
16WiFiClient client;
17
18#define MyApiKey "Set your API KEY" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard -SET YOURS
19#define MySSID "YOURSSID" // TODO: Change to your Wifi network SSID -SET YOURS
20#define MyWifiPassword "YOURPASS" // TODO: Change to your Wifi network password-SET YOURS
21
22#define HEARTBEAT_INTERVAL 300000 // 5 Minutes
23
24
25#include <FastLED.h>
26#define LED_PIN 4 // GPIO 4. This is D2 on Wemos.
27#define NUM_LEDS 3 // Three LEDs on WS8211 led strip
28CRGB leds[NUM_LEDS];
29
30uint64_t heartbeatTimestamp = 0;
31bool isConnected = false;
32
33// deviceId is the ID assgined to your smart-home-device in sinric.com dashboard. Copy it from dashboard and paste it here
34void turnOn(String deviceId) {
35 if (deviceId == "5axxxxxxxxxxxxxxxxxxx") // Device ID of first device - SET YOURS HERE
36 {
37 Serial.print("Turn on device id: ");
38 Serial.println(deviceId);
39 leds[0] = CRGB(255, 0, 100); //This is the optimal light for plants
40 leds[1] = CRGB(255, 0, 100); //This is the optimal light for plants
41 leds[2] = CRGB(255, 0, 100); //This is the optimal light for plants
42 FastLED.show();
43 }
44}
45
46void turnOff(String deviceId) {
47 if (deviceId == "5axxxxxxxxxxxxxxxxxxx") // Device ID of first device -SET YOURS
48 {
49 Serial.print("Turn off Device ID: ");
50 Serial.println(deviceId);
51 leds[0] = CRGB(0, 0, 0); //Turn Off
52 leds[1] = CRGB(0, 0, 0); //Turn Off
53 leds[2] = CRGB(0, 0, 0); //Turn Off
54 FastLED.show();
55 }
56}
57
58void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
59 switch (type) {
60 case WStype_DISCONNECTED:
61 isConnected = false;
62 Serial.printf("[WSc] Webservice disconnected from sinric.com!\n");
63 break;
64 case WStype_CONNECTED: {
65 isConnected = true;
66 Serial.printf("[WSc] Service connected to sinric.com at url: %s\n", payload);
67 Serial.printf("Waiting for commands from sinric.com ...\n");
68 }
69 break;
70 case WStype_TEXT: {
71 Serial.printf("[WSc] get text: %s\n", payload);
72
73#if ARDUINOJSON_VERSION_MAJOR == 5
74 DynamicJsonBuffer jsonBuffer;
75 JsonObject& json = jsonBuffer.parseObject((char*)payload);
76#endif
77#if ARDUINOJSON_VERSION_MAJOR == 6
78 DynamicJsonDocument json(1024);
79 deserializeJson(json, (char*) payload);
80#endif
81 String deviceId = json ["deviceId"];
82 String action = json ["action"];
83
84 if (action == "setPowerState") { // Switch or Light
85 String value = json ["value"];
86 if (value == "ON") {
87 turnOn(deviceId);
88 } else {
89 turnOff(deviceId);
90 }
91 }
92 else if (action == "SetTargetTemperature") {
93 String deviceId = json ["deviceId"];
94 String action = json ["action"];
95 String value = json ["value"];
96 }
97 else if (action == "test") {
98 Serial.println("[WSc] received test command from sinric.com");
99 }
100 }
101 break;
102 case WStype_BIN:
103 Serial.printf("[WSc] get binary length: %u\n", length);
104 break;
105 }
106}
107
108void setup() {
109 Serial.begin(115200);
110 FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
111 leds[0] = CRGB(0, 100, 0 );
112 FastLED.show();
113 WiFiMulti.addAP(MySSID, MyWifiPassword);
114 Serial.println();
115 Serial.print("Connecting to Wifi: ");
116 Serial.println(MySSID);
117 leds[1] = CRGB(0, 100, 0 );
118 FastLED.show();
119 delay(1000);
120 // Waiting for Wifi connect
121 while (WiFiMulti.run() != WL_CONNECTED) {
122 delay(500);
123 Serial.print(".");
124 }
125 if (WiFiMulti.run() == WL_CONNECTED) {
126 Serial.println("");
127 Serial.print("WiFi connected. ");
128 Serial.print("IP address: ");
129 Serial.println(WiFi.localIP());
130 }
131 leds[1] = CRGB(0, 100, 0 );
132 FastLED.show();
133 // server address, port and URL
134 webSocket.begin("iot.sinric.com", 80, "/");
135
136 // event handler
137 webSocket.onEvent(webSocketEvent);
138 webSocket.setAuthorization("apikey", MyApiKey);
139
140 // try again every 5000ms if connection has failed
141 webSocket.setReconnectInterval(5000); // If you see 'class WebSocketsClient' has no member named 'setReconnectInterval' error update arduinoWebSockets
142// Turn all LEDs off on startup
143 leds[0] = CRGB(0, 0, 0 );
144 leds[1] = CRGB(0, 0, 0 );
145 leds[2] = CRGB(0, 0, 0 );
146 FastLED.show();
147}
148
149void loop() {
150 webSocket.loop();
151
152 if (isConnected) {
153 uint64_t now = millis();
154
155 // Send heartbeat in order to avoid disconnections during ISP resetting IPs over night. Thanks @MacSass
156 if ((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) {
157 heartbeatTimestamp = now;
158 webSocket.sendTXT("H");
159 }
160 }
161}