· 6 years ago · Oct 23, 2019, 03:38 PM
1#include <Arduino.h>
2#include <ESP8266WiFi.h>
3#include <ESP8266WiFiMulti.h>
4#include <WebSocketsClient.h> // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries
5#include <ArduinoJson.h> // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries (use the correct version)
6#include <StreamString.h>
7
8
9ESP8266WiFiMulti WiFiMulti;
10WebSocketsClient webSocket;
11WiFiClient client;
12
13#define MyApiKey "" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard
14
15#define HEARTBEAT_INTERVAL 300000 // 5 Minutes
16
17uint64_t heartbeatTimestamp = 0;
18bool isConnected = false;
19
20#define MyApiKey "c8978fd8-2c8c-4c7f-87f6-438ec5e037ca" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard
21#define MySSID "NaBr" // TODO: Change to your Wifi network SSID
22#define MyWifiPassword "$3.14159265$" // TODO: Change to your Wifi network password
23
24
25//Devices. तैले जोड्ने भनेको जाति छ.
26//tero setup ko chij haru kata kata connect garne commented cha. whitelight relay ko laagi d3 connect gar
27
28
29#define whiteLight 0 //D3
30#define secondWhiteLight 2 //D4
31#define RGB 4 //D2
32#define yellowLight 5 //D1
33#define chargingPort 12 //D6
34#define router 13 //D7
35#define terminator 14 //D5
36#define antiChor 15 //D8
37
38
39void turnOn(String deviceId) {
40
41 //this is where you add commands
42 if (deviceId == "5db053ffa0866720351ed93d"){ //whiteLight
43 Serial.print("WhiteLight turned on . Device Id is");
44 Serial.println(deviceId);
45 digitalWrite(whiteLight,LOW);
46
47
48 }
49 else if(deviceId =="5db054b1a0866720351ed96b"){
50 Serial.print("second whiteLight turned on. Device Id is");
51 Serial.println(deviceId);
52 digitalWrite(secondWhiteLight,LOW);
53
54 }
55 else if(deviceId == "5db054cca0866720351ed974"){
56 Serial.print("yellow light turned on.Device Id is");
57 Serial.println(deviceId);
58 digitalWrite(yellowLight,LOW);
59
60 }
61 else if(deviceId == "5db054eba0866720351ed97f"){
62 Serial.print("Charging Port light turned on.Device Id is");
63 Serial.println(deviceId);
64 digitalWrite(chargingPort,LOW);
65 }
66 else if(deviceId == "5db0551ea0866720351ed98b"){
67 Serial.print("router turned on.Device Id is");
68 Serial.println(deviceId);
69 digitalWrite(router,LOW);
70 }
71 else if(deviceId =="5db06d15a0866720351ee08a"){
72 Serial.print("terminator turned on.Device Id is");
73 Serial.println(deviceId);
74 digitalWrite(terminator,LOW);
75 }
76 else{
77 Serial.print("No such device. Would you mind adding the devices.");
78 Serial.println(deviceId);
79
80 }
81
82
83}
84
85void turnOff(String deviceId) {
86 if (deviceId == "5db053ffa0866720351ed93d"){ //whiteLight
87 Serial.print("WhiteLight turned off . Device Id is");
88 Serial.println(deviceId);
89 digitalWrite(whiteLight,HIGH);
90
91
92 }else if(deviceId =="5db054b1a0866720351ed96b"){
93 Serial.print("second whiteLight turned off. Device Id is");
94 Serial.println(deviceId);
95
96 digitalWrite(secondWhiteLight,HIGH);
97
98 }else if(deviceId == "5db054cca0866720351ed974"){
99 Serial.print("yellow light turned off.Device Id is");
100 Serial.println(deviceId);
101 digitalWrite(yellowLight,HIGH);
102
103 }else if(deviceId == "5db054eba0866720351ed97f"){
104 Serial.print("Charging Port light turned off.Device Id is");
105 Serial.println(deviceId);
106 digitalWrite(chargingPort,HIGH);
107 }
108 else if(deviceId == "5db0551ea0866720351ed98b"){
109 Serial.print("router turned off.Device Id is");
110 Serial.println(deviceId);
111 digitalWrite(router,HIGH);
112 }
113 else if(deviceId =="5db06d15a0866720351ee08a"){
114 Serial.print("terminator turned off.Device Id is");
115 Serial.println(deviceId);
116 digitalWrite(terminator,HIGH);
117 }
118 else{
119 Serial.print("No such device. Would you mind adding the devices.");
120 Serial.println(deviceId);
121
122 }
123}
124
125void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
126 switch(type) {
127 case WStype_DISCONNECTED:
128 isConnected = false;
129 Serial.printf("[WSc] Webservice disconnected from sinric.com!\n");
130 break;
131 case WStype_CONNECTED: {
132 isConnected = true;
133 Serial.printf("[WSc] Service connected to sinric.com at url: %s\n", payload);
134 Serial.printf("Waiting for commands from sinric.com ...\n");
135 }
136 break;
137 case WStype_TEXT: {
138 Serial.printf("[WSc] get text: %s\n", payload);
139 // Example payloads
140
141 // For Switch or Light device types
142 // {"deviceId": xxxx, "action": "setPowerState", value: "ON"} // https://developer.amazon.com/docs/device-apis/alexa-powercontroller.html
143
144 // For Light device type
145 // Look at the light example in github
146
147 DynamicJsonBuffer jsonBuffer;
148 JsonObject& json = jsonBuffer.parseObject((char*)payload);
149 String deviceId = json ["deviceId"];
150 String action = json ["action"];
151
152 if(action == "setPowerState") { // Switch or Light
153 String value = json ["value"];
154 if(value == "ON") {
155 turnOn(deviceId);
156 } else {
157 turnOff(deviceId);
158 }
159 }
160 else if (action == "SetTargetTemperature") {
161 String deviceId = json ["deviceId"];
162 String action = json ["action"];
163 String value = json ["value"];
164 }
165 else if (action == "test") {
166 Serial.println("[WSc] received test command from sinric.com");
167 }
168 }
169 break;
170 case WStype_BIN:
171 Serial.printf("[WSc] get binary length: %u\n", length);
172 break;
173 }
174}
175
176void setup() {
177 Serial.begin(115200);
178 pinMode(LED_BUILTIN,OUTPUT);
179 digitalWrite(LED_BUILTIN,HIGH);
180
181 //adding devices
182 pinMode(whiteLight,OUTPUT);
183 pinMode(secondWhiteLight,OUTPUT);
184 pinMode(RGB,OUTPUT);
185 pinMode(yellowLight,OUTPUT);
186 pinMode(chargingPort,OUTPUT);
187 pinMode(router,OUTPUT);
188 pinMode(terminator,OUTPUT);
189 pinMode(antiChor,OUTPUT);
190
191
192 //My nodemcu has reverse pin problem.i.e HIGH ko laagi LOW use garna parcha and LOW ko laagi HIGH use garna parcha. so by default OFF
193digitalWrite(whiteLight,HIGH);
194digitalWrite(secondWhiteLight,HIGH);
195digitalWrite(RGB,HIGH);
196digitalWrite(yellowLight,HIGH);
197digitalWrite(chargingPort,HIGH);
198digitalWrite(router,HIGH);
199digitalWrite(terminator,HIGH);
200digitalWrite(antiChor,HIGH);
201
202
203
204
205 WiFiMulti.addAP(MySSID, MyWifiPassword);
206 Serial.println();
207 Serial.print("Connecting to Wifi: ");
208 Serial.println(MySSID);
209
210 // Waiting for Wifi connect
211 while(WiFiMulti.run() != WL_CONNECTED) {
212 delay(500);
213 Serial.print(".");
214 }
215 if(WiFiMulti.run() == WL_CONNECTED) {
216 Serial.println("");
217 Serial.print("WiFi connected. ");
218 Serial.print("IP address: ");
219 Serial.println(WiFi.localIP());
220 }
221
222 // server address, port and URL
223 webSocket.begin("iot.sinric.com", 80, "/");
224
225 // event handler
226 webSocket.onEvent(webSocketEvent);
227 webSocket.setAuthorization("apikey", MyApiKey);
228
229 // try again every 5000ms if connection has failed
230 webSocket.setReconnectInterval(5000); // If you see 'class WebSocketsClient' has no member named 'setReconnectInterval' error update arduinoWebSockets
231}
232
233void loop() {
234 webSocket.loop();
235
236 if(isConnected) {
237 uint64_t now = millis();
238
239 // Send heartbeat in order to avoid disconnections during ISP resetting IPs over night. Thanks @MacSass
240 if((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) {
241 heartbeatTimestamp = now;
242 webSocket.sendTXT("H");
243 }
244 }
245}