Posts: 14
Joined: Mon Sep 24, 2012 1:55 pm

CAN for home automation

Tue Jan 29, 2013 11:11 pm


I have installed two ventilators, several temperature sensors and one humidity sensor in my basement. I also installed one humidity sensor outside of my house. The humidity sensors actually measure the relative humidity, but the sensors translate the values into absolute humidity by doing some math (http://en.wikipedia.org/wiki/Humidity). So the reason I have done all this, is that the ventilators blow air into the basement, if the air outside is dryer, than inside. This is how I try to reduce the humidity in the basement.

All sensors are connected to a CAN bus and to a controller, which has a USB port. So if I want to get all measured values onto my laptop, I need to go down to the basement and retrieve the values from usb. So I prefer to have something with wireless. Here comes the raspberry pi into play. So what I want is, to connect the raspberry pi with a WLAN stick and to the CAN bus. And I can retrieve the measured values from upstairs.

The raspberry does not have CAN on its ports. But it has SPI or I2C. So I need something which translates SPI to CAN. There are chips available for doing this. One of them is MCP2515 http://www.microchip.com/wwwproducts/De ... e=en010406. The chip is widely available, and runs with 3.3V which can be powered from the raspberry. Also I needed another chip which is a CAN transceiver. It connects the MCP2515 chip to the CAN bus. I used SN 65HVD230D from texas instruments, which is also widely available and which operates also on 3.3V.

The schematics I created:
Screenshot.png (12.25 KiB) Viewed 3854 times
I soldered this all together with silver wire and it looks like this from top:
board.PNG (55.55 KiB) Viewed 3854 times
Here how to install all this:
Retrieve latest wheezy version and put on your sd card

Ref.: I described this before, so see link above.

Install Emacs

Ref.: http://www.raspberrypi.org/phpBB3/viewtopic.php?t=18314

Check link, or use any other editor you like.

Install vnc

Ref.: http://www.raspberrypi.org/phpBB3/viewtopic.php?t=18314

Check link.

Resize linux partition

Ref.: http://raspberrypi.stackexchange.com/qu ... -partition

This is an important step, since the installation below will fail if you wont do. So dont skip it.
  • sudo fdisk -u -c /dev/mmcblk0
  • type p, then remember the start value of the partition you want to resize. On my raspberry the line /dev/mmcblk0p2 had the start value 122880.
  • delete the partition. So I typed d and then 2 to remove /dev/mmcblk0p2
  • create a new partition. I typed n and then p for primary. Next I typed 2. You will be asked for the start number, so I typed 122880. This was the start value I remembered above. It might differ for you. Then I hit enter and accepted the default.
  • save the configuration with w.
  • sudo reboot.
  • Finally sudo resize2fs /dev/mmcblk0p2.
Install SPI Libraries

Ref.: http://neophob.com/2012/08/raspberry-pi ... pi-device/
  • sudo apt-get update
  • sudo apt-get upgrade . This takes a while ...
  • sudo wget http://goo.gl/1BOfJ -O /usr/bin/rpi-update
  • sudo chmod +x /usr/bin/rpi-update
  • sudo rpi-update
  • reboot the system: sudo reboot
  • wget http://apt.bootc.net/debian/pool/main/l ... _armel.deb
  • sudo dpkg -i linux-image-3.2.23-rpi1+_1_armel.deb
  • sudo dpkg --force-architecture -i linux-image-3.2.23-rpi1+_1_armel.deb
  • emacs /etc/modprobe.d/raspi-blacklist.conf. Comment out blacklist spi-bcm2708 with #
  • sudo reboot
  • check if spi has been installed: ls -al /dev/spi*
  • retrieve test program from http://git.kernel.org/?p=linux/kernel/g ... dev_test.c
  • compile it: gcc spidev_test.c -o spidev_test to your local dir.
  • Apply wire between MISO and MOSI pin. I think it is GPIO 9 and GPIO 10.
  • execute spidev_test -D /dev/spidev0.0 and you should see matrix a filled with mostly "FF"s, few "00" and some other values. This means, your SPI works! If the matrix shows only "00", it is not working.
with the latest wheezy versions we have a WIFI Config tool to setup the WLAN stick. So the old installation script I used before becomes obsolete. I dont know why, but I cannot get WLAN started automatically after reboot. I always have to press manually the connect button to get WLAN attached to my router. So I did setup my WLAN with the WIFI Config tool. Then I modified the automatically generated /etc/network/interfaces file with my own settings:

Code: Select all

auto lo
iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
auto wlan0
iface wlan0 inet dhcp
wpa-ssid "xxxxxxxxxxx"
wpa-psk "xxxxxxxxxxxxxxxxx"
Now WLAN works just fine after reboot.


Ref.: http://www.bitwizard.nl/wiki/index.php/ ... spberry_Pi

At the reference you find code (bw_lcd.c) which was very valuable for me. In order to work for the MCP2515, I added a couple of other functions, such as the one below. It can be used for the read instruction of the MCP2515.

Code: Select all

static int get_addr_reg_value8 (int fd, int add, int reg)
  char buf[5]; 
  buf[0] = add;
  buf[1] = reg;
  buf[2] = 0;
  int len = 3;

  int ret;
  uint8_t rx[0x80] = {0, };
  struct spi_ioc_transfer tr = {
    .tx_buf = 0,
    .rx_buf = (unsigned long)rx,
    .len = 0,
    .delay_usecs = delay,
    .speed_hz = speed,
    .bits_per_word = bits,

  tr.tx_buf = (unsigned long)buf;	
  tr.len = len;
  ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
  if (ret < 3)
    pabort("can't send spi message");

Here another command, the bit modify instruction of the MCP2515:

Code: Select all

static void set_addr_reg_value8_value8 (int fd, int add, int reg, int val0, int val1)
  char buf[5]; 
  buf[0] = add;
  buf[1] = reg;
  buf[2] = val0;
  buf[3] = val1;
  transfer (fd, 4, buf);
If you need source code for the MCP2515, I recommend to google for it. There are many examples out there which you can use.

Posts: 1
Joined: Fri Feb 15, 2013 9:03 am

Re: CAN for home automation

Sun Feb 17, 2013 1:20 pm

Regarding the section 'Resizing the Linux Partition' ..
Is this ( complex looking ) process different to the Menu Item 'Expand Root Partition to Fill SD Card' that is available on first boot or config of the Pi?

Posts: 14
Joined: Mon Sep 24, 2012 1:55 pm

Re: CAN for home automation

Tue Feb 19, 2013 3:44 pm

I was not aware that there is such a menu item during first boot:

'Expand Root Partition to Fill SD Card'

Next time I will pay attention during the first boot. In case you have not done it, you can do it the way I described here. Although I dont think it is really complicated.

User avatar
Posts: 1979
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: CAN for home automation

Tue Feb 19, 2013 6:54 pm

tupf wrote:I was not aware that there is such a menu item during first boot:

'Expand Root Partition to Fill SD Card'

Next time I will pay attention during the first boot. In case you have not done it, you can do it the way I described here. Although I dont think it is really complicated.
Also the current kernel supplied with Raspbian has the SPI drivers installed. (and has had them installed for some time - like 5-6 months), you just need to un-blacklist them if you need them loaded at boot time. Use vi or some other editor to edit /etc/modprobe.d/raspi-blacklist.conf

Gordons projects: https://projects.drogon.net/

Posts: 6
Joined: Thu Jan 02, 2014 5:26 pm

Re: CAN for home automation

Thu Jan 23, 2014 4:06 pm

Very interesting post. I have done a similar network:


has a CAN controller board for the Raspberry Pi with essentially the same circuit. It is ready made and they also have some other modules which you could add to your network (motor control, tilt sensor, etc).

Beyond Kinetics is my company, however so far we are just trying to get started with some sales. The prices are reasonable and should save much work.

Best regards,
Steve Tenniswood

Return to “Automation, sensing and robotics”

Who is online

Users browsing this forum: No registered users and 11 guests