<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Nogrend</title>
    <description>The latest articles on Forem by Nogrend (@nogrend).</description>
    <link>https://forem.com/nogrend</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1140204%2F5eb92923-e984-42e6-8585-d7ff0700fbfa.jpeg</url>
      <title>Forem: Nogrend</title>
      <link>https://forem.com/nogrend</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nogrend"/>
    <language>en</language>
    <item>
      <title>Awareness of Energy Consumption</title>
      <dc:creator>Nogrend</dc:creator>
      <pubDate>Thu, 12 Oct 2023 20:05:58 +0000</pubDate>
      <link>https://forem.com/nogrend/awareness-of-energy-consumption-k39</link>
      <guid>https://forem.com/nogrend/awareness-of-energy-consumption-k39</guid>
      <description>&lt;p&gt;"Awareness" with that in mind I start the journey for measuring electrical energy consumption. I’ve a workspace and I wonder how much my total electrical energy consumption is.&lt;/p&gt;

&lt;h2&gt;
  
  
  What have I come up with?
&lt;/h2&gt;

&lt;p&gt;The electricity is delivered through a one 16 ampère group. After some googling, I came up with the following kWh meter; Eastron SMD120C. It's a kWh meter for one phase up to 45 A. The most important key feature is connectivity, this is done via Modbus RS-485 and fortunately there is a Python package available.&lt;/p&gt;

&lt;p&gt;The SMD120C gives voltage, current, power, energy, power factor, frequency and some variations on it like imported or exported energy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Proof of concept
&lt;/h2&gt;

&lt;p&gt;Before altering the electrical installation, firstly I put all components together with some ducktape and wires.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hardware
&lt;/h3&gt;

&lt;p&gt;The idea is to read measurements with a Raspberry Pi, therefor I used an USB to RS485 converter, with an USB isolator just to be sure.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbh4phn2n4vf70zx88bpa.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbh4phn2n4vf70zx88bpa.jpeg" alt="Eastron SMD120C, USB to RS485 converter and USB isolator"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Software
&lt;/h3&gt;

&lt;p&gt;A Python script on the Raspberry Pi reads the values and send it over MQTT. From this point Node-RED will take over.&lt;/p&gt;

&lt;h4&gt;
  
  
  Write code for measurment
&lt;/h4&gt;

&lt;p&gt;In the next code block I import &lt;code&gt;sdm_modbus&lt;/code&gt; library, set parameters accordingly to the datasheet, create an instance, start measuring and print the results in the terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import sdm_modbus

# set parameters
com_port = "COM7"
modbus_address = 1 
baud_rate = 9600
parity = "N"

# create an instance of the SDM120 class
device = sdm_modbus.SDM120(device=com_port,
                           unit=modbus_address, 
                           baud=baud_rate,
                           parity=parity)

# read measured values
device_measurement: dict = {}
device_measurement = device.read_all(
                     sdm_modbus.registerType.INPUT)

# print measured values
for key, value in device_measurement.items():
        print(key, value)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Extend code with MQTT
&lt;/h4&gt;

&lt;p&gt;With some help from Chat GPT the following code send the measurements over MQTT in my local network.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import sdm_modbus
import paho.mqtt.client as mqtt
import json

load_dotenv()

# Parameters for communication through USB
com_port = "/dev/tty.usbserial-00000000"
modbus_address = 1
baud_rate = 9600
parity = "N"

# MQTT Broker Settings
mqtt_broker_address: str = os.getenv("MQTT_BROKER_ADDRESS")
mqtt_topic: str = os.getenv("MQTT_TOPIC")
mqtt_port: int = int(os.getenv("MQTT_PORT"))
mqtt_username: str = os.getenv("MQTT_USERNAME")
mqtt_password: str = os.getenv("MQTT_PASSWORD")

# Create an MQTT client instance
client = mqtt.Client()


# Callback when the client connects to the MQTT broker
def on_connect(client, userdata, flags, rc):
    print("Connected to MQTT Broker with result code " + str(rc))


# Set the username and password for the MQTT broker
client.username_pw_set(username=mqtt_username,
                       password=mqtt_password)

# Connect to the MQTT broker
client.on_connect = on_connect
client.connect(mqtt_broker_address, mqtt_port, 60)

# create an instance of the SDM120 class
device = sdm_modbus.SDM120(device=com_port,
                           unit=modbus_address, 
                           baud=baud_rate,
                           parity=parity)

device_measurement = device.read_all(
                     sdm_modbus.registerType.INPUT)

# Convert the device_measurement dictionary to a JSON string
measurement_json = json.dumps(device_measurement)

# Publish the measurement to the MQTT topic
client.publish(mqtt_topic, measurement_json)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Success
&lt;/h3&gt;

&lt;p&gt;When running the script I received in my Node-RED debugger screen the following object:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fldhmmj8j0rqecbk2hohk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fldhmmj8j0rqecbk2hohk.png" alt="Results in debugger screen for Node-RED"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In this project, my goal was to measure electrical energy consumption in my workspace for the sake of awareness. I selected the Eastron SMD120C kWh meter, which communicates via Modbus RS-485, and set up the necessary hardware, including a Raspberry Pi and MQTT integration for real-time data transmission to Node-RED.&lt;/p&gt;

</description>
      <category>mqtt</category>
      <category>smd120</category>
      <category>python</category>
      <category>raspberrypi</category>
    </item>
  </channel>
</rss>
