RaspberryPi

I own a RaspberryPi B already quite a while (June 2012). Originally I bought it to make can4linux running on it with a MCP2515 connected via SPI.
After starting with porting the already available can4linux SPI driver I had to stop for working on other projects.

I spring 2014 I anyway ordered the HDMIPi at Kickstarter,which I finally got in October 2014.

Now, we have November 11., The very cheap model RaspberryPi A+ was announced at electronica Munich. So I think it is time to go back to my original project and work on can4linux for the MCP2515 CAN controller.

Download NOOB

NOOBS is the latest user friendly installation system for the Raspberry Pi. It allows you to choose one of 6 (currently) available Operating Systems to run on the RaspberryPi. You can pick and choose and change your OS at any time you wish simply by rebooting and holding the SHIFT key down. However, any user data on the SD card will be lost each time.
I found easy to use installation instructions for Linux users on Rants & Raves Blog.

Used NOOB and installed the latest Raspbian
after that, did:

sudo apt-get update
sudo apt-get upgrade
rpi-update

After that I had

Linux raspberrypi 3.12.32+ #721 PREEMPT Fri Nov 7 16:50:31 GMT 2014 armv6l GNU/Linux

Display resolution

My HDMIPi has a very high resolution and small size, 7". It's very hard to read the displayed characters. The good, it is easy to change the font size by editing the file /etc/default/console-setup. I changed it to:

FONTFACE="Terminus"
FONTSIZE="16x32"

Changing the host name

If you have more than one of the RaspberryPis in your network, like me, having a B+ and an A+, its mor comfortable to assign different names to all these boards. The Raspbian OS image always has assigned "raspberrypi".

sudo vi /etc/hosts
sudo vi /etc/hostname
sudo /etc/init.d/hostname.sh start

Setting up the WiFi Network

In Order to use the WiFi WLAN the Raspberry should know about your wireless network. The minimum is telling about the network name, known as SSID and the password. Edit one file as user root:

sudo vi /etc/wpa_supplicant/wpa_supplicant.conf

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
        ssid="network-name"
        psk="password"
        key_mgmt=WPA-PSK
}

Of course, networkname and **password" have to be yours.
This will allow the Pi to get a dynamic IP from your network. Setting a ststac IP is explaind BananPi

If the RasperryPi is using DHCP instead having assigned fixed IP address, it makes sens to scan the network for open ssh ports

Prepare for Kernel driver development

get the rpi-source tool

sudo wget https://raw.githubusercontent.com/notro/rpi-source/master/rpi-source -O /usr/bin/rpi-source
sudo chmod +x /usr/bin/rpi-source
/usr/bin/rpi-source -q --tag-update

Update for gcc 4.8 which is used for newest kernel developments

sudo apt-get install -y gcc-4.8 g++-4.8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 50
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 50
gcc --version
  gcc (Raspbian 4.8.2-21~rpi3rpi1) 4.8.2

sudo apt-get install ncurses-dev

Another way to get a newer gcc (4.8) is described here.

call rpi-source to get the kernel sources

rpi-source

After checking the gcc version, the tool downloads the current kernel sources and unpacks it into the users home directory. A link named linux is created to the linux source directory. To compile can4linux with the standard Makefile an additional link is placed in /lib/modules/uname -r/build pointing to the linux source tree.

Helpful spi-utility

Typically a spi device driver has to be configured with spi platform data like

    static struct mcp251x_platform_data mcp251x_info = {
        .oscillator_frequency = 19000000,
        .board_specific_setup = myboard_mcp251x_initfunc,
        .device_reset = myboard_mcp251x_reset,
        .transceiver_enable = NULL,
    };

    static struct spi_board_info spi_board_info[] __initdata = {
    {
        .modalias   = "mcp251x",
        .platform_data  = &mcp251x_info,
        .irq        = 10,
        .max_speed_hz   = 10000000,
        .bus_num    = 1,
        .chip_select    = 0
    };

And this has to be compiled into the kernel.
To configure the spi device driver without recompiling the kernel the kernel module spi-config can be used. After downloading using git, it compiles and can be used.

$ git clone https://github.com/msperl/spi-config.git
$ make
$ sudo make install

can4linux

As prerequisite some modules have to be loaded and configured

sudo modprobe spi-bcm2708 # obsolete  if spi is enabled using raspi-config
sleep 0.5
sudo modprobe spi-config devices=force_release:\
bus=0:cs=0:modalias=mcp2515:speed=10000000:gpioirq=25:pd=20:pdu32-0=16000000:pdu32-4=0x2002,
/bin/echo -n "spi0.0: "; cat /sys/bus/spi/devices/spi0.0/modalias
/bin/echo -n "spi0.1: "; cat /sys/bus/spi/devices/spi0.1/modalias

The SPI system can alternatively be enabled using the raspi-config command

sudo raspi-config

Select ' 5 Interfacing Options->P4 SPI '.
At next reboot, two modules are loaded: spidev and spi_bcm2835.

Then you can do as usual

/sbin/insmod can4linux.ko

As hardware interface I have used the AVRcard CAN-SPI Adapter

CAN-SPI Adapter
(it seems that this board is no longer available). Of course I have exchanged the CAN Transceiver by a 3.3V type (Texas Instruments) and connected the 5 V pin to the Raspberry 3V3 pin 1.

Basic functions were working already per Nov. 30. The driver is now stable.
For performace measurement the time command can be used. Get it by doing

sudo apt-get install time

And a simple performance measurement for transmitting frames, here used 250 Kbit/s and send 100,000 frames

oe@raspberrypi ~ $ /usr/bin/time can_send -Dcan0 -b 250 -t11 -T100000
0.21user 2.92system 1:15.53elapsed 4%CPU (0avgtext+0avgdata 400maxresident)k
0inputs+0outputs (0major+156minor)pagefaults 0swaps

Sending with full speed needs 4% CPU. Receiving frames is more difficult. The MCP2515 has only two receive buffers which have to be read out in a short time. With a standard Linux it can not be guaranteed that the ISR can be handled fast enough. Other drivers my block the CAN interrupt handling for some time and in this case CAN frames get lost.
Using 250 kbit/s, frames can arrive every 240µs in case of short frames without data or ca. 500 µs in case of 8 data byte. The exact length of a CAN frame depends on the data content, because depending on it, the CAN controller includes a certain amount of stuff bits. Anyway the following measurement was done with bus speed 250 kbit/s and a busload of around 97% generated on a PC with _can__send -t11 -T100000.

root@raspberrypi:/# /usr/bin/time /home/oe/bin/receive -H -t1 -b250 > /run/logfile
5.81user 4.20system 0:55.40elapsed 18%CPU (0avgtext+0avgdata 676maxresident)k
0inputs+0outputs (0major+581minor)pagefaults 0swaps

As can bee seen, CPU load is larger than with sending. BUT, only 99794 frames were received correctly, others got lost because of CAN receiver message overflow which was reported by receive 150 times. The result is only slightly better using the option -t0, don't capture a time stamp in the driver. Received 99826 frames, number of message lost events 150.

Experimenting with changing the RoundRobin priority between 1 and 99 for the receive process did not change anything.

Raspberry A+

Nice little device. To configure it, I connected the 7" HDMIPi Display, USB Hub with Logitech keyboard and the Edimax USB-Wlan adapter. Besides seting the hostname, keyboard layout, enabling ssh and extending the root partition, the only task really needed to do is configuring the WLAN interface. The easiest way is starting the X Server by

$ startx

The Raspberry starts the window manager and also looks for available WLAN access points. In my version (Image Raspbian 5.5.2015) network scanning was monitored in the uper right corner of the screen display. After the network SSID was recogniced and shown, select it and enter the pass phrase. Thats it. Now you can reboot and log in using ssh, without the need to connect a HDMI display and keyboard.

pi@raspiA ~ $ uname -a
Linux raspiA 3.18.11+ #781 PREEMPT Tue Apr 21 18:02:18 BST 2015 armv6l GNU/Linux

Using Blynk and DHT22 temperature-humidity sensor

Jan 2016
Adafruit DHT11/DHT12 Data Sheet.
AM2302 Data Sheet.
Lets see how this will work Raspberry Pi + Node.js + Blynk App + DHT22. My first try was on an PiZero. The first step mentioned there is getting the bcm2835 library.

cd ~/src
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.49.tar.gz
tar zxvf bcm2835-1.49.tar.gz
rm bcm2835-1.49.tar.gz

First look at http://www.airspayce.com/mikem/bcm2835/ for the latest version, which was 1.49 in my case.

cd bcm2835-1.49
./configure
make
sudo make check
sudo make install
sudo npm install -g node-dht-sensor

npm is the package manager for JavaScript. In case ist is not already installed, do

sudo apt-get install npm

I get an error:

sudo npm install -g node-dht-sensor
npm WARN engine node-dht-sensor@0.0.8: wanted: {"node":">=0.12 <0.13"} (current: {"node":"0.10.29","npm":"1.4.21"})
  ....
and cpp compiler errors

Because of this version mismatch I searched and found https://learn.adafruit.com/node-embedded-development/installing-node-dot-js. The following commands are used to get node v0.12

curl -sLS https://apt.adafruit.com/add | sudo bash
sudo apt-get install node

Now version testing and compilation works.

oe@pizero:~/src/bcm2835-1.49 $ sudo npm install -g node-dht-sensor
/
> node-dht-sensor@0.0.8 install /usr/local/lib/node_modules/node-dht-sensor
> ./check-bcm2835

Library bcm2835 found.
gyp WARN EACCES user "root" does not have permission to access the dev dir "/root/.node-gyp/0.12.6"
gyp WARN EACCES attempting to reinstall using temporary dev dir "/usr/local/lib/node_modules/node-dht-sensor/.node-gyp"

make: Entering directory '/usr/local/lib/node_modules/node-dht-sensor/build'
  CXX(target) Release/obj.target/node-dht-sensor/node-dht-sensor.o
  SOLINK_MODULE(target) Release/obj.target/node-dht-sensor.node
  COPY Release/node-dht-sensor.node
make: Leaving directory '/usr/local/lib/node_modules/node-dht-sensor/build'

node-dht-sensor@0.0.8 /usr/local/lib/node_modules/node-dht-sensor

Getting the Blynk library

cd ~/src
git clone https://github.com/blynkkk/blynk-library.git
cd blink-library/linux
./build.sh raspberry
 ...

What else works and is easier is

sudo npm install -g blynk-library

First test, the auth token obtained from an Android based small Blynk UI example.

oe@pizero:~/src/blynk-library/linux $ sudo ./blynk -t xxxxxxxxxxxx
[1] Blynk v0.3.3-beta
[5001] Connecting to cloud.blynk.cc:8442
[5507] Ready (ping: 173ms).
^C
node --version
v0.12.6
nodejs --version
v0.10.29

Now I connected the Sensor

and

sudo sudo NODE_PATH=/usr/local/lib/node_modules node ./sensor

To make it available after a Pi cold start. the Blynk app can be started by adding a entry at /etc/rc.local

# start the blynk application reading temp and humidity
NODE_PATH=/usr/local/lib/node_modules node /home/oe/src/blynk-applications/sensor

I often had problems with long term run when started from /etc/rc.local and decided to follow this link and use Upstart to start the application. (another link to Upstart) The script is stored at /etc/init/sensor.conf.

description "node.js sensor application"
author      "hjoe"

# Used to Be: Start on Startup
start on filesystem and net-device-up IFACE=wlan0
stop on shutdown

# Automatically Respawn:
respawn
respawn limit 99 5

script
    # Not sure why $HOME is needed, but we found that it is:
    export HOME="/home/oe"
    export NODE_PATH=/usr/local/lib/node_modules
    exec /usr/local/bin/node  /home/oe/src/blynk-applications/sensor >> /var/log/node.log 2>&1
end script

post-start script
   # Optionally put a script here that will notifiy you node has (re)started
end script

Later I added a script to watch the network for a Amzon Dash Button press event following the same principle

Both are jobs with start on, but no stop on, waiting for the WLAN be ready.

Some WLAN adapters are using a power saving mode and will switch off after some time. If the Blynk app should be reached at any time. it is necessary to have always an active WLAN adapter. The default way is to add the following line in the /etc/network/interfaces entry

wireless-power off

Another source suggested putting

iwconfig wlan0 power off

into /etc/rc.local.

Raspberry Zero

Using

sudo raspi-config

to make the samll Pi Zero a headless system, that means switch off the the X-server which is running with the default image.
Select :
"3 Boot Options Configure options for start-up"
"B1 Desktop / CLI Choose whether to boot into a desktop environment or the command line
" B1 Console Text console, requiring user to login"