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.
- 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.
- Download the image from this web and unzip it.
- Mount the image on the Raspberry Pi SD card. For windows users, use Win32DiskImage which can be found in this sourceforge webpage.
- 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.
sudo apt-get update
sudo apt-get upgrade
- Install scrot to take screenshoots
sudo apt-get install scrot
- Install Xrdp to be able to do remote desktop from Windows (Xp in my case)
sudo apt-get install xrdp
- 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
- Change the Raspberry Pi password for pi user
- 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
- 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
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.
- We start by installing the required modules.
sudo pip install dropbox
- 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.
- 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' client=dropbox.client.DropboxClient(access_token) f = open('testdocument.txt','w+') response = client.put_file('/documentindropbox.txt',f) print 'uploaded:',response
- 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('mytest.py', 'w+') g,metadata = client.get_file_and_metadata('mytest.py') out.write(g.read()) out.close() import mytest ##end
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 mytest.py is just an example, just something like this,
prod=1 for k in range(13): prod = prod*(k+1) print prod
save it in Dropbox main folder with the name mytest.py.
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' client=dropbox.client.DropboxClient(access_token) f,metadata = client.get_file_and_metadata('tasklist.txt') mycommand = f.read() try: exec(mycommand) except: pass finally: pass
- If now you go to the Raspberry Pi and run dropread.py, 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.
- 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
- 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/dropread.pyNOTE:Instead of /media/pi… you need to put the path where you saved the script.
- 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!