Awareness of Energy Consumption

nogrend

Nogrend

Posted on October 12, 2023

Awareness of Energy Consumption

"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.

What have I come up with?

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.

The SMD120C gives voltage, current, power, energy, power factor, frequency and some variations on it like imported or exported energy.

Proof of concept

Before altering the electrical installation, firstly I put all components together with some ducktape and wires.

Hardware

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.

Eastron SMD120C, USB to RS485 converter and USB isolator

Software

A Python script on the Raspberry Pi reads the values and send it over MQTT. From this point Node-RED will take over.

Write code for measurment

In the next code block I import sdm_modbus library, set parameters accordingly to the datasheet, create an instance, start measuring and print the results in the terminal.

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)
Enter fullscreen mode Exit fullscreen mode

Extend code with MQTT

With some help from Chat GPT the following code send the measurements over MQTT in my local network.

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)
Enter fullscreen mode Exit fullscreen mode

Success

When running the script I received in my Node-RED debugger screen the following object:
Results in debugger screen for Node-RED

Summary

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.

💖 💪 🙅 🚩
nogrend
Nogrend

Posted on October 12, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Awareness of Energy Consumption
mqtt Awareness of Energy Consumption

October 12, 2023