-
-
Save sivar2311/6ac0df732c27bc1ffe59d98dd63b6440 to your computer and use it in GitHub Desktop.
Multiple garage doors (single file version) with WiFiManager and reset button
This file contains 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> | |
#include <SinricPro.h> | |
#include <SinricProGarageDoor.h> | |
#include <WiFiManager.h> | |
const char* APP_KEY = "YOUR_APP_KEY_HERE"; | |
const char* APP_SECRET = "YOUR_APP_SECRET_HERE"; | |
const char* WIFIMANAGER_AP = "GarageDoorAP"; | |
const int RESET_BUTTON_PIN = 21; | |
struct GarageDoor { | |
GarageDoor(const String& name, const String& deviceId, const int pin, const int delayTime) | |
: name(name), deviceId(deviceId), pin(pin), delayTime(delayTime), timer(0) {} | |
const String name; | |
const String deviceId; | |
const int pin; | |
const unsigned long delayTime; | |
unsigned long timer; | |
}; | |
GarageDoor garageDoors[]{ | |
// name, deviceId, pin, delay time (ms) | |
{"GarageDoor1", "GARAGE_DOOR_1_ID_HERE", 22, 20000}, | |
{"GarageDoor2", "GARAGE_DOOR_2_ID_HERE", 23, 20000}}; | |
WiFiManager wifiManager; | |
void triggerGarageDoor(GarageDoor& garageDoor) { | |
digitalWrite(garageDoor.pin, LOW); | |
delay(500); | |
digitalWrite(garageDoor.pin, HIGH); | |
Serial.printf("[%s]: triggered\r\n", garageDoor.name.c_str()); | |
} | |
void startGarageDoorTimer(GarageDoor& garageDoor) { | |
Serial.printf("[%s]: timer started\r\n", garageDoor.name.c_str()); | |
garageDoor.timer = millis(); | |
} | |
void stopGarageDoorTimer(GarageDoor& garageDoor) { | |
Serial.printf("[%s]: timer stopped\r\n", garageDoor.name.c_str()); | |
garageDoor.timer = 0; | |
} | |
void openGarageDoor(GarageDoor& garageDoor) { | |
Serial.printf("[%s]: opening door\r\n", garageDoor.name.c_str()); | |
triggerGarageDoor(garageDoor); | |
startGarageDoorTimer(garageDoor); | |
} | |
void closeGarageDoor(GarageDoor& garageDoor) { | |
Serial.printf("[%s]: closing door\r\n", garageDoor.name.c_str()); | |
triggerGarageDoor(garageDoor); | |
stopGarageDoorTimer(garageDoor); | |
} | |
bool garageDoorTimerIsExpired(GarageDoor& garageDoor) { | |
if (garageDoor.timer && millis() - garageDoor.timer >= garageDoor.delayTime) { | |
Serial.printf("[%s]: timer expired\r\n", garageDoor.name.c_str()); | |
return true; | |
} | |
return false; | |
} | |
void sendGarageDoorCloseEvent(GarageDoor& garageDoor) { | |
SinricProGarageDoor& myGarageDoor = SinricPro[garageDoor.deviceId]; | |
myGarageDoor.sendDoorStateEvent(true); | |
Serial.printf("[%s]: close event sent\r\n", garageDoor.name.c_str()); | |
} | |
void handleGarageDoorTimer(GarageDoor& garageDoor) { | |
if (garageDoorTimerIsExpired(garageDoor)) { | |
stopGarageDoorTimer(garageDoor); | |
sendGarageDoorCloseEvent(garageDoor); | |
} | |
} | |
void handleGarageDoorTimers() { | |
for (auto& garageDoor : garageDoors) handleGarageDoorTimer(garageDoor); | |
} | |
bool onDoorState(const String& deviceId, bool& state) { | |
for (auto& garageDoor : garageDoors) { | |
if (deviceId == garageDoor.deviceId) { | |
if (state) { | |
closeGarageDoor(garageDoor); | |
} else { | |
openGarageDoor(garageDoor); | |
} | |
} | |
} | |
return true; | |
} | |
bool resetButtonIsPressed() { | |
static bool lastButtonState; | |
bool currentButtonState = digitalRead(RESET_BUTTON_PIN); | |
if (lastButtonState == currentButtonState) return false; | |
lastButtonState = currentButtonState; | |
if (currentButtonState == LOW) return true; | |
return false; | |
} | |
void clearWiFiSettings() { | |
wifiManager.resetSettings(); | |
} | |
void handleResetButton() { | |
if (resetButtonIsPressed()) { | |
clearWiFiSettings(); | |
ESP.restart(); | |
} | |
} | |
void setupResetButton() { | |
pinMode(RESET_BUTTON_PIN, INPUT_PULLUP); | |
}; | |
void setupGarageDoor(GarageDoor& garageDoor) { | |
int pin = garageDoor.pin; | |
pinMode(pin, OUTPUT); | |
digitalWrite(pin, HIGH); | |
SinricProGarageDoor& door = SinricPro[garageDoor.deviceId]; | |
door.onDoorState(onDoorState); | |
} | |
void setupGarageDoors() { | |
for (auto& garageDoor : garageDoors) setupGarageDoor(garageDoor); | |
} | |
void setupWiFi() { | |
if (!wifiManager.autoConnect(WIFIMANAGER_AP)) { | |
Serial.println("failed to connect and hit timeout"); | |
ESP.restart(); | |
delay(1000); | |
} | |
Serial.println("connected... :)"); | |
} | |
void setupSinricPro() { | |
SinricPro.onConnected([]() { Serial.println("[SinricPro]: connected"); }); | |
SinricPro.onDisconnected([]() { Serial.println("[SinricPro]: disconnected"); }); | |
Serial.println("[SinricPro]: connecting..."); | |
SinricPro.begin(APP_KEY, APP_SECRET); | |
} | |
void setup() { | |
Serial.begin(115200); | |
setupResetButton(); | |
setupGarageDoors(); | |
setupWiFi(); | |
setupSinricPro(); | |
} | |
void loop() { | |
SinricPro.handle(); | |
handleGarageDoorTimers(); | |
handleResetButton(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment