Guest User

Untitled

a guest
Jul 12th, 2020
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.42 KB | None | 0 0
  1. /*
  2. Version 0.3 - March 06 2018
  3. Grensom Version
  4. */
  5.  
  6. #include <Arduino.h>
  7. #include <ESP8266WiFi.h>
  8. #include <ESP8266WiFiMulti.h>
  9. #include <WebSocketsClient.h> // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries
  10. #include <ArduinoJson.h> // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries
  11. #include <StreamString.h>
  12.  
  13. ESP8266WiFiMulti WiFiMulti;
  14. WebSocketsClient webSocket;
  15. WiFiClient client;
  16.  
  17. ///////////WiFi Setup/////////////
  18.  
  19. #define MyApiKey "a852c587-2630-49c0-bde0-9b889bf88d5d" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard
  20. #define MySSID "SP-LINK_LUCAS E MAY" // TODO: Change to your Wifi network SSID
  21. #define MyWifiPassword "cordeiro2019" // TODO: Change to your Wifi network password
  22.  
  23.  
  24. /////////////////////////////////
  25.  
  26. #define HEARTBEAT_INTERVAL 300000 // 5 Minutes
  27.  
  28. uint64_t heartbeatTimestamp = 0;
  29. bool isConnected = false;
  30.  
  31. void setPowerStateOnServer(String deviceId, String value);
  32. void setTargetTemperatureOnServer(String deviceId, String value, String scale);
  33.  
  34. // deviceId is the ID assgined to your smart-home-device in sinric.com dashboard. Copy it from dashboard and paste it here
  35.  
  36. void turnOn(String deviceId) {
  37. if (deviceId == "5f08e4c6ad7a48327f34c158") // PORTAO
  38. {
  39. Serial.print("Turn on device id: ");
  40. Serial.println(deviceId);
  41. digitalWrite(4, HIGH); // LIGA LED OU/ ativa rele, abre trava solenoide
  42. delay(1000); // DELAY /espera 1,5 segundos
  43. digitalWrite(4, LOW); // DESlIGA LED OU /desativa rele, fecha trava solenoide
  44. }
  45. else if (deviceId == "5f08f3abad7a48327f34cce0") // PORTAO BASCULANTE
  46. {
  47. Serial.print("Turn on device id: ");///
  48. Serial.println(deviceId);
  49. digitalWrite(5, HIGH); // LIGA LED OU/ ativa rele, abre trava solenoide
  50. delay(1000); // DELAY /espera 1,5 segundos
  51. digitalWrite(5, LOW); // DESlIGA LED OU /desativa rele, fecha trava solenoide
  52. }
  53. else {
  54. Serial.print("Turn on for unknown device id: ");
  55. Serial.println(deviceId);
  56. }
  57. }
  58.  
  59. void turnOff(String deviceId) {
  60. if (deviceId == "5f08e4c6ad7a48327f34c158") // Device ID of first device
  61. {
  62. Serial.print("Turn off Device ID: ");
  63. Serial.println(deviceId);
  64.  
  65. digitalWrite(4, HIGH); // DESlIGA LED OU /desativa rele, fecha trava solenoide
  66. delay(1000); // DELAY /espera 1,5 segundos
  67. digitalWrite(4, LOW); // LIGA LED OU/ ativa rele, abre trava solenoide
  68. }
  69. else if (deviceId == "5f08f3abad7a48327f34cce0") // Device ID of second device
  70. {
  71. Serial.print("Turn off Device ID: ");
  72. Serial.println(deviceId);
  73.  
  74. digitalWrite(5, HIGH); // DESlIGA LED OU /desativa rele, fecha trava solenoide
  75. delay(1000); // DELAY /espera 1,5 segundos
  76. digitalWrite(5, LOW); // LIGA LED OU/ ativa rele, abre trava solenoide
  77. }
  78. else {
  79. Serial.print("Turn off for unknown device id: ");
  80. Serial.println(deviceId);
  81. }
  82. }
  83.  
  84. void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
  85. switch(type) {
  86. case WStype_DISCONNECTED:
  87. isConnected = false;
  88. Serial.printf("[WSc] Webservice disconnected from sinric.com!\n");
  89. break;
  90. case WStype_CONNECTED: {
  91. isConnected = true;
  92. Serial.printf("[WSc] Service connected to sinric.com at url: %s\n", payload);
  93. Serial.printf("Waiting for commands from sinric.com ...\n");
  94. }
  95. break;
  96. case WStype_TEXT: {
  97. Serial.printf("[WSc] get text: %s\n", payload);
  98. // Example payloads
  99.  
  100. // For Switch or Light device types
  101. // {"deviceId": xxxx, "action": "setPowerState", value: "ON"} // https://developer.amazon.com/docs/device-apis/alexa-powercontroller.html
  102.  
  103. // For Light device type
  104. // Look at the light example in github
  105.  
  106. DynamicJsonBuffer jsonBuffer;
  107. JsonObject& json = jsonBuffer.parseObject((char*)payload);
  108. String deviceId = json ["deviceId"];
  109. String action = json ["action"];
  110.  
  111. if(action == "setPowerState") { // Switch or Light
  112. String value = json ["value"];
  113. if(value == "ON") {
  114. turnOn(deviceId);
  115. } else {
  116. turnOff(deviceId);
  117. }
  118. }
  119. else if (action == "SetTargetTemperature") {
  120. String deviceId = json ["deviceId"];
  121. String action = json ["action"];
  122. String value = json ["value"];
  123. }
  124. else if (action == "test") {
  125. Serial.println("[WSc] received test command from sinric.com");
  126. }
  127. }
  128. break;
  129. case WStype_BIN:
  130. Serial.printf("[WSc] get binary length: %u\n", length);
  131. break;
  132. }
  133. }
  134.  
  135. void setup() {
  136. Serial.begin(115200);
  137.  
  138. WiFiMulti.addAP(MySSID, MyWifiPassword);
  139. Serial.println();
  140. Serial.print("Connecting to Wifi: ");
  141. Serial.println(MySSID);
  142.  
  143. // Waiting for Wifi connect
  144. while(WiFiMulti.run() != WL_CONNECTED) {
  145. delay(500);
  146. Serial.print(".");
  147. }
  148. if(WiFiMulti.run() == WL_CONNECTED) {
  149. Serial.println("");
  150. Serial.print("WiFi connected. ");
  151. Serial.print("IP address: ");
  152. Serial.println(WiFi.localIP());
  153. }
  154.  
  155. // server address, port and URL
  156. webSocket.begin("iot.sinric.com", 80, "/");
  157.  
  158. // event handler
  159. webSocket.onEvent(webSocketEvent);
  160. webSocket.setAuthorization("apikey", MyApiKey);
  161.  
  162. // try again every 5000ms if connection has failed
  163. webSocket.setReconnectInterval(5000); // If you see 'class WebSocketsClient' has no member named 'setReconnectInterval' error update arduinoWebSockets
  164. pinMode(4, OUTPUT);///Portao social
  165. pinMode(5, OUTPUT);///Portao basculante
  166. }
  167.  
  168. void loop() {
  169. webSocket.loop();
  170.  
  171. if(isConnected) {
  172. uint64_t now = millis();
  173.  
  174. // Send heartbeat in order to avoid disconnections during ISP resetting IPs over night. Thanks @MacSass
  175. if((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) {
  176. heartbeatTimestamp = now;
  177. webSocket.sendTXT("H");
  178. }
  179. }
  180. }
  181.  
  182. // If you are going to use a push button to on/off the switch manually, use this function to update the status on the server
  183. // so it will reflect on Alexa app.
  184. // eg: setPowerStateOnServer("deviceid", "ON")
  185.  
  186. // Call ONLY If status changed. DO NOT CALL THIS IN loop() and overload the server.
  187.  
  188. void setPowerStateOnServer(String deviceId, String value) {
  189. DynamicJsonBuffer jsonBuffer;
  190. JsonObject& root = jsonBuffer.createObject();
  191. root["deviceId"] = deviceId;
  192. root["action"] = "setPowerState";
  193. root["value"] = value;
  194. StreamString databuf;
  195. root.printTo(databuf);
  196.  
  197. webSocket.sendTXT(databuf);
  198. }
  199.  
  200. //eg: setPowerStateOnServer("deviceid", "CELSIUS", "25.0")
  201.  
  202. // Call ONLY If status changed. DO NOT CALL THIS IN loop() and overload the server.
  203.  
  204. void setTargetTemperatureOnServer(String deviceId, String value, String scale) {
  205. DynamicJsonBuffer jsonBuffer;
  206. JsonObject& root = jsonBuffer.createObject();
  207. root["action"] = "SetTargetTemperature";
  208. root["deviceId"] = deviceId;
  209.  
  210. JsonObject& valueObj = root.createNestedObject("value");
  211. JsonObject& targetSetpoint = valueObj.createNestedObject("targetSetpoint");
  212. targetSetpoint["value"] = value;
  213. targetSetpoint["scale"] = scale;
  214.  
  215. StreamString databuf;
  216. root.printTo(databuf);
  217.  
  218. webSocket.sendTXT(databuf);
  219. }
Add Comment
Please, Sign In to add comment