Last active
March 24, 2018 20:19
-
-
Save ItKindaWorks/56422d515f4281ee44bc1637c0823aaf to your computer and use it in GitHub Desktop.
A small Arduino script to control a garage door opener.
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 "ESPHelper.h" | |
#define STATUS "/status" | |
#define RELAY_TOPIC "/home/down/garage" | |
#define HOSTNAME "RLY-GarageDoor" | |
#define RELAY_PIN 3 | |
char* relayTopic = RELAY_TOPIC; | |
String statusTopic; | |
char replyTopic[50]; | |
const int relayPin = RELAY_PIN; | |
//will track the door state but is currently unused. | |
bool currentState = false; | |
netInfo homeNet = {.name = "NETWORK NICKNAME", .mqtt = "YOUR MQTT-IP", .ssid = "YOUR SSID", .pass = "YOUR NETWORK PASS"}; | |
ESPHelper myESP(&homeNet); | |
void setup() { | |
//generate status topic string | |
statusTopic = relayTopic; | |
statusTopic.concat(STATUS); | |
statusTopic.toCharArray(replyTopic, 50); | |
myESP.OTA_enable(); | |
myESP.OTA_setPassword("OTA_PASSWORD"); | |
myESP.OTA_setHostnameWithVersion(HOSTNAME); | |
myESP.enableHeartbeat(1); | |
myESP.setHopping(false); | |
myESP.addSubscription(RELAY_TOPIC); | |
myESP.begin(); | |
myESP.setCallback(callback); | |
pinMode(relayPin, OUTPUT); | |
digitalWrite(relayPin, LOW); | |
} | |
void loop(){ | |
myESP.loop(); | |
yield(); | |
} | |
void callback(char* topic, byte* payload, unsigned int length) { | |
if(payload[0] == '1'){ | |
toggleDoor(); | |
} | |
} | |
//toggle the garage door between open and closed | |
void toggleDoor(){ | |
digitalWrite(relayPin, HIGH); | |
delay(100); | |
digitalWrite(relayPin, LOW); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, would this work on a NodeMCU based esp board ? Been trying but having some issues that I'm busy troubleshooting.