Galileo Unread Email Counter

This Tutorial is Retired!

This tutorial covers concepts or technologies that are no longer current. It's still here for you to read and enjoy, but may not be as useful as our newest tutorials.

Pages
Contributors: jimblom
Favorited Favorite 1

The Arduino Sketch

With Python taking care of the email checking, our Arduino sketch is free to update the display and interact with any other hardware. The trick is getting the Arduino sketch to interact with the Python script. We'll use a special function -- system() -- to call the script from our Arduino sketch.

Uploading the Sketch

There are two versions of this Arduino sketch -- one for WiFi and another for Ethernet. Both achieve the same result, they just include different libraries, and use different interfaces to get there. Download your preferred sketch below:

If you're using the WiFi sketch, there are a couple variables you'll have to edit to connect to your WiFi network. On line 23 you'll need to set the ssid[] variable to your network SSID (network name). And, if your network has a password, on the line below you'll need to modify the pass[] variable as well.

For both sketches, you can modify the emailUpdateRate variable above setup() to adjust the frequency of your mail checks. At 10000, it'll check your mail every 10 seconds. We don't recommend checking more often than that.

Once you've made your fine tunings, upload the sketch to your Galileo.

Running the Sketch

For debug purposes, the Arduino sketch will print a handful of messages to the serial monitor (9600 bps). This is especially handy if you don't have a display attached -- the unread email count will be printed here too.

If you do have a OpenSegment Shield attached, you should initially see the display turn to "0000". Every 10 seconds, as the email checker runs, you may see that value change. Try sending yourself an email! Does the counter go up? Read the email. Does it go down?

alt text

15 unread emails. Can Python answer my emails too?

Now you'll never be the last to see the latest silly email thread!

Dissecting the Sketch

There are a few key lines of code here that you'd never see on an Arduino Uno, Mega, or the like. First and foremost to that are the system() function calls.

System Calls

The system(const char * command) function is a standard C function which is used to issue a command to your operating system's command processor. With this function, your Arudino sketch can interact with Linux, just as you might through a terminal, over the command line.

Each system function takes a single parameter: an array of char's (i.e. a string of characters). The character array can be any system function -- you could stick something like "mkdir foo" in the system function, and your Arduino would ask the Linux kernel to make a directory called "foo".

In this sketch, we use the system function to run our Python script. The first system call is in the getEmailCount() function:

language:c
// Send a system call to run our python script and route the
// output of the script to a file.
system("python /media/realroot/pyMailCheck.py > /media/realroot/emails");

This is a call to run the pyMailCheck.py script, and route the output to a file called "emails". So instead of printing the number of unread emails to the terminal, our script will place that value in a file. The "/media/realroot/" directory is where the Linux kernel finds the "top level" of our SD card.

Reading the SD Card

To read the unread email count returned by the Python script, all we need to do is read the contents of the "emails" file. This can be done with the included Arduino SD library.

To read from the SD card, simply open the "emails" file with the SD.open function, and read to the end with the File.read function. A little bit of parsing and converting is required to turn the string of characters to an integer value.

SPI-ing to the Display

The OpenSegment supports all sorts of communication standards -- serial, I2C, and SPI. All are supported by the Galileo, but SPI was the only one that didn't seem to require any extra modification to the shield.

To interface with the display, the SPI library (included with Arduino) is used. Check the bottom of the sketch to see a couple of helper functions, which can write a single byte or a string of bytes to the display.