-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnectedCustomLamp.ino
347 lines (296 loc) · 9.64 KB
/
ConnectedCustomLamp.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#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 (use the correct version)
#include <StreamString.h>
#include <FastLED.h>
#include "Config.h"
ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;
WiFiClient client;
#define HEARTBEAT_INTERVAL 300000 // 5 Minutes
uint64_t heartbeatTimestamp = 0;
bool isConnected = false;
int leds_number = 13;
CRGB leds[13];
float brightness = 1.0;
String color = "white";
boolean Multicolor = false;
boolean Pacours = false;
int MulticolorColor = 0;
void SetColorOnOneLED(int i, int R, int G, int B){
R = R * brightness;
G = G * brightness;
B = B * brightness;
leds[i] = CRGB ( R, G, B);
}
void SetAColorAllTheLEDSripe(int R, int G, int B){
for(int i=0;i<leds_number;i++){
SetColorOnOneLED(i, R, G, B);
}
}
void ChangeColorLEDSripe(String color){
if(color == "white"){
SetAColorAllTheLEDSripe(255, 255, 255);
}
else if(color == "red"){
SetAColorAllTheLEDSripe(255, 0, 0);
}
else if(color == "blue"){
SetAColorAllTheLEDSripe(0, 0, 255);
}
else if(color == "green"){
SetAColorAllTheLEDSripe( 0, 255, 0);
}
else if(color == "orange"){
SetAColorAllTheLEDSripe(255,69,0);
}
else if(color == "skyBlue"){
SetAColorAllTheLEDSripe(135,206,235);
}
FastLED.show();
}
void OneLEDCircuit(){
SwitchOffLights();
if((Pacours)){
for(int i=1;i<leds_number-1;i++){
SetColorOnOneLED(i-1, 0, 0, 0);
SetColorOnOneLED(i, 255, 0, 0);
SetColorOnOneLED(i+1, 0, 0, 255);
FastLED.show();
delay(35);
}
for(int i=1;i<leds_number-1;i++){
SetColorOnOneLED(i-1, 0, 0, 0);
SetColorOnOneLED(i, 0, 255, 0);
SetColorOnOneLED(i+1, 0, 0, 255);
FastLED.show();
delay(35);
}
}
}
void MulticolorChange(){
if(MulticolorColor == 0){
ChangeColorLEDSripe("red");
}
else if(MulticolorColor == 1){
ChangeColorLEDSripe("orange");
}
else if(MulticolorColor == 2){
ChangeColorLEDSripe("blue");
}else{
ChangeColorLEDSripe("green");
MulticolorColor = -1;
}
MulticolorColor++;
delay(5000);
}
void SwitchOffLights(){
for(int i=0;i<leds_number;i++){
leds[i] = CRGB ( 0, 0, 0);
}
FastLED.show();
}
// deviceId is the ID assgined to your smart-home-device in sinric.com dashboard. Copy it from dashboard and paste it on the config file
void turnOn(String deviceId) {
if (deviceId == FirstDeviceID)
{
Serial.print("Turn on device Esp: ");
Serial.println(deviceId);
Pacours = false; // We turn off parcours
ChangeColorLEDSripe("white");
}
else if (deviceId == SecondDeviceID || (Multicolor))
{
Pacours = false; // We turn off parcours
Multicolor = true;
MulticolorColor = 0;
}else if(deviceId == ThirdDeviceID){
Serial.print("Turn on device Esp: ");
Serial.println(deviceId);
Pacours = true;
}
else {
Serial.print("Turn on for unknown device id: ");
Serial.println(deviceId);
}
}
void turnOff(String deviceId) {
if (deviceId == FirstDeviceID)
{
Serial.print("Turn off Device Esp: ");
Serial.println(deviceId);
Multicolor = false; //Pour prevenir l'usage du mauvais nom
Pacours = false; // We turn off parcours
SwitchOffLights();
}
else if (deviceId == SecondDeviceID)
{
Multicolor = false;
Pacours = false; // We turn off parcours
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
SwitchOffLights();
}
else if(deviceId == ThirdDeviceID){
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
Multicolor = false; //Pour prevenir l'usage du mauvais nom
Pacours = false; // We turn off parcours
SwitchOffLights();
}
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" || (action == "action.devices.commands.OnOff")) { // Switch or Light
String value = json ["value"];
Serial.println("value: " + value);
if(value == "ON" || value == "OFF"){ // On / Off with Amazon Alexa
if(value == "ON") {
turnOn(deviceId);
} else {
turnOff(deviceId);
}
}
else{ // On / Off with Google Assistant
bool value = json["value"]["on"];
if(value){
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");
}
else if((action == "SetColor") || (action == "SetColorTemperature")|| (action == "action.devices.commands.ColorAbsolute")){
if(action == "SetColor"){
int hue = json["value"]["hue"];
if(hue == 0){ color = "red"; }
else if(hue == 39){ color = "orange"; }
else if(hue == 120){ color = "green"; }
else if(hue == 197){ color = "skyBlue"; }
else if(hue == 240){ color = "blue"; }
else if(hue == 300){ color = "violet"; }
Serial.println("hue: "+ String(hue));
}else if(action == "SetColorTemperature"){
int value = json ["value"];
if(value = 4000){
color = "white";
}
}
else{
String couleur = json["value"]["color"]["name"];
if(couleur == "rouge"){
color = "red";
}
else if(couleur == "bleu"){
color = "blue";
}
else if(couleur == "orange"){
color = "orange";
}
else if(couleur == "blanc"){
color = "white";
}
else if((couleur == "verre") || (couleur = "vert")){ //vert est écrit de cette manière dans le code de Google
// Ajout d'un patch au cas où ils fixeraient le bug
color = "green";
}
}
Serial.println("color: " + color);
ChangeColorLEDSripe(color);
}else if((action == "action.devices.commands.BrightnessAbsolute") || (action == "SetBrightness")){
if(action == "action.devices.commands.BrightnessAbsolute"){
brightness = float(int(json["value"]["brightness"])) / 100;
}else{
brightness = float(int(json["value"])) / 100;
}
if((!Multicolor) && (!Pacours)){
ChangeColorLEDSripe(color);
}
}
}
break;
case WStype_BIN:
Serial.printf("[WSc] get binary length: %u\n", length);
break;
}
}
void setup() {
pinMode( D6, OUTPUT);
FastLED.addLeds<WS2812B, D6, GRB>(leds, leds_number);
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());
}
// 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
}
void loop() {
webSocket.loop();
if(Multicolor){
MulticolorChange();
}
if(Pacours){
OneLEDCircuit();
}
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 want a push button: https://github.com/kakopappa/sinric/blob/master/arduino_examples/switch_with_push_button.ino