Last active
April 27, 2025 13:36
-
-
Save kakopappa/86f6f4c65483d6b5c21ba33f9af48d87 to your computer and use it in GitHub Desktop.
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
#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 "" // App Key | |
#define APP_SECRET "" // App Secret | |
#define SWITCH_ID_1 "" // Device Id | |
#define BAUD_RATE 115200 // Change baudrate to your need | |
#if defined(ESP8266) | |
#define BUTTON_PIN 15 | |
#define RELAYPIN_1 12 | |
#elif defined(ESP32) | |
#define BUTTON_PIN 17 | |
#define RELAYPIN_1 16 | |
#elif (ARDUINO_ARCH_RP2040) | |
#define BUTTON_PIN 7 | |
#define RELAYPIN_1 6 | |
#endif | |
bool myPowerState = false; | |
unsigned long lastBtnPress = 0; | |
bool onPowerState(const String &deviceId, bool &state) { | |
Serial.printf("Device %s turned %s (via SinricPro) \r\n", deviceId.c_str(), 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); */ | |
myPowerState = state; | |
return true; // request handled properly | |
} | |
void handleButtonPress() { | |
unsigned long actualMillis = millis(); | |
if (digitalRead(BUTTON_PIN) == HIGH && actualMillis - lastBtnPress > 1000) { | |
if (myPowerState) { | |
myPowerState = false; | |
} else { | |
myPowerState = true; | |
} | |
// get Switch device back | |
SinricProSwitch& mySwitch = SinricPro[SWITCH_ID_1]; | |
// send powerstate event to server | |
mySwitch.sendPowerStateEvent(myPowerState); // send the new powerState to SinricPro server | |
Serial.printf("Device %s turned %s (manually via button)\r\n", mySwitch.getDeviceId().c_str(), myPowerState?"on":"off"); | |
// set relay status | |
digitalWrite(RELAYPIN_1, myPowerState ? HIGH:LOW); | |
lastBtnPress = actualMillis; // update last button press variable | |
} | |
} | |
// 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 device to SinricPro | |
SinricProSwitch& mySwitch = SinricPro[SWITCH_ID_1]; | |
// set callback function to device | |
mySwitch.onPowerState(onPowerState); | |
// 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() { | |
pinMode(BUTTON_PIN, INPUT); | |
pinMode(RELAYPIN_1, OUTPUT); | |
Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n"); | |
setupWiFi(); | |
setupSinricPro(); | |
} | |
void loop() { | |
handleButtonPress(); | |
SinricPro.handle(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment