Installing C-Gate on a Raspberry Pi

Originally posted on my old blog.

I’ve got C-Bus lights at home and I wanted to tie them into openHAB so I decided to (surprise surprise) use a Raspberry Pi to bridge the gap. I already had a USB to RS-232 cable, and the C-Bus setup had a Serial PCI Module, so I could dive straight into it. Here’s what I did to get C-Gate running.

FIrst up, I grabbed C-Gate off the clipsal website and unzipped the files into /usr/local/bin/cgate. (here)

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

I had trouble with getting the serial connection going so I found some handy help here on the cbus forums. It uses a script to turn the serial connection into a socket connection, which makes the PCI module act like a CNI module. The link to the ser2sock script was broken so I had to find it again. 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

and here’s the startup script to go into ser2sock (again, from the cbus forums post by cheekymonkey here:

#! /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

Edit: If you’re using Debian Jessie, you’ll need to do this instead of adding /etc/init.d/ser2sock:

sudo nano /etc/systemd/system/ser2sock.service

 

And paste this in:

[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

I also added a startup script for C-Gate. The cbus forums were a help again (particularly this thread)

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 $CGATEJAR”
LOCKFILE=”$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

Edit: Again, if on Debian Jessie you’ll need this instead:

sudo nano /etc/systemd/system/cgate.service

And paste this in:

[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

You can do all the setup through the cgate telnet interface, but I went with the easy route which is to use C-Bus Toolkit. Just grab it from here and install it on a windows computer. You then need to enable access on the raspberry pi:

nano /usr/local/bin/cgate/config/access.txt

Then add a line, using the ip address of your windows computer:

remote XXX.XXX.XXX.XXX Program

Save, then restart cgate:

sudo service cgate restart

Now you can connect with your computer using File>Connect to Remote C-Gate… with the IP address of your Pi and whatever site name you want. Create a new project and add a CNI network using the Pi’s ip address and the port 10001.

One more step! On the Pi, edit /usr/local/bin/cgate/config/C-gateConfig.txt and set the project.default and project.start lines to your project’s name.

I think that’s it. I’ll post later about how I hooked C-Gate into openHAB MQTT.

4 thoughts on “Installing C-Gate on a Raspberry Pi

  1. Thanks for the write up. I managed to get this going on Ubuntu 16.04 but I had issues with the line below:
    ExecStart=/usr/bin/java -Djava.awt.headless=true -jar -noverify /usr/local/bin/$
    I had to point to it directly for anyone who is interested:
    ExecStart=/usr/bin/java -Djava.awt.headless=true -jar -noverify /usr/local/bin/cgate/cgate.jar

Leave your comment