Facebook
From Loui Handwerker, 3 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 244
  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
  6. #include <Servo.h>
  7.  
  8. ESP8266WiFiMulti WiFiMulti;
  9. WebSocketsClient webSocket;
  10. WiFiClient client;
  11. Servo myservo;
  12. boolean b;
  13.  
  14. #define MyApiKey "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard
  15. #define MySSID "mywifi" // TODO: Change to your Wifi network SSID
  16. #define MyWifiPassword "password" // TODO: Change to your Wifi network password
  17.  
  18. #define API_ENDPOINT "http://sinric.com"
  19. #define HEARTBEAT_INTERVAL 300000 // 5 Minutes
  20.  
  21. uint64_t heartbeatTimestamp = 0;
  22. bool isConnected = false;
  23.  
  24. void turnOn(String deviceId) {
  25.   if (deviceId == "xxxxxxxxxxxxxxxxxxxxxxxx") // Device ID of first device
  26.   {  
  27.     Serial.print("Turn on device id: ");
  28.     Serial.println(deviceId);
  29.     while(b) {
  30.       myservo.attach(2);
  31.       myservo.write(15);
  32.       delay(1000);
  33.       myservo.detach();
  34.       b = false;
  35.     }
  36.   }
  37.  
  38.   else {
  39.     Serial.print("Turn on for unknown device id: ");
  40.     Serial.println(deviceId);    
  41.   }    
  42. }
  43.  
  44. void turnOff(String deviceId) {
  45.    if (deviceId == "xxxxxxxxxxxxxxxxxxxxxxxx") // Device ID of first device
  46.    {  
  47.      Serial.print("Turn off Device ID: ");
  48.      Serial.println(deviceId);
  49.      while(!b) {
  50.       myservo.attach(2);
  51.       myservo.write(0);
  52.       delay(1000);
  53.       myservo.detach();
  54.       b = true;
  55.      }
  56.    }
  57.  
  58.   else {
  59.      Serial.print("Turn off for unknown device id: ");
  60.      Serial.println(deviceId);    
  61.   }
  62. }
  63.  
  64. void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
  65.   switch(type) {
  66.     case WStype_DISCONNECTED:
  67.       isConnected = false;    
  68.       Serial.printf("[WSc] Webservice disconnected from sinric.com!\n");
  69.       break;
  70.     case WStype_CONNECTED: {
  71.       isConnected = true;
  72.       Serial.printf("[WSc] Service connected to sinric.com at url: %s\n", payload);
  73.       Serial.printf("Waiting for commands from sinric.com ...\n");        
  74.       }
  75.       break;
  76.     case WStype_TEXT: {
  77.         Serial.printf("[WSc] get text: %s\n", payload);
  78.        
  79. #if ARDUINOJSON_VERSION_MAJOR == 5
  80.         DynamicJsonBuffer jsonBuffer;
  81.         JsonObject& json = jsonBuffer.parseObject((char*)payload);
  82. #endif
  83. #if ARDUINOJSON_VERSION_MAJOR == 6        
  84.         DynamicJsonDocument json(1024);
  85.         deserializeJson(json, (char*) payload);      
  86. #endif        
  87.         String deviceId = json ["deviceId"];    
  88.         String action = json ["action"];
  89.        
  90.         if(action == "setPowerState") { // Switch or Light
  91.             String value = json ["value"];
  92.             if(value == "ON") {
  93.                 turnOn(deviceId);
  94.             } else {
  95.                 turnOff(deviceId);
  96.             }
  97.         }
  98.     }
  99.       break;
  100.     case WStype_BIN:
  101.       Serial.printf("[WSc] get binary length: %u\n", length);
  102.       break;
  103.     default: break;
  104.   }
  105. }
  106.  
  107. void setup() {
  108.   Serial.begin(9600);
  109.  
  110.   WiFiMulti.addAP(MySSID, MyWifiPassword);
  111.   Serial.println();
  112.   Serial.print("Connecting to Wifi: ");
  113.   Serial.println(MySSID);
  114.   myservo.attach(2);
  115.   myservo.write(0);
  116.   b = true;
  117.  
  118.   // Waiting for Wifi connect
  119.   while(WiFiMulti.run() != WL_CONNECTED) {
  120.     delay(500);
  121.     Serial.print(".");
  122.   }
  123.   if(WiFiMulti.run() == WL_CONNECTED) {
  124.     Serial.println("");
  125.     Serial.print("WiFi connected. ");
  126.     Serial.print("IP address: ");
  127.     Serial.println(WiFi.localIP());
  128.   }
  129.  
  130.   // server address, port and URL
  131.   webSocket.begin("iot.sinric.com", 80, "/");
  132.  
  133.   // event handler
  134.   webSocket.onEvent(webSocketEvent);
  135.   webSocket.setAuthorization("apikey", MyApiKey);
  136.  
  137.   // try again every 5000ms if connection has failed
  138.   webSocket.setReconnectInterval(5000);   // If you see 'class WebSocketsClient' has no member named 'setReconnectInterval' error update arduinoWebSockets
  139. }
  140.  
  141. void loop() {
  142.   webSocket.loop();
  143.   if(isConnected) {
  144.       uint64_t now = millis();
  145.      
  146.       // Send heartbeat in order to avoid disconnections during ISP resetting IPs over night. Thanks @MacSass
  147.       if((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) {
  148.           heartbeatTimestamp = now;
  149.           webSocket.sendTXT("H");          
  150.       }
  151.   }  
  152. }