Now it is really easy to do, using gmail’s atom capability and the power of Webhooks…
first set up your Webhook in the Spark CLI:
spark webhook create gmail_count https://<GMAIL_USER_NAME>:<GMAIL_PASSWORD>@mail.google.com/mail/feed/atom/
you will get:
Sending webhook request { uri: 'https://api.spark.io/v1/webhooks',
method: 'POST',
json: true,
form:
{ event: 'gmail_count',
url: 'https://YourUsername:YourPassword@mail.google.com/mail/feed/atom/',
deviceid: undefined,
access_token: 'yourAccessToken',
requestType: undefined,
headers: undefined,
json: undefined,
query: undefined,
auth: undefined,
mydevices: undefined } }
Successfully created webhook!
then the code (right from the Spark Documents):
int emailCount = -99;
unsigned long myTimer;
void setup() {
//Serial.begin(115200);
Spark.subscribe("hook-response/gmail_count", gotGmailAtomFeed, MY_DEVICES);
Spark.variable("EmailCount", &emailCount, INT);
Spark.publish("gmail_count");
myTimer = millis();
}
void loop()
{
if (millis() - myTimer > 60000UL)
{
//Serial.println("Requesting Gmail Count!");
Spark.publish("gmail_count");
myTimer = millis();
//char buffer[25];
//sprintf(buffer, "You have %d email%s", emailCount, (emailCount == 0 || emailCount > 1)? "s." :".");
//Serial.println(buffer);
}
}
void gotGmailAtomFeed(const char *event, const char *data)
{
String buffer = String(data);
String emailReturn = extractString(buffer, "<fullcount>", "</fullcount>");
if (emailReturn != NULL)
emailCount = emailReturn.toInt(); //
}
String extractString(String myString, const char* start, const char* end)
{
if (myString == NULL)
{
return NULL;
}
int idx = myString.indexOf(start);
if (idx < 0) {
return NULL;
}
int endIdx = myString.indexOf(end);
if (endIdx < 0) {
return NULL;
}
return myString.substring(idx + strlen(start), endIdx);
}
and it will quite nicely display your count!
EDIT: pasted wrong code, is correct now.