Skip to content

Instantly share code, notes, and snippets.

@kakopappa
Created September 21, 2023 08:09
Show Gist options
  • Save kakopappa/0416a1f26b8398ce6dd9f71a6a70755a to your computer and use it in GitHub Desktop.
Save kakopappa/0416a1f26b8398ce6dd9f71a6a70755a to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32) || defined(ARDUINO_ARCH_RP2040)
#include <WiFi.h>
#endif
#include "SinricPro.h"
#include "SinricProSwitch.h"
#define WIFI_SSID "" // Change WIFI_SSID to your WiFi Name.
#define WIFI_PASS "" // Change WIFI_PASS to your WiFi password.
#define APP_KEY "" // Paste App Key from above
#define APP_SECRET "" // Paste App Secret from above
#define SWITCH_ID_1 "" // Paste Device Id from above
#if defined(ESP8266)
#define RELAYPIN_1 12
#elif defined(ESP32)
#define RELAYPIN_1 16
#elif (ARDUINO_ARCH_RP2040)
#define RELAYPIN_1 6
#endif
#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);
/* If your relay is activated with low signal, change the above to below code
digitalWrite(RELAYPIN_1, state ? LOW : HIGH); */
return true; // request handled properly
}
// setup function for WiFi connection
void setupWiFi() {
Serial.printf("\r\n[Wifi]: Connecting");
#if defined(ESP8266)
WiFi.setSleepMode(WIFI_NONE_SLEEP);
WiFi.setAutoReconnect(true);
#elif defined(ESP32)
WiFi.setSleep(false);
WiFi.setAutoReconnect(true);
#endif
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();
setupSinricPro();
}
void loop() {
SinricPro.handle();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment