The first implementation of The Electric Time used a WebSocket connection between the Raspberry Pi and the display device to stream the measured mains frequency in real time.
While this worked fine for short sessions, I started to experience unstable communication after several hours / days of uptime: dropped connections, frozen values, and occasional buffering delays.
WebSockets are great for browser-based communication, but in this project the Raspberry Pi acts more like a data source, and the display device (an ESP32) behaves like an IoT client.
That’s why I decided to migrate the data exchange to MQTT, which is far better suited for this kind of setup.
Architecture Overview
-
The Raspberry Pi runs the frequency measurement script and publishes the measured frequency (in Hz, with 3 decimal precision) to an MQTT topic, for example:
topic: grid/frequency payload: time_stamp: 1690000000.123456 last_update_time: 1690000000.123456 frequency: 50.480A lightweight MQTT broker (Mosquitto) runs locally on the same Raspberry Pi. -
The ESP32 connects as an MQTT client, subscribes to the same topic, and updates the display in real time.
This new setup offers several advantages:
-
Automatic reconnection handled natively by MQTT libraries (no custom retry logic needed).
-
Message persistence and guaranteed delivery, even if the Wi-Fi connection briefly drops.
-
Decoupled architecture: measurement, messaging, and display are now independent and can evolve separately.
-
Scalability: any additional device can subscribe to the topic to monitor the frequency remotely.
Détourner
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.