Skip to content

Instantly share code, notes, and snippets.

@Bougakov
Created May 22, 2020 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bougakov/749a71b3a7d91d654275684eee5e267b to your computer and use it in GitHub Desktop.
Save Bougakov/749a71b3a7d91d654275684eee5e267b to your computer and use it in GitHub Desktop.
Code for ESP8266 "smart" door bell
#ifdef ARDUINO_ARCH_ESP8266
#include <ESP8266WiFi.h>
#endif
#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#endif
#include <esp8266-google-home-notifier.h>
#include <ESP8266HTTPClient.h>
const char* ssid = "WiFi_network";
const char* password = "WiFi_pass";
const int buttonPin = 15; // D8 is "15"
/*
D0/GPIO16 can not be used as input as it's connected to the RESET: pulling it low causes a reset of the processor (which is why you see the LED blink).
The ADC has a voltage divider attached to it, so its range is 0-3.3V rather than the 0-1.06V the ESP8266 normally has. This divider may mess up an analog signal that itself is also based on a voltage divider!
D4/GPIO2 is connected to the internal LED (which is lit when the pin is LOW).
D9/GPIO3 is TX (Serial).
D10/GPIO1 is RX (Serial).
*/
const int ledPin = 2; // D4 is "2"
const int resetPin = 16; // D0 is "16"
GoogleHomeNotifier ghn;
const char displayName[] = "Name of your Google Home speaker" // for example, "Bedroom speaker";
void resetFunc () {
Serial.println("Self-resetting...");
WiFi.forceSleepBegin();
delay(100);
ESP.restart(); // To restart the ESP8266, we need to call the restart method on the ESP extern variable, which is an object of class EspClass
delay(1000);
digitalWrite(0, HIGH);
digitalWrite(resetPin, LOW);
ESP.reset(); // If above didn't help...
}
void setup () {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.begin(115200);
Serial.println("");
WiFi.begin(ssid, password);
WiFi.hostname("esp8266_doorbell");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting..");
}
Serial.print("Connected to [");
Serial.print(ssid);
Serial.println("] access point.");
// power();
}
void loop() {
if (digitalRead(buttonPin) == HIGH) {
Serial.println("Ding!");
digitalWrite(ledPin, LOW);
bell();
}
digitalWrite(ledPin, HIGH);
if (WiFi.status() != WL_CONNECTED) {
Serial.println("No WiFi connection!");
resetFunc();
} else {
// Serial.println("Connected.");
}
}
void bell() {
digitalWrite(ledPin, LOW);
if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
HTTPClient http; //Declare an object of class HTTPClient
http.begin("http://example.com/doorbell-rings/do/something"); //Specify request destination
int httpCode = http.GET(); //Send the request
Serial.println(httpCode);
if (httpCode > 0) { //Check the returning code
String payload = http.getString(); //Get the request response payload
Serial.println(payload); //Print the response payload
} else {
Serial.println("No WiFi connection!");
resetFunc();
}
http.end(); //Close connection
googlehome();
} else {
Serial.println("No WiFi connection!");
resetFunc();
}
Serial.println("Idling to prevent multiple messages being sent repeatedly.");
delay(30000); //Send a next request only after half a minute has passed.
}
void googlehome() {
Serial.println("connecting to Google Home...");
if (ghn.device(displayName, "en") != true) {
Serial.println(ghn.getLastError());
return;
}
Serial.print("found Google Home(");
Serial.print(ghn.getIPAddress());
Serial.print(":");
Serial.print(ghn.getPort());
Serial.println(")");
if (ghn.notify("Hey! Someone is ringing the door bell!") != true) {
Serial.println(ghn.getLastError());
return;
}
Serial.println("Done.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment