zondag 27 juli 2014

Pi-files: Heartbeat

It has been a bit quite here on Deddies lab's blog. That doesn't mean that nothing came out of the lab. Actually a lot has happened, but I didn't had the opportunity (or was a bit too lazy) to post it here. Since I took a lot from the DIY community, I feel obliged to return also my findings, struggles and results back to that same community. Only then we all can grow!

Last couple of years, I invested most of my (spare) time in the Raspberry Pi platform. I don't think I have to explain anything about this versatile mini computer. Just google around and you will find tons of projects. I myself was attracted at first to the Rpi because of the General Purpose IO's (GPIO) possibilities which can be used to connect simple inputs (switches) and outputs (nothing beats a blinking led!). I was (still am) used to work with Atmel microcontrollers, where also the pins can be configured as input and output pins. So being able to use the same IO functionality but then combined with ethernet, a decent user interface, etc. etc. etc. opened a whole new world of possibilities.

Let's start easy; Sometimes it is difficult to see from the outside of the Pi is running or not. It has some status leds onboard, but when built in a box and stuffed away in a cupboard (like I have) the status leds are not so easy to see. So for that purpose I made a simple heartbeat. It is just a blinking led, but starts automatically when the Pi boots up, so it indicates when the Pi is alive and kicking!

I have a revision A of the RPi in my 'meterkast' (is it really 'meter cupboard'?).  For this heartbeat and some other projects (doorbell alert, front door lights) I made a dedicated interface board. It also has a led which is clearly visible from the outside


This led is connected to pin 16 of the GPIO header, which is called GPIO4. No, this doesn't make sense, and apparently there is a difference between the physical Raspberry Pi names and Broadcom names. I made a Python script (called heartbeat.py) where I can first configure the IO's:

#Libraries/modules
import RPi.GPIO as GPIO
from time import sleep

#display no warnings (on command line)

GPIO.setwarnings(False)
#use RPi board pin numbers
GPIO.setmode(GPIO.BOARD)
#set pin 16 as output
GPIO.setup(16, GPIO.OUT)


The 'sleep' module is inserted to make the led blink. The while loop makes the led blink indefinite:

print "Start heartbeat\n"
while 1:

  GPIO.output(16,1)
  sleep(0.5)
  GPIO.output(16,0)
  sleep(0.5)

You can run this script from the command line:

pi@raspberrypi ~ $ sudo python /home/pi/gpio/scripts/heartbeat.py &

When you want to run the script automatically after boot up of the Rpi, you have to edit /etc/rc.local:

pi@raspberrypi ~ $ sudo nano /etc/rc.local

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.


python /home/pi/gpio/scripts/heartbeat.py &

exit 0

Save the file with Ctrl+X. Make sure that the Python script is executable:

pi@raspberrypi ~ $ sudo chmod 755 /home/pi/gpio/scripts/heartbeat.py

You can check if the script is executable if you see the 'x' in the permission overview:

pi@raspberrypi ~ $ ls -la

-rwxr-xr-x 1 root root   261 Jul 21 21:34 heartbeat.py

Reboot the Pi and see the led blink:

pi@raspberrypi ~ $ sudo shutdown -r now

Geen opmerkingen:

Een reactie plaatsen