ESP32 Flowmeter and RS485 Modbus

 

As described on the article ESP32 programming, Arduino - install requirements, my first goal was to read out a TUF-2000M Ultrasonic Flow Meter via an ESP32. I found an example for an ESP8266 on the internet: Reading a TUF-2000M Ultrasonic Flow Meter with an Arduino or ESP8266 and https://forum.arduino.cc/t/comunicacion-rs485/698786/2. I described the setup of the TUF-2000M in the following article: Field Report: Ultrasonic Flow Meter TUF-2000M. To be able to read out the TUF-2000M via RS485, it has to be connected to the ESP32 via the RS485 converter:

Connect hardware

RS485 and ESP32

RS485 to TTL 5V converter with MAX13487 chip for Raspberry Pi Arduino and other MCU:

The supplied cable can be connected to the ESP32 as follows:

 

Connect TUF-2000M

After we have connected everything, we can take care of the software:

Arduino

As an alternative to the Arduino IDE, ESP-Home can be used for HomeAssistant.

As preparation for the sketch we need the packages in Arduino: ModbusMaster and EspSoftwareSerial:

Modbus Master

Software Serial

Error 226

At the first try I used the two PINs RX and TX for the communication to the RS485 board out of ignorance, which ended in an error 226. The display mirrored the output of the serial monitor to the TUF-2000M: Menu 49, see: Serial Port Traffic

...
RX_PIN RX
TX-PIN TX
...

After connecting the RS485 board to PINS 16 and 17 of the ESP32, I was able to read out the flowmeter:

...
RX_PIN 17
RX_PIN 16 
...

WLAN, MQTT and Flow

Combined with ESP32 WiFi and ESP32 MQTT - Send Data, I was able to write the current value to the MQTT broker and thus into HomeAssistant with the following sketch:

[+]
#include 
#include 
#include 
#include 

#define RX_PIN 17 // connect to converter's RX wire
#define TX_PIN 16 // connect to converter's TX wire
#define MODBUS_DEVICE_ID 1
SoftwareSerial swSerial(RX_PIN, TX_PIN);
ModbusMaster sensor;

const char* ssid = "home";
const char* password = "???";
const char* mqttServer = "192.168.1.5";
const int mqttPort = 1883;
const char* mqttUser = "mqtt";
const char* mqttPassword = "???";

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  Serial.begin(9600);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(5000);
    Serial.println("Connecting to WiFi..");
  }
  
  Serial.println("Connected to the WiFi network");
  
  swSerial.begin(9600);
  sensor.begin(MODBUS_DEVICE_ID, swSerial);
    
  client.setServer(mqttServer, mqttPort);
  
  while (!client.connected()) {
    Serial.println("Connecting to MQTT...");    
    if (client.connect("ESP32Client", mqttUser, mqttPassword )) {    
      Serial.println("connected");    
    } else {    
      Serial.print("failed with state ");
      Serial.print(client.state());
      delay(10000);
    }
  }
}

void loop() {
  client.loop();
  readFlow();
  delay(3000);
  if (!client.connected()) {
    delay(60000);
    if (!client.connected()) {
      ESP.restart();
    }    
  }
}

void readFlow() {
  uint8_t result;
  uint16_t buf[2];
  float flow;
  result = sensor.readHoldingRegisters(1, 2);
  if (result == sensor.ku8MBSuccess)
  {
    buf[1] = sensor.getResponseBuffer(0);
    buf[0] = sensor.getResponseBuffer(1);
    memcpy(&flow, &buf, sizeof(float));
    client.publish("flowmeter/flow",  String(flow).c_str());
  }
}

For integration into Home-Assistant, see: Home-Assistant MQTT

ESPHome version

In the meantime I use ESP-Home for the readout. The ESPHome version replaces the sketch presented here and looks like this for me:

[+]
esphome:
  name: heating

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "??"

ota:
  password: "??"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Heating Fallback Hotspot"
    password: "??"

captive_portal:

uart:
   id: uart_1
   tx_pin: 16
   rx_pin: 17
   baud_rate: 9600
   stop_bits: 1   
   parity: none

modbus:
   id: modbus_1
   uart_id: uart_1
   send_wait_time: 1000ms

modbus_controller:
 - id: tuf2000m
   address: 0x1
   modbus_id: modbus_1
   setup_priority: -10
   update_interval: 3s


# Individual sensors
sensor:
  - platform: modbus_controller
    modbus_controller_id: tuf2000m
    name: "flowmeter"
    id: flow
    register_type: holding
    address: 0x1
    register_count: 2
    response_size: 2
    accuracy_decimals: 3
    value_type: FP32
    unit_of_measurement: "m³/h"
  - platform: modbus_controller
    modbus_controller_id: tuf2000m
    name: "flowmeter_travelTimeRate"
    id: flow_travelTimeRate
    register_type: holding
    address: 0x61
    register_count: 2    
    accuracy_decimals: 2
    response_size: 2
    value_type: FP32
  - platform: modbus_controller
    modbus_controller_id: tuf2000m
    name: "flowmeter_fluidSoundSpeed"
    id: flow_fluidSoundSpeed
    register_type: holding
    address: 0x7
    register_count: 2    
    accuracy_decimals: 2
    response_size: 2
    value_type: FP32

see: esp-home

 

positive Bewertung({{pro_count}})
Rate Post:
{{percentage}} % positive
negative Bewertung({{con_count}})

THANK YOU for your review!

Publication: 2023-07-13 from Bernhard | Übersetzung Deutsch |🔔 | Comments:0

ESP32 MQTT - Send data | ESP32 | ESPHome: Temperature and humidity sensors DHT11/22
Field report: Ultrasonic flowmeter TUF-2000M

Top articles in this section


DS18B20 - Temperature sensors in ESP-Home

Complementary to the article: DS18B20 Temperature Sensors ESP32, MQTT and WiFi - HowTo, I have meanwhile replaced the Arduino project with ESP-Home. Simple projects can be implemented much easier in ESPHome. As an example, in ESPHome these 2 lines are enough to address the temperature sensors:


Home Assistant + DIY Microcontroller + ESP Home (Docker)

With ESPHome it is very easy to program your own microcontroller for use in HomeAssistant. My first use for a self-programmed microcontroller was to record the water flow and temperature values of my heater, see: ESP32 programming, Arduino - install requirements.


add a Relay Board ESP32 - ESPHome

To be able to switch certain relays in Home Assistant via an ESP32, I tested a relay board and integrated it via ESP-Home.

Questions / Comments


By continuing to browse the site, you agree to our use of cookies. More Details