Category Archives: Python

How Far Can We Go?

Kurzgesagt has done it again with a very interesting video about the expansion of the universe and the visible horizon running away from us faster than the speed of light.


Maker Faire UK

The Maker Faire UK will be held on April in Newcastle.

maker faire

To publicize the event, they are giving away one of these

maker faire prices

All you have to do is go to their webpage, share the event in Facebook and join the mailing list. Good luck!

Raspberry Pi 004: Python task scheduler (Raspbian Jesse, dropbox, Cron and much more…)

Time has finally come to write another Raspberry Pi tutorial. In this one we are going to create a Python program that will read a task list file from Dropbox, and it will be scheduled to run at specific times. Examples of tasks? Take a picture, measure the value of a sensor, save a file, download something from a webpage, check the news… or run any Python script you want. The good things? This script will be scheduled using Cron, hence it will not be running all the time. Additionally, we will use Dropbox to communicate, so the Raspberry Pi will be running on its own and the task list will be available through Dropbox to edit from another computer (also, the results can be saved to Dropbox, so it will be possible to see them without accessing the Pi).

So, here we go.

Update to the latest Raspbian: Jesse.

  1. Updating the OS in the Raspberry. We start by upgrading to the latest Raspbian version, which is Jesse (they are named after Toy Story characters). The benefits of updating are that we will be running Python 2.7.9 (solves some authentication problems when performing https calls), LibreOffice comes pre-installed, and not necessary but helps… it comes with a Minecraft.IMAG4407
  2. Download the image from this web and unzip it.
  3. Mount the image on the Raspberry Pi SD card. For windows users, use Win32DiskImage which can be found in this sourceforge webpage.
  4. Put the SD card on the Pi and you are ready to roll. The next things are a list of things that you might want to do after reinstalling.
  5. Udpate
    sudo apt-get update
    sudo apt-get upgrade
  6. Install scrot to take screenshoots
    sudo apt-get install scrot
  7. Install Xrdp to be able to do remote desktop from Windows (Xp in my case)
    sudo apt-get install xrdp
  8. Install Samba (not really necessary, but it will allow to discover the Pi through his hostname instead of using the IP)
    sudo apt-get install samba samba-common-bin
  9. Change the Raspberry Pi password for pi user
    sudo raspi-config
  10. Change hostname (the name of the Pi in the local network to do remote desktop in the same network). Just edit the file and change raspberry for whatever name you like.
    sudo nano /etc/hosts
    Also edit the hostname file.
    sudo nano /etc/hostname
  11. Last thing, edit the Samba configuration file to enable the network to discover the Pi by its name.
    sudo nano /etc/samba/smb.conf
    Simply specify the workgroup and enable wins support.
    workgroup = your_workgroup_name
    wins support = yes

Now the Raspberry Pi is ready to rock and roll again. The next step is preparing the Dropbox access through Python. In our previous post

Raspberry Pi 003: Automatic upload to dropbox a camerashot

we used a different technique to access Dropbox, but now Dropbox has its own Python SDK, so lets use it!

Installing Dropbox Python SDK and using it.

  1. We start by installing the required modules.
    sudo pip install dropbox
  2. Now, we need to go to Dropbox developers and create a new App (described in our previous post). Once created, we need to generate an access token. This token will allow your Python script to access your Dropbox.
  3. This is an example script in Python that creates a file and uploads it to the main folder. Try running it to check that everything is correct.
    import dropbox
    app_key = 'your key here'
    app_secret = 'your secret here'
    access_token = 'yout access token here'
    f = open('testdocument.txt','w+')
    response = client.put_file('/documentindropbox.txt',f)
    print 'uploaded:',response
  4. Now, lets create a text file in the Dropbox folder. Call it tasklist for instance. Inside of it, just put a list of commands, like
    ##First command
    print 1+2
    ##Second command
    for k in range(10):
    	print k
    out = open('', 'w+')
    g,metadata = client.get_file_and_metadata('')
    import mytest

    As you can see, task list is a mixture of things, from just commands to download and run another python script.
    The script refereed as is just an example, just something like this,

    for k in range(13):
    	prod = prod*(k+1)
    	print prod

    save it in Dropbox main folder with the name
    The python script that will run in the Raspberry Pi is the next one. It is called dropread and it will open the tasklist.txt and run all the commands.

    import dropboxapp_key = 'your key here'
    app_secret = 'your secret here'
    access_token = 'yout access token here'
    f,metadata = client.get_file_and_metadata('tasklist.txt')
    mycommand =
  5. If now you go to the Raspberry Pi and run, it will look into the dropbox for the tasklist file and run its commands (which in this case include donwloading another script and running it).

Edit crontab to schedule tasks.

  1. The last part is editing crontab to schedule tasks. Crontab is a list of the scheduled tasks of the current user. In order to edit it, simply run
    sudo nano crontab -e
  2. A in-detail description of crontab can be found here,however if we just want to execute our tasklist on boot, we need to add this line
    @boot sudo python /media/pi/REDY/ NOTE:Instead of /media/pi… you need to put the path where you saved the script.
  3. If for instance you want to run the script every hour, you will need something like
    0 0 * * * * command
    0 2 * * * * command
    0 3 * * * * command
    0 4 * * * * command
    0 5 * * * * command
    0 6 * * * * command
    0 7 * * * * command
    0 8 * * * * command
    0 9 * * * * command
    0 10 * * * * command
    0 11 * * * * command
    0 12 * * * * command
    0 13 * * * * command
    0 14 * * * * command
    0 15 * * * * command
    0 16 * * * * command
    0 17 * * * * command
    0 18 * * * * command
    0 19 * * * * command
    0 20 * * * * command
    0 21 * * * * command
    0 22 * * * * command
    0 23 * * * * command

Hope it was useful!

Python + Arduino + XBee + Zumo robot

One year ago, I received many things from Coolcomponents.


among these things, was a Zumo Robot for Arduino.

Today I’m going to show you how to control the Zumo remotely from the computer using an Arduino UNO board and two Xbee. Basically, it’s the continuation of my previous tutorial:

XBee 002: radio-chat between PC and Arduino

this time the Arduino will be driving the Zumo instead of lightning LEDs.


  • Breadboard
  • 2 XBee with USB adaptor
  • Arduino UNO
  • Zumo Robot
  • Few wires for connections


As you can see, I already have done 2 modifications. One of them is that I soldered the wires to the Zumo in order to have connection with my all my Arduino pins. The other modification is that my breadboard is glued on the back of the Arduino.

Step 1. Install Zumo libraries on Arduino IDE. You are going to need these libraries in order to send commands to the Zumo shield. Go to Zumo-shield on GitHub and on the rigth hand side, download as a zip. Once you have that file, decompress it, and move each library folder (Pushbutton, QTRSensors, ZumoBuzzer, ZumoExamples, ZumoMotors, and ZumoReflectanceSensorArray) into the “libraries” subdirectory inside your Arduino sketchbook directory. You can view your sketchbook location by selecting File→Preferences in the Arduino environment; if there is not already a “libraries” folder in that location, you should create it yourself. After installing the library, restart the Arduino environment so it can find the Zumo Shield libraries and their examples. You will know that it is properly installed because it will show in the IDE Examples like this:

serial control for zumo shield


Step 2. Write code to drive Zumo from serial. Basically, we are going to modify the “ZumoMotor Example” to run the motors for a short period of time when a string is send through the serial port.

#include <SoftwareSerial.h>
#define Rx    4
#define Tx    5
SoftwareSerial Xbee (Rx, Tx);
#include <ZumoMotors.h>
//This script will control the Zumo motors by reading strings through the serial port.
// a w s d will control left forward bakwards and right
ZumoMotors motors;
int inByte = 0;         // incoming serial byte
int speedr = 0;         //speed of left engine
int speedl = 0;         //speed of rigth engine
void setup()
void loop(){
  //This will speed down the engine unless a command to speed up is received
  //If a command is received through the serial and it is a driving comand,
  //then it speeds up or down the corresponding engine
  if (Serial.available() > 0){
    inByte =;
      case 'a':// turn left
     case 'd': // turn right
     case 'w': // forward
     case 's':// backward
  if (Xbee.available() > 0){
    inByte =;
      case 'a':// turn left
     case 'd': // turn right
     case 'w': // forward
     case 's':// backward

To test how it works, simply put the Arduino on top of the Zumo shield and load the code into it. Without disconnecting the USB, turn the Zumo on and the open the serial port on the Arduino IDE.


You migth notice the reference in the code to the XBee. We are going to use the XBee connected as in our previous tutorial (XBee 002: radio-chat between PC and Arduino) the only change is that here we have pins 4 and 5 free (check Zumo shield pins usage here).

Step 3. Write Python code to read Keyboard and print to serial port. First, we need to install pygame, which is a set of libraries and functions designed to program games…. and they are very handy for reading the keyboard.

First, go to the pygame downloads webpage and look for the installation file that match your system and Python version. For me it works

pygame-1.9.2a0.win32-py2.7.msi 6.4MB

Simply run it and it will be installed. Now the code for Python, which is very simple:

import sys, serial, pygame, time
size= width, height = 60,40
#The next command creates a window,
#everytime you are on that window and press a key
# in the leyboard, it will be analysed and
#if it is one of the driving commands, it will be send
#through the serial port.
screen = pygame.display.set_mode(size)
while 1:
    events = pygame.event.get()
    for event in events:
        #This is just to quit
        if event.type == pygame.QUIT:
        if event.type == pygame.KEYDOWN:
        if event.type == pygame.KEYUP:
#The command is being send through the serial until we release the key
#Because we have a loop in the arduino and only checks the serial port every
#5 ms, by adding here a 10ms delay we make sure robot follows the orders in real time
    if key == 97:
    elif key == 115:
    elif key == 119:
    elif key == 100:


Step 4. Connect XBee, and enjoy (use same connection diagram for the XBee as in XBee 002: radio-chat between PC and Arduino but changing 6 and 7 pins for 4 and 5.). Our XBee is ready from the previous tutorial, and our code already includes reading XBee or USB… so everything is ready.

By the way… you might ask why I have 2 Lego bricks on the top… it is for this


maths with Python 7: Data logging from Arduino

This small project will help me later when I introduce a Matlab script I’m preparing, so stay tuned to the blog!

Today I’m going to do a simple example of data logging with Python and Arduino (my old Duemilanove). The Arduino is going to read a signal from a sensor and is going to report that value through the serial port. The Python script is going to scan the port from time to time and get the latest value. It’s not a clean implementation and can be improved, but it is an easy example.

But first, remember similar projects.


Arduino + LCD MD216-01 Rev.A


Father and Daugther


Raspberry Pi 001: Setup and Run first Arduino-Python project.

And of course, all the Python projects:

maths with Python

maths with Python 2: Rössler system

maths with Python 3: Diffusion Equation

maths with Python 4: Loading data.

Raspberry Pi 001: Setup and Run first Arduino-Python project.

Raspberry Pi 002: Pi Camera, start up scripts and remote desktop

maths with Python 5: Double Compound Pendulum Chaotic Map

maths with Python 6: Twitter API – Tweepy for social media and networks (with Gephi)

So, let’s begin.

Step 1. As a sensor I’m going to use a photodiode. Basically a photodiode works absorbing light and turning it into a voltage. To find out which terminal is the positive, simple connect the photodiode to a voltmeter and apply some light.


This model in particular is not very good for visible light (usually these models are designed to work at certain ranges, so maybe this one has its maximum sensitivity at IR or UV), but it will work.

Attach the photodiode between ground pin (GND) and Analog Input A0.


In this way we are going to read the voltage generated by the photodyode and detect the amount of ligth in the room.

Step 2. Program the Arduino to read an analog input and write the value to the serial port. We already did it in the post (Raspberry Pi 001: Setup and Run first Arduino-Python project). But just in case, this is a simple program that reads pin A0 and writes the value (in volts) to the serial port.

int sensorPin = A0;    // select the input pin for the photodiode
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
Serial.begin(9600);// initialize serial communications at 9600 bps:

void loop() {
sensorValue = analogRead(sensorPin);  // read the value from the sensor:
// Convert value to volts and print the results to the serial monitor with 4 digits precision:   
delay(20);   // stop the program for for 20 milliseconds:                

Upload the program to the Arduino and you will be ready. Just in case, check the serial monitor to see that the Arduino is sending the values and simply putting your hand over the sensor test if the values change.


Step 3. Write a Python code that reads the serial port, gets the last value and stores it.

import csv
import serial
import time

#First we create a name for the CSV file that is going to be date and time
name=time.strftime("%Y_%m_%d_%H_%M_%S", time.gmtime())
#Open the file
f=open(name+'.csv', 'wt');
#Initialize the CSV writer to write data into file
writer = csv.writer(f,delimiter='\t')

#Now we write a few lines to swhow how to write data
date=time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())

#Open the serial port. From the Arduino program we know the Arduino is in com3,
#and we selected 9600 as baudrate
ser = serial.Serial('com3',9600,timeout=1)

#For some reason, when the serial port is initialized,
#its first value is not real, so just read it and discard

#We are going to take 250 data points
for t in range(250):
    #Read last string from serial port
    #String is in the form <value>\r\n we only need the <value>
    #We put each data point in a new row of the data file.
    #waits 10 seconds for next data point

#When finish, close the file and the serial port.


When we run this script it generates a data file that looks like this:


Now you can import the data into any program you like, for instance Open Office Calc.


Hope you find this interesting. Now you can try things like reading humidity sensor for a week, or saving home temperature, or any other idea you get.

Raspberry Pi 003: Automatic upload to dropbox a camerashot

I have been with this idea in my mind for a while. Basically I want to have the Raspberry Pi to get a picture every time it starts and try to upload it to Dropbox.



In this way, it is like a security option and we will learn a few tricks on our way.

First of all. Do you remember our other projects with the Pi?

Raspberry Pi 001: Setup and Run first Arduino-Python project.

Raspberry Pi 002: Pi Camera, start up scripts and remote desktop

So, first step, let’s change Raspberry Pi password to have in in the local Wifi network.

1. Change Password. Open a LXTerminal and run

sudo raspi-config

The configuration window will open. From there just select to change the Password.


2. Change Raspberry Pi network name. By default, when in a Wifi network, the Raspberry Pi will try to get the local hostname “raspberrypi” (that is the name you will write when doing remote desktop to look for the Raspberry Pi). This configuration will create conflicts if there is various Raspberry Pi in the same network. So let’s change the name of the Raspberry Pi in the local network (original tutorial here).

Type the next command to open the host file:

sudo nano /etc/hosts

It will show this window, the last one is the actual name of the raspberry pi in the network


Edit that and put the name you want. In my case I’m going to call it Eneas. Just Ctrl+C and Yes and Enter.

Now edit the hostname file:

sudo nano /etc/hostname

Change here the name also


Save and run the file:

sudo /etc/init.d/

Now just reboot the terminal and you will be able to locate it in the network with the new name. Or in my case to do remote access from windows I don’t need any more the IP, I just type the name (remember the tutorial where I explained how to do remote desktop?).

3 More memory. Do you have a 512mB Raspberry Pi model B? Is it using the 512mB? It seems that by default most of the Pi with 512mB need a firmware update before being able to use the whole memory! How to check it?

free -m


This window will appear. Here we can see the amount of memory used by the system. It is not the whole 512mB since part of the memory is reserved for the GPU. Raspberry Pi shares memory between CPU and GPU. So in my case it is not necessary, but still if you need it:

First install Git Core

sudo apt-get install git-core

Now download the required files

sudo wget -O /usr/bin/rpi-update && sudo chmod +x /usr/bin/rpi-update

The next command will update the SSL

sudo apt-get install ca-certificates

Install rpi

sudo apt-get install rpi-update

mkdir -p /lib/modules/3.10.25+/build

and finally update

sudo rpi-update

and voilà!

4 Turbo boost. There is a few things that can be done in order to overclock the Pi, like adjusting the CPU voltage, but most of them will break the warranty. Here is one that will not.

sudo apt-get update && sudo apt-get install raspberrypi* raspi-config

Now launch the config and turn the TURBO mode on

sudo raspi-config

005 006

Reboot and changes will be made.

5 Dropbox. Next step is to connect your Raspberry Pi with Dropbox. We do it through the Dropbox API. First ensure you have Git Core

sudo apt-get install git-core

Next copy the files you need in order to invoke the API

git clone

Now you will be able to see the next directory

cd Dropbox-Uploader


And inside the folder…


You should now see three files, one of which is called This is the script we’re going to use. Run it.


You should see something like this:


Now you need to go to Dropbox webpage for developers, login to DropBox and create an “app” by clicking the “create app” button.

Then choose “Dropbox API app”, “Files and Datastores”, and answer the final question “Can your app be limited to its own, private folder?” – either answer is OK, depending on your needs.

Fill the information from Dropbox into the script and everything should be ready.

Now you can try uploading a file from the main folder (in my case named image.jpg).

Command line

cd home/pi/Dropbox-Uploader
./ upload /home/pi/name_of_upload_file name_of_upload_file


    from subprocess import call
    photofile = "/home/pi/Dropbox-Uploader/ upload /home/pi/image.jpg image.jpg"
    call ([photofile], shell=True)

6 Python and saving the start image to Dropbox. This is a nice idea I have that can be implemented as a safety feature. Basically now that we have access to Dropbox API and with the PiCamera installed… (how to setup the PiCamera) it will be a great idea making a script that runs at the start-up takes a picture and uploads it to Dropbox. You can think it as a safety feature. If your Raspberry Pi gets stolen, every time somebody starts it a picture will be taken and send to Dropbox.

First create a Python script that looks like this:

from subprocess import call
try:#We put everything inside a try to avoid running if the camera doesn't work
    import picamera
    import datetime"%Y-%m-%d_%H-%M-%S")
    camera = picamera.PiCamera()

    photofile = "/home/pi/Dropbox-Uploader/ upload /home/pi/"+timestamp+".jpg "+timestamp+".jpg "
    call([photofile], shell=True)

This script will try to run the camera, take a picture and upload it to Dropbox. The format of the time is a little bit tricky and will not work for naming files in Dropbox. It should be fine as it is.

Now as we explained in

Raspberry Pi 002: Pi Camera, start up scripts and remote desktop

Put the script into the start up sequence to run after starting the Pi.

and every time you start the Pi it will take a picture and send it to Dropbox!


In this way if your Pi is stolen and they are not aware of this, you will be able to see images and maybe locate it.

Did you like it?

Tip Jar

We love what we do and we want to do it better. That is why now we have a “Tip’s Jar“. You can find it in the right hand side menu or through this link..

We will appreciate quite a lot if you donate and share it with your friends. Rising some money will help us maintaining this blog and trying to go for bigger projects.

Thank you all.







No, there is no more here. Just hope you like what we do.