/*
* Example
*
* If you encounter any issues:
* - check the readme.md at https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md
* - ensure all dependent libraries are installed
* - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#arduinoide
* - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#dependencies
* - open serial monitor and check whats happening
* - check full user documentation at https://sinricpro.github.io/esp8266-esp32-sdk
* - visit https://github.com/sinricpro/esp8266-esp32-sdk/issues and check for existing issues or open a new one
*/
// Custom devices requires SinricPro ESP8266/ESP32 SDK 2.9.6 or later
// 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 "WaterTank.h"
#define APP_KEY "109c5efc-5cdd-46fd-9a03-3c789ccba2e3"
#define APP_SECRET "a57cd713-7985-48fe-9062-1e840925ddfe-ae4cb0b5-91d8-42fb-b25d-c5812c63fdc1"
#define DEVICE_ID "6551b0ca93a1f6be5eafae99"
#define SSID "YOUR_WIFI_SSID"
#define PASS "YOUR_WIFI_PASS"
#define BAUD_RATE 9600
WaterTank &waterTank = SinricPro[DEVICE_ID];
/*************
* Variables *
***********************************************
* Global variables to store the device states *
***********************************************/
// RangeController
std::map<String, int> globalRangeValues;
/*************
* Callbacks *
*************/
// RangeController
bool onRangeValue(const String &deviceId, const String& instance, int &rangeValue) {
Serial.printf("[Device: %s]: Value for \"%s\" changed to %d\r\n", deviceId.c_str(), instance.c_str(), rangeValue);
globalRangeValues[instance] = rangeValue;
return true;
}
bool onAdjustRangeValue(const String &deviceId, const String& instance, int &valueDelta) {
globalRangeValues[instance] += valueDelta;
Serial.printf("[Device: %s]: Value for \"%s\" changed about %d to %d\r\n", deviceId.c_str(), instance.c_str(), valueDelta, globalRangeValues[instance]);
globalRangeValues[instance] = valueDelta;
return true;
}
/**********
* Events *
*************************************************
* Examples how to update the server status when *
* you physically interact with your device or a *
* sensor reading changes. *
*************************************************/
// RangeController
void updateRangeValue(String instance, int value) {
waterTank.sendRangeValueEvent(instance, value);
}
// PushNotificationController
void sendPushNotification(String notification) {
waterTank.sendPushNotification(notification);
}
/*********
* Setup *
*********/
void setupSinricPro() {
// RangeController
waterTank.onRangeValue("rangeInstance1", onRangeValue);
waterTank.onAdjustRangeValue("rangeInstance1", onAdjustRangeValue);
SinricPro.onConnected([]{ Serial.printf("[SinricPro]: Connected\r\n"); });
SinricPro.onDisconnected([]{ Serial.printf("[SinricPro]: Disconnected\r\n"); });
SinricPro.begin(APP_KEY, APP_SECRET);
};
void setupWiFi() {
#if defined(ESP8266)
WiFi.setSleepMode(WIFI_NONE_SLEEP);
WiFi.setAutoReconnect(true);
#elif defined(ESP32)
WiFi.setSleep(false);
WiFi.setAutoReconnect(true);
#endif
WiFi.begin("Wokwi-GUEST", "", 6);
Serial.printf("[WiFi]: Connecting to %s", SSID);
while (WiFi.status() != WL_CONNECTED) {
Serial.printf(".");
delay(250);
}
Serial.printf("connected\r\n");
}
void setup() {
Serial.begin(BAUD_RATE);
setupWiFi();
setupSinricPro();
}
/********
* Loop *
********/
void loop() {
SinricPro.handle();
}