I like to make things, some of them useless, some not, but all of them respond to an internal worry or curiosity.
This allways lead in an internal good feeling. You know what i am talking about...

A random sequence for GPIO Xmas tree raspberry pi

This 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, is here, www.pocketmoneytronics.co.uk, btw, thanks Andrew!!

I wanted to share what i did. As is my first experience with raspberry pi GPIO and i think a good one to start making.

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. Then 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
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 chose.

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
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 processes 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 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
exit 1
sudo python /path/to/ledprogram.py

At last for making 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 inmany guides it wasn't so obvious for me.

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

@hourly "user" script.sh

The video:

I hope this has been useful in some way.

Again, Thanks to all those people that share info, guides and codes!!

An Ir remote programmable switch with arduino

This project is about giving a solution to a problem that is common to close every house and regards the planet care about emissions problems. At same time it helps home economy by lowering the energy bill.
The accumulation, normally around one point of devices such TV, HI-FI, game consoles, laptops, tv adapters etc is the point where a power stripe takes sense and value, because with a switch in it you cut four or five devices from stand by energy waste.
There are currently existing power strips with switch, much of them mechanical, other ones with remote control, IR or wifi.
What I was looking after this project, is to avoid the lack habit of getting used to a new practice for people that is not go to switch each time they have end up their activity, watch tv, play videogame etc… or even search were the remote control’s switch is etc…
With this configuration the user can program a whatever key from any remote or Ir device to program the switch. This way normally the most used and important remote can be used to switch off.
This project uses arduino uno and its ATmega328P. Arduino will be used to program the chip using arduino IDE but in order make it smaller will be going standalone.
The receiver used is the tsop34838 from Vishay, it is tuned for 38 kHz. I have used some others that have worked good but this one has ran perfectly for all my remote controls that I had around home.
At the time I was making this project I chose to go this way, this days smaller solution can be taken.
Maybe a further implementation could group more receivers, smaller solution arduino micro/ mini a attyni, a sleep improved code…
As the project is designed to use a power strip and the Arduino configuration is powered with 5 volts, the idea is to power it from plug. It took me some time to finally find a solution. The one I chose has been an ac-dc converter the HLK-PM01 from HI-Link chinese manufacturer that satisfies safety standards and requirements. One important feature is that it has a low power consumption, less than 0,1 watts according to manufacturer.
Finally the device charges on the switching function is a 5v coil latching relay for switching 220v like the Omron G6S-2-5VDC up to 10 amp into their terminals.
This solution was adopted in order to isolate both circuits. At the ac-dc converter and for the relay switching section.
You can see circuit and hardware on the standalone final diagram.

The code:

One of the best reasons of using and playing with open source, is the vast amount of resources than can be found and suit your needs.
In my case while I was playing around the project I found am Arduino library called IRremote.h.
His author Ken Shirriff has put it on Github and you can find at https://github.com/z3t0/Arduino-Irremote

The library implements a bunch of IR protocols to send and receive codes. As Ken’s blog explains:

This IR remote library lets you both send and receive IR remote codes in multiple protocols. It supports NEC, Sony SIRC, Philips RC5, Philips RC6, and raw protocols. If you want additional protocols, they are straightforward to add. The library can even be used to record codes from your remote and re-transmit them, as a minimal universal remote.

The best part of this code to me remains on the raw decoding part. This feature makes simple the decoding of non specified protocol and is going to help us to register the selected key of our remote.
The other libraries that will be used are EEPROM.h and EEPROMAnything.h.
Those libraries will let us save the received code into Eeprom memory and compare it with the code received. Whit this we will identified the correct code and perform the switching action.
Code is always waiting for remote keys, in the figure below showing the circuit, a button can be seen. The button will allow the user to program a remote key of his choice, by clicking it for a time lapse between 3 to 9 seconds, it will make the led blink twice. After this it will be waiting for the key code. Once the code has been acknowledged, by writing it to the Eeprom memory, another blink of the led will aware us about code has been registered.
The code uses the receiving function from IRremote.h code for reading and decoding, stores in memory and then waits for keycodes. Once received, led blinks and compares keycodes with the one stored. If it match’s, then looks for the relay state, then t switch on or off the relay.
The numbers are the corresponding to the Arduino UNO board, which I use to upload to the ATmega328p.


int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
int pulsador = 12;
unsigned long tecla;
int controled = 8;
unsigned long recibido;
int rele = 7;
int on = 0;
long duracion;
int estado;
long cont;
void setup()
irrecv.enableIRIn(); // Start the receiver
pinMode(pulsador, INPUT); //Button as input
pinMode(controled, OUTPUT); //Led as Output
pinMode(rele, OUTPUT); // Rele control pin as ouput
EEPROM_readAnything(0, tecla); //Reads stored value on eeprom and Stores in tecla
void loop() {
results.value=0; //each loop the reading variable is emptied
cont=0;// As well as the counter
estado=digitalRead(pulsador);// Read Button state if pressed Start while below
while (estado==HIGH){
if (duracion>=3 && duracion<9){
digitalWrite(controled, HIGH);
delay (100);
digitalWrite(controled, LOW);
delay (100);
digitalWrite(controled, HIGH);
delay (100);
digitalWrite(controled, LOW);
delay (100);
while (results.value==0){
if (irrecv.decode(&results)){ //Uses decoding code from IRremote.
digitalWrite(controled, HIGH);
delay (100);
digitalWrite(controled, LOW);
EEPROM_writeAnything(0, results.value);//Writes Key in Eeprom
irrecv.resume();Restarts Receiving code

if (irrecv.decode(&results)){ //if a keycode is received
recibido=results.value; //stores in variable
digitalWrite(controled, HIGH);
delay (50);
digitalWrite(controled, LOW);
delay (50);
EEPROM_readAnything(0, tecla);
if (recibido == tecla){ //compares Variable with the one stored for matching
on = !on;
digitalWrite(rele, on ? HIGH : LOW); //switchs on or off the relay
results.value=0; // clears reading
recibido=0; clears stored value

The video:


When I started making this project I had not very much idea about electronics and programing.
Since I discovered arduino I was tempted to achieve it. I have always been an open source enthusiast and I think this project would have been very painful without such a library like IRremote.
Anyway this permitted me to learn about coding, wireless communications, modulation, electronic devices… etc with the added value of trying to develop a device that can make lazy people save up on his monthly budget and reduce contaminant emissions as every little amount of energy counts, and every family home counts.
The next step would be improve energy saving by establishing a sleeping pattern. The components can be improved as well. May be the Atmega 328p is a bit overperformant and smaller microcontrollers will perform better.
The step down and direct voltage conversion can be improved, but in this field it took a long time to find a device adapted to this kind of developments. In a previous prototype I made a Capacitive power supply, but I was not satisfied in order to go for a production board, and it increased the number of components and may be the price…
In this way a power consumption study must have to be made in order to measure and decide.
For me it has been a challenging and educating project by searching a solution to a real problem in both parts, a technical one and a behavioral one bearing in mind human habits.
I wish to thank all those open source developers that make possible to go further by sharing their work and the free spread of the knowledge and resources.

Phone unlock door intercom hack

This hack is about a simple way of making a door unlock with your mobile phone.
It is simple because it uses a common intercom apartment to open the door.
The elements we are going to use are:
A raspberry pi.
A 2n3904 NPN amplifier.
A led.
A 100mA fuse.
A 1k and 680 ohm Resistance.
One Screw connector.
On the other hand, you will need to configure a server on the Pi, and depending on your situation may be Dynamic dns service running and configured.
Finally i use a http request app on my mobile to make the thing happen.
The first part will be build a small circuit to act as a switch.
My first design was to make a normally open switch, then i noticed that my intercom's switch opener was configured as normally closed.
You can find a lot of information on this configuration on the net. Mine is this one:

On your intercom you will have to search the push button terminals, and wire them through the circuit above with the gpio from the raspberry pi
In my installation i used the Rpi ground to ground the intercom switch as well. In two year of use no problem reported. The fuse was installed in case of...
On the raspberry pi: You will have to download the pigpio library.
Pigpio is a python library that lets you control the gpio port from your raspberry pi.
Then i made a script to put the pin high for one second, time enough to open our door manually.
The script suits my needs, probably other tweaking have to be made depending on yours.
Heres is the code:


import RPi.GPIO as GPIO
import time


GPIO.setup(40, GPIO.OUT)

time1 = time.time()

while time.time()-time1 < 1:

GPIO.output(40, GPIO.HIGH)
if time.time()-time1 >= 1:

GPIO.output(40, GPIO.LOW)

time1 = 0


You can see that I used gpio 40 on the rpi.

Once you have set up your server (lot of information about this on the web, Thanks everybody for the great job) we will have to make a php script to execute the python script.
The code is:

exec('sudo python /path/to/script/openscript.py');

Finally, for the mobile section. I was looking for a shortcut on my phone to tap and open the doorway.
After searching i finally used http shortcuts https://github.com/Waboodoo/HTTP-Shortcuts. It can be installed from Google play. It's free, it's customizable and works like a charm. Thanks to developers!!
That’s all. I omitted some parts of the configuration because everything it is on the web nicely explained, and you may do it as you want or you are used to.
Hope you like it. For me it has been two years working perfectly, the rpi is super stable. At first I had a little problem with wifi, but despite my rpi is an old one, this issue was solved easily.
On the other hand it is a useful hack for me. It takes less time to open than using your keys and can be integrated in future projects to a keyless solution for coming home.

The video:

If you listen carefully, you will hear the hallway open, Sorry for the barks

Phone unlock door intercom hack with esp8266

Under Development