Communicate with a Modbus RTU RS485 device using a Raspberry Pi

A Raspberry Pi is a tiny computer that can talk to other devices using something called Modbus protocol. This allows us to read information from these devices, like temperature values, which is very important in many industries. For example, if we want to know the temperature of a machine in a factory, we can use a Raspberry Pi to read this information and make sure the machine is working correctly. This is very helpful because it can save us time and money by detecting problems early and fixing them before they become bigger issues. Plus, a Raspberry Pi is small, cheap, and easy to use, which makes it a great tool for controlling and automating many different devices. All we need is a little bit of code, and we can read and control all kinds of machines and sensors.

To communicate with a Modbus RTU RS485 device using a Raspberry Pi, you will need to install the “pymodbus” package in your Raspberry Pi. To install the package, you can use the pip package manager with the following command:

sudo pip install pymodbus

You will also need to connect a USB to RS485 adapter to your Raspberry Pi and connect it to the Modbus RTU device. Once you have done that, you can use the following Python code to read temperature values from the device:

#!/usr/bin/env python
from pymodbus.client.sync import ModbusSerialClient
from pymodbus.register_read_message import ReadInputRegistersResponse

# Configuration
baudrate = 9600
parity = 'N'
bytesize = 8
stopbits = 1
timeout = 1
unit_id = 1
start_address = 0
num_registers = 1

# Create a Modbus client
client = ModbusSerialClient(method='rtu', port='/dev/ttyUSB0', baudrate=baudrate, parity=parity, bytesize=bytesize, stopbits=stopbits, timeout=timeout)

# Open the communication port
client.connect()

# Read temperature value from the device
response: ReadInputRegistersResponse = client.read_input_registers(start_address, num_registers, unit=unit_id)
if response.isError():
    print(f"Error: {response}")
else:
    temperature = response.registers[0] / 10.0 # assuming the temperature value is stored in a 16-bit register and multiplied by 10
    print(f"Temperature: {temperature}°C")

# Close the communication port
client.close()

This code assumes that the temperature value is stored in a 16-bit register and multiplied by 10. You may need to adjust the start_address and num_registers variables depending on the register where the temperature value is stored and the number of registers it occupies.

It's only fair to share...Share on facebook
Facebook
Share on google
Google
Share on twitter
Twitter
Share on linkedin
Linkedin

The BarBot – Touchscreen Automated Drink Maker

I’ve been working with 3D printers for a while now. Watching them move and print can be mesmerizing. I wanted to make a fun gadget that would demonstrate my skills and create an interactive conversation piece for a party. Hence The BarBot. It’s not a new idea but, I knew I could do it better than most that I’ve seen online and take it a step or two further.

Custom Designed Printed Circuit Board

My BarBot features two stepper motors to create linear actuation in two directions; Move the drink from bottle to bottle and dispense the ingredients. I wanted to keep it as simple as possible. Along with the motors there are two end switches to home the motors so they know their positions. I designed and printed all the plastic parts. The horizontal linear actuator is a single steal rail with two rail guides. eBay Link to Steal Rails

Also my dispensers measure one shot every time. Making it do half shots and quarter shots is much tricker. Here’s a link to those racks where I used two for 12 bottles. https://www.amazon.com/gp/product/B07Q42XL9Q

I have an Arduino Uno controlling the motors and acting as the I/O controller. From there I have the Uno connected via USB Serial to the Raspberry Pi. Connected to the Raspberry Pi is a 7″ touchscreen and a 1080p Camera with an extended ribbon cable for camera placement.

The main brains (RasPi) boots up directly to my custom application that holds the drink database, system state (making drink, current ingredient, motors homed, etc), and real-time communication to the motors. I developed the applications using Javascript with Node.JS, C programming language, Socket.IO and JSON.

This project is intended to demonstrate my Software, Electrical, Mechanical and Automation skills. I hope you like it. I am still adding more features and fine tuning the system. Stay tuned for more. Check out my YouTube Channel for updates. https://www.youtube.com/c/ConsultingjoeOnline

It's only fair to share...Share on facebook
Facebook
Share on google
Google
Share on twitter
Twitter
Share on linkedin
Linkedin

Magic Time-Lapse Video of Mask

Yesterday I started this print last night and it took about 15 hours to complete. It was very large and I experimented with a different pattern for the top and bottom infill, the. It also has a 5% infill on the base with a 3D honeycomb design for strength while saving plastic and print time.

FileFinalMask5.gcode
Filament (Tool 0): 48.91m / 117.65cm³
Approx. Total Print Time15 hours


Print Time14:58:55

My Make on Thingiverse: https://www.thingiverse.com/make:487301
Please watch the video below. It turned out great. LIKE & SUBSCRIBE