ESP32 & ESP8266 Connectivity · Intermediate · MQTT
ESP8266 MQTT Sensor Node
A sensor that publishes to your own broker and subscribes to a command topic.
Firmware status: Generated from this project's own pin map and compiled with arduino-cli for esp8266:esp8266:nodemcuv2 before publication; bench validation on your hardware is still required.
Board: ESP8266 (NodeMCU v3 / Wemos D1 mini) · ESP8266EX @ 80 MHz · 3.3 V logic · build for esp8266:esp8266:nodemcuv2.
What you will build
A node publishing a reading every fifteen seconds and switching an output from a topic, with reconnect handled rather than hoped for.
Components
- ESP8266 board
- Analog sensor on A0
- LED or relay on a GPIO
- Local MQTT broker (Mosquitto on any machine)
Pin map for this build
| Sketch constant | Pin | What it is |
|---|---|---|
PIN_OUT_1 | 5 | Switched output — relay / MOSFET channel, 5 V coil |
PIN_SENSE | A0 | Sensor — analog output, 3.3 V maximum |
Tools
- USB programming cable
- Multimeter
- Current-limited bench supply (for anything that switches a load)
- Small screwdriver and wire cutters
Build sequence
- Run a broker and confirm it is reachable from the network with a laptop client first.
- Set the broker address and flash. Watch the connect line and the subscribe acknowledgement.
- Publish `1` to `<prefix>/set` from the broker's own client and watch the output change.
- Stop the broker, watch the node report failures, restart it and watch the node reconnect unattended.
Test and expected result
Telemetry appears on the broker every fifteen seconds, the command topic switches the output, and the node reconnects after the broker restarts.
Acceptance checks
telemetry published, command switches output, reconnect after broker restart
Safety and limits
MQTT here is unencrypted and unauthenticated on the local network. Do not carry credentials or personal data over it.
Hardware scope
MQTT publish and subscribe, retained state, reconnect with backoff.
Connection diagram
Download the connection diagram (SVG)
Download the Arduino sketch (E09_esp8266_mqtt_sensor_node.ino)
Guide source: the sketch on this page was generated from the pin map above and compiled for esp8266:esp8266:nodemcuv2 before publication.
Related projects
- ESP32 Captive Portal Wi-Fi Manager — Boot the ESP32 with no credentials and configure its network from a phone, with the portal saved to flash.
- ESP8266 Config Portal — The same idea on the cheapest board that can do it: an ESP8266 that configures itself from a browser.
- ESP8266 Sensor Portal Logger — A battery-minded sensor node you configure in the field, with the reading shown on its own setup page.