ESP32 Modbus RTU to a cloud dashboard in 15 minutes

By Synacl · 9 min read · Published

Most of the sensors and meters already installed in a plant speak Modbus RTU over RS485, and most of the pain in getting their readings onto a screen is not the protocol — it is the firmware, the broker, the database and the dashboard you end up building around it. This guide skips all of that. You will wire one RS485 sensor to an ESP32, flash it from a browser tab, and have its readings on a live dashboard with an email alert, with every Modbus detail entered in a form rather than in code. It takes about fifteen minutes the first time and five the second.

What you'll build and what you need

By the end you will have an ESP32 acting as a Modbus master on an RS485 bus, polling a sensor every ten seconds, publishing readings to the cloud once a minute, a dashboard with a chart and a gauge, and a rule that emails you when the temperature crosses a line.

You need:

Modbus RTU in three minutes

Modbus RTU is a master–slave protocol on a shared two-wire bus. One master (your gateway) asks; each slave answers only when addressed. Four things identify a reading:

  1. Unit ID (also called slave address): 1 to 247, set on the device with DIP switches, a register write or the factory default. Every device on one bus needs a different one.
  2. Serial settings: baud rate, data bits, parity and stop bits — typically 9600 8N1 for sensors and 9600 8E1 or 2400 8N1 for energy meters. The master and every slave must match exactly; a mismatch looks like silence.
  3. Register type and address. Function code 03 reads holding registers (40001-style in datasheets), 04 reads input registers (30001-style), 01 and 02 read coils and discrete inputs. Registers are 16 bits wide.
  4. Scale. A 16-bit register cannot hold 23.6, so the device stores 236 and the datasheet says "×0.1" or "divide by 10". You undo that on the platform, not on the wire.

The classic gotcha is the address offset. Datasheets that say "register 30002" or "40002" are using 1-based table numbering; the address that goes on the wire, and into the form, is one less: 1. Some vendors print the wire address already; some print hex. If your first read returns nonsense, try the address one lower.

Wire the RS485 bus

RS485 is a differential pair, so polarity matters and a common ground helps.

From To
ESP32 GPIO17 (UART2 TX) RS485 module TXD / DI
ESP32 GPIO16 (UART2 RX) RS485 module RXD / RO
ESP32 3V3 and GND RS485 module VCC and GND (check the module's voltage; most take 3.3 V or 5 V)
RS485 module A (D+) Sensor A (D+)
RS485 module B (D−) Sensor B (D−)
Sensor GND Common ground with the module, if the sensor exposes one
Sensor V+ Its own supply (the XY-MD02 takes 5–30 V)

Rules that save an afternoon: keep A with A and B with B (vendors do swap the labels, so if nothing answers, swap them once); run the bus as a daisy chain, not a star; put a 120 Ω terminator across A and B at each far end once the cable is more than a few metres; and keep the bus away from motor cables. For a single sensor on the bench, none of the termination advice matters yet.

Step 1: Flash the gateway from the browser

Plug the ESP32 in over USB and open the Synacl web flasher. Choose Synacl gateway firmware, click Connect, pick the board's serial port, and click Flash. The page detects the chip, writes the bootloader, partition table and application, and resets the board — about half a minute. If the chip is not detected, hold the board's BOOT button while clicking Connect.

Nothing is uploaded anywhere and no account is needed for this step; the board simply boots into a gateway that is waiting to be told which Wi-Fi and which account it belongs to. Leave it plugged in.

Step 2: Register and provision the gateway

Create a free account and open Gateways → Add gateway. Because the board is already flashed, skip step 1 on that page and go to step 2, Register: give the gateway a name and save. Then step 3, Connect to Wi-Fi: click Connect, pick the same serial port, and enter your Wi-Fi name and password. The credentials and the gateway's own MQTT identity are written over USB — there is no captive portal to fight — and the board reboots onto your network.

Within about ninety seconds of its first message the gateway shows online in the list, with its signal strength. From this point on you never need the USB cable again: configuration, firmware updates and diagnostics all arrive over the air.

Step 3: Add the Modbus device and its tags

Open Devices → Add device, choose the protocol rs485, pick the gateway you just registered, and fill in the bus settings. For the XY-MD02 at factory defaults:

Field Value Why
modbusId 1 Factory default unit ID
baudRate 9600 Factory default
dataBits / stopBits / parity 8 / 1 / None 8N1
tickDuration 60000 Publish to the cloud once a minute
readIntervalMs 10000 Poll the sensor every ten seconds

Publishing and reading are separate on purpose. The gateway polls the bus every ten seconds regardless, so alerts and macros that run on the gateway react within ten seconds, while the cloud only stores one reading a minute and your quota stays small. Ten seconds is also the floor: the gateway's polling loop runs on a ten-second cycle, so a smaller tickDuration does not publish any faster.

Now define one tag per register you want:

Tag name Register type Address Data type Scaling Unit
temperature input 1 int Divide by 10 °C
humidity input 2 int Divide by 10 %RH

The sensor stores tenths, so choose Divide and type 10 exactly as the datasheet lists it; the platform stores the raw register value and applies the scale for display, which means you can correct a scale later and every chart, past and present, re-scales. Set min and max on each tag if you want gauges to have a sensible range.

Two notes before you save. The gateway reads one 16-bit register per tag; if your meter exposes only 32-bit floats for a value, look for the 16-bit integer registers most meters also publish, or pick a different value. And if the datasheet says the values are in holding registers rather than input registers, change the register type — reading the right address in the wrong table returns zeros or an exception, not an error you can see on the wire.

Save. The gateway receives its new configuration over the air, starts polling, and the device's green dot appears on the Devices page after its first published reading.

Step 4: Build the dashboard

Go to Dashboards → New dashboard and open the Add widget drawer. Switch to the From device tags tab: it lists your device with both tags. Click temperature and it lands on the canvas as a ready-configured readout with the unit filled in; click humidity for the second. Then add a line chart bound to temperature for the trend and a gauge for the current value, and arrange the four on the grid. Changes auto-save; there is no save button to forget.

Breathe on the sensor. Within a poll cycle the readout ticks up and the chart draws the bump — that is your Modbus register, scaled, timestamped and streaming over a websocket to the browser.

Step 5: Add an alert

Open Rules → New rule. Keep A reading crosses a threshold, pick the device and the temperature tag, set the operator to greater than and the threshold to 30. Thresholds are entered in the displayed, scaled value — 30 means 30 °C, not the raw 300. Choose a severity, set a cooldown of 15 minutes so a value hovering around the line does not send fifteen emails, and add an email action. Save, warm the sensor past 30 °C, and check your inbox.

When it doesn't work

Where to go next

Once one sensor works, the rest is repetition: more devices on the same bus each get their own unit ID and their own entry under the same gateway, up to the gateway's tag capacity shown in the app. An energy meter with voltage, current and kWh registers is the same recipe with more tags and, for most meters, even parity. From there, gateway firmware updates arrive over the air with your settings kept, offline buffering holds readings through an internet outage, and macros let the gateway act on a reading — close a relay, ramp a Modbus setpoint — without a round trip to the cloud.

If you are still choosing a platform, the ThingsBoard alternatives guide compares this gateway-first approach with the other ways of getting Modbus data onto a dashboard.

Try it on your own hardware

Synacl is free for five devices — no card, no sales call. Flash a gateway from the browser, add your first device, and see live data in a few minutes.