Tasmota: Proposal for Modbus devices integration

Created on 22 May 2018  Â·  29Comments  Â·  Source: arendst/Tasmota

Various Modbus (https://en.wikipedia.org/wiki/Modbus) devices (sensors, relays board, ...)
are available on the market at affordable price. These kind of devices can be connected
in "daisy chain" mode up to 247 different items using only three pins on ESP (TX/RX/GND)

I developed (and integrated in Sonoff-Tasmota) two power meter (SDM120/SDM630) using
the common "pattern" of pins specification in sonoff_template.h. Users can select two free pins and
assign them to SDM-Tx and SDM-Rx in order to collect on Sonoff power consumption statistics and
publish these metrics on MQTT.

Although this solution works fine it does not exploit all the flexibility of Modbus/RS485 because to use
two different meters user need to "sacrify" two pins for each device making "daisy chain" of sensors
not applicable by design...

With this feature request I'would like to discuss a possible integration of Modbus/RS485 as "bus"
inside Sonoff-Tasmota (like KNX) with a custom menu' where it is possible to select id and device
type available on the Modbus chain. Btw as a first approach we can think to configure bus id and type of devices only by MQTT messages...

let me know what do you think about !

p.s. take a look to these kind of Modbus relays board:

https://www.aliexpress.com/item/8-Channel-DC-12V-RS485-Relay-Module-Modbus-RTU-485-Remote-Control-Switch-for-PLC-PTZ/32862918287.html

https://www.aliexpress.com/item/Modbus-RTU-4-Way-Relay-Module-DIY-STM8S103-System-4-Road-Input-485-Communication/32831233056.html

https://www.aliexpress.com/item/16-MODBUS-485-serial-interface-module-relay-control-board-module/32348091089.html

enhancement

Most helpful comment

Hi, i like this gateway thing.
I dont want to have dozens of semi inteligent esp devices. they should be as stupid as possible.
i want to have one main thing (nodered, hass, home...) that steers everthing.
who cares what is shown on the webpage of esp device. You can do much more with grafana / nodered or all the other tools for visualisation.

but the idea for specialized drivers is also valid to get a easy start with some mainstream rs485 devices like powermeter, switches.

thumps up for both.

All 29 comments

Hi Gennaro,

I thought of this a while ago but found no time to implement it. My
initial idea was to do a kind of Modbus-GW not using Modbus TCP but MQTT
to send the required registers and get the result values back via MQTT.
This way everything has to be handled by your automation software on the
other side. The SDM integration is nice but I have some more
Modbus-Powermeters in use with different register sets and calculations
to do. When talking about even more sensors it would be rather difficult
to do more than a gateway solution or you will blow up the code when
putting all those register-maps and required calculations for many
different sensors into Tasmota or you need to be able to not only define
the address and query registers but also the read type (coil, register,
holding register eg...and this for read and write), how many response
packets to expect, what type they are and if there are multipliers for
the read values (eg. Janitza UMG96 and current transformer ratio). Nice
idea, maybe small change to make modbus a real bus when using multiple
SDM on one bus but having lots of different Modbus-Devices as pre
defined senors could be a bit too much for Tasmota. Did you test your
code with other devices? I can't see start or end-frames or inter
character spacing or timeout-handling. The SDM seems to be very kind in
handling the RTU requests but other devices might be more strict in
protocol implementation
(http://www.modbus.org/docs/Modbus_over_serial_line_V1.pdf). Depending
on the RS485-converter you use there's also a need to toggle Send and
Receive-Mode. So YES, it's a great Idea to support more Modbus-Devices
but this requires testing every single device to see if it works with
this simple modbus protocol implementation and if there is not to much
code size required to support all this devices and how to group them to
use the tasmota web and mqtt data format.

Greetings and thank you for your SDM-Code,

      Altelch.

Am 22.05.2018 um 16:20 schrieb Gennaro Tortone:
>

Various Modbus (https://en.wikipedia.org/wiki/Modbus) devices
(sensors, relays board, ...)
are available on the market at affordable price. These kind of devices
can be connected
in "daisy chain" mode up to 247 different items using only three pins
on ESP (TX/RX/GND)

I developed (and integrated in Sonoff-Tasmota) two power meter
(SDM120/SDM630) using
the common "pattern" of pins specification in sonoff_template.h. Users
can select two free pins and
assign them to SDM-Tx and SDM-Rx in order to collect on Sonoff power
consumption statistics and
publish these metrics on MQTT.

Although this solution works fine it does not exploit all the
flexibility of Modbus/RS485 because to use
two different meters user need to "sacrify" two pins for each device
making "daisy chain" of sensors
not applicable by design...

With this feature request I'would like to discuss a possible
integration of Modbus/RS485 as "bus"
inside Sonoff-Tasmota (like KNX) with a custom menu' where it is
possible to select id and device
type available on the Modbus chain.

let me know what do you think about !

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/arendst/Sonoff-Tasmota/issues/2803, or mute the
thread
https://github.com/notifications/unsubscribe-auth/ACdAcdahfnZqjl-cWRjzTyyQ9aXUyq7dks5t1B6ugaJpZM4UIvm7.

Hi Altelch,
thanks a lot for your comments and suggestions !

The suggestion of a MQTT message containing the required register to read/write is very interesting, as you write this "gateway" mechanism is very nice because it relieves developers to write new code for each sensor to integrate... a drawback for this solution is that Sonoff-Tasmota is completely "transparent" to values read/write from outside and it is difficult to make available on Sonoff-Tasmota homepage a kind of report about metrics;

regarding Modbus protocol I'm not a Modbus expert but after some read of web pages I learned that a lot of devices implement "Modbus-RTU" protocol (https://www.rtaautomation.com/technologies/modbus-rtu) a subset of "full" Modbus protocol and reading description of protocol I see that is the same SDM devices decode; the relay boards I linked on previous post also implement this kind of protocol;

If it will be possible I would like to follow both ways: the gateway and Modbus-RTU with specialized drivers...

Hi, i like this gateway thing.
I dont want to have dozens of semi inteligent esp devices. they should be as stupid as possible.
i want to have one main thing (nodered, hass, home...) that steers everthing.
who cares what is shown on the webpage of esp device. You can do much more with grafana / nodered or all the other tools for visualisation.

but the idea for specialized drivers is also valid to get a easy start with some mainstream rs485 devices like powermeter, switches.

thumps up for both.

Last Tasmota update contains

* Add Eastron SDM630 energy meter by Gennaro Tortone (#2735)

based on modbus.

Hi Genarro, as expected the user wants it all and that will provide challenges...

I guess the amount of resources Modbus needs, especially if you want to extent the webpages, will make tasmota even more "sluggish" and prone to short of memory exceptions. Currently I try to keep HTML pages as small as possible as there is just not enough memory to provide a plain HTML page for a module configuration for instance. Also rules will bare a heavy load on memory and processing time slots.

By now you probably guess right that I'm more a fan of the MQTT gateway approach providing a stable MQTT-Modbus interface without all kinds of Modbus sensors configuration options.

So I suggest you focus on the gateway option and if that's available possibly think of the all-in-one approach if room permits.

Good news @arendst, I think that mqtt to modbus gateway (similar to mqtt to serial gateway) is the best choice. Then the HA system must take care of modbus registers.

Hi Theo,
thanks for your warnings and suggestions... message received !

I will investigate on a MQTT-Modbus bridge taking a look at serial bridge available in Sonoff-Tasmota

Regards,
Gennaro

Just for reference: #3133

Hi,
regarding #3133 I'm not a RS485 expert but I understand that Modbus is a Master-Slave protocol where a Master (ESP) request a measurement and a Slave (SDM), identified by its id number, answers to that request. In Modbus protocol there is not a Slave-Master communication without a Master request. If I understand correctly a direction pin is only required when other pattern of communication are requested. I suppose also that RS485-TTL adapter provide a correct implementation of RS485 half-duplex communication... my SDM120 is up and running since 5 months without problems...

Hi,

normally a direction pin is needed. Most Modbus devices use half duplex so you need to tell your transceiver the communication direction. If you use eg. the max3485 IC you need to toggle the mode. When using RS485-boards it's possible that direction handling is done by on board logic like toggling send direction when RX becomes active. For generic half duplex modbus toggling a gpio before send is required to tell most of the standard transceivers that they are now in send mode but there are more expensive types handling this on their own or using external logic.

Greetings, Altelch.

Am 7. September 2018, um 10:38, schrieb Gennaro Tortone notifications@github.com:

Hi,
regarding #3133 I'm not a RS485 expert but I understand that Modbus is a Master-Slave protocol where a Master (ESP) request a measurement and a Slave (SDM), identified by its id number, answers to that request. In Modbus protocol there is not a Slave-Master communication without a Master request. If I understand correctly a direction pin is only required when other pattern of communication are requested. I suppose also that RS485-TTL adapter provide a correct implementation of RS485 half-duplex communication... my SDM120 is up and running since 5 months without problems...

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.

{"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/arendst/Sonoff-Tasmota","title":"arendst/Sonoff-Tasmota","subtitle":"GitHub repository","main_image_url":"https://assets-cdn.github.com/images/email/message_cards/header.png","avatar_image_url":"https://assets-cdn.github.com/images/email/message_cards/avatar.png","action":{"name":"Open in GitHub","url":"https://github.com/arendst/Sonoff-Tasmota"}},"updates":{"snippets":[{"icon":"PERSON","message":"@gtortone in #2803: Hi,rnregarding #3133 I'm not a RS485 expert but I understand that Modbus is a Master-Slave protocol where a Master (ESP) request a measurement and a Slave (SDM), identified by its id number, answers to that request. In Modbus protocol there is not a Slave-Master communication without a Master request. If I understand correctly a direction pin is only required when other pattern of communication are requested. I suppose also that RS485-TTL adapter provide a correct implementation of RS485 half-duplex communication... my SDM120 is up and running since 5 months without problems..."}],"action":{"name":"View Issue","url":"https://github.com/arendst/Sonoff-Tasmota/issues/2803#issuecomment-419366748"}}} [ { "@context": "http://schema.org", "@type": "EmailMessage", "potentialAction": { "@type": "ViewAction", "target": "https://github.com/arendst/Sonoff-Tasmota/issues/2803#issuecomment-419366748", "url": "https://github.com/arendst/Sonoff-Tasmota/issues/2803#issuecomment-419366748", "name": "View Issue" }, "description": "View this Issue on GitHub", "publisher": { "@type": "Organization", "name": "GitHub", "url": "https://github.com" } }, { "@type": "MessageCard", "@context": "http://schema.org/extensions", "hideOriginalBody": "false", "originator": "AF6C5A86-E920-430C-9C59-A73278B5EFEB", "title": "Re: [arendst/Sonoff-Tasmota] Proposal for Modbus devices integration (#2803)", "sections": [ { "text": "", "activityTitle": "Gennaro Tortone", "activityImage": "https://assets-cdn.github.com/images/email/message_cards/avatar.png", "activitySubtitle": "@gtortone", "facts": [ ] } ], "potentialAction": [ { "name": "Add a comment", "@type": "ActionCard", "inputs": [ { "isMultiLine": true, "@type": "TextInput", "id": "IssueComment", "isRequired": false } ], "actions": [ { "name": "Comment", "@type": "HttpPOST", "target": "https://api.github.com", "body": "{n"commandName": "IssueComment",n"repositoryFullName": "arendst/Sonoff-Tasmota",n"issueId": 2803,n"IssueComment": "{{IssueComment.value}}"n}" } ] }, { "name": "Close issue", "@type": "HttpPOST", "target": "https://api.github.com", "body": "{n"commandName": "IssueClose",n"repositoryFullName": "arendst/Sonoff-Tasmota",n"issueId": 2803n}" }, { "targets": [ { "os": "default", "uri": "https://github.com/arendst/Sonoff-Tasmota/issues/2803#issuecomment-419366748" } ], "@type": "OpenUri", "name": "View on GitHub" }, { "name": "Unsubscribe", "@type": "HttpPOST", "target": "https://api.github.com", "body": "{n"commandName": "MuteNotification",n"threadId": 337836475n}" } ], "themeColor": "26292E" } ]

Hi @altech,
currently developments for SDM (RS485) device on Sonoff-Tasmota imply to use a RS485-TTL adapter connected to two free pins and (as you suggest) usage of an additional direction pin is not useful... Further developments (if needed) could be provide a solution to use a SDM (RS485) device without RS485-TTL adapter, but in that case we will need three free pins but these future developments IMHO are not very useful if we will not identify a real benefit that at this moment I don't see...

Hi Gennaro,

Can you provide me a link with the adapter you use? I did some modbus development a while ago but always based on max485 or max3485 (3,3V version). Also a lot of china boards are just a breakout board based on those chips, so I'm interested in what board you use not requiring direction pin.

Thanks,

Altelch.

Am 7. September 2018, um 12:17, schrieb Gennaro Tortone notifications@github.com:

Hi @Altech,
currently developments for SDM (RS485) device on Sonoff-Tasmota imply to use a RS485-TTL adapter connected to two free pins and (as you suggest) usage of an additional direction pin is not useful... Further developments (if needed) could be provide a solution to use a SDM (RS485) device without RS485-TTL adapter, but in that case we will need three free pins but these future developments IMHO are not very useful if we will not identify a real benefit.

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.

{"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/arendst/Sonoff-Tasmota","title":"arendst/Sonoff-Tasmota","subtitle":"GitHub repository","main_image_url":"https://assets-cdn.github.com/images/email/message_cards/header.png","avatar_image_url":"https://assets-cdn.github.com/images/email/message_cards/avatar.png","action":{"name":"Open in GitHub","url":"https://github.com/arendst/Sonoff-Tasmota"}},"updates":{"snippets":[{"icon":"PERSON","message":"@gtortone in #2803: Hi @altech,rncurrently developments for SDM (RS485) device on Sonoff-Tasmota imply to use a RS485-TTL adapter connected to two free pins and (as you suggest) usage of an additional direction pin is not useful... Further developments (if needed) could be provide a solution to use a SDM (RS485) device without RS485-TTL adapter, but in that case we will need three free pins but these future developments IMHO are not very useful if we will not identify a real benefit. "}],"action":{"name":"View Issue","url":"https://github.com/arendst/Sonoff-Tasmota/issues/2803#issuecomment-419393777"}}} [ { "@context": "http://schema.org", "@type": "EmailMessage", "potentialAction": { "@type": "ViewAction", "target": "https://github.com/arendst/Sonoff-Tasmota/issues/2803#issuecomment-419393777", "url": "https://github.com/arendst/Sonoff-Tasmota/issues/2803#issuecomment-419393777", "name": "View Issue" }, "description": "View this Issue on GitHub", "publisher": { "@type": "Organization", "name": "GitHub", "url": "https://github.com" } }, { "@type": "MessageCard", "@context": "http://schema.org/extensions", "hideOriginalBody": "false", "originator": "AF6C5A86-E920-430C-9C59-A73278B5EFEB", "title": "Re: [arendst/Sonoff-Tasmota] Proposal for Modbus devices integration (#2803)", "sections": [ { "text": "", "activityTitle": "Gennaro Tortone", "activityImage": "https://assets-cdn.github.com/images/email/message_cards/avatar.png", "activitySubtitle": "@gtortone", "facts": [ ] } ], "potentialAction": [ { "name": "Add a comment", "@type": "ActionCard", "inputs": [ { "isMultiLine": true, "@type": "TextInput", "id": "IssueComment", "isRequired": false } ], "actions": [ { "name": "Comment", "@type": "HttpPOST", "target": "https://api.github.com", "body": "{n"commandName": "IssueComment",n"repositoryFullName": "arendst/Sonoff-Tasmota",n"issueId": 2803,n"IssueComment": "{{IssueComment.value}}"n}" } ] }, { "name": "Close issue", "@type": "HttpPOST", "target": "https://api.github.com", "body": "{n"commandName": "IssueClose",n"repositoryFullName": "arendst/Sonoff-Tasmota",n"issueId": 2803n}" }, { "targets": [ { "os": "default", "uri": "https://github.com/arendst/Sonoff-Tasmota/issues/2803#issuecomment-419393777" } ], "@type": "OpenUri", "name": "View on GitHub" }, { "name": "Unsubscribe", "@type": "HttpPOST", "target": "https://api.github.com", "body": "{n"commandName": "MuteNotification",n"threadId": 337836475n}" } ], "themeColor": "26292E" } ]

Hi @altelch, I'm using this adapter: https://www.hotmcu.com/uart-ttl-to-rs485-twoway-converter-module-p-267.html

but take into account that two ICs soldered on the board have model number erased...

Hi, If i want to connect SDM120/SDM630 to Tasmot, do i need any converter RS485/Serial (3.3V) to do this or i can connect directly?
Thanks in advance

Thanks @gtortone for reply, any additional configuration(like RS485 adress on meter, etc...) counter is needed to get data from meter?

Hi @jarcikw, you can use default SDM settings (id = 1, default speed = 9600 baud) and you don't need to change anything in user_config.h (remember only to enable USE_SDM120 or USE_SDM630)

@gtortone , thanks for help.

@gtortone, one more question. How should i connect converter with meter, do i need connect wires between A, B and G?

@jarcikw, yes A,B and G of SDM on A,B and G of converter

@gtortone . I have Tasmota 6.2.1.5 with configured SDM120 in user_config:
#define USE_SDM120 // Add support for Eastron SDM120-Modbus energy meter (+1k7 code) #define SDM120_SPEED 2400 // SDM120-Modbus RS485 serial speed (default: 2400 baud)
I'm using Wemos mini board, wiring (Converter and SDM120) A+ <-> A+ , B-<->B- , GND <-> GND .
RX <-> D2 , TX <->D1 (converter and Wemos), when I'm replace D1 with D2 in Tasmota i have got Nan values
My converter:
image
My configuration in Tasmota:
image

Output in console i have :
tele/sonoff/SENSOR = {"Time":"2018-09-26T22:51:40","ENERGY":{"Total":0.000,"ActivePower":0,"ApparentPower":0,"ReactivePower":0,"Frequency":0,"Factor":0.00,"Voltage":0,"Current":0.000}}

What I'm doing wrong?

@gtortone there was a problem with converter, everythings works as expected :)

https://github.com/jarcikw/SDM120_Homeassistant/wiki/SDM120-&-nodemcu-&-RS485-wiring

@jarcikw Great ! I'm very glad of this ! What do you think to copy your page about SDM120 configuration in Sonoff-Tasmota wiki ?

Great idea! It Will be very helpfull for anyone who starting with this.
Please use my page :)

Hi,

Was added TasmotaModbus library for very basic modbus wrapper for TasmotaSerial

https://github.com/arendst/Sonoff-Tasmota/commit/fb6cc194a909d971ed119853c761eb30318c53d1

Hi, i would like to read modbus registers from a pv converter / Growatt. Can you explain me, where to change the code or better what to change to read the registers from another device than a SDM120 / 630 ?

@smartboart

Give Smart Meter Interface a try.

Please address further questions to the Tasmota Support Discord Chat. The chat is a better and more dynamic channel for helping you. Github issues are best used for Tasmota _software feature requests and bug reporting_. Troubleshooting and setup assistance is more effective using an interactive forum.

Please check the Contributing Guideline and Policy and the Support Guide.

Thanks.

Support Information

See Wiki for more information.
See Chat for more user experience.
See Community for forum.
See Code of Conduct

Hey guys! nice work over there. I having issues with ESP easy and SDM 630 to working together.
Does anybody have a how to set the device to read the SDM 630 meter, please?
It is working on raspberry pi, but I wish to use it with esp 8266 as I have multiple SDM readers.
Anybody here who has done something like this? Thanks.

SDM630

I have a couple of quite-advanced power analysers / power-meters which have literally hundreds of modbus registers. I really liked the idea of the mqtt-modbus broker if anyone is still interested in it. I dont mind giving it a go but I was wondering if there was a bare-bone device on which to start basing myself (not the modbus part but rather the mqtt command/response logic). I,e, how do I add a new command to the CLI/MQTT ? thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kckepz picture kckepz  Â·  3Comments

Vujagig picture Vujagig  Â·  3Comments

esp32x picture esp32x  Â·  3Comments

he-so picture he-so  Â·  3Comments

Joeyhza picture Joeyhza  Â·  3Comments