-- Leo's gemini proxy
-- Connecting to capsule.adrianhesketh.com:1965...
-- Connected
-- Sending request
-- Meta line: 20 text/gemini; charset=utf-8
I'm on holiday this week, so I've had a chance to do some fun stuff at home. My 8 year old son saw the Google Nest products on YouTube and wanted to create his own version with a Raspberry Pi with a camera module.
Rather than spend £30 on a Raspberry Pi, £12 for a camera module etc., I suggested we could build a minimal viable product first using a much cheaper ESP 8266 (£3) hooked up to a reed switch on the door, plus I had one lying around. I ordered a reed switch from Amazon for £2.
First step was to get the ESP 8266 to react to switch events on a digital input. We got it working based on this [0] but instead of lighting an LED, sending the switch status using `Serial.write` and using the Ardiuino IDE Serial Monitor to check it worked.
Once that was working, we wanted a Web server to notify. The cheapest and easiest way to do this is to use the Serverless Framework to set up an API inside the AWS infrastructure. I chose JavaScript, because my son knows a bit of that, but I should have stuck to Go, because I had to try and explain callbacks and promises to an 8 year old.
Once it was printing to the console, I set the API up to email him when the API receives a POST.
At this point, we had an API and the ESP 8266 handling switch notifications, the next part was to get that ESP 8266 onto the home wifi, and send a POST notification to the API.
Here's all the code:
#include <WiFiServerSecure.h> #include <WiFiClientSecure.h> #include <ESP8266WiFi.h> #include <ESP8266WiFiMulti.h> #include <WiFiUdp.h> #include <ESP8266WiFiType.h> #include <ESP8266WiFiAP.h> #include <WiFiClient.h> #include <WiFiServer.h> #include <ESP8266WiFiScan.h> #include <ESP8266WiFiGeneric.h> #include <ESP8266WiFiSTA.h> #include "RestClient.h" #include "Debounce.h" // Uses: // * https://github.com/wkoch/Debounce // * https://github.com/esp8266/Arduino/tree/master/doc/esp8266wifi // * https://github.com/csquared/arduino-restclient // To install libraries: // * cd ~/Documents/Arduino/libraries // * git clone <repo> // * (For https://github.com/csquared/arduino-restclient, git clone https://github.com/csquared/arduino-restclient RestClient) const char *ssid = ""; const char *password = ""; RestClient client = RestClient("xxxxx.execute-api.eu-west-1.amazonaws.com", 443, 1); const char *eventDoorOpened = "{ \"event\": \"door_opened\" }"; const char *eventDoorClosed = "{ \"event\": \"door_closed\" }"; const int inPin = D1; void setup() { pinMode(inPin, INPUT); Serial.begin(115200); // Using WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.print("Connecting.."); } Serial.println("Connected..."); } Debounce doorSwitch(inPin); // the number of the input pin, D1 is set for the ESP8266. int previousState = -1; void loop() { int newState = doorSwitch.read(); if (previousState == -1) { // On the first iteration, just set it to the latest value and don't trigger an event. // This makes the device succeptible to a power outage making it not work. previousState = newState; } if (newState != previousState) { if (newState == HIGH) { Serial.println("Door was closed"); postSecure(eventDoorClosed); } else { Serial.println("Door was opened"); postSecure(eventDoorOpened); } } previousState = newState; } void postSecure(const char *event) { String response = ""; Serial.println("Posting..."); int statusCode = client.post("/dev/sensor/detect", event, &response); Serial.println("Results (status / response)..."); Serial.println(statusCode); Serial.println(response); }
I simplified the micro-controller code by using a few libraries. A debounce library to handle switch noise, and a HTTPS RestClient library, since the default doesn't support HTTPS. Since there's hardly any RAM and no disk, there's no space to store information about valid root certificates etc. For a toy project, there's no need to protect against invalid or expired certificates.
Once we had it working on the table it was time to upscale it. I bought 25m of doorbell cable from the local ScrewFix for £3, and soldered the ends of it onto a bit of stripboard I had lying around, along with the required resistor. I did this more for the fun of introducing my son to soldering than anything else, because we could have just used a connector block for a single resistor and a couple of cable connections.
Then, we put it in an ice cream tub to make it more professional.
So, we're onto the final step - installation and decoration of the box!
Of course, I already had a few pre-requisites (laptop for coding, multimeter for testing, screwdrivers, optional soldering iron) but the parts themselves cost less than £10:
- ESP 8266 - £3
- Breadboard - £2
- Cable - £3
- Switch - £2
- Ice cream tub - free
-- Response ended
-- Page fetched on Sun Apr 28 01:33:24 2024