Skip to content

Instantly share code, notes, and snippets.

@davidhq
Last active July 27, 2017 10:10
Show Gist options
  • Save davidhq/67cddf1aa1fb3f0b3df749b74d5e7554 to your computer and use it in GitHub Desktop.
Save davidhq/67cddf1aa1fb3f0b3df749b74d5e7554 to your computer and use it in GitHub Desktop.
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include "ota.h"
#include "sounds.h"
WiFiServer server(80);
const char* ssid = "ssid";
const char* password = "pass";
// ********** NAME IO ***********
const int doorbellButtonPin = 2;
const int speakerPin = 5;
const int movementSensorPin = 14;
const int permitButtonPin = 16;
const int bellPermittedLedPin = 12;
int bellPermitted = false;
void permitBell(bool enable) {
bellPermitted = enable;
if(enable) {
digitalWrite(bellPermittedLedPin, LOW);
} else {
digitalWrite(bellPermittedLedPin, HIGH);
}
}
void setup() {
Serial.begin(115200);
delay(10);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
Serial.println("");
Serial.println("WiFi connected");
SetupOTA();
// ********** DEFINE IO **********
pinMode(doorbellButtonPin, INPUT);
pinMode(permitButtonPin, INPUT);
pinMode(speakerPin, OUTPUT);
digitalWrite(speakerPin, LOW);
pinMode(movementSensorPin, INPUT);
pinMode(bellPermittedLedPin, OUTPUT);
digitalWrite(bellPermittedLedPin, HIGH);
// Start TCP (HTTP) server
server.begin();
Serial.println("TCP server started");
Serial.println("SETUP FINISHED");
}
int permitButtonReadCycle = true;
void loop() {
if(WiFi.status() == WL_CONNECTED) {
ArduinoOTA.handle();
WiFiClient client = server.available();
if(!client) {
delay(10);
// doorbell button sounds a tone if doorbell is currently enabled
// after sounding the tone, doorbell is automatically disabled (then needs manual enable again)
int doorbellButtonState = digitalRead(doorbellButtonPin);
if (bellPermitted && doorbellButtonState == LOW) {
permitBell(false);
sound(speakerPin);
delay(500);
}
// button inside the house that enables/disables the doorbell and indicates this with a LED
int permitButtonState = digitalRead(permitButtonPin);
if (permitButtonState == LOW) {
if(permitButtonReadCycle) {
permitButtonReadCycle = false;
permitBell(!bellPermitted);
delay(100);
}
} else {
permitButtonReadCycle = true;
}
} else {
while(client.connected() && !client.available()) {
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
// Match the request
if (request.indexOf("/on") != -1) {
permitBell(true);
}
if (request.indexOf("/off") != -1) {
permitBell(false);
}
if (request.indexOf("/toggle") != -1) {
permitBell(!bellPermitted);
}
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.5\"></head><body>");
client.print("Doorbell is now: ");
if(bellPermitted) {
client.print("<b>ENABLED</b>");
client.println("<br><br>");
client.println("<a href=\"/off\">Disable</a><br>");
} else {
client.print("DISABLED");
client.println("<br><br>");
client.println("<a href=\"/on\">Enable until first ring</a>");
}
client.println("</body></html>");
}
} else {
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment