Back to engineering archive

Record 032 / Connected systems

ESP32 Modbus and RS485 data logger

Read field instrument values over a wired industrial interface and make them available through a connected logger.

Development work
ESP32ModbusRS485Wi-FiData logging
Project design or interface view — ESP32 Modbus and RS485 data logger
Project design or interface view — ESP32 Modbus and RS485 data logger

Firmware flow

How instrument registers become useful readings

Selected Modbus flow-rate and accumulator readers.

Each reader requests its registers, returns an unavailable value on communication failure, and otherwise decodes the words into either instantaneous flow or an accumulated reading.

The two readers are separate functions, not a guaranteed call sequence. Register layout and hardware multiplier settings are assumed by the code; no instrument validation is shown.

Read the implementation

regToFloat / readFlowRate / readPositiveAccum · selected source lines 31–52. Source inspection is not a hardware test.

float regToFloat(uint16_t hi, uint16_t lo) {
  union { uint32_t U; float F; } u;
  u.U = ((uint32_t)hi << 16) | lo;
  return u.F;
}
// Read instantaneous flow (m³/h), then convert to L/s
float readFlowRate() {
  if (node.readHoldingRegisters(0x0000, 2) != node.ku8MBSuccess) return NAN;
  return regToFloat(node.getResponseBuffer(0), node.getResponseBuffer(1)) * 0.2778;  
}

// Read positive accumulator (LONG + fraction)
float readPositiveAccum() {
  // INTEGER part at reg 0009 -> offset 8, fraction at 0011 -> offset 10
  if (node.readHoldingRegisters(0x0008, 4) != node.ku8MBSuccess) return NAN;
  // lower two regs = integer, upper two = fraction
  int32_t N  = (int32_t)node.getResponseBuffer(0) << 16
             | node.getResponseBuffer(1);
  float   Nf = regToFloat(node.getResponseBuffer(2), node.getResponseBuffer(3));
  // multiplier n is handled in hardware (M33/M31), so assume direct (no extra 10ⁿ)
  return N + Nf;
}
Open the exact source-level diagram
ActionRead the requested registers
Result · NoReturn an unavailable reading
Action · YesDecode the register wordsFlow uses a floating-point pair; accumulated volume uses an integer plus a floating-point fraction.
ActionApply the reader’s conversionFlow is multiplied by 0.2778; the accumulator adds its integer and fractional parts.
ResultReturn the reading

Technical notes

Register conversion

Separate routines read flow rate and positive accumulation. Register conversion translates the raw device response into application values.

Connected reporting

Wi-Fi, WebSocket updates and a broadcast routine connect the instrument-facing code to the viewing interface.

Flow rate and accumulation

Flow rate describes current activity; accumulated values describe a total. Both pass through the logger with their meanings kept distinct.

Engineering archiveArchive record

Project enquiries

Discuss your project.

Tell us what you’re developing and where you need engineering support.

Discuss your project