Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Unread Email Counter for the Intel/Arduino Galileo
# - Logs into your gmail and prints the number of unread emails.
# Place this file in the top level of your Galileo's SD card.
import imaplib # Used to connect to an IMAP4 server.
obj = imaplib.IMAP4_SSL('', '993') # Connect to an IMAP4 sever over SSL, port 993
obj.login('','myPassword') # Identify the client user and password # Select a the 'INBOX' mailbox (default parameter)
# Search mailbox no (None) charset, criterion:"UnSeen". Will return a tuple, grab the second part,
# split each string into a list, and return the length of that list:
print len(,'UnSeen')[1][0].split())
/* Galileo Email Checker
by: Jim Lindblom
SparkFun Electronics
date: January 7, 2013
license: Beerware. Please use, reuse, and modify this code.
If you find it useful, you can buy me a beer when we meet.
This code connects a Galileo to WiFi. Then runs a Python script
- -
that lives on the Galileo's SD card to check how many unread
emails you have. That number is displayed on an OpenSegment
Shield - - which is
controlled over SPI.
#include <SPI.h> // SPI is used to control the OpenSegment Shield
#include <WiFi.h> // WiFi (could be swapped out for Ethernet)
#include <SD.h> // The SD library is used to read a temporary file,
// where the py script stores an unread email count.
// WiFi Definitions //
char ssid[] = "WiFiSSID"; // your network SSID (name)
char pass[] = "WiFiPassword"; // your network password (use for WPA, or use as key for WEP)
IPAddress ip;
int status = WL_IDLE_STATUS;
// Pin Definitions //
const int SS_PIN = 10; // SPI slave select pin (10 on the shield)
const int STAT_LED = 13; // The Galileo's status LED on pin 13
// Email-Checking Global Vars //
const int emailUpdateRate = 10000; // Update rate in ms (10 s)
long emailLastMillis = 0; // Stores our last email check time
int emailCount = 0; // Stores the last email count
// setup() initializes the OpenSegment, the SD card, and WiFi
// If it exits successfully, the status LED will turn on.
void setup()
digitalWrite(STAT_LED, LOW);
Serial.begin(9600); // Serial monitor is used for debug
initDisplay(); // starts up SPI and resets the OS Shield
initSDCard(); // Initializes the SD class
// Initialize WiFI. On success turn on the stat LED. On fail,
// print an error on the LED, and go into an infinite loop.
if (initWiFi() == 1) // If WiFi connects, turn on Stat LED
digitalWrite(STAT_LED, HIGH);
{ // If WiFi connect fails, print error, inifinite loop
SPIWriteString("----", 4);
while (1)
// loop() checks for the unread email count every emailUpdateRate
// milliseconds. If the count has changed, update the display.
void loop()
// Only check email if emailUpdateRate ms have passed
if (millis() > emailLastMillis + emailUpdateRate)
emailLastMillis = millis(); // update emailLastMillis
// Get unread email count, and store into temporary variable
int tempCount = getEmailCount();
if (tempCount != emailCount) // If it's a new value, update
{ // Do this to prevent blinking the same #
emailCount = tempCount; // update emailCount variable
printEmailCount(emailCount); // print the unread count
// Bit of protection in case millis overruns:
if (millis() < emailLastMillis)
emailLastMillis = 0;
// printEmailCount(emails) prints the given number to both the
// serial monitor, and the OpenSegment Shield.
void printEmailCount(int emails)
Serial.print("You have ");
Serial.println(" unread mail.");
SPIWriteByte('v'); // Clear display
for (int i=3; i>= 0; i--)
SPIWriteByte((int) emails / pow(10, i));
emails = emails - ((int)(emails / pow(10, i)) * pow(10, i));
// getEmailCount runs the script, and reads the output.
// It'll return the value printed by the script.
int getEmailCount()
int digits = 0;
int temp[10];
int emailCnt = 0;
// Send a system call to run our python script and route the
// output of the script to a file.
system("python /media/realroot/ > /media/realroot/emails");
File emailsFile ="emails"); // open emails for reading
// read from emails until we hit the end or a newline
while ((emailsFile.peek() != '\n') && (emailsFile.available()))
temp[digits++] = - 48; // Convert from ASCII to a number
emailsFile.close(); // close the emails file
system("rm /media/realroot/emails"); // remove the emails file
// Turn the inidividual digits into a single integer:
for (int i=0; i<digits; i++)
emailCnt += temp[i] * pow(10, digits - 1 - i);
return emailCnt;
// initDisplay() starts SPI, and clears the display to "0000"
void initDisplay()
SPIWriteString("0000", 4);
// initSPI() begins the SPI class, and sets up our SS_PIN output
void initSPI()
pinMode(SS_PIN, OUTPUT);
digitalWrite(SS_PIN, HIGH);
// initSDCard() begins the SD class
void initSDCard()
// initWiFi() initializes WiFi, connecting to the ssid/passkey
// combo defined up above.
// This function will attempt 3 connections to WiFi. If we
// succeed a 1 is returned, and the IP address is printed.
// If connections fail, a 0 is returned.
uint8_t initWiFi()
byte timeout = 0;
while ((status != WL_CONNECTED) && (timeout < 3))
Serial.print("Setting up WiFi : attempt ");
Serial.println(timeout + 1);
// Connect to WPA/WPA2 network. Change this line if using
// open or WEP network:
status = WiFi.begin(ssid, pass);
//delay(10000); // Wait 10 seconds between connect attempts
if (timeout >= 3)
return 0;
Serial.print("Connected to ");
ip = WiFi.localIP();
// !!! Idea here: print IP address to seven segment display.
// That'll make it easier to know which address to ssh over to
// if we need.
return 1;
// SPIWriteString will write an array of chars (str) of length
// len out to SPI. Write order is [0], [1], ..., [len-1].
void SPIWriteString(char * str, uint8_t len)
digitalWrite(SS_PIN, LOW);
for (int i=0; i<len; i++)
digitalWrite(SS_PIN, HIGH);
// SPIWriteByte will write a single byte out of SPI.
void SPIWriteByte(uint8_t b)
digitalWrite(SS_PIN, LOW);
digitalWrite(SS_PIN, HIGH);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment