Skip to content

Instantly share code, notes, and snippets.

@ItKindaWorks
Last active March 24, 2018 20:19
Show Gist options
  • Save ItKindaWorks/56422d515f4281ee44bc1637c0823aaf to your computer and use it in GitHub Desktop.
Save ItKindaWorks/56422d515f4281ee44bc1637c0823aaf to your computer and use it in GitHub Desktop.
A small Arduino script to control a garage door opener.
#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);
}
@jsiravo07
Copy link

@rqleslie did you ever figure it out with a NodeMCU board? i was looking for some direction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment