/*
* Simple example for how to use multiple SinricPro Switch device:
* - setup 4 switch devices
* - handle request using multiple callbacks
*
* 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
*/
#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
#define ESP32
#ifdef ESP32
#include <WiFi.h>
#endif
#include "SinricPro.h"
#include "SinricProSwitch.h"
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASS ""
#define APP_KEY "55faf6b5-3453-42b5-8b1c-e44470d6b4ba"
#define APP_SECRET "aa4d19dc-aade-4ba6-86ef-0b1e636dc9cc-0b5eefaf-955f-42c7-a6d2-adb73d852159"
#define SWITCH_ID_1 "635ed2bcb8a7fefbd62c251f"
#define RELAYPIN_1 23
#define BAUD_RATE 115200 // Change baudrate to your need
bool onPowerState1(const String &deviceId, bool &state) {
Serial.printf("Device 1 turned %s", state?"on":"off");
digitalWrite(RELAYPIN_1, state ? HIGH:LOW);
return true; // request handled properly
}
// setup function for WiFi connection
void setupWiFi() {
Serial.printf("\r\n[Wifi]: Connecting");
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
Serial.printf(".");
delay(250);
}
Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.localIP().toString().c_str());
}
// setup function for SinricPro
void setupSinricPro() {
// add devices and callbacks to SinricPro
pinMode(RELAYPIN_1, OUTPUT);
SinricProSwitch& mySwitch1 = SinricPro[SWITCH_ID_1];
mySwitch1.onPowerState(onPowerState1);
// 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);
}
// main setup function
void setup() {
Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");
setupWiFi();
delay(8000);
setupSinricPro();
}
void loop() {
SinricPro.handle();
}