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 constantPinWhat it is
PIN_OUT_15Switched output — relay / MOSFET channel, 5 V coil
PIN_SENSEA0Sensor — 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

  1. Run a broker and confirm it is reachable from the network with a laptop client first.
  2. Set the broker address and flash. Watch the connect line and the subscribe acknowledgement.
  3. Publish `1` to `<prefix>/set` from the broker's own client and watch the output change.
  4. 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

Connection diagram for ESP8266 MQTT Sensor Node

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

Back to all project guides · Study the course