~jonahbron/doorbell

Doorbell

5ca07ee Added MIT license

4 months ago

3c6b55e Allowed HTTP server to restart after WDT for remote diagnostics, plus a logs/reset endpoint. Caught exceptions on Push

4 months ago

#Status

This product currently works, with a few workarounds.

Setup requires accessing the web server over localhost so that the browser treats the context as secure. This can be done by reverse-proxying port 8001 from the Android device to the development machine with ADB (wireless debugging works), the again reverse-proxying that port to the IP of the doorbell device.

After setting that up, http://localhost:8001 can be visited on a mobile device, notifications enabled and Push subscription registered with the doorbell. With that complete, the port forwarding can be halted, and the doorbell/phone will operate as normal.

#To-do

  • [x] Design hardware
  • [x] Expose web server for setting up push subscription
  • [x] Send push requests with VAPID headers directly from embedded device
  • [x] Accept API calls to unlock the door
  • [x] Build firmware with Nix instead of manual
  • [x] Add watchdog timer with machine.WDT
  • [ ] Open port via UPnP
    • Prevents the need for manual router port forwarding
  • [ ] Send current IP in Push
    • Prevents the need for dynamic DNS
  • [ ] Secure the unlock command
    • Supply the hash of the Push endpoint?
    • Must be safe against MITM attach if used without TLS
  • [ ] ACME protocol for web serving over HTTPS from embedded device
    • Allows setup without ADB port forwarding
  • [ ] Nicer web application
  • [ ] Wi-Fi setup without mpremote edit
  • [x] Python/web code OTA update
  • [ ] Atomic safe updates
    • [x] Switch to LittleFS
    • [ ] Monitor for repeated immediate failure with automatic OTA rollback
  • [ ] firmware OTA update
  • [ ] Export Gerber/drill files with kicad_cli instead of manual

#Software

Currently using MicroPython firmware on the XIAO.

Execute the current program on-device by running:

nix run .#

Write the program to the hardware's memory by instead running:

nix run .#deploy-lib; nix run .#deploy-bin

The device listens for incoming requests on port 80. In order to access it from outside of the local network, the network router must be configure to forward an external port to the doorbell device. The external port can be different from the device's open port as long as the forwarding maps correctly.

Browsers only support Service Workers and the Notification API in "secure contexts". Localhost is one such secure context. In order to test on the development machine, forward a local port to the doorbell.

nix run .#forward-port

#Android

For debugging with Android, it's helpful to use ADB to forward a device-local port to the doorbell device. Use WiFi debugging.

  1. Enable Wireless Debugging on Android
  2. Run nix run .#forward-port-android

This reverse-proxies from Android to the host computer, AND forward the port on the dev machine.

#Firmware

The Seeed Studio XIAO ESP32C3 can have its MicroPython firmware flashed fresh by running

nix run .#flash-firmware

A new microcontroller needs to be loaded with configuration. Run

mpremote edit etc/config.json

To open the config file, and insert a JSON object with keys ssid, key, and port. For example:

{"ssid":"My WiFi","key":"wifi password","port":"80"}

Dependencies must also be installed.

nix run .#deploy-lib

#Windows Notes

Requires USBIPD-WIN set up with the device shared into the guest system.

https://learn.microsoft.com/en-us/windows/wsl/connect-usb#attach-a-usb-device

Ensure that NixOS WSL'd USBIP configuration is set up too.

https://nix-community.github.io/NixOS-WSL/options.html#wslusbipenable

Run this command to see the devices:

usbipd list

Ensure the bus ID is in the auto attach list for the NixOS WSL config.

https://nix-community.github.io/NixOS-WSL/options.html#wslusbipautoattach

Ensure user is in the dialout group.

#KiCad Designs

The doorbell/ directory contains the KiCad project. Inside of that, is the kicad-deps dir, which contains third-party dependency symbol/footprint files.

#Fabrication

See Oshpark documentation: https://docs.oshpark.com/design-tools/kicad/generating-kicad-gerbers/

Uploading KiCad files directly is easier, but using Gerber files is necessary because it vectorizes the text, preserving font choice.

  1. Open PCB in KiCad
  2. File > Fabrication Outputs > Gerbers
  3. Export to doorbell/export/
  4. Include layers {F,B}.{Cu,Paste,Silkscreen,Mask} and Edge.Cuts
  5. Click "Plot"
  6. Click "Generate Drill Files..."
  7. Zip everything in export/
  8. Upload archive to Oshpark