· 5 years ago · May 21, 2020, 11:22 PM
1/*
2 Version 0.4 - April 26 2019
3*/
4
5#include <Arduino.h>
6#include <ESP8266WiFi.h>
7#include <ESP8266WiFiMulti.h>
8#include <WebSocketsClient.h> // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries
9#include <ArduinoJson.h> // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries (use the correct version)
10#include <StreamString.h>
11
12int device_1 = 5;
13int device_2 = 4;
14int device_3 = 0;
15int device_4 = 2;
16
17ESP8266WiFiMulti WiFiMulti;
18WebSocketsClient webSocket;
19WiFiClient client;
20
21#define MyApiKey "f9414fff-5e88-4516-a4e6-4013274f63c8" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard
22#define MySSID "Fios-LYHJC" // TODO: Change to your Wifi network SSID
23#define MyWifiPassword "tin8376few2538hope" // TODO: Change to your Wifi network password
24
25#define HEARTBEAT_INTERVAL 300000 // 5 Minutes
26
27uint64_t heartbeatTimestamp = 0;
28bool isConnected = false;
29
30
31// deviceId is the ID assgined to your smart-home-device in sinric.com dashboard. Copy it from dashboard and paste it here
32
33void turnOn(String deviceId) {
34 if (deviceId == "5ec6e4c6f2f71462da8a2cf2") // Device ID of first device
35 {
36 Serial.print("Turn on device id: ");
37 Serial.println(deviceId);
38 digitalWrite(device_1, LOW);
39 }
40 else if (deviceId == "5ec6e4e0f2f71462da8a2cf8") // Device ID of second device
41 {
42 Serial.print("Turn on device id: ");
43 Serial.println(deviceId);
44 digitalWrite(device_2, LOW);
45 }
46 else if (deviceId == "5ec6e4eaf2f71462da8a2cfc") // Device ID of third device
47 {
48 Serial.print("Turn on device id: ");
49 Serial.println(deviceId);
50 digitalWrite(device_3, LOW);
51 }
52 else if (deviceId == "5ec6e4f8f2f71462da8a2d00") // Device ID of fourth device
53 {
54 Serial.print("Turn on device id: ");
55 Serial.println(deviceId);
56 digitalWrite(device_4, LOW);
57 }
58 else {
59 Serial.print("Turn on for unknown device id: ");
60 Serial.println(deviceId);
61 }
62}
63
64void turnOff(String deviceId) {
65 if (deviceId == "5ec6e4c6f2f71462da8a2cf2") // Device ID of first device
66 {
67 Serial.print("Turn off Device ID: ");
68 Serial.println(deviceId);
69 digitalWrite(device_1, HIGH);
70 }
71 else if (deviceId == "5ec6e4e0f2f71462da8a2cf8") // Device ID of second device
72 {
73 Serial.print("Turn off Device ID: ");
74 Serial.println(deviceId);
75 digitalWrite(device_2, HIGH);
76 }
77 else if (deviceId == "5ec6e4eaf2f71462da8a2cfc") // Device ID of third device
78 {
79 Serial.print("Turn on device id: ");
80 Serial.println(deviceId);
81 digitalWrite(device_3, HIGH);
82 }
83 else if (deviceId == "5ec6e4f8f2f71462da8a2d00") // Device ID of fourth device
84 {
85 Serial.print("Turn on device id: ");
86 Serial.println(deviceId);
87 digitalWrite(device_4, HIGH);
88 }
89 else {
90 Serial.print("Turn off for unknown device id: ");
91 Serial.println(deviceId);
92 }
93}
94
95void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
96 switch(type) {
97 case WStype_DISCONNECTED:
98 isConnected = false;
99 Serial.printf("[WSc] Webservice disconnected from sinric.com!\n");
100 break;
101 case WStype_CONNECTED: {
102 isConnected = true;
103 Serial.printf("[WSc] Service connected to sinric.com at url: %s\n", payload);
104 Serial.printf("Waiting for commands from sinric.com ...\n");
105 }
106 break;
107 case WStype_TEXT: {
108 Serial.printf("[WSc] get text: %s\n", payload);
109 // Example payloads
110
111 // For Switch or Light device types
112 // {"deviceId": xxxx, "action": "setPowerState", value: "ON"} // https://developer.amazon.com/docs/device-apis/alexa-powercontroller.html
113
114 // For Light device type
115 // Look at the light example in github
116#if ARDUINOJSON_VERSION_MAJOR == 5
117 DynamicJsonBuffer jsonBuffer;
118 JsonObject& json = jsonBuffer.parseObject((char*)payload);
119#endif
120#if ARDUINOJSON_VERSION_MAJOR == 6
121 DynamicJsonDocument json(1024);
122 deserializeJson(json, (char*) payload);
123#endif
124 String deviceId = json ["deviceId"];
125 String action = json ["action"];
126
127 if(action == "setPowerState") { // Switch or Light
128 String value = json ["value"];
129 if(value == "ON") {
130 turnOn(deviceId);
131 } else {
132 turnOff(deviceId);
133 }
134 }
135 else if (action == "SetTargetTemperature") {
136 String deviceId = json ["deviceId"];
137 String action = json ["action"];
138 String value = json ["value"];
139 }
140 else if (action == "test") {
141 Serial.println("[WSc] received test command from sinric.com");
142 }
143 }
144 break;
145 case WStype_BIN:
146 Serial.printf("[WSc] get binary length: %u\n", length);
147 break;
148 }
149}
150
151void setup() {
152 Serial.begin(115200);
153 pinMode (device_1, OUTPUT);
154 pinMode (device_2, OUTPUT);
155 pinMode (device_3, OUTPUT);
156 pinMode (device_4, OUTPUT);
157
158 WiFiMulti.addAP(MySSID, MyWifiPassword);
159 Serial.println();
160 Serial.print("Connecting to Wifi: ");
161 Serial.println(MySSID);
162
163 // Waiting for Wifi connect
164 while(WiFiMulti.run() != WL_CONNECTED) {
165 delay(500);
166 Serial.print(".");
167 }
168 if(WiFiMulti.run() == WL_CONNECTED) {
169 Serial.println("");
170 Serial.print("WiFi connected. ");
171 Serial.print("IP address: ");
172 Serial.println(WiFi.localIP());
173 }
174
175 // server address, port and URL
176 webSocket.begin("iot.sinric.com", 80, "/");
177
178 // event handler
179 webSocket.onEvent(webSocketEvent);
180 webSocket.setAuthorization("apikey", MyApiKey);
181
182 // try again every 5000ms if connection has failed
183 webSocket.setReconnectInterval(5000); // If you see 'class WebSocketsClient' has no member named 'setReconnectInterval' error update arduinoWebSockets
184}
185
186void loop() {
187 webSocket.loop();
188
189 if(isConnected) {
190 uint64_t now = millis();
191
192 // Send heartbeat in order to avoid disconnections during ISP resetting IPs over night. Thanks @MacSass
193 if((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) {
194 heartbeatTimestamp = now;
195 webSocket.sendTXT("H");
196 }
197 }
198}
199
200// If you want a push button: https://github.com/kakopappa/sinric/blob/master/arduino_examples/switch_with_push_button.ino