Category: Raspberry Pi

Raspberry Pi -> Fonera relay control

So, now that I have a Raspberry Pi and a hacked fonera with 4 relays, it’s time to let the Raspberry Pi control the relays of the fonera. This can be done by logging in to the Fonera with ssh and giving some commands.

Of course, having to type these commands everytime you want to switch a relay on or off is cumbersome and useless for automatisation.

Luckily there is an interesting tool available Expect (apt-get install expect).

With expect, I’ve created a small script named gpio.sh that I can use to remotely control the relays of my hacked fonera.

#!/usr/bin/expect -f

set ip		192.168.242.201
set user	root
set passwd 	averysecretpasswd

set gpio	""
set relay  	[lindex $argv 0]
set onoff 	[lindex $argv 1]

# Translate relay to gpio
# -----------------------

if { "$relay" == "1" } {
	set gpio 3
}

if { "$relay" == "2" } {
        set gpio 4
}

if { "$relay" == "3" } {
        set gpio 1
}

if { "$relay" == "4" } {
        set gpio 7
}

# Default is switch relay off
# ---------------------------

if { "$onoff" == "" } {
	set onoff 0
}


# Check if port is valid, if not -> error
# ---------------------------------------

if { "$gpio" == "" } {
        puts "Usage : gpio.sh relay \[1\]\n"
	exit
}

# Put some info on the screen
# ---------------------------

puts "Send $onoff to relay/gpio $relay/$gpio\n"

# Check if $ip is alive, if not -> error
#---------------------------------------

spawn ping -c 1 -W 1 $ip
expect -re "100%"	{
	puts "No connection to $ip"
	exit
}

# DO IT
# ----

set timeout 10
spawn ssh $user@$ip
expect -re "password" 	{send "$passwd\r"}
expect -re "#"		{send "echo 1 > /proc/gpio/$gpio\_dir\r"}
expect -re "#"		{send "echo $onoff > /proc/gpio/$gpio\_out\r"}
expect -re "#"		{send "exit\r"}
close $spawn_id

To switch relay 1 on, I just type ./gpio.sh 1 1.
To switch it off, I type ./gpio.sh 1 0.
Expect does all the magic like entering the password and ”typing” the right commands.

Use a Raspberry Pi to hack la Fonera 2100

Now that I have a great pfSense box with Wi-Fi, I don’t need my Fonera 2100 anymore. So, why not try to do something interesting with it? 😉

At http://www.dd-wrt.com/wiki/index.php/LaFonera_Hardware_Serial-Cable-Port you can read how to access the serial console on the Fonera.

The serial port of the fonera uses 3.3V instead of the normal RS-232 voltage levels. Because of that, you need some level converters to connect the Fonera to a normal serial port.

But, the Raspberry Pi also has a serial port which operates at 3.3V. This means that you can connect the serial port of a Raspberry Pi and la Fonera without the need of a level shifter.


“Schematic”

Photo

With a serial communication program like minicom it is now possible to access the Fonera.


starting minicom -b 9600 -D /dev/ttyAMA0

minicom : fonera booting

Start a program at boot up

I have no keyboard and display connected to my Raspberry Pi (running Raspbian), but log in to it over the network by using ssh.

Now, I wanted to automatically start a program when the Raspberry Pi boots up, but also be able to see the console output of that program when needed.

To accomplish that, I’ve done the following:

  • sudo apt-get install mingetty
  • sudo apt-get install screen
  • Edit /etc/inittab, and changed the part with getty’s to autologin on tty3 with mingetty as user pi
...snip...
# /sbin/getty invocations for the runlevels.
#
# The "id" field MUST be the same as the last
# characters of the device (after "tty").
#
# Format:
#  :::
#
# Note that on most Debian systems tty7 is used by the X Window System,
# so if you want to add more getty's go ahead but skip tty7 if you run X.
#
1:2345:respawn:/sbin/getty --noclear 38400 tty1 
2:23:respawn:/sbin/getty 38400 tty2
#3:23:respawn:/sbin/getty 38400 tty3
#4:23:respawn:/sbin/getty 38400 tty4
#5:23:respawn:/sbin/getty 38400 tty5
#6:23:respawn:/sbin/getty 38400 tty6
T:23:respawn:/sbin/mingetty --autologin=pi tty3
...snip...
  • Add the following to /home/pi/.profile, to autostart a program when user pi login on tty3
if [ -z "$DISPLAY" ] && [ $(tty) == /dev/tty3 ]; then
	screen -d -m /home/pi/tstat
fi

So, when the Raspberry Pi boots, user pi will automatically log in and run the program tstat. Because tstat is started with screen, I can log in with ssh and run screen -R to see the output of tstat and press ctrl-a d to exit screen, but leave tstat running.

login and typing screen -R

output of tstat

After pressing ctrl-a d

Raspberry Pi and Wi-Fi

I’ve bought this USB WiFi Adapter at dx.com.

This cheap (5.25€) WiFi adapter has a Ralink5370 chipset,

and works very well with a Raspberry Pi. Just follow the instructions at http://elinux.org/RPi_Ralink_WLAN_devices.

Because I’ve seen some strange behavior (route – default gateway not set correctly) when both eth0 and wlan0 were up, I’ve changed /etc/network/interfaces so that when the USB WiFi adapter is plugged in, eth0 is disabled.

pi@raspberrypi ~ $ cat /etc/network/interfaces
# The loopback network interface
auto lo
iface lo inet loopback

# my wifi device
allow-hotplug wlan0
iface wlan0 inet dhcp
 	pre-up	   ifconfig eth0 down
	wpa-driver wext
 	wpa-conf   /etc/wpa_supplicant.conf
	post-down  ifconfig eth0 up 
	
# The primary network interface
# allow-hotplug eth0
iface eth0 inet static
	pre-up	ifdown wlan0
	address 192.168.1.21
	netmask 255.255.255.0
	network 192.168.1.1
	broadcast 192.168.1.255
	gateway 192.168.1.1
	post-down ifup wlan0
pi@raspberrypi ~ $ 

Raspbian I²C

These are the steps to enable I²C on Raspbian after downloading the new Raspbian “wheezy” image.

Add i2c-dev and i2c-bcm2708 to /etc/modules.

pi@raspberrypi ~ $ cat /etc/modules
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
# Parameters can be specified after the module name.

# snd-bcm2835
i2c-dev
i2c-bcm2708
pi@raspberrypi ~ $ 

Install i2c-tools

pi@raspberrypi ~ $ sudo apt-get install i2c-tools
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Suggested packages:
  libi2c-dev python-smbus
The following NEW packages will be installed:
  i2c-tools
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 0 B/58.9 kB of archives.
After this operation, 223 kB of additional disk space will be used.
Selecting previously unselected package i2c-tools.
(Reading database ... 56011 files and directories currently installed.)
Unpacking i2c-tools (from .../i2c-tools_3.1.0-1_armhf.deb) ...
Processing triggers for man-db ...
Setting up i2c-tools (3.1.0-1) ...
pi@raspberrypi ~ $

Add pi to the group i2c if you want to access i2c without sudo.

pi@raspberrypi ~ $ sudo usermod -a -G i2c pi

Use i2cdetect to see if there are devices connected on the i²C-bus.

pi@raspberrypi ~ $ i2cdetect 0
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will probe file /dev/i2c-0.
I will probe address range 0x03-0x77.
Continue? [Y/n] y
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --                         
pi@raspberrypi ~ $ 

Mount Raspberry Pi on PCB

The Raspberry Pi is a interesting little computer to use in embedded projects, especially when you need something more powerfull than a common microcontroller.

A disavantage of the Raspberry Pi is that it doesn’t have mounting holes, so it’s difficult to mount it on a PCB.

Because in embedded projects the yellow RCA connector is not usefull, I have used that connector to mount the Raspberry Pi vertical on a experiment board. This is done by using a 3mm bolt with heat shrink, which holds the Raspberry Pi steady in place, but also allows to remove it easily when needed.



To avoid rotating the Raspberry Pi, I have used a second 3mm bolt at the place of the audio connector.



The final result of this quick and dirty hack looks like this:



Note, no Raspberry Pi’s were harmed while doing this experiment 😉

Raspberry Pi TC74 I²C

In my previous post I have connected a TC74 via USB to the Raspberry Pi. It works, but I consider it a waste because the Raspberry Pi has GPIO’s available.

So, I have connected a TC74 directly to GPIO-0 & GPIO-1 of the Raspberry Pi.

On the software side, I make use of the Debian Sqeeuze Image, but upgraded the kernel to Chris Boot’s Raspberry Pi Kernel. I have also installed i2c-tools.

With i2cdetect I can see the TC74 on the I²C bus.

And with i2cget, I can read the current temperature (hex) of the TC74.

To make all this available to a webserver like in my previous post, I have changed cgi-bin/temp to the following:

#!/bin/bash
echo Content-type: text/html
echo

# USB : 
# echo "document.write(\"`/home/pi/usb/temp`\");"

# I2C :
Thex=`/usr/sbin/i2cget -y 0 0x48 0 b | cut -d"x" -f2 | tr '[:lower:]' '[:upper:]'`
T=`echo "ibase=16 ; $Thex" | bc`
echo "document.write(\"$T\");"

Which gives the following result when I connect with a web browser to the Raspberry Pi:

Raspberry Pi + PIC18F4550 TC74 USB

So, now that I have a Raspberry Pi and I can read the temperature with a TC74 over USB, I thought it would be nice to combine them together.

Because the Raspberry Pi makes use of the ARM architecture, a re-compilation of temp.c is necessary.

(click for larger image)

After compiling temp.c, everything just works like on a normal PC… Great! 🙂

As a test, I have then installed a webserver (boa) on the Raspberry Pi and created a little webpage that displays the current temperature.

The index.html of this simple webpage looks like this:

<html>
<head>
<meta http-equiv="Refresh" content="4">
</head>
<body style="margin:0px;" background="background.png" text="#000000"  bgcolor="#E0E0E0" link="#0000FF" vlink="#800080" alink="#FF0000">

<center>
<h1>Test : Raspberry Pi Debian GNU/Linux + PIC18F4550 TC74 USB</h1>
</center>
<hr>

<center>
<h2>The temperature is now <b><script type="text/javascript" src="/cgi-bin/temp"></script><noscript>No JavaScript support!</noscript></b>C.</h2>
</center>

</body>
</html>

And cgi-bin/temp looks like this:

#!/bin/bash
echo Content-type: text/html
echo
echo "document.write(\"`/home/pi/usb/temp`\");"

When I connect with a normal web browser to the Raspberry Pi, I get a simple webpage that displays the temperature:

It should also be possible to connect a TC74 directly (via I²C) to the GPIO pins of the Raspberry Pi, mmmm,… maybe something for a next post 😉