kakopappa / sinric

Amazon Alexa Smart home skill / Google Home Action for ESP8266 / ESP32 / Arduino
https://sinric.com
286 stars 166 forks source link

Update Device status from Sinric server How to add EPPROM #217

Open Talha909 opened 5 years ago

Talha909 commented 5 years ago

I want to add EPPROM in the fallowing script. I am new in this. Can someone help me with this. `

int device1 = 23;

int device2 = 22; int device3 = 19; int device4 = 18; int device5 = 5; int device6 = 17; int device7 = 16; int device8 = 4;  

include

include

include

include // get it from https://github.com/Links2004/arduinoWebSockets/releases

include // get it from https://arduinojson.org/ or install via Arduino library manager

include

  WiFiMulti WiFiMulti; WebSocketsClient webSocket; WiFiClient client;  

define MyApiKey "34af406e-6244-4663-997c-34b5f56ddb8a" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard

define MySSID "Home" // TODO: Change to your Wifi network SSID

define MyWifiPassword "53883098" // TODO: Change to your Wifi network password

 

define HEARTBEAT_INTERVAL 300000 // 5 Minutes

  uint64_t heartbeatTimestamp = 0; bool isConnected = false;   void setPowerStateOnServer(String deviceId, String value); void setTargetTemperatureOnServer(String deviceId, String value, String scale);   // deviceId is the ID assgined to your smart-home-device in sinric.com dashboard. Copy it from dashboard and paste it here   void turnOn(String deviceId) { if (deviceId == "5c35ed05010d8c32b5565580") // Device ID of device 1 { Serial.print("Turnevice id: "); Serial.println(deviceId); digitalWrite(device1, LOW); } else if (deviceId == "5c35ed93010d8c32b5565588") // Device ID of device 2 { Serial.print("Turnevice id: "); Serial.println(deviceId); digitalWrite(device2, LOW); } else if (deviceId == "5c35ee0e010d8c32b556558b") // Device ID of device 3 { Serial.print("Turnevice id: "); Serial.println(deviceId); digitalWrite(device3, LOW); } else if (deviceId == "5c35ee59010d8c32b556558d") // Device ID of device 4 { Serial.print("Turnevice id: "); Serial.println(deviceId); digitalWrite(device4, LOW); } else if (deviceId == "5c36e5b304018c4adb252073") // Device ID of device 5 { Serial.print("Turnevice id: "); Serial.println(deviceId); digitalWrite(device5, LOW); } else if (deviceId == "5c36e5be04018c4adb252075") // Device ID of device 6 { Serial.print("Turnevice id: "); Serial.println(deviceId); digitalWrite(device6, LOW); } else if (deviceId == "5c36e5c804018c4adb252077") // Device ID of device 7 { Serial.print("Turnevice id: "); Serial.println(deviceId); digitalWrite(device7, LOW); } else if (deviceId == "5c36e5d204018c4adb252079") // Device ID of device 8 { Serial.print("Turnevice id: "); Serial.println(deviceId); digitalWrite(device8, LOW); } else { Serial.print("Turnor unknown device id: "); Serial.println(deviceId); } }   void turnOff(String deviceId) { if (deviceId == "5c35ed05010d8c32b5565580") // Device ID of device 1 { Serial.print("TurnDevice ID: "); Serial.println(deviceId); digitalWrite(device1, HIGH); } else if (deviceId == "5c35ed93010d8c32b5565588") // Device ID of device 2 { Serial.print("TurnDevice ID: "); Serial.println(deviceId); digitalWrite(device2, HIGH); } else if (deviceId == "5c35ee0e010d8c32b556558b") // Device ID of device 3 { Serial.print("TurnDevice ID: "); Serial.println(deviceId); digitalWrite(device3, HIGH); } else if (deviceId == "5c35ee59010d8c32b556558d") // Device ID of device 4 { Serial.print("TurnDevice ID: "); Serial.println(deviceId); digitalWrite(device4, HIGH); } else if (deviceId == "5c36e5b304018c4adb252073") // Device ID of device 5 { Serial.print("TurnDevice ID: "); Serial.println(deviceId); digitalWrite(device5, HIGH); } else if (deviceId == "5c36e5be04018c4adb252075") // Device ID of device 6 { Serial.print("TurnDevice ID: "); Serial.println(deviceId); digitalWrite(device6, HIGH); } else if (deviceId == "5c36e5c804018c4adb252077") // Device ID of device 7 { Serial.print("TurnDevice ID: "); Serial.println(deviceId); digitalWrite(device7, HIGH); } else if (deviceId == "5c36e5d204018c4adb252079") // Device ID of device 8 { Serial.print("TurnDevice ID: "); Serial.println(deviceId); digitalWrite(device8, HIGH); } else { Serial.print("Turnfor unknown device id: "); Serial.println(deviceId); } }   void webSocketEvent(WStype_t type, uint8_t payload, size_t length) { switch(type) { case WStype_DISCONNECTED: isConnected = false; Serial.printf("[WSc] Webservice disconnected from sinric.com!\n"); break; case WStype_CONNECTED: { isConnected = true; Serial.printf("[WSc] Service connected to sinric.com at url: %s\n", payload); Serial.printf("Waiting for commands from sinric.com ...\n"); } break; case WStype_TEXT: { Serial.printf("[WSc] get text: %s\n", payload); // Example payloads   // For Switch or Light device types // {"deviceId": xxxx, "action": "setPowerState", value: "ON"} // https://developer.amazon.com/docs/device-apis/alexa-powercontroller.html   // For Light device type // Look at the light example in github   DynamicJsonBuffer jsonBuffer; JsonObject& json = jsonBuffer.parseObject((char)payload); String deviceId = json ["deviceId"]; String action = json ["action"];   if(action == "setPowerState") { // Switch or Light String value = json ["value"]; if(value == "ON") { turnOn(deviceId); } else { turnOff(deviceId); } } else if (action == "SetTargetTemperature") { String deviceId = json ["deviceId"]; String action = json ["action"]; String value = json ["value"]; } else if (action == "test") { Serial.println("[WSc] received test command from sinric.com"); } } break; case WStype_BIN: Serial.printf("[WSc] get binary length: %u\n", length); break; } }   void setup() { pinMode(device1, OUTPUT); pinMode(device2, OUTPUT); pinMode(device3, OUTPUT); pinMode(device4, OUTPUT); pinMode(device5, OUTPUT); pinMode(device6, OUTPUT); pinMode(device7, OUTPUT); pinMode(device8, OUTPUT); digitalWrite(device1, HIGH); digitalWrite(device2, HIGH); digitalWrite(device3, HIGH); digitalWrite(device4, HIGH); digitalWrite(device5, HIGH); digitalWrite(device6, HIGH); digitalWrite(device7, HIGH); digitalWrite(device8, HIGH);   Serial.begin(115200);   WiFiMulti.addAP(MySSID, MyWifiPassword); Serial.println(); Serial.print("Connectingifi: "); Serial.println(MySSID);   // Waiting for Wifi connect while(WiFiMulti.run() != WL_CONNECTED) { delay(500); Serial.print("."); } if(WiFiMulti.run() == WL_CONNECTED) { Serial.println(""); Serial.print("WiFiected. "); Serial.print("IPess: "); Serial.println(WiFi.localIP()); }   // server address, port and URL webSocket.begin("iot.sinric.com",80,"/");   // event handler webSocket.onEvent(webSocketEvent); webSocket.setAuthorization("apikey", MyApiKey);   // try again every 5000ms if connection has failed webSocket.setReconnectInterval(5000); // If you see 'class WebSocketsClient' has no member named 'setReconnectInterval' error update arduinoWebSockets }   void loop() {   webSocket.loop();   if(isConnected) { uint64_t now = millis();   // Send heartbeat in order to avoid disconnections during ISP resetting IPs over night. Thanks @MacSass if((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) { heartbeatTimestamp = now; webSocket.sendTXT("H"); } } }   // 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 // so it will reflect on Alexa app. // eg: setPowerStateOnServer("deviceid", "ON") void setPowerStateOnServer(String deviceId, String value) { DynamicJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.createObject(); root["deviceId"] = deviceId; root["action"] = "setPowerState"; root["value"] = value; StreamString databuf; root.printTo(databuf);   webSocket.sendTXT(databuf); }   //eg: setPowerStateOnServer("deviceid", "CELSIUS", "25.0") void setTargetTemperatureOnServer(String deviceId, String value, String scale) { DynamicJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.createObject(); root["action"] = "SetTargetTemperature"; root["deviceId"] = deviceId;   JsonObject& valueObj = root.createNestedObject("value"); JsonObject& targetSetpoint = valueObj.createNestedObject("targetSetpoint"); targetSetpoint["value"] = value; targetSetpoint["scale"] = scale;   StreamString databuf; root.printTo(databuf);   webSocket.sendTXT(databuf); }

`

AshokBaijal commented 5 years ago

Attaching a sample file which saves the state of the switch in EEPROM whenever the status change and restores the old state on startup. This is required for places which have frequent power outages. However keep in mind that the builtin EEPROM only can handle around max 100000 write cycles. SinricSwitch.ino.txt