STM32 plus ESP32 industrial IoT gateway architecture diagram

STM32 + ESP32 Industrial IoT Gateway: Build a Reliable Edge-to-Cloud Bridge

Key Takeaway: Pairing an STM32 for deterministic sensor aggregation with an ESP32 for secure wireless uplink gives you an industrial-grade IoT gateway that is more robust, cheaper to maintain, and easier to scale than a single general-purpose board.

STM32 plus ESP32 industrial IoT gateway architecture

1. Why Split the Workload Between Two MCUs

A common mistake in industrial IoT projects is to overload a single microcontroller with everything: reading analog sensors, running communication stacks, handling Wi-Fi, and storing data. A STM32 + ESP32 split fixes this by giving each chip a job it is best at. The STM32 handles real-time analog acquisition, digital I/O, CAN bus and RS-485 — tasks that demand deterministic timing. The ESP32 handles Wi-Fi, Bluetooth, TLS encryption and MQTT — tasks that need a rich protocol stack but not hard real-time guarantees.

This division matters on a factory floor. If the Wi-Fi drops, the STM32 keeps sampling and buffering locally, so you never lose a measurement. If the STM32 reboots, the ESP32 keeps the cloud session alive. The two-processor design is inherently more fault-tolerant than a single-board approach.

2. Hardware Architecture

The reference design uses an STM32F4 or STM32G4 as the sensor hub and an ESP32-WROOM-32 as the radio. They talk over a simple UART running a lightweight framing protocol, or over SPI if you need higher throughput.

Key interfaces on the STM32 side:

  • Multiple 12-bit ADC channels for temperature, vibration and 4-20 mA current loops
  • I2C and SPI for digital sensors and local FRAM/EEPROM
  • CAN bus for motor drives and industrial actuators
  • RS-485 (via transceiver) for Modbus RTU to legacy PLCs

The ESP32 connects to the plant network over Wi-Fi or, with a modem, cellular. It runs an MQTT client with TLS 1.2 and pushes batched JSON telemetry to a broker such as Mosquitto or a cloud endpoint.

3. STM32 Firmware: Sensor Aggregation and Pre-processing

On the STM32, the focus is sampling discipline. Use timer-triggered DMA to capture ADC bursts without CPU intervention, then apply a simple moving-average or first-order IIR filter to remove noise before sending. Only transmit reduced data — averages, peaks, alarm flags — over the UART link. This keeps the ESP32’s queue small and the radio airtime low.

A clean pattern is a ring buffer on the STM32 that stores the last N samples. On a trigger (threshold exceeded, or a heartbeat timer), the STM32 packages a frame and hands it to the ESP32. If the link is down, the buffer holds the data until connectivity returns — true edge buffering.

This is the same timing discipline we rely on in STM32 timer interrupts for precise motor timing, where deterministic sampling is non-negotiable.

4. ESP32 Firmware: Secure MQTT Uplink

The ESP32 runs a lightweight RTOS task loop: receive frames from the STM32 over UART, queue them, and publish to the broker. Use the Arduino ESP32 core or the ESP-IDF. Always enable TLS and store certificates in secure NVS. Implement a reconnect backoff so a flaky access point does not cause a storm of connection attempts.

OTA (over-the-air) firmware updates are a major advantage of the ESP32. You can push gateway firmware fixes without touching the STM32, and vice versa. This is what makes the platform maintainable across a fleet of machines.

5. Bridging Legacy PLCs with Modbus/TCP

Most factories already run PLCs. The STM32 can speak Modbus RTU over RS-485 to a controller, then the ESP32 exposes that data to the cloud as Modbus/TCP or as MQTT topics. This turns a decades-old machine into a connected asset without replacing its control system. For PLC retrofits on a budget, see building a custom PLC with Arduino Mega for the low-cost control angle.

Protocol translation is straightforward: poll holding registers on a fixed scan cycle, map them to JSON, and publish. Keep the poll rate modest (1-5 Hz) to avoid overloading the PLC.

6. Power, Enclosure and Deployment

Power the gateway from a 24 V DC DIN-rail supply with a small buck converter for the 3.3 V rails. Put it in an IP65 enclosure near the machine. Add a watchdog on each MCU and a supervised heartbeat between them, so a hung processor forces a recovery. For a broader view of gateway options, our edge computing in manufacturing guide covers where gateways sit in the smart factory stack.

Frequently Asked Questions

Why not use a single Raspberry Pi or ESP32 for everything?

A Pi is overkill for deterministic I/O and SD cards fail in vibration. A lone ESP32 cannot guarantee real-time ADC sampling under Wi-Fi load. Splitting the work gives you both reliability and low cost.

What baud rate should the STM32-ESP32 UART use?

115200 baud is a safe default for JSON frames. If you stream raw waveforms, move to SPI at several MHz or compress before sending.

Can this gateway run offline?

Yes. The STM32 ring buffer and the ESP32’s queue let the system ride out hours of network loss, then flush on reconnect.

Is TLS on the ESP32 fast enough?

For 1-5 Hz telemetry, absolutely. Handshake overhead is amortised across many small publishes.

Sources

  1. STMicroelectronics — STM32 32-bit ARM Cortex MCUs
  2. Espressif — ESP32 SoC
  3. MQTT.org — MQ Telemetry Transport

Disclosure: This post contains affiliate links. If you buy through them, justLast.in may earn a commission at no extra cost to you. We only recommend gear we would use in our own builds.

STM32 plus ESP32 industrial IoT gateway architecture diagram