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.

37 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

  2. For any other Linux newbies on Jessie (or Stretch)….

    The pasted code into cgate.service needs a newline or two inserting before
    {Service]
    and {Install]
    i.e.


    Description=cgate

    [Service]

    and

    WorkingDirectory=/usr/local/bin/cgate/

    [Install]
    ..

  3. Great work.
    I have found your MQTT interface for C-Bus, but I need install instructions. Any chance you could whip up something simple either in the read.me file on git hum or a blog post here?

  4. Hi there! This is kind of off topic but I need some advice from an established blog. Is it hard to set up your own blog? I’m not very techincal but I can figure things out pretty quick. I’m thinking about setting up my own but I’m not sure where to start. Do you have any points or suggestions? Thank you

  5. hey man i am using yur instructions
    for Cgate and Ser2sock to connect Windows 10 Cbus Toolkit 1.15.2 remotely to Cgate 2.11.1 on Ubuntu 16.04 server

    i was able to open a Cbus project network using type CNI IP and port 10001 and scan/program all the Cbus modules and inputs via a USB-RS232 cable connected to a 5500PC

    i have followed the instructions for installing MQTT, Cgateweb, Node and Java
    but it seems MQTT messages are not being sent from Cgateweb/Cgate
    i can send MQTT messages from Homeassistant but i dont think Cgateweb can pass them to Cgate

    can Cgateweb work with Cgate 2.11.1 or should i revert to Cgate 2.10.2

    i found a blog by Jamescowan http://blog.jamescowan.com/2017/04/home-automation-homeassistant-and.html
    with updated index.js and settings.js for Cgate 2.10.6
    his Cgate updates also work for Cgate 2.11.1 with Cbus Toolkit 1.15.2
    but MQTT messages are still not being sent from Cgateweb/Cgate

    if the Cgate version is the problem is their chance you can update the Cgateweb/Cgate scripts for Cgate 2.11.1 or am i just doing something wrong.
    i only started learning linux 1 week ago

    thanks dave!

    1. Hi Dave, I had a look at Jamescowan’s page and it looks like he hadn’t set a default project in cgate, which he solved with the code changes, nothing to do with cgate version.

      Could you run this on your machine that’s running cgateweb?
      sudo service cgateweb status
      Check to see if there are any errors.

      1. Thanks, I tried the code there is some error i had to ControlC to escape it.

        [email protected]:~$ sudo service cgateweb status
        [sudo] password for dd9:
        ● cgateweb.service – cgateweb
        Loaded: loaded (/etc/systemd/system/cgateweb.service; enabled; vendor preset:
        Active: activating (auto-restart) (Result: exit-code) since Mon 2017-12-25 19
        Process: 2107 ExecStart=/usr/bin/nodejs /usr/local/bin/cgateweb/index.js (code
        Main PID: 2107 (code=exited, status=1/FAILURE)

        Dec 25 19:59:48 hostubuntu systemd[1]: cgateweb.service: Main process exited, co
        Dec 25 19:59:48 hostubuntu systemd[1]: cgateweb.service: Unit entered failed sta
        Dec 25 19:59:48 hostubuntu systemd[1]: cgateweb.service: Failed with result ‘exi
        lines 1-9/9 (END)

        1. i went through the instructions again from: Setting up cgateweb (C-Bus to MQTT) on Linux
          i had a few LSB errors when i tried to update-rc-d cgateweb defaults
          i think everything else seemed to go thru without errors

          [email protected]:/usr/local/bin/cgateweb$ sudo chmod +x /etc/init.d/cgateweb
          [email protected]:/usr/local/bin/cgateweb$ sudo update-rc.d cgateweb defaults
          insserv: warning: script ‘K01cgateweb’ missing LSB tags and overrides
          insserv: warning: script ‘cgateweb’ missing LSB tags and overrides
          [email protected]:/usr/local/bin/cgateweb$ sudo service cgateweb start

          1. [email protected]:/usr/local/bin/cgateweb$ stat -c ‘%A %a %n’ *stat -c ‘%A %a %n’ *
            -rwxr–r– 744 cgateweb.service
            -rwxr–r– 744 index.js
            -rwxr–r– 744 index-original.js
            -rwxr–r– 744 LICENSE.txt
            drwxrwxr-x 775 log
            drwxr-xr-x 755 node_modules
            -rwxr–r– 744 package.json
            -rwxr–r– 744 README.md
            -rwxr–r– 744 settings.js
            -rwxr–r– 744 settings-original.js

          2. Hmm, doesn’t seem to be allowing what the actual error is. Maybe try running the peripheral directly and see what happens:

            node /usr/local/bin/cgateweb/index.js

          3. I went a 3rd time through all the tutorials
            (but this is with the jamescowan index.js and settings.js)
            Maybe i should replace those updates with your originals scripts?

            Here is a rundown of installs into /usr/local/bin/cgateweb

            [email protected]:/usr/local/bin/cgateweb$ sudo apt-get install nodejs
            Reading package lists… Done
            Building dependency tree
            Reading state information… Done
            nodejs is already the newest version (4.2.6~dfsg-1ubuntu4.1).
            0 to upgrade, 0 to newly install, 0 to remove and 0 not to upgrade.

            [email protected]:/usr/local/bin/cgateweb$ sudo apt-get install npm
            Reading package lists… Done
            Building dependency tree
            Reading state information… Done
            npm is already the newest version (3.5.2-0ubuntu4).
            0 to upgrade, 0 to newly install, 0 to remove and 0 not to upgrade.

            [email protected]:/usr/local/bin/cgateweb$ sudo npm install mqtt –save

            [email protected]:/usr/local/bin/cgateweb$ sudo npm install -g
            /usr/local/lib
            +– [email protected]

            [email protected]:/usr/local/bin/cgateweb$ ln -s /usr/bin/nodejs /usr/bin/node
            ln: failed to create symbolic link ‘/usr/bin/node’: File exists

            I rebooted and then got this error when i tried to check status of cgateweb:

            [email protected]:/usr/local/bin/cgateweb$ sudo service cgateweb status
            ● cgateweb.service – cgateweb
            Loaded: loaded (/etc/systemd/system/cgateweb.service; enabled; vendor preset: enabled)
            Active: activating (auto-restart) (Result: exit-code) since Mon 2017-12-25 23:21:43 AEDT; 9s ago
            Process: 1905 ExecStart=/usr/bin/nodejs /usr/local/bin/cgateweb/index.js (code=exited, status=1/FAILURE)
            Main PID: 1905 (code=exited, status=1/FAILURE)

            Dec 25 23:21:43 hostubuntu nodejs[1905]: at Function.Module.runMain (module.js:442:10)
            Dec 25 23:21:43 hostubuntu nodejs[1905]: at startup (node.js:136:18)
            Dec 25 23:21:43 hostubuntu nodejs[1905]: at node.js:966:3
            Dec 25 23:21:43 hostubuntu systemd[1]: cgateweb.service: Main process exited, code=exited, status=1/FAILURE
            Dec 25 23:21:43 hostubuntu systemd[1]: cgateweb.service: Unit entered failed state.
            Dec 25 23:21:43 hostubuntu systemd[1]: cgateweb.service: Failed with result ‘exit-code’.

            then i tried your next suggestion:

            [email protected]:/usr/local/bin/cgateweb$ sudo node /usr/local/bin/cgateweb/index.js
            /usr/local/bin/cgateweb/index.js:139
            for (i = 0;i 1 && parts1[0] == “300”) {
            ^

            ReferenceError: Invalid left-hand side in assignment
            at exports.runInThisContext (vm.js:53:16)
            at Module._compile (module.js:374:25)
            at Object.Module._extensions..js (module.js:417:10)
            at Module.load (module.js:344:32)
            at Function.Module._load (module.js:301:12)
            at Function.Module.runMain (module.js:442:10)
            at startup (node.js:136:18)
            at node.js:966:3

            hope this has some useful information i dont understand any of it but realy want to get this working 😀

            thanks dave.

          4. Looks like some of the code is missing so it’s erroring out because it’s now invalid. Better use the original scripts.

          5. omg its working…
            i uploaded the default index.js and settings.js (and added the required details)
            and i was able to turn my lights on with home assistant 😀
            this is amazing
            i dont know if it was an issue with the other scripts i used
            or just that when i copied the codes using vim
            my mouse buttons keep double pasting
            i might have lost a couple lines
            im sure ill be back with some other questions
            but its working!

          6. im back 😀
            now im stuck at getting cbus dimming/mem toggle2 to work from home assistant
            ive tried to flook some settings
            but home assistant always turns brightness to 100%
            would know the correct codes?

            – platform: mqtt
            name: Bedroom 3 Main Lights
            state_topic: “cbus/read/254/56/5/state”
            command_topic: “cbus/write/254/56/5/switch”
            brightness_state_topic: “cbus/read/254/56/5/level”
            brightness_command_topic: “cbus/write/254/56/5/ramp”

            thanks dave

          7. I don’t know anything about home assistant or NEO, but I had a quick look at the thread.

            When sending commands to cgateweb, you always send to the cbus/write/… topics. The actual light states get reported back on the cbus/read/… topics.

            I’m guessing you’d need to make sure you’re using the right topics?

            platform: mqtt
            name: Bedroom 3 Desk Lights
            state_topic: “cbus/read/254/56/10/state”
            command_topic: “cbus/write/254/56/10/switch”
            brightness_state_topic: “cbus/write/254/56/10/ramp” <- change to "cbus/read/254/56/10/level"? brightness_command_topic: "cbus/read/254/56/10/level" <- get rid of this maybe? brightness_command_topic: "cbus/write/254/56/10/ramp" payload_on: "ON" payload_off: "OFF" brightness_scale: 100 on_command_type: 'brightness’ qos: 2

          8. i originally used those codes from the post you first referred me to but with just those brightness topics its worse result.

            brightness_state_topic: “cbus/read/254/56/10/level”
            brightness_command_topic: “cbus/write/254/56/10/ramp”

            home assistant pumps the brightness to 100% every time i turn the light on unless the light is dimmed by home assistant immediately before it is turned off and back on.

            with the codes i flooked the lights never get pumped to 100% home assistant always remembers the last brightness it used just the brightness is not synced with the neo wall switches. they both recall their own last brightness levels. its not too bad as i usually dimm to 50% anyways but they definitly dont update each other with brightness values. its still better than them being pumped to 100%

            if its something you cant work out i bet that brightness bug is still in home assistant :/

          9. On this page under the section “BRIGHTNESS WITHOUT ON COMMANDS” it suggests using this parameter:
            on_command_type: ‘brightness’
            It’s supposed to stop home assistant from sending the ON command, which will be what’s pumping the brightness. Maybe try this with the configuration I suggested?

          10. yup i had tried that initially to include: on_command_type: ‘brightness’
            with the codes from nrepians comment Oct 28, 2017
            which i think are the same as your suggestion
            https://github.com/the1laz/cgateweb/issues/6

            still pumped to 100% tho

            i haven’t tried drmcinnes comment Nov 4, 2017
            he says he made a modification of cgateweb to stop the the on command after a ramp but for openhab so not sure if i should try that.

            i think his solution was before home assistant implemented the on_command_type. but that command seems only to work if a brightness level is ramped in home assistant right before the light is turned off and back on.

          11. with on_command_type: ‘brightness’

            you literally have to dim the light, turn it off, turn it back on for it to obey the last dim level.

            if you use another light or do anything else between dimming the light and turning it off / back on it will pump to 100%.

            my latest code that shouldn’t work does pretty well lol
            it always remembers the last dim level but its not synced
            home assistant and my cbus wall buttons remember their own dim levels
            but as i normally dim to 50% its not noticeable and far better than the lights pumping to 100%

            https://community.home-assistant.io/t/mqtt-switch-state-not-matching-payload/30837/4

            be great if you could find out why 😀
            but im still very happy to get this working as well as it is

          12. Sorry, I haven’t got any other ideas. Because I don’t know anything about the systems you’re using it with I don’t really know what’s going on. It’s good that it’s usable now.

      2. I went through the tutorials again and got a different error:

        [email protected]:/usr/local/bin/cgateweb$ sudo service cgateweb status
        ? cgateweb.service – cgateweb
        Loaded: loaded (/etc/systemd/system/cgateweb.service; enabled; vendor preset: enabled)
        Active: activating (auto-restart) (Result: exit-code) since Mon 2017-12-25 21:58:42 AEDT; 6s ago
        Process: 7908 ExecStart=/usr/bin/nodejs /usr/local/bin/cgateweb/index.js (code=exited, status=1/FAILURE)
        Main PID: 7908 (code=exited, status=1/FAILURE)

        Dec 25 21:58:42 hostubuntu systemd[1]: cgateweb.service: Unit entered failed state.
        Dec 25 21:58:42 hostubuntu systemd[1]: cgateweb.service: Failed with result ‘exit-code’.

  6. Thanks, I tried the code there is some error i had to ControlC to escape it.

    [email protected]:~$ sudo service cgateweb status
    [sudo] password for dd9:
    ● cgateweb.service – cgateweb
    Loaded: loaded (/etc/systemd/system/cgateweb.service; enabled; vendor preset:
    Active: activating (auto-restart) (Result: exit-code) since Mon 2017-12-25 19
    Process: 2107 ExecStart=/usr/bin/nodejs /usr/local/bin/cgateweb/index.js (code
    Main PID: 2107 (code=exited, status=1/FAILURE)

    Dec 25 19:59:48 hostubuntu systemd[1]: cgateweb.service: Main process exited, co
    Dec 25 19:59:48 hostubuntu systemd[1]: cgateweb.service: Unit entered failed sta
    Dec 25 19:59:48 hostubuntu systemd[1]: cgateweb.service: Failed with result ‘exi
    lines 1-9/9 (END)

  7. I went through the tutorials again and got a different error:

    [email protected]:/usr/local/bin/cgateweb$ sudo service cgateweb status
    ? cgateweb.service – cgateweb
    Loaded: loaded (/etc/systemd/system/cgateweb.service; enabled; vendor preset: enabled)
    Active: activating (auto-restart) (Result: exit-code) since Mon 2017-12-25 21:58:42 AEDT; 6s ago
    Process: 7908 ExecStart=/usr/bin/nodejs /usr/local/bin/cgateweb/index.js (code=exited, status=1/FAILURE)
    Main PID: 7908 (code=exited, status=1/FAILURE)

    Dec 25 21:58:42 hostubuntu systemd[1]: cgateweb.service: Unit entered failed state.
    Dec 25 21:58:42 hostubuntu systemd[1]: cgateweb.service: Failed with result ‘exit-code’.

Leave your comment