[Arduino] Mail Notifier with C#

Hi all,
as my first experiment, an arduino connected to COM9 to recognize incoming mails from google mail (gmail)!!

Very easy to do!!!

As experiment I connected Arduino board to a breadboard with a GREEN LED and a PIEZO SPEACKER in this way:


Arduino Gmail Notifier

Rif: albertopasca.it

I connected the LED to Arduino pin 13 and speacker to arduino pin 12.

The code sended to Arduino ATMEGA328 chip was this:
-It was programmed to read serial port data (sended from server by C# app)-

Arduino CODE (C):

[code lang=”cpp” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]/* green led*/
const int ledPin = 13;
/* piezo */
const int beepPin = 12;</code>

/* check gmail mails 😀 */
int mail = LOW;
int val;

void setup()
{
/* setup pin iniziale */
pinMode(ledPin, OUTPUT);
pinMode(beepPin, OUTPUT);

Serial.begin(9600);

mail = HIGH;
}

void loop()
{
/* loop gmail check mails */
val = Serial.read();
Serial.println(val, BYTE);

if (val == 110) { // "n" = 110 in dec from ascii
mail = HIGH;
} else if (val == 109) { //109 = "m" in dec from ascii
mail = LOW;

/* accendo il led*/
digitalWrite(ledPin, HIGH);

/* fischio avviso mail
tipo fischio di fine
partita di calcio */
digitalWrite(beepPin, HIGH);
delay(500);
digitalWrite(beepPin, LOW);
delay(200);
digitalWrite(beepPin, HIGH);
delay(500);
digitalWrite(beepPin, LOW);
delay(200);
digitalWrite(beepPin, HIGH);
delay(3000);

/* rimetto tutto a posto */
digitalWrite(beepPin, LOW);
digitalWrite(ledPin, LOW);
}
}[/code]

After that Arduino code was uploaded to chip and programmed successfully, I make a C# app that loop on “while true” to check google mails every 10 seconds.
If there is a new incoming mail, it send to serial port (COM9, 9600) a char, “m” for new mail and “n” for NO mail that are interpreted from arduino respectly to 109 and 110 in DEC format.
Arduino CODE recognize the DEC (ascii code, m||n) and executed code relatively condition.
If there was a “m” code, it send an HIGH signal (1) to GREEN LED and an HIGH to speacker (temporized).
Until you don’t read your message, it beeps every 10 seconds!!

To do all of this tricks, you need to create a C# Windows || Console app and run on COM9 listening arduino.

This is C# code (VS2010 – C#4.0):

[code lang=”csharp” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]/* SEND / READ SERIAL PORT COM9 DATA */

try {
SerialPort port = new SerialPort( "COM9", 9600, Parity.None, 8, StopBits.One );
port.Open();string Unreadz = "0";
while ( true ) {
Unreadz = CheckMail();
Console.WriteLine( "Unread Mails: " + Unreadz );
if ( !Unreadz.Equals( "0" ) ) port.Write( "m" );
else port.Write( "n" );

Thread.Sleep( 10000 );
}
}catch ( Exception ee ) { Console.WriteLine( ee.Message ); }[/code]

This ones check mails:

[code lang=”csharp” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]private string CheckMail() {
string result = "0";
try {
var url = @"https://gmail.google.com/gmail/feed/atom";
var USER = "your.user.name";
var PASS = "youRp@ssw0rd";var encoded = TextToBase64( USER + ":" + PASS );
var myWebRequest = HttpWebRequest.Create( url );
myWebRequest.Method = "POST";
myWebRequest.ContentLength = 0;
myWebRequest.Headers.Add( "Authorization", "Basic " + encoded );</code>

var response = myWebRequest.GetResponse();
var stream = response.GetResponseStream();

XmlReader reader = XmlReader.Create( stream );
while ( reader.Read() )
if ( reader.NodeType == XmlNodeType.Element )
if ( reader.Name == "fullcount" ) {
result = reader.ReadElementContentAsString();
return result;
}
} catch ( Exception ee ) { Console.WriteLine( ee.Message ); }
return result;
}
}[/code]

…and last one, text convertion:

[code lang=”csharp” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]public static string TextToBase64( string sAscii ) {
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] bytes = encoding.GetBytes( sAscii );
return System.Convert.ToBase64String( bytes, 0, bytes.Length );
}[/code]

Now, upload code to arduino, press PLAY to C# project… and send you a mail to your gmail address!!!

enjoy beeping!

Rif: albertopasca.it

Alberto Pasca
Latest posts by Alberto Pasca (see all)