Skip to content

Instantly share code, notes, and snippets.

@andresanches
Last active April 4, 2021 05:10
Show Gist options
  • Save andresanches/9ec97fa02342197da9d8850a4203aaea to your computer and use it in GitHub Desktop.
Save andresanches/9ec97fa02342197da9d8850a4203aaea to your computer and use it in GitHub Desktop.
Arduino sketch for the garage door opener
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
String wifi_ssid = "ADD WIFI SSID HERE";
String wifi_password = "ADD WIFI PASSWORD HERE";
ESP8266WebServer server(80);
// Function prototypes
void connectWifi();
void handleActivate();
void handleHealthCheck();
void handleNotFound();
void setup() {
Serial.begin(115200);
Serial.println();
// Connect to wifi
connectWifi();
pinMode(0, INPUT_PULLUP);
digitalWrite(0, HIGH);
pinMode(0, OUTPUT);
digitalWrite(0, HIGH);
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
// Set up mdns name
if (MDNS.begin("ESP8266-GARAGE")) {
Serial.println("mDNS responder started");
} else {
Serial.println("Error setting up MDNS responder!");
}
// Set up http request handlers
server.on("/activate", handleActivate);
server.on("/healthcheck", handleHealthCheck);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
if(WiFi.status() != WL_CONNECTED) {
Serial.println("WARN: Wifi disconnected");
connectWifi();
}
server.handleClient();
}
void handleActivate() {
if(!server.hasArg("username") || !server.hasArg("password")
|| server.arg("username") == NULL || server.arg("password") == NULL) {
Serial.println("Error: empty credentials.");
server.send(400, "text/plain", "400: Invalid Request");
return;
} else if (server.arg("username") != "admin" && server.arg("password") != "Missy4us") {
Serial.print("Error: unauthorized user/pass: ");
Serial.print(server.arg("username"));
Serial.print("/");
Serial.println(server.arg("password"));
server.send(401, "text/plain", "401: Unauthorized");
return;
}
Serial.println("Setting GPIO0 to LOW");
digitalWrite(0, LOW);
delay(200);
Serial.println("Setting GPIO0 to HIGH");
digitalWrite(0, HIGH);
server.send(200, "text/plain", "Activated relay by request of user " + server.arg("username"));
}
void handleHealthCheck() {
Serial.println("INFO: healthcheck requested.");
server.send(200, "application/json", "{ \"status\": \"up\" }");
}
void handleNotFound() {
String method;
switch(server.method()) {
case HTTP_GET: method = "GET"; break;
case HTTP_POST: method = "POST"; break;
case HTTP_PUT: method = "PUT"; break;
case HTTP_DELETE: method = "DELETE"; break;
case HTTP_ANY: method = "ANY"; break;
case HTTP_HEAD: method = "HEAD"; break;
case HTTP_PATCH: method = "PATCH"; break;
case HTTP_OPTIONS: method = "OPTIONS"; break;
default: method = "UNKNOWN";
}
Serial.println("WARN: invalid endpoint requested: " + method + " " + server.uri());
server.send(404, "text/plain", "");
}
void connectWifi() {
WiFi.setPhyMode(WIFI_PHY_MODE_11N);
WiFi.mode(WIFI_STA);
WiFi.begin(wifi_ssid, wifi_password);
Serial.print("Connecting to wifi network");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment