Gmail Checker

Lady Ada, godmother of physical computing, believes "the blinking gmail checker is the hello world of pervasive computing." Let's make it wireless using our Sentilla Perk Kit.

Mote Applicaton

This is a very basic application. We'll implement a single receiver and an infinite loop that just waits for a message from the client application letting us know if we've got mail and how much we've got. To instantiate the receiver we import com.sentilla.system.Receiver and com.sentilla.system.ReceiverDriver, and then

1
2
3
//Set up reciever and submit
Receiver recv = ReceiverDriver.create(GmailMoteApp.GmailCount.class);
recv.setReceive().submit();

To instantiate the leds we import com.sentilla.system.Leds and

1
2
// Use the LedsDriver factory to create an Leds instance
Leds leds = LedsDriver.create();

Finally, we'll run an infinite loop where we check the receiver, and display the number of messages, in Binary, across the 8 Leds on the mote.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
//Initialize Mail Counter
int count = 0;

//Start Infinite loop
while (true) {

    //Set count and blink
    leds.set(count);
    Thread.sleep(200);
    leds.set(0);
    Thread.sleep(200);

    //Check if we've got a count, and store it
    if (recv.isDone())
    {
        GmailCount gcount = (GmailCount) recv.getData();
        count = gcount.count;
        recv.setReceive().submit();
    }
}

The complete code can be found here

Client Application

We'll use GMail for our client as they provide a very handy RSS feed from which we can get our count. To instantiate our sender we'll import the com.sentilla.system.Sender and com.sentilla.system.SenderDriver libraries:

1
2
//Create Sender
Sender sender = SenderDriver.create("local");

To check for mail, we'll use the java.net library like so

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//Code from http://www.java-forums.org/java-net/7242-reading-urls-protected-http-authentication.html
static class MyAuthenticator extends Authenticator {
        private String username, password;

        public MyAuthenticator(String user, String pass) {
                username = user;
        password = pass;
            }

           protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password.toCharArray());
           }
}

    //Fill in gmail info
    String name = "foo@gmail.com"; //expects someone@gmail.com
    String password = "abc123";  //expects your password, don't worry, we're using HTTPS 

    //Create SSL authentication, send to gmail, get XML
    Authenticator.setDefault(new MyAuthenticator(name,password));
    URL gmail = new URL("https://mail.google.com/gmail/feed/atom");
    BufferedReader in =  new BufferedReader(
                        new InputStreamReader(
                    gmail.openStream()));

    //Check for message count
    String line = ""; 
    String total = "";
    while ((line = in.readLine()) != null )
    {
        if (line.contains("fullcount")) 
        {
            total = line.substring("<fullcount>".length(), line.length()- "</fullcount>".length());
        }
    }

Finally, we'll have the client send the update to the mote

1
2
    out.count = Integer.parseInt(total);
    sender.send(out);

The complete code can be found here

Conclusion

So there we have it, a full client/mote combination in less than 150 lines of code. Enjoy!



#Gmail Checker

Lady Ada, godmother of physical computing, believes "the blinking gmail checker is the hello world of pervasive computing." Let's make it wireless using our Sentilla Perk Kit.

##Mote Applicaton

This is a very basic application. We'll implement a single receiver and an infinite loop that just waits for a message from the client application letting us know if we've got mail and how much we've got. To instantiate the receiver we import com.sentilla.system.Receiver and com.sentilla.system.ReceiverDriver, and then

<code>
#!java
//Set up reciever and submit
Receiver recv = ReceiverDriver.create(GmailMoteApp.GmailCount.class);
recv.setReceive().submit();
</code>

To instantiate the leds we import com.sentilla.system.Leds and

<code>
#!java
// Use the LedsDriver factory to create an Leds instance
Leds leds = LedsDriver.create();
</code>

Finally, we'll run an infinite loop where we check the receiver, and display the number of messages, in Binary, across the 8 Leds on the mote.

<code>
#!java
//Initialize Mail Counter
int count = 0;

//Start Infinite loop
while (true) {

//Set count and blink
leds.set(count);
Thread.sleep(200);
leds.set(0);
Thread.sleep(200);

//Check if we've got a count, and store it
if (recv.isDone())
{
GmailCount gcount = (GmailCount) recv.getData();
count = gcount.count;
recv.setReceive().submit();
}
}
</code>

The complete code can be found @[here][[GmailMoteApp.java]]

##Client Application

We'll use GMail for our client as they provide a very handy RSS feed from which we can get our count. To instantiate our sender we'll import the com.sentilla.system.Sender and com.sentilla.system.SenderDriver libraries:

<code>
#!java
//Create Sender
Sender sender = SenderDriver.create("local");
</code>

To check for mail, we'll use the java.net library like so

<code>
#!java
//Code from http://www.java-forums.org/java-net/7242-reading-urls-protected-http-authentication.html
static class MyAuthenticator extends Authenticator {
private String username, password;

public MyAuthenticator(String user, String pass) {
username = user;
password = pass;
}

protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password.toCharArray());
}
}

//Fill in gmail info
String name = "foo@gmail.com"; //expects someone@gmail.com
String password = "abc123"; //expects your password, don't worry, we're using HTTPS

//Create SSL authentication, send to gmail, get XML
Authenticator.setDefault(new MyAuthenticator(name,password));
URL gmail = new URL("https://mail.google.com/gmail/feed/atom");
BufferedReader in = new BufferedReader(
new InputStreamReader(
gmail.openStream()));

//Check for message count
String line = "";
String total = "";
while ((line = in.readLine()) != null )
{
if (line.contains("fullcount"))
{
total = line.substring("<fullcount>".length(), line.length()- "</fullcount>".length());
}
}

</code>

Finally, we'll have the client send the update to the mote
<code>
#!java
out.count = Integer.parseInt(total);
sender.send(out);
</code>

The complete code can be found @[here][[GmailAppClient.java]]

##Conclusion
So there we have it, a full client/mote combination in less than 150 lines of code. Enjoy!