viernes, 2 de enero de 2015

GPIO Xmas tree Raspi


This post is about an add-on for the Raspberry pi that i found on kickstater.
You can see here were it started for me: https://www.kickstarter.com/projects/1937541932/gpio-xmas-tree-kit-for-the-raspberry-pi

All the information and where to purchase it, i think for next year xmas, is here, www.pocketmoneytronics.co.uk, btw, thanks Andrew!!

I wanted to share what i did and how I configured it for. As is my first experience with raspberry pi GPIO and I am not very versed on electronics and coding, I think this could be a good guide for someone who get stuck on some issues.

Well all the information on how to start, the way to solder and how the leds and all the stuff works is available on the page above.

I am going to focus on what i did and paste here my code.
My tree came up with a bicolour led  which i put on top. so i started with the example_bicolour.py and modified it.


 import tree

# some constants to identify each LED
L1 = 2
L2 = 4
L3 = 8
L4 = 16
L5 = 32
L6 = 64
AMBER = 1 # LED 0 = amber
RED = 128 # LED 0 = red
GREEN = 256 # LED 0 = green
NO_LEDS = 0
BOTTOM6 = 2+4+8+16+32+64 # the 6 standard red LEDs

# note that we must tell setup() that we have a bicolour LED
tree.setup() # you must always call setup() first!

# All the red LEDs will be permanently illuminated and we rotate
# between the various colours for the bicolour LED at the top.
for i in range(7): # repeat 7 times
  tree.leds_on_and_wait(BOTTOM6, 0.8) # top LED off
  tree.leds_on_and_wait(BOTTOM6 + GREEN, 0.8) # top LED green
  tree.leds_on_and_wait(BOTTOM6 + RED,   0.8) # top LED red
  tree.leds_on_and_wait(BOTTOM6 + AMBER, 0.8) # top LED amber
 
  tree.leds_on_and_wait(NO_LEDS, 0.8) # all LEDs off
  tree.leds_on_and_wait(GREEN, 0.8) # top LED green
  tree.leds_on_and_wait(RED,   0.8) # top LED red
  tree.leds_on_and_wait(AMBER, 0.8) # top LED amber
 
tree.all_leds_off() # extinguish all LEDs

# All done!
tree.cleanup() # call cleanup() at the end


As you can see leds are represented with bits, you can see the tree.py for a description on how it works, so I created my own groups of leds and made a list with all of them.

i used the random function on python in order to pick the elements of my list, it will make them to blink randomly the time specifies and by groups I choosed.

Here is my code:

import tree
import random
import time

# some constants to identify each LED
L1 = 2
L2 = 4
L3 = 8
L4 = 16
L5 = 32
L6 = 64
AMBER = 1 # LED 0 = amber
RED = 128 # LED 0 = red
GREEN = 256 # LED 0 = green
NO_LEDS = 0
BOTTOM6 = 2+4+8+16+32+64 # the 6 standard red LEDs
PARES = 4+16+64
IMPARES = 2+8+32
CENTRO =8+16+34
PERIFERIA = 2+16+64


lista = [L1, L2, L3, L4, L5, L6, AMBER, RED, GREEN, NO_LEDS, BOTTOM6,  BOTTOM6 + GREEN, BOTTOM6 + RED, BOTTOM6 + AMBER, PARES, IMPARES, CENTRO, PERIFERIA]; #This is the list with all groups of leds


t = time.strftime ('%H') #here i pick up the hour and pass it to t


# note that we must tell setup() that we have a bicolour LED
 

tree.setup() # you must always call setup() first!
 

print (t) # only to know if all was going ok

while (t >='18') and (t < '24'): # start loop between 18 and midnight
  tree.leds_on_and_wait(random.choice(lista), 0.8) # randomly pick one of the list
  tree.all_leds_off() # extinguish all LEDs
 

 t = time.strftime ('%H') #need to know wich time after ech loop in order to exit it

 The last part of the code will pick the time of the system, only the hours, for triggering the loop between chosen times.

Next part of the code will pass to the tree.leds_on_and_wait function one element of the list chosen randomly by the random.choice function.


After that it is time to tell the raspberry to launch it. At first i told raspberry to check for the hour at o'clock each hour a day, but this way the process was launched each hour in the interval we chosen so i saw three processed at same time when i noticed something went wrong.

The way I managed to make it work was by making a very little script that checked if the process was executing and launching in it in case it wasn't. If you have more than one python process executing it won't work.
Here is the script:

#! /bin/bash

if pgrep python >/dev/null 2>&1
then
  exit 1
else
  sudo  python /path/to/ledprogram.py
fi


At last how we make this script run when we want. We will use cron, http://en.wikipedia.org/wiki/Cron there are several guides to configure the job we want, i will not explain here, the only thing i will explain is that you have to find crontab which in the raspberry case running raspbian and for root user, is in /etc/crontab and in guides in google it wasn't obvious for me.

in my case for executing the script each hour a day i used:

@hourly "user" script.sh

Then a video for watching the result.




I hope this has been useful in any way. I assume there are lots of better and smarter codes which they make better and beautiful things.

Next year I will improve a little with the priceless help of all those people on the web that share their jobs.

Thanks to all.

No hay comentarios:

Publicar un comentario