· 6 years ago · Nov 01, 2019, 05:18 PM
1#include <Arduino.h>
2#include <IRremote.h>
3#include <ESP8266WiFi.h>
4#include <ArduinoJson.h>
5#include <SimpleTimer.h>
6#include <StreamString.h>
7#include <ESP8266WiFiMulti.h>
8#include <WebSocketsClient.h>
9#include <BlynkSimpleEsp8266.h>
10
11IRsend irsend;
12WiFiClient client;
13SimpleTimer timer;
14ESP8266WiFiMulti WiFiMulti;
15WebSocketsClient webSocket;
16
17#define BLYNK_GREEN "#23C48E"
18#define BLYNK_BLUE "#04C0F8"
19#define BLYNK_YELLOW "#ED9D00"
20#define BLYNK_RED "#D3435C"
21#define BLYNK_DARK_BLUE "#5F7CD8"
22
23#define auth "xxxx" // Blynk Authentication Key
24#define api "xxxx" // Sinric API Key
25#define ssid "xxxx" // WiFi SSID
26#define pass "xxxx" // WiFi Password
27
28#define blynkserialprintvport "V0"
29#define hexcolorLampuUtama "V1"
30#define hexcolorLampuKerja "V2"
31#define hexcolorLampuTidur "V2"
32
33int khz = 38;
34int LampuUtama = 2;
35int LampuKerja = 16;
36int LampuTidur = 16;
37
38unsigned int rawACON[] = {9000, 4500, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 39416, 9000, 2210, 560};
39unsigned int rawACAUTO[] = {9000, 4500, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 39416, 9000, 2210, 560};
40unsigned int rawACLOW[] = {9000, 4500, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 39416, 9000, 2210, 560};
41unsigned int rawACMAX[] = {9000, 4500, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 39416, 9000, 2210, 560};
42unsigned int rawACOFF[] = {9000, 4500, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 39416, 9000, 2210, 560};
43unsigned int rawLEDON[] = {9000, 4500, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 39416, 9000, 2210, 560};
44unsigned int rawLEDFADE1[] = {9000, 4500, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 39416, 9000, 2210, 560};
45unsigned int rawLEDFADE2[] = {9000, 4500, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 39416, 9000, 2210, 560};
46unsigned int rawLEDWHITE[] = {9000, 4500, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 39416, 9000, 2210, 560};
47unsigned int rawLEDOFF[] = {9000, 4500, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 39416, 9000, 2210, 560};
48unsigned int rawDOOR[] = {9000, 4500, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 39416, 9000, 2210, 560};
49
50#define HEARTBEAT_INTERVAL 300000
51#define BLYNK_PRINT Serial
52
53uint64_t heartbeatTimestamp = 0;
54bool isConnected = false;
55
56void turnOn(String deviceId) { // Start Turn ON -----------------------------------
57 if (deviceId == "xxxx")
58 {
59 Serial.println("? Tidur - ? Utama - ?");
60 digitalWrite(LampuUtama, LOW);
61 }
62 else if (deviceId == "xxxx")
63 {
64 Serial.println("? Tidur - ? Tidur - ?");
65 digitalWrite(LampuTidur, HIGH);
66 }
67 else if (deviceId == "xxxx")
68 {
69 Serial.println("? Tidur - ? StripRGB - ?");
70 irsend.sendRaw(rawLEDON, sizeof(rawLEDON) / sizeof(rawLEDON[0]), khz);
71 }
72 else if (deviceId == "xxxx")
73 {
74 Serial.println("? Tidur - AC - ?");
75 irsend.sendRaw(rawACON, sizeof(rawACON) / sizeof(rawACON[0]), khz);
76 }
77 else {
78 Serial.print("Turn on for unknown device id: ");
79 Serial.println(deviceId);
80 }
81} // End Turn ON ------------------------------------------------------------------
82
83void turnOff(String deviceId) { // Start Turn OFF ---------------------------------
84 if (deviceId == "xxxx")
85 {
86 Serial.println("? Tidur - ? Utama - ?");
87 digitalWrite(LampuUtama, HIGH);
88 }
89 else if (deviceId == "xxxx")
90 {
91 Serial.println("? Tidur - ? Tidur - ?");
92 digitalWrite(LampuTidur, LOW);
93 }
94 else if (deviceId == "xxxx")
95 {
96 Serial.println("? Tidur - ? StripRGB - ?");
97 irsend.sendRaw(rawLEDOFF, sizeof(rawLEDOFF) / sizeof(rawLEDOFF[0]), khz);
98 }
99 else if (deviceId == "xxxx")
100 {
101 Serial.println("? Tidur - AC - ?");
102 irsend.sendRaw(rawACOFF, sizeof(rawACOFF) / sizeof(rawACOFF[0]), khz);
103 }
104 else {
105 Serial.print("Turn off for unknown device id: ");
106 Serial.println(deviceId);
107 }
108} // End Turn OFF -----------------------------------------------------------------
109
110void brightness(String deviceId, int percent) { // Start Brightness ---------------
111 if (deviceId == "xxxx")
112 {
113 analogWrite(LampuUtama, 1024-percent*10+24);
114 Serial.print("? Tidur - ? Utama - ? ");
115 Serial.print(percent);
116 Serial.println("%");
117 }
118 else if (deviceId == "xxxx")
119 {
120 analogWrite(LampuTidur, percent*10+24);
121 Serial.print("? Tidur - ? Tidur - ? ");
122 Serial.print(percent);
123 Serial.println("%");
124 }
125 else {
126 Serial.print("Turn off for unknown device id: ");
127 Serial.println(deviceId);
128 }
129} // End Brightness ---------------------------------------------------------------
130
131void colour(String deviceId, int spectrumRGB) { // Start RGB ----------------------
132 String strRGB = String(spectrumRGB, HEX);
133
134 while (strRGB.length() < 6) strRGB = "0" + strRGB;
135 strRGB.toUpperCase();
136 strRGB = "#" + strRGB ;
137
138 byte r = spectrumRGB >> 16;
139 byte g = spectrumRGB >> 8 & 0xFF;
140 byte b = spectrumRGB & 0xFF;
141
142 if (deviceId == "5dab4737a0866720351d51c7")
143 {
144 Serial.print("? Tidur - ? Utama - ? ");
145 Serial.print(spectrumRGB);
146 Serial.println(strRGB);
147 Blynk.setProperty(hexcolorLampuUtama, "color", strRGB);
148 }
149 else if (deviceId == "5dac83e0a0866720351db123")
150 {
151 Serial.print("? Tidur - ? Tidur - ? ");
152 Serial.print(spectrumRGB);
153 Serial.println(strRGB);
154 Blynk.setProperty(hexcolorLampuTidur, "color", strRGB);
155 }
156 else {
157 Serial.print("Turn off for unknown device id: ");
158 Serial.println(deviceId);
159 }
160} // End RGB ----------------------------------------------------------------------
161
162void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) { // Event ---
163 switch(type) {
164 case WStype_DISCONNECTED:
165 isConnected = false;
166 Serial.println("");
167 Serial.println("❌ Layanan terputus");
168 Serial.println("");
169 break;
170 case WStype_CONNECTED: {
171 isConnected = true;
172 Serial.printf("✔ Layanan terkoneksi ? url: %s\n", payload);
173 Serial.printf("✔ Menunggu perintah ...\n");
174 }
175 break;
176 case WStype_TEXT: {
177 // Serial.printf("[RAW]: %s\n", payload);
178
179 DynamicJsonBuffer jsonBuffer;
180 JsonObject& json = jsonBuffer.parseObject((char*)payload);
181 String deviceId = json ["deviceId"];
182 String action = json ["action"];
183
184 if(action == "action.devices.commands.OnOff") {
185 String value = json ["value"]["on"];
186
187 if(value == "true") {
188 turnOn(deviceId);
189 } else {
190 turnOff(deviceId);
191 }
192 }
193 else if(action == "action.devices.commands.BrightnessAbsolute") {
194 int percent = json ["value"]["brightness"];
195
196 brightness(deviceId, percent);
197 }
198 else if(action == "action.devices.commands.ColorAbsolute") {
199 String color = json ["value"]["color"]["name"];
200 int spectrumRGB = json ["value"]["color"]["spectrumRGB"];
201
202 colour(deviceId, spectrumRGB);
203 }
204 else if(action == "test") {
205 Serial.println("Perintah Test diterima");
206 }
207 }
208 break;
209 case WStype_BIN:
210 Serial.printf("[RAW] get binary length: %u\n", length);
211 break;
212 }
213} // End Event --------------------------------------------------------------------
214
215void sentserial() { // Start Send Serial to V0 Blynk ------------------------------
216 String content = "";
217 char character;
218 while(Serial.available()) {
219 character = Serial.read();
220 content.concat(character);
221 }
222 if (content != "") {
223 Blynk.virtualWrite (V0, content);
224 }
225} // End Send Serial to V0 Blynk --------------------------------------------------
226
227void setup() { // Start setup -----------------------------------------------------
228 Serial.begin(115200);
229
230 Blynk.begin(auth, ssid, pass); // Connect to Blynk
231 timer.setInterval(100, sentserial); // Send serial to V0 Blynk with delay
232
233 WiFiMulti.addAP(ssid, pass);
234 Serial.println();
235 Serial.print("⏳ Menghubungkan ? ");
236 Serial.println(ssid);
237
238 // Waiting for Wifi connect
239 while(WiFiMulti.run() != WL_CONNECTED) {
240 delay(500);
241 Serial.print(".");
242 }
243 if(WiFiMulti.run() == WL_CONNECTED) {
244 Serial.println("");
245 Serial.print("✔ Terhubung ");
246 Serial.print("IP");
247 Serial.println(WiFi.localIP());
248 }
249
250 // server address, port and URL
251 webSocket.begin("iot.sinric.com", 80, "/"); //"iot.sinric.com", 80
252
253 // event handler
254 webSocket.onEvent(webSocketEvent);
255 webSocket.setAuthorization("apikey", api);
256
257 // try again every 5000ms if connection has failed
258 webSocket.setReconnectInterval(5000); // If you see 'class WebSocketsClient' has no member named 'setReconnectInterval' error update arduinoWebSockets
259} // End setup --------------------------------------------------------------------
260
261void loop() { // Start loop -------------------------------------------------------
262 Blynk.run();
263 timer.run();
264 webSocket.loop();
265
266 if(isConnected) {
267 uint64_t now = millis();
268 // Send heartbeat in order to avoid disconnections during ISP resetting IPs over night. Thanks @MacSass
269 if((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) {
270 heartbeatTimestamp = now;
271 webSocket.sendTXT("H");
272 }
273 }
274} // End loop ---------------------------------------------------------------------