Skip to content

Instantly share code, notes, and snippets.

@kakopappa
Created February 15, 2018 02:24
Show Gist options
  • Save kakopappa/aaea482f44df2e3158eaeba7dc638524 to your computer and use it in GitHub Desktop.
Save kakopappa/aaea482f44df2e3158eaeba7dc638524 to your computer and use it in GitHub Desktop.
thermostat_example.ino
/*
Version 0.2 - Feb 15 2018
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsClient.h> // get it from https://github.com/Links2004/arduinoWebSockets/releases
#include <ArduinoJson.h> // get it from https://arduinojson.org/ or install via Arduino library manager
ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;
#define MyApiKey "xxxxxxxxxxxxxxxx" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard
#define MySSID "xxxxxxxxxxxxxxxx" // TODO: Change to your Wifi network SSID
#define MyWifiPassword "xxxxxxxxxxxxxxxx" // TODO: Change to your Wifi network password
#define HEARTBEAT_INTERVAL 300000 // 5 Minutes
uint64_t heartbeatTimestamp = 0;
bool isConnected = false;
void turnOn(String deviceId) {
if (deviceId == "5axxxxxxxxxxxxxxxxxxx") // Device ID of first device
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
}
else if (deviceId == "5axxxxxxxxxxxxxxxxxxx") // Device ID of second device
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
}
else {
Serial.print("Turn on for unknown device id: ");
Serial.println(deviceId);
}
}
void turnOff(String deviceId) {
if (deviceId == "5axxxxxxxxxxxxxxxxxxx") // Device ID of first device
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
}
else if (deviceId == "5axxxxxxxxxxxxxxxxxxx") // Device ID of second device
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
}
else {
Serial.print("Turn off for 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 Thermostat
// {"deviceId": xxxx, "action": "setPowerState", value: "ON"} // https://developer.amazon.com/docs/device-apis/alexa-thermostatcontroller.html
// {"deviceId": xxxx, "action": "SetTargetTemperature", value: "targetSetpoint": { "value": 20.0, "scale": "CELSIUS"}} // https://developer.amazon.com/docs/device-apis/alexa-thermostatcontroller.html#settargettemperature
// {"deviceId": xxxx, "action": "AdjustTargetTemperature", value: "targetSetpointDelta": { "value": 2.0, "scale": "FAHRENHEIT" }} // https://developer.amazon.com/docs/device-apis/alexa-thermostatcontroller.html#adjusttargettemperature
// {"deviceId": xxxx, "action": "SetThermostatMode", value: "thermostatMode" : { "value": "COOL" }} // https://developer.amazon.com/docs/device-apis/alexa-thermostatcontroller.html#setthermostatmode
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject((char*)payload);
String deviceId = json ["deviceId"];
String action = json ["action"];
if(action == "setPowerState") { // On or Off
String value = json ["value"];
}
if(action == "SetTargetTemperature") {
// Alexa, set thermostat to 20
//String value = json ["value"];
String value = json["value"]["targetSetpoint"]["value"];
String scale = json["value"]["targetSetpoint"]["scale"];
}
else if(action == "AdjustTargetTemperature") {
//Alexa, make it warmer in here
//Alexa, make it cooler in here
String value = json["value"]["targetSetpointDelta"]["value"];
String scale = json["value"]["targetSetpointDelta"]["scale"];
}
else if(action == "SetThermostatMode") {
//Alexa, set thermostat name to mode
//Alexa, set thermostat to automatic
//Alexa, set kitchen to off
String value = json["value"]["thermostatMode"]["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() {
Serial.begin(115200);
WiFiMulti.addAP(MySSID, MyWifiPassword);
Serial.println();
Serial.print("Connecting to Wifi: ");
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("WiFi connected. ");
Serial.print("IP address: ");
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");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment