Monthly Archives: January 2014

How to: Run a command on start / boot up on Raspberry Pi Wheezy (Autostart)

I recently had the problem that my OpenVPN tunnel did not assigned to an IP address. Since i was not in a mood to find out what causes the problem i simply wrote a bash script that runs “dhclient tap0” after booting up the system.

for example, “sudo nano /home/pi/myautoip.sh” :

printf “Trying to get an IP address for tap0”
dhclient tap0

In order to achieve that this command is executed we have to add a command to /etc/rc.local.
so “sudo nano /etc/rc.local”. Add this ABOVE the “exit 0”:

bash /home/pi/myautoip.sh

Advertisements

HowTo: Build your own temperature logger with Raspberry Pi (DS18B20)

Last week I received my fourth Raspberry Pi. Yes, I really love this tiny machines. I already build a media center (xbmc), a smart home light switch + android app, a remote backup system using openvpn, samba and rsync, security camera using motion and kinect and a temperature logger.

Since some people wanted to know how to build it, i will make a “short” tutorial.
chart of temperature logging

1. Go Shopping:
1x DS18B20 (Datasheet / Amazon US / Amazon UK / Amazon DE  )
DS18B20

1x Resistor 4,7 k Ohm ( Amazon US / Amazon DE )
4700 Ohm Resistor

(optional) 1x Breadboard + Wires (Amazon US / Amazon DE)

2. Circuit

First of all we have a look at the sensor, the Dallas 18b20. On the illustration you can see the function of each pin.
ds18b20
(Source: Datasheet of DS18B20)

Since the raspberry pi is not able to work with analog signals it seem to be strange that the sensor is working. The answer is: It is a one wire bus sensor. It outputs the temperature in binary code, e.g.: 0000 0001 1001 0001 = 25 C°
Each sensor has a UNIQUE number which allows us to use even multiple sensors with one wire !

Ok now it’s time to wire all together.  Since i have no more red and black wires its bit colorful  🙂 sorry for that.

wiring raspberry pi gpio

btw.: i guess the circuit is also in the datasheet… anyway:
Now connect it to your breadboard:

breadboard temperature

That’s it ! Ok let’s check if you did it correct.

Step 3: “installing” the sensor end write a bashscript to read the temperature in °C.

After starting you Raspberry Pi, type:

sudo apt-get install bc
sudo modprobe w1-gpio
sudo modprobe w1-therm

This allows us to use the one wire transmission. Now we double check whether the raspberry pi recognized the sensor.

cd /sys/bus/w1/devices/

Bildschirmfoto 2014-01-10 um 14.37.50

There it is! 28-0000054bb784 is the id of mine. To read from the sensor we can type:

cat  /sys/bus/w1/devices/28-0000053bb784/w1_slave

Bildschirmfoto 2014-01-10 um 17.46.14

Now we can write our bash script to read the temperature. Since i have no idea of regex it was hard for me. So if someone has a better solution to get the value behind ‘t=’ it would be nice if you could write it in the comments. So “nano readtemp.sh” and copy/paste:

#!/bin/bash
catresult=`cat  /sys/bus/w1/devices/28-0000053bb784/w1_slave | tail -n 1| egrep -o ‘.{5}$’`
temperature=`echo “scale=2; $catresult / 1000” | bc`
echo $temperature

Bildschirmfoto 2014-01-10 um 18.13.46

Testing:
Bildschirmfoto 2014-01-10 um 18.16.50

BINGO ! 🙂

Step 4: Installing postgres

First of all install postgres and other stuff we will need later.

sudo apt-get install postgresql libpq-dev python-dev python-setuptools easy_install pip

login as postgres user and create new user :

sudo su – postgres
createuser tempuser -P
psql

Enter a password and pack it superuser. Start psql and add this new db:

create database temperature;

CRTL+D and type “psql temperature” to log into and create new table:

(if you had problems with tempuser try: )
(ALTER USER tempuser with password ‘12345678’; )

CREATE TABLE temp_record(
temp_id    serial primary key,
temperature       double not null,
stamptime        TIMESTAMP
);

press CTRL+D to exit psql. Type “exit” to logout as postgres user.
If you like to reach the DB from other machines in your network, we need to configure that.

sudo nano /etc/postgresql/9.1/main/pg_hba.conf

add this line:

host    all             all             192.168.178.0/24        trust

next:

sudo nano /etc/postgresql/9.1/main/postgresql.conf

change listen_addresses to this line:

listen_addresses = ‘*’

restart the service:

sudo service postgresql restart

Step 5: A little python script

To use python with postgresql a special library is necessary

pip install psycopg2

No create new file with “nano pushdata.py” and copy/paste this:
(Mine Pi’s IP: 192.168.1.103 change it to yours Pi’s IP)

import psycopg2
import commands
import datetime

call = commands.getoutput(“bash /home/pi/readtemp.sh”)

print call

try:
    conn = psycopg2.connect(“dbname=’temperature’ user=’tempuser’ host=’192.168.1.103′ port=’5432′ password=’12345678′”)
except:
print “I am unable to connect to the database”

cur = conn.cursor()
cur.execute(“INSERT INTO temp_record (temperature,stamptime) VALUES (“+str(call)+”,NOW()); “)

conn.commit()

Save and next Step ! 😉

Step 6: add cron job:

to temporarily add a cron job write:

crontap -e

5 0 0 0 0 python /home/pi/pushdata.py

HowTo: Use WD myBook external Drive with Raspberry Pi

If you want to mount your WD MyBook HDD (external Drive) with the Raspberry pi long term, you need to first install ntfs-3g. after than you need to create a folder to mount the drive … so Type:

sudo apt-get install ntfs-3g
sudo mkdir /media/myBook

ok 50% id already done. Next you have to check whats the name of the device. So Disconnect your drive. And call “ls /dev/”. Than connect the drive and again write “ls /dev/”. Compare both outputs. On the second call there should be apprear a device starting with “sd*”. My is called “sda” but “sdb” could also be possible. Now we will add a line to /etc/fstab to automatically mount this drive.

sudo nano /etc/fstab
add this line:
/dev/sda1 /media/myBook/      ntfs-3g default           0       0

By pressing ctrl+o you can save the file. And to leave nano press ctrl+x.
To refresh the system and mount the drive call:

sudo mount -a

now go to /media/myBook/ and check if it’s working 🙂

cd /media/myBook/

Another example for FAT32:

/dev/sda1 /media/myBook/ vfat defaults 0 2

HowTo: Raspberry Pi mount SMB Share

If you want to mount a SMB Share in your Network this short Tutorial might be helpful for you.

Lets say the SMB Folder, “myFolder”,  is on a pc with the ip 192.168.2.100.
It is ptrotected with username “Peter” and password “miller123”.

On you Raspberry, type:

smbclient -U Peter -L 192.168.2.100
… it will ask you for the password (miller123).

to see all shares. It should also show the share “myFolder”. Now we will Create a Folder on the Pi to mount the share to it.

sudo mkdir /mnt/theFolder

To actually mount “myFolder” type:

sudo mount -t cifs //192.168.2.100/myFolder /mnt/theFolder -o username=Peter

add -r for READ ONLY

That’s it. You can now go to /mnt/theFolder and check your remote files.

cd /mnt/theFolder
ls -la

Have Fun.