#include <Arduino.h>
#include <SinricPro.h>
#include <SinricProSwitch.h>
#include <WiFi.h>
const char* WIFI_SSID = "Wokwi-GUEST";
const char* WIFI_PASS = "";
const char* APP_KEY = "YOUR_APP_KEY";
const char* APP_SECRET = "YOUR_APP_SECRET";
const char* SWITCH_ID = "YOUR_DEVICE_ID";
const int BTN_RUN = 16;
const int BTN_START = 17;
const unsigned long timerDuration = 1000; // 1 second
unsigned long timer = 0;
void startTimer() {
timer = millis();
}
void stopTimer() {
timer = 0;
}
void pressStartButton() {
Serial.println("Pressing Start Button");
digitalWrite(BTN_START, HIGH);
}
void releaseStartButton() {
Serial.println("Releasing Start Button");
digitalWrite(BTN_START, LOW);
}
void pressRunButton() {
Serial.println("Pressing Run Button");
digitalWrite(BTN_RUN, HIGH);
}
void releaseRunButton() {
Serial.println("Releasing Run Button");
digitalWrite(BTN_RUN, LOW);
}
bool timerIsRunning() {
return timer > 0;
}
void startGenerator() {
if (timerIsRunning()) return;
Serial.println("Starting Generator");
pressRunButton();
pressStartButton();
startTimer();
}
void stopGenerator() {
Serial.println("Stopping Generator");
releaseRunButton();
}
bool timerIsExpired() {
return (millis() - timer) >= timerDuration;
}
bool onPowerState(const String& deviceId, bool& state) {
if (state) {
startGenerator();
} else {
stopGenerator();
}
return true;
}
void handleTimer() {
if (timerIsRunning() && timerIsExpired()) {
Serial.println("Timer is expired");
releaseStartButton();
stopTimer();
}
}
void setupGeneratorButtons() {
pinMode(BTN_RUN, OUTPUT);
pinMode(BTN_START, OUTPUT);
}
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());
}
void setupSinricPro() {
SinricProSwitch& mySwitch = SinricPro[SWITCH_ID];
mySwitch.onPowerState(onPowerState);
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(115200);
setupGeneratorButtons();
setupWiFi();
setupSinricPro();
}
void loop() {
SinricPro.handle();
handleTimer();
delay(10); // wokwi simulator delay
}