Created
October 7, 2020 17:44
-
-
Save sivar2311/4c015d6a6b4004aab21fed6f7c88af67 to your computer and use it in GitHub Desktop.
2_Thermostats at same time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Uncomment the following line to enable serial debug output | |
#define ENABLE_DEBUG | |
#ifdef ENABLE_DEBUG | |
#define DEBUG_ESP_PORT Serial | |
#define NODEBUG_WEBSOCKETS | |
#define NDEBUG | |
#endif | |
#include <Arduino.h> | |
#ifdef ESP8266 | |
#include <ESP8266WiFi.h> | |
#endif | |
#ifdef ESP32 | |
#include <WiFi.h> | |
#endif | |
#include "SinricPro.h" | |
#include "SinricProThermostat.h" | |
#define WIFI_SSID "xxxx" | |
#define WIFI_PASS "xxxx" | |
#define APP_KEY "5ecef3xxxxxxx" // Should look like "de0ax2abxxxxxxxx" | |
#define APP_SECRET "axxxxxx" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx" | |
#define THERMOSTAT_ID1 "5e4bxxxxxx" // Should look like "5dc1564130xxxxxx" | |
#define THERMOSTAT_ID2 "5e4bxxxxxx" // Should look like "5dc1564130xxxxxx" | |
#define BAUD_RATE 9600 // Change baudrate to your need | |
float globalTemperature; | |
bool globalPowerState; | |
bool onPowerState(const String &deviceId, bool &state) { | |
Serial.printf("Thermostat %s turned %s\r\n", deviceId.c_str(), state?"on":"off"); | |
globalPowerState = state; | |
return true; // request handled properly | |
} | |
bool onTargetTemperature(const String &deviceId, float &temperature) { | |
Serial.printf("Thermostat %s set temperature to %f\r\n", deviceId.c_str(), temperature); | |
globalTemperature = temperature; | |
return true; | |
} | |
bool onAdjustTargetTemperature(const String & deviceId, float &temperatureDelta) { | |
globalTemperature += temperatureDelta; // calculate absolut temperature | |
Serial.printf("Thermostat %s changed temperature about %f to %f", deviceId.c_str(), temperatureDelta, globalTemperature); | |
temperatureDelta = globalTemperature; // return absolut temperature | |
return true; | |
} | |
bool onThermostatMode(const String &deviceId, String &mode) { | |
Serial.printf("Thermostat %s set to mode %s\r\n", deviceId.c_str(), mode.c_str()); | |
return true; | |
} | |
void setupWiFi() { | |
Serial.printf("\r\n[Wifi]: Connecting"); | |
WiFi.begin(WIFI_SSID, WIFI_PASS); | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.printf("."); | |
delay(250); | |
} | |
IPAddress localIP = WiFi.localIP(); | |
Serial.printf("connected!\r\n[WiFi]: IP-Address is %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]); | |
} | |
void setupSinricPro() { | |
SinricProThermostat &myThermostat1 = SinricPro[THERMOSTAT_ID1]; | |
myThermostat1.onPowerState(onPowerState); | |
myThermostat1.onTargetTemperature(onTargetTemperature); | |
myThermostat1.onAdjustTargetTemperature(onAdjustTargetTemperature); | |
myThermostat1.onThermostatMode(onThermostatMode); | |
SinricProThermostat &myThermostat2 = SinricPro[THERMOSTAT_ID2]; | |
myThermostat2.onPowerState(onPowerState); | |
myThermostat2.onTargetTemperature(onTargetTemperature); | |
myThermostat2.onAdjustTargetTemperature(onAdjustTargetTemperature); | |
myThermostat2.onThermostatMode(onThermostatMode); | |
// setup SinricPro | |
SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); }); | |
SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); }); | |
SinricPro.begin(APP_KEY, APP_SECRET); | |
} | |
void setup() { | |
Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n"); | |
setupWiFi(); | |
setupSinricPro(); | |
} | |
void loop() { | |
SinricPro.handle(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment