ESP-01 Wifi-button, using MQTT to trigger, using fast wifi-connection tricks. Regularly connects to wifi and sends MQTT requests within 1300ms.
MIT license / (c) 2022 John Mueller
Sample hardware uses an ESP-01 module, with pin 3 being used to control the power to the module. The hardware could be simplified to go to deep sleep, but I like the idea of pulling out your own power cord :).
- Button turns power on briefly (needs ca 100ms at least)
- ESP-01 wakes up, pulls power line high to keep power on
- ESP-01 does a fast connect to the wifi AP
- ESP-01 sends MQTT packet as desired
- ESP-01 turns power line low & goes offline
Part of what makes the wifi connection to the AP slow is getting the BSSID of the AP, getting the right channel, getting a local IP address, getting the DNS information, and getting the IP of the MQTT server. We can cache all of these and reduce the timing significantly. This is particularly possible if the device we're using is in a stable location (same AP, channel), and the AP being connected to is mostly static (same channel, local IPs).
-
Check if we have a cache available
- If so:
- try to connect to the last-known BSSID
- try to use the last-provided device IP, DNS
- pre-connect to MQTT using the IP address, port
- send MQTT packets
- If not:
- Connect using traditional methods
- Get MQTT server's IP address
- save cached connection information (BSSID, IPs, etc)
- connect & send MQTT packets
- If no valid settings:
- Jump to AP mode below
- If so:
-
Blink a bit and pull own power plug
-
If someone's still pushing the button, refresh the wifi connection cache
-
Wait 4 seconds
-
If someone's still pushing the button, start AP mode (blink at 0.5Hz).
-
Remain in AP mode 5 minutes, await connection
-
If connection: remain in AP mode for 5 minutes
Implements a captive portal so you just need to connect to the AP with your (Android) phone and it'll take you to the home page directly. For direct access, use http://192.168.4.1/ .
The name of the access point depends on the device's MAC address, it looks something like "AP_AA1122".
The homepage of the access point allows configuration of wifi name, authentication, MQTT server settings, and MQTT request to send upon click. It does not check the wifi settings, but if they're wrong, it'll revert to the AP mode again.
- add hardware schematic, circuit board
- confirm timings of individual steps
- double-check char-array sizes
- OTA not possible on 500MB devices: Flash usage > 250MB