Pi 4 Channel Relay


Project:PiACC - Relay Controller

Started On:10/22/2018

Upated On:

10/27/2018: BT host is now working to control relays via GPIO. (testing on p3, not pi zero yet)
I currenlty have 4 channel toggles a,b,c,d to match the four 10amp relays via gpio.
"o" triggers outer relays,"i" triggers inner relays, "p" triggers plow lamp relay,
"a" triggers all relays on (1 sec wait), "x" triggers all off (1 sec wait) and q triggers client close with socket restart (disconnect).
I have not written the toggles, so there are also matching numerics 1=on, 2=off, 3=on, 4=f.. etc.
I will be writting an If on, toggle off... if off, toggle on.. for brevity..

10/7/2018: Updated py src.
Intent: To create a viable under-hood BT relay controller that can be leveraged by the piACC console unit.
Access:Web Server - Flask monitoring local address[port tbd]
Console:Chrome browser in kiosk mode.
Raspberry Pi 3 - piACC Console.
The console pi will operate as the main controller with an entrance module to control standard button clicks via BT.
Raspberry Pi zero - Relay Controller
The pi zero will operate as the lamp bar relay control using blue tooth encrypted commands sent from either mobile device or the console.
pi HAT: Four 10 amp relays will control the accessories. Current config will be the two outer, two inner and plow/emergency flasher lamps.
Robogaia.com - 4x10a Raspberry Pi Plate v3

Steps taken to date: 10/24/2018

Installing blueman and python drivers
1.sudo apt-get install bluetooth blueman bluez
2.sudo reboot
3. sudo apt-get install python-bluetooth (this actually said selecting python-bluez on install)
4. sudo apt-get install python-rpi.gpio

One step that I will do differently is encrypting the transmission based on the mac address of the incoming connection.
Python code: (btctl.py)... Become a Patron!This page is being written. Become a patron to get a preview.
abitowhit's picture

I was able to get everything working exactly as I wanted it.
Direct connection via BT, client drop, client disconnect and then restart back into waiting for connection mode.
This is the loop that I was having a hard time to get it working as expected.

The next step is to test it out on a headless pi zero to see if I can have it working with both the dashboard console and a cellular connection.
Once I have this working as expected it will just be a matter of cleaning up the source code and encrypting the communications.
After all of the pi side is completed I will start up on the Android phone app. My current thought is that it will just be a controller for the overhead light bar, tripping the relays, but now that I have everything working it may include much more than that.
I can hopefully pull much of the source code from my security app PinKeyRing, at least the encryption side.

abitowhit's picture

I will be creating a new book for the finished documentation.
It will describe how each process works, with images.
Look for it soon.

abitowhit's picture

Deleting the original forum topic.
Here are the old notes...
Bluetooth is working now
I was able to get it all working as expected. I will probably set it to allow 2 connections which means that I can hit it from both my cell phone and console. I have include the ability to track the mac address to only allow input from specific devices. I also set it to allow non encrypted but will test with encrypted only. If I have to allow unencrypted, I will encrypt the data internally, which I may do anyway for additional security. Probably encrypted using the mac as the key + another key. My thought is to use a pi zero for just the switching via BT, then use the console to fire the light commands (or phone) sent directly to a pi zero under the hood. Saves on having to run wiring into the cab.

Maybe not
Ok, so I was able to connect to the pi from my phone.. thought conversation was short lived and it was disconnected.. but a hint of light at the end of the tunnel. Guess I will work on it further.
Script ends with one keyclick but it does however register.. Will probably need to put in a try catch block. Looks promising.

So after a first attempt at getting bt to work with the pi and my samsung it was unsuccessful I was able to pair the two but not much beyond that. BlueTerm basically came up with a blank terminal but there never seemed to be any communications. Possibly the bt on the pi is not as expected. I do have a dongle bt but not sure if it is worth testing with it.
I have written a few android apps and communications does not look too difficult so I may simply write my own android app.
I did however try the python script without having the relay mentioned above so possibly it was just confused.. but never even got a bt connection attempt on either device.
Giving up on it for the time being, it is getting a bit frustrating.

abitowhit's picture

After installing the relay controller in the Jeep, I realized that I needed a method to perform shutdown and reboot commands.
I have added two more string items it will look for.
R for reboot
S for shutdown
This allows me to send a shutdown command to the relay control so that I can remove power and remove the unit. Since it is running headless on battery power, to remove it cleanly you have to do a shutdown first.
I have also added this ability on the piACC console, so that it can be shut down prior to turning off the ignition/accessory. The console will contain a simple 'short' relay via GPIO pins that will go normally open when the acc power is lost... performing a clean shutdown, but for now it will be manual.