Share This
//CBus MQTT Bridge on Raspberry PI

CBus MQTT Bridge on Raspberry PI

Installing C-Gate on a Raspberry Pi, and adding a MQTT Bridge.

Turn back to 2007; My wife and I built our home, integrating many smart technologies, including the Clipsal C-Bus lighting system. This solution is classified as a Prosumer technology, and is designed to integrate into whole house automation systems.

The C-Bus system implements however a propriatory technology, and utilizes a communication protocol which is not ‘open source’; however, accepting a licence agreement will permit access to this protocol for creating an programming interface.

To simplify (arguable) the process of integrating with the CBus environment Clipsal released a Bridge solution which enables a TCP interface using a special Java application called ‘C-GATE’.

Using a Raspberry Pi, with a USB to RS-232 cable, which is then connected to a Clipsal interface called the Serial PCI Module,

TCP to Serial Bridge

Configuring the CBus system over TCP however will not work with just C-Gate alone, we need to also establish a TCP connection directly to the Serial PCI Module.

Serial To Socket

This requires that we compile and run a small C application (Don’t worry, this is painless and fast); It took a lot of searching to find, posted the source to the following GIT repository; the application is called ser2sock.

Here’s the steps to get it set up:

git clone https://github.com/nutechsoftware/ser2sock.git  
sudo mv ser2sock /usr/local/bin  
cd /usr/local/bin/ser2sock  
chown -R pi:pi .  
mv config.h.in config.h  
cc -o ser2sock ser2sock.c  
sudo nano /etc/init.d/ser2sock

Running as a service - Init.d

Adding the following ‘init.d’ startup script, to have the daemon auto-start with the operating system

#! /bin/sh  
# /etc/init.d/ser2sock

### BEGIN INIT INFO  
# Provides: ser2sock  
# Required-Start: $remote_fs $syslog  
# Required-Stop: $remote_fs $syslog  
# Default-Start: 2 3 4 5  
# Default-Stop: 0 1 6  
# Short-Description: Accept simultaneous tcp connections and fwd them to a serial port  
# Description: A simple script to start / stop the serial port network interface at boot / shutdown.  
### END INIT INFO

# If you want a command to always run, put it here

# Carry out specific functions when asked to by the system  
case$1” in  
start)  
echo “Starting ser2sock”  
# run application you want to start  
/usr/local/bin/ser2sock/ser2sock -d -p 10001 -s /dev/ttyUSB0 -b 9600  
;;  
stop)  
echo “Stopping ser2sock”  
# kill application you want to stop  
killall ser2sock  
;;  
*)  
echo “Usage: /etc/init.d/ser2sock {start|stop}exit 1  
;;  
esac

exit 0

Finishing setting up the startup script:

sudo chmod 755 /etc/init.d/ser2sock  
sudo update-rc.d ser2sock defaults

Running as a service - System.d

Adding the following ‘system.d’ startup script, to have the daemon auto-start with the operating system /etc/systemd/system/ser2sock.service

[Unit]  
Description=ser2sock

[Service]  
ExecStart=/usr/local/bin/ser2sock/ser2sock -p 10001 -s /dev/ttyUSB0 -b 9600  
Restart=always  
User=root  
Group=root  
Environment=PATH=/usr/bin:/usr/local/bin  
Environment=NODE_ENV=production  
WorkingDirectory=/usr/local/bin/ser2sock/

[Install]  
WantedBy=multi-user.target

Then activate using:

sudo systemctl enable ser2sock.service  
sudo systemctl start ser2sock.service

Installing C-Gate

Download the current software release of C-Gate off the clipsal website and unzipped the files into /usr/local/bin/cgate. ()

wget http://203.41.170.20/downloads/CGate/cgate-2.10.2_3106.zip  
unzip cgate-2.10.2_3106.zip  
sudo mv cgate /usr/local/bin

Running C-Gate as Service - Init.d

Similar to the pervious deamon, we will also add a startup script for C-Gate sudo nano /etc/init.d/cgate

#!/bin/sh  
### BEGIN INIT INFO  
# Provides: cgate  
# Required-Start: $remote_fs $syslog  
# Required-Stop: $remote_fs $syslog  
# Should-Start: $portmap  
# Should-Stop: $portmap  
# Default-Start: 2 3 4 5  
# Default-Stop: 0 1 6  
# Short-Description: Example initscript  
# Description: This file should be used to construct scripts to be  
# placed in /etc/init.d.  
### END INIT INFO  
test -x /usr/local/bin/cgate/cgate.jar || exit 0  
CGATEDIR=/usr/local/bin/cgate  
CGATEJAR=$CGATEDIR/cgate.jar  
CGATEPID=$CGATEDIR/cgate.pid  
JAVARE=/usr/bin/java  
PARAMS=”-Djava.awt.headless=true -jar -noverify $CGATEJARLOCKFILE=$CGATEDIR/cgate.lock”  
case$1” in  
start)  
echo -n “Starting C-Gate server:”  
cd $CGATEDIR  
start-stop-daemon –start \  
–chuid pi \  
–chdir $CGATEDIR \  
–make-pidfile –pidfile $CGATEPID \  
–exec $JAVARE \ $PARAMS &  
echo “.”  
;;  
stop)  
echo -n “Stopping C-Gate server:”  
start-stop-daemon –stop –quiet \  
–pidfile $CGATEPID  
if [ -e $CGATEPID ]  
then rm $CGATEPID  
fi  
# rm $CGATEPID  
# rm $LOCKFILE  
echo “.”  
;;  
restart)  
echo -n “Restarting C-Gate server:”  
cd $CGATEDIR  
start-stop-daemon –stop –quiet \  
–pidfile $CGATEPID  
# rm $CGATEPID  
# rm $LOCKFILE  
start-stop-daemon –start \  
–chuid pi \  
–chdir $CGATEDIR \  
–make-pidfile –pidfile $CGATEPID \  
–exec $JAVARE \ $PARAMS &  
echo “.”  
;;  
*)  
echo “Usage: /etc/init.d/cgate (start|stop|restart)exit 1  
;;  
esac  
exit 0

Change permissions on cgate folder and on the startup script:

sudo chown -R pi:pi /usr/local/bin/cgate  
sudo chmod +x /usr/local/bin/cgate/cgate.jar  
sudo chmod 755 /etc/init.d/cgate  
sudo update-rc.d cgate defaults

Running C-Gate as Service - System.d

Adding the following ‘system.d’ startup script, to have the daemon auto-start with the operating system /etc/systemd/system/cgate.service

[Unit]  
Description=cgate

[Service]  
ExecStart=/usr/bin/java -Djava.awt.headless=true -jar -noverify /usr/local/bin/cgate/cgate.jar  
Restart=always  
User=root  
Group=root  
Environment=PATH=/usr/bin:/usr/local/bin  
Environment=NODE_ENV=production  
WorkingDirectory=/usr/local/bin/cgate/

[Install]  
WantedBy=multi-user.target

Then activate using

sudo systemctl enable cgate.service  
sudo systemctl start cgate.service

C-Gate Access Control

We must configure the C-Gate service to allow remote connections from machines on the network by editing the access control file nano /usr/local/bin/cgate/config/access.txt; adding a line, providing the ip address of the remote system

remote XXX.XXX.XXX.XXX Program

Save, then restart cgate:

sudo service cgate restart

C-Gate Auto-Connect

Edit the file /usr/local/bin/cgate/config/C-gateConfig.txt. Set the project.default and project.start lines to the name of the C-Gate Project.

Comments

01. About Author

Damian Flynn

I define myself as an evangelist; an entrepreneur & author with an ideology rooted in business insights, technology exploration, pattern analysis and high energy. I envision, theorize and develop system architecture and strategic business platforms, soaked in storytelling and innovative technology.

Find on :

02. Last Posts

Do you want to learn more ?

© / 2020 / All rights reserved.
Get in Touch
Close