Modbus Controller with MAX485 Module RS-485 TTL

Hi all,

I’m trying to use the new Modbus Controller component to “replicate” the following arduino IDE code without success. I’d appreciate any input.

I’m using a Modbus to TTL converter like this one: MAX485 Module RS 485 TTL Turn To RS485 MAX485CSA Converter Module For Arduino Microcontroller MCU Development Accessories|Integrated Circuits| - AliExpress

//RAH: 170721, Updated to free up I2C pins (5 & 4). Anemometer is on separate ESP.

#include <ESP8266WiFi.h>
#include <ModbusMaster.h>
#include <PubSubClient.h>
#include <SoftwareSerial.h>

// Wind Vane
#define WDIR_MAX485_DE       D5   // DE, Driver Output Enable
#define WDIR_MAX485_RE_NEG   D6   // RE, Receiver Output Enable
#define WDIR_RX              D7   // RO, Receiver Output (RX)
#define WDIR_TX              D0   // DI, Driver Input (TX)
#define WDIR_TOPIC "w7_station2/sensor/bearing"

#include "secrets.h"

SoftwareSerial serialWDir(WDIR_RX, WDIR_TX);

ModbusMaster modbusWDir;

WiFiClient espClient;
PubSubClient mqtt_client(espClient);

//-------------------------------------------------------------------------

void enableTxWDir () {
  digitalWrite(WDIR_MAX485_RE_NEG, 1);
  digitalWrite(WDIR_MAX485_DE,     1);
}

void disableTxWDir () {
  digitalWrite(WDIR_MAX485_RE_NEG, 0);
  digitalWrite(WDIR_MAX485_DE,     0);
}

//-------------------------------------------------------------------------
void wifi_setup() {
  delay(2000);
  WiFi.hostname(hostname);
  WiFi.begin(wifi_ssid, wifi_pass);
  while (WL_CONNECTED != WiFi.status()) {
    delay(500);
  }
}

//-------------------------------------------------------------------------
void mqtt_reconnect() {
  while (!mqtt_client.connected()) {
    if (mqtt_client.connect(hostname, mqtt_user, mqtt_pass));
    delay(5000);
  }
}

//-------------------------------------------------------------------------
void setup() {
  // Wind Vane
  pinMode(WDIR_MAX485_RE_NEG, OUTPUT);
  pinMode(WDIR_MAX485_DE,     OUTPUT);
  disableTxWDir();

  wifi_setup();
  mqtt_client.setServer(mqtt_server, mqtt_port);
  serialWDir.begin(9600);
  Serial.begin(9600);

  modbusWDir.begin(1, serialWDir);
  modbusWDir.preTransmission(  enableTxWDir);
  modbusWDir.postTransmission(disableTxWDir);
}

//-------------------------------------------------------------------------
void loop() {
  if (!mqtt_client.connected()) {
    mqtt_reconnect();
  }
  mqtt_client.loop();

  // Wind vane
  uint8_t resultMain_d;
  resultMain_d = modbusWDir.readHoldingRegisters(0x0000, 1);
  if (modbusWDir.ku8MBSuccess == resultMain_d) {
    float wdir = (modbusWDir.getResponseBuffer(0x00) / 100.0f);
    mqtt_client.publish(WDIR_TOPIC, String(wdir).c_str(), true);
  }
  delay(5000);
}

//-------------------------------------------------------------------------

My ESPhome config is currently:

logger:
  level: INFO
  baud_rate: 0

uart:
  id: mod_bus
  tx_pin: D0
  rx_pin: D7
  baud_rate: 9600
  stop_bits: 1

modbus:
  flow_control_pin: D6
  id: modbus1

modbus_controller:
  - id: wdir
    address: 0x00
    modbus_id: modbus1

sensor:
  - platform: modbus_controller
    modbus_controller_id: wdir
    name: "Bearing"
    id: bearing
    register_type: coil
    address: 0x0000
    unit_of_measurement: "°"
    value_type: U_WORD

Thanks all.


Roland

hello!
you solve the problem?
How did you solve the problem?

Hi @avense - I never solved this and just use my working Arduino code instead.

@HillNet @avense

You might want to check out https://github.com/syssi/esphome-modbus-solax-x1/issues/18
It has a suggested usage/wiring diagram for this RS485 board, with which I was able to communicate with a temperature regulator from esphome.

2 Likes