Yet Another LED Gmail Notifier using a Nanode

This year we've begun having an "unplugged" day once a week - with no-one in the house using iphones, ipads, chromebooks, tvs, kindle, etc. But we still run a vacation rental business, and need to respond promptly to inquiries. So we wanted a way to be visually notified of those inquiries (without having to check our email, and being tempted to respond to non-business emails). The other requirement was that this device be standalone, and not required to be connected to a computer.

There's lots of other projects like this out there. Several of them need to be plugged into a computer. Others use the Raspberry Pi. I happened to have a Nanode sitting around unused, and wanted to build it with that. This article will show you how I light an LED when we receive an important email, and turn it out once it is read.

What you will need:

  • A Nanode
  • A Server on which you can host a PHP script
  • A Gmail account
Let's start by creating the PHP script. I decided to define a new label in my gmail account called "alert". The PHP script will identify any unread emails with that label. This allows you to modify which emails should light the LED simply by changing your gmail filters to assign that label.

On your server create a notifier.php with the following code


<?php
$email    = 'YOUR_GMAIL_ADDRESS';
$password = 'YOUR_GMAIL_PASSWORD';
$curl     = curl_init('https://mail.google.com/mail/feed/atom/alert/');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, $email.':'.$password);
$content  = curl_exec($curl);
$feed = new SimpleXMLElement($content);
if ($feed->fullcount==0) {
echo "N";
} else {
echo "Y";
}
?>

This PHP requires that you have the modules "curl" and "SimpleXML" installed already. Run "php -m" to check whether you have them.

Now we need to write the sketch for the Nanode which will query this notifier.php. Here's the sketch.

// Gmail Notifier

#include <EtherCard.h>
int led = 3;

// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

byte Ethernet::buffer[700];
static uint32_t timer;

// change this to to server where you hosted your notifier.php
char website[] PROGMEM = "example.com";

// called when the client request is complete
static void my_callback (byte status, word off, word len) {
  char newMail = Ethernet::buffer[off + len - 1];
  if (newMail=='N') {
    digitalWrite(led, LOW);
  } else {
    digitalWrite(led, HIGH); 
  } 
}

void setup () {
  Serial.begin(57600);
  Serial.println("\n[webClient]");
  pinMode(led, OUTPUT);

  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) 
    Serial.println( "Failed to access Ethernet controller");
  if (!ether.dhcpSetup())
    Serial.println("DHCP failed");

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);  
  ether.printIp("DNS: ", ether.dnsip);  

  if (!ether.dnsLookup(website))
    Serial.println("DNS failed");
    
  ether.printIp("SRV: ", ether.hisip);
}

void loop () {
  ether.packetLoop(ether.packetReceive());
  
  if (millis() > timer) {
    timer = millis() + 5000;
    ether.browseUrl(PSTR("/notifier.php"), "", website, my_callback);
  }
}

That's it. Now you just wire an LED to pin 3 on your Nanode (which can be changed by adjusting the value of the led variable in the sketch). The sketch checks with notifier.php service every 5 seconds. If there are unread emails with the label "alert" then the LED is turned on. If there are no unread emails, then the LED is turned off. Currently the Nanode and LED are hanging over the top of our TV connected to the router behind the TV. Next step is to decide on a more elegant mounting.