<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Claudio Petrini on Medium]]></title>
        <description><![CDATA[Stories by Claudio Petrini on Medium]]></description>
        <link>https://medium.com/@claudiopetrini?source=rss-3d8cbafe19ae------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*mbo1KS4nZJK2cq4i</url>
            <title>Stories by Claudio Petrini on Medium</title>
            <link>https://medium.com/@claudiopetrini?source=rss-3d8cbafe19ae------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 18:31:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@claudiopetrini/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Integrate Modbus with MQTT, the free, easy way.]]></title>
            <link>https://medium.com/instathings/integrate-modbus-with-mqtt-the-free-easy-way-4edc8f1f2990?source=rss-3d8cbafe19ae------2</link>
            <guid isPermaLink="false">https://medium.com/p/4edc8f1f2990</guid>
            <category><![CDATA[iot]]></category>
            <category><![CDATA[modbus]]></category>
            <category><![CDATA[mqtt]]></category>
            <category><![CDATA[internet-of-things]]></category>
            <dc:creator><![CDATA[Claudio Petrini]]></dc:creator>
            <pubDate>Mon, 20 Apr 2020 13:37:43 GMT</pubDate>
            <atom:updated>2020-04-23T08:07:57.357Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ROVEGUq1iOaej41HMVcElw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@cbpsc1">Clint Patterson</a> on <a href="https://unsplash.com/">Unsplash</a></figcaption></figure><p>Machineries and hardware devices can be connected to a network using many different protocols that require a specific knowledge to be used and integrated. Instathings’ mission is to convert these protocols into a simple API that any developer can consume with little or no knowledge of the technology stack that is used to deploy and manage hardware.</p><p>Instathings has two main components:</p><ul><li>a gateway (based on Docker and Node.js) that manages different automation protocols and convert them to MQTT;</li><li>a cloud service, that is used to:<br>1) manage the gateway and the devices connected to it; <br>2) collect the data from the gateway via MQTT and make them available via REST API or websocket.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/621/1*VHqceDKCPPvxSBKfTcUrYA.png" /><figcaption>Architectural overview</figcaption></figure><p>Using Instathings, the integration of any hardware device becomes seamless and fast. While I’m writing this post, we support two different automation protocols: Zigbee and Modbus. In both cases, we use a bridge to translate the protocols to MQTT.</p><p>In this post, I will show you:</p><ol><li>how to create a driver that describes a Modbus device contributing to Instathings open source catalogue of drivers;</li><li>test the driver via API using a Raspberry Pi, a <a href="https://www.amazon.it/DSD-TECH-SH-U10-Convertitore-Compatibile/dp/B078X5H8H7/ref=sr_1_1">Modbus USB dongle DSD TECH SH-U10</a> and Instathings Cloud.</li></ol><h3>Requirements</h3><p>To write the driver you’ll need:</p><ul><li>a Modbus device (check if it’s already in the <a href="https://docs.instathings.io/docs/all-devices.html">supported devices list</a>);</li><li>the device datasheet or proper documentation regarding the Modbus protocol on the device;</li><li>Git.</li></ul><p>To test the driver you’ll need:</p><ul><li>a Raspberry Pi and a Modbus dongle (or any equivalent Linux based hardware that can be connected to a device via Modbus);</li><li>an <a href="https://editor.instathings.io/">Instathings</a> account (or an MQTT server).</li></ul><p>In this tutorial I’ll show how to add support for the Eastron SDM120 energy meter. You can download the <a href="http://www.eastroneurope.com/media/_system/tech_specs/3914/SDM120%20PROTOCOL.pdf">technical documentation</a> of the device as a PDF file from the manufacturer’s website.</p><h3>Modbus2MQTT</h3><p><a href="https://github.com/Instathings/modbus2mqtt">Modbus2MQTT</a> is an open source bridge that we created to manage the communication between a Modbus device and an MQTT broker.</p><p>The bridge depends on <a href="https://github.com/Instathings/modbus-herdsman-converters">modbus-herdsman-converters</a>, which is the actual library of drivers. Anyone can contribute.</p><p>To support a specific device, you need to:</p><ol><li>write a descriptor;</li><li>contribute with a pull request.</li></ol><h3>Create a Modbus driver</h3><p>The driver is a Javascript object, for most of the devices you can think it as a JSON descriptor (an example).</p><p>For our example let’s define the structure of our new driver:</p><pre>{<br> &quot;model&quot;: &quot;SDM120&quot;,<br> &quot;vendor&quot;: &quot;Eastron&quot;,<br> &quot;supports&quot;: &quot;&quot;,<br> &quot;description&quot;: &quot;Energy meter&quot;,<br> &quot;fromModbus&quot;: {<br>  &quot;input&quot;: {},<br>  &quot;keep&quot;: {}<br> },<br> &quot;toModbus&quot;: {}<br>}</pre><p>Required keys:</p><ul><li><strong>model </strong>(string): this is by far the most important key, it describes the device model and you can think it like a device id</li><li><strong>vendor </strong>(string): the device vendor</li><li><strong>supports</strong> (string): a small but readable description of the data that the device will read. This is only used for documentation.</li><li><strong>description </strong>(string): a concise description of the device</li><li><strong>fromModbus </strong>(object): an object that contains 2 keys <strong>input</strong>, <strong>keep. <br></strong>Those keys will describe the device addresses to read in order to retrieve data, we’ll dig into this in a bit</li><li><strong>toModbus </strong>(object): unused so far.</li></ul><p>Now let’s populate the <strong>input</strong> key. <br>From the device document we read that:</p><ul><li>we can read Voltage at address 30001</li><li>we can read Current at address 30007</li><li>etc.</li></ul><p>Our descriptor will become:</p><pre>{<br> &quot;model&quot;: &quot;SDM120&quot;,<br> &quot;vendor&quot;: &quot;Eastron&quot;,<br> &quot;supports&quot;: &quot;&quot;,<br> &quot;description&quot;: &quot;Energy meter&quot;,<br> &quot;fromModbus&quot;: {<br>  &quot;input&quot;: {<br>   &quot;voltage&quot;: {<br>    &quot;address&quot;: 30001<br>   },<br>   &quot;current&quot;: {<br>    &quot;address&quot;: 30007<br>   }<br>  },<br>  &quot;keep&quot;: {}<br> },<br> &quot;toModbus&quot;: {}<br>}</pre><p>If this were the complete driver, then Modbus2MQTT would publish a JSON message like the following:</p><pre>{<br> &quot;voltage&quot;: &lt; value read at address 30001 &gt; ,<br> &quot;current&quot;: &lt; value read at address 30007 &gt;<br>}</pre><p>We’re using the key as a key of the output object and the address as an actual address to read the data from.</p><p>The descriptor supports additional parameters which, for the sake of this tutorial, are not described here (for instance you can post process data read before it gets published).<br>The complete descriptor is available <a href="https://github.com/Instathings/modbus-herdsman-converters/blob/development/devices/SDM120.js">here</a>.</p><p>Note: So far we read from the input registers only but we’ll improve this :)</p><h4>Make a pull request</h4><p>After your descriptor is ready make a pull request to the <a href="https://github.com/Instathings/modbus-herdsman-converters">modbus-herdsman-converter repo</a> or contact us at <a href="https://www.instathings.io/">Instathings</a>.</p><h3>Testing the device</h3><p>Once your pull request has been accepted, you’ll be able to use Instathings to read data from the device seamlessly.</p><p>First, set up the Gateway in order to connect it to the Instathings Cloud. <br><a href="https://docs.instathings.io/docs/guides/gateway-setup.html">Here</a>’s a detailed step by step guide on how to do this.</p><p>When the Gateway is connected you’ll see something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-MCwxzv0iFjPAFHcgXF9hQ.png" /><figcaption>Gateway correctly connected</figcaption></figure><p>You can now plug in the USB Modbus adapter and click on the “Install” button in the “Protocols” section.<br>When the protocol is correctly installed the Protocols section will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bggz_rFCQw1_y5loIlJ8bg.png" /><figcaption>Modbus software support installed successfully</figcaption></figure><p>We can now add your device to the Gateway; to do so go to the “Devices” tab and select the device from the list.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mBsg8PaH1U7qcFhDZVcuCg.png" /></figure><p>Just click on Create.</p><p>We now need to “Pair” the Modbus device; this case requires only to specify the Modbus Id of the device.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LEpv2Sbqj73RQGWsWJEJIg.png" /></figure><p>Again, just click “Pair”.<br>If everything went well, you’ll see a success message and the Instathings Editor will report the device as “Paired”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Mo17TTH58KPjw7-4s9gG_g.png" /></figure><p>From now on, the Instathings Gateway will poll every 10s the device in order to report data to the Cloud.<br>You’ll see data flowing in the “Data” section.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OSJScLa0t7Hf3dvXDEPRBA.png" /></figure><p>You can access this data via API as well, check out <a href="https://docs.instathings.io/docs/guides/api-access.html">this</a> detailed guide about how to enable API access.</p><h3>Using Modbus2MQTT without Instathings</h3><p>After your contribution wait for a new release that includes your descriptor and then just use the Modbus2MQTT bridge.</p><p>In order to pair the bridge with the device you’ll need to publish a message to:</p><pre>modbus2mqtt/configure/set</pre><p>With the following payload:</p><pre>{<br> &quot;id&quot;: &quot;uniqueidentifier&quot;,<br> &quot;modbus_id&quot;: &quot;the modbus id of the device&quot;,<br> &quot;baud_rate&quot;: &quot;the baud rate to use&quot;,<br> &quot;interval&quot;: 10000, // in ms the polling interval<br> &quot;model&quot;: &quot;SDM120&quot; // this must be the same model of the descriptor <br>}</pre><p>You’ll receive an ACK message here:</p><pre>modbus2mqtt/bridge/log</pre><p>Then you’ll need to subscribe to the topic with the format:</p><pre>modbus2mqtt/&lt;id&gt; </pre><p>where in this case id = uniqueidentifier and you’ll see data flowing :)</p><h3>Conclusions</h3><p>In this article, I introduced Modbus2MQTT and illustrated how to add and test a driver to the library of supported devices using Instathings.</p><p>Finally, I showed how to use Modbus2MQTT as a stand alone library to translate Modbus to MQTT (viceversa coming soon).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4edc8f1f2990" width="1" height="1" alt=""><hr><p><a href="https://medium.com/instathings/integrate-modbus-with-mqtt-the-free-easy-way-4edc8f1f2990">Integrate Modbus with MQTT, the free, easy way.</a> was originally published in <a href="https://medium.com/instathings">Instathings</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>