The Sweeney Brewery v3 has lots of electrical controls. I have six 2-way electric ball valves – three for tun control and three for gas control, and two 3-way valves for various fluid routing processes. I also have a new Omega CN7500 (more on that later), and the system pump.
Controlling everything, I have an RPi v2. The first problem was how to control all of the devices. the RPi offers support for multiple communication protocols including one-wire, RS232, RS485, and lots of others. After flirting with one-wire, I settled on RS485 for several reasons. First, the Omega could communicate with either RS232 or RS485. I had to design the controllers for the electric ball valves from scratch, so that wasn’t a limiting factor. RS485 can also support up to 32 devices per communications port. Also, RS485 can communicate over long distances. These all added up to a design decision to use RS485.
There’s lots of information on using RS485 with RPi – most of it poorly written, erroneous, or just plain wrong. I went down the road of trying to get the RPi to control RS485 directly but soon gave that up because of voltage conversions. Then I found a neat little RPi shield that takes care of the voltage shifting for you. This was the solution that would allow me to control both the Omega PID and an array of relays that could control the mechanical controls.
And so I set out to configure the RPi to communicate to the serial devices. This was NOT easy. I installed a minimal Raspbian image (September 2016, Kernel 4.4) and updated all packages. I setup SSH and a USB wireless dongle to communicate with my house network so I could work on the RPi headless. I then added the following additional packages using apt-get:
This package should be installed with pip:
- minimalmodbus (has a custom driver for the Omega CN7500!!)
The setup instructions for the LinkSprite shield are old (2013) and inaccurate. They detail the following tasks which have to be modified on the new systemd enabled Raspbian image.
- Install library dependencies (detailed above)
- Disable use of the serial port as a TTY. Use raspi-config to do this. Run
sudo raspi-configand check if it has the option
serial. If it has, set it to disabled and you’re done.
- Set the serial GPIO flags to ALT0:
gpio readall gpio mode 15 ALT0; gpio mode 16 ALT0 gpio readall
You should now see pins 15 and 16 set to ALT0.
- Disable the login messages on the serial port. This is tricky because the instructions assume an old version of unix that uses SystemV (i.e., edit /etc/inittab). The newest version of Raspbian uses systemd, which doesn’t have /etc/inittab. The correct way to do this under systemd is to run:
sudo systemctl mask serial-getty@ttyAMA0.service
After I took these steps, I was able to communicate to the Omega controller via a simple python script that extended the custom Omega CN7500 driver for python. Here’s the simple script which returns the current process temp from the controller:
#!/usr/bin/env python import omegacn7500 instrument = omegacn7500.OmegaCN7500('/dev/ttyAMA0', 2) # port name, slave address print instrument.get_pv() # print temperature
And so, I made my first successful RS485 communication. More to follow.
Well, it turns out that the steps to enable the RS485 on the RPi3 are different. I found an article that deals with much of the subtlety of this. Because of the addition of bluetooth on the RPi3, /dev/ttyAMA0 is reserved for that, and so the procedure is slightly different. In the following steps, we will move bluetooth from the high-performance /dev/ttyAMA0 interface to the lower performing /dev/ttyS0.