Andrea S.
Published © CC BY-NC

E-mail watch

Check your Gmail mailbox (with labels) in a glance!

IntermediateFull instructions provided7,660
E-mail watch

Things used in this project

Hardware components

Spark/Particle Core or Photon or Electron
39$ or 19$ or 29$
×1
3d printed cover
Make yours or buy one
×1
Mini-breadboard
×1
LEDs + adequate resistors
One RGB + one Yellow
×1

Story

Read more

Schematics

Breadboard circuit

It's very simple, no real need for Fritzing!
2015 08 24%2012.01.23

Code

Spark firmware (copy and paste in Spark IDE)

C/C++
Spark firmware
// -------------------------------------------
// E-mail Watch - Particle firmware (Aug 2015)
// -------------------------------------------

// Pin numberss
int led_yell = D0;
int led_blue = D7;
int rgbPin[3] = {A6, A1, A0};
// Variables
int starVal = 0;                    // Special / cloud connection status
int bankVal = 0;                    // Bank label
int persVal = 0;                    // Personal label
int servVal = 0;                    // Service/social label
int notiVal = 0;                    // Notifications label
int mailErr = 0;                    // Gmail error
int colorDelay = 1000;              // Delay between colors
int watchdog = 0;                   // Watchdog counter
int resetdog = 150;                 // ~ 160s
unsigned long uptime = 0;           // Uptime value
int rVal = 0;                       // Current red value
int gVal = 0;                       // Current green value
int bVal = 0;                       // Current blue value

// This routine runs only once upon reset
void setup()
{
   //Register our Spark functions (and variable) here
   Spark.function("mail", mailControl);
   Spark.function("label", labelControl);
   Spark.variable("uptime", &uptime, INT);

   // Configure the pins to be outputs
   pinMode(led_yell, OUTPUT);
   pinMode(led_blue, OUTPUT);
   for (int i=0; i<3; i++) {
        pinMode(rgbPin[i], OUTPUT);
   }

   // Do a LED test: all ON for 3 secs
   digitalWrite(led_yell, HIGH);
   delay(200);
   digitalWrite(led_blue, HIGH);
   delay(200);
   for (int i=0; i<3; i++) {
        analogWrite(rgbPin[i], 255);
        delay(200);
   }
   delay(3000);
   
   // Initialize all the LEDs to be OFF
   digitalWrite(led_yell, LOW);
   delay(200);
   digitalWrite(led_blue, LOW);
   delay(200);
   for (int i=0; i<3; i++) {
        analogWrite(rgbPin[i], LOW);
        delay(200);
   }
   delay(500);
}


// This routine loops forever 
void loop()
{
   // Update uptime
   uptime = millis();
   
    if (--watchdog < 0) {                                       // Decrease watchdog and enter in warning state if timed out
        watchdog = -1;
        //blueState2 = LOW;
        digitalWrite(led_blue, LOW);                            // Blue status LED off
        digitalWrite(led_yell, LOW);                            // Yellow star off
        ledColors(255, 128, 0, 5);                              // Yellow warning
    }
    else {
        if (mailErr) {                                          // In case of Gmail error
            digitalWrite(led_yell, LOW);                        // Yellow star off
            ledColors(255, 0, 0, 5);                            // Gmail error (red)
        }
        else {
            for (int i=0; i<3; i++) {
                digitalWrite(led_yell, starVal);                // Write Special (star) LED
                if (notiVal) {                                  // Notifications
                    ledColors(notiVal*16, notiVal*8, notiVal*4, 1);
                }
                if (servVal) {                                  // Service/Social
                    ledColors(servVal*24, 0, servVal*20, 1);
                }
                if (persVal) {                                  // Personal
                    ledColors(0, persVal*28+3, 0, 1);
                }
                if (bankVal) {                                  // Bank (0-3)
                    ledColors(0, 0, bankVal*85, 1);
                }
                if (servVal+persVal+bankVal+notiVal == 0) {     // None
                    ledColors(0, 0, 0, 2);
                }
            }
        }
    }
}
// End of loop


// Function to activate RGB LED colors
int ledColors(int r, int g, int b, int delMult)
{
    if ((r != rVal) || (g != gVal) || (b != bVal)) {
        // Fade off
        while (rVal+gVal+bVal > 0) {
            if (rVal > 0) rVal--;
            if (gVal > 0) gVal--;
            if (bVal > 0) bVal--;
            analogWrite(rgbPin[0], rVal);        // old R val
            analogWrite(rgbPin[1], gVal);        // old G val
            analogWrite(rgbPin[2], bVal);        // old B val
            delay(10);
        }
        delay (10);
        // Fade to new RGB color
        while (rVal+gVal+bVal < r+g+b) {
            if (rVal < r) rVal++;
            if (gVal < g) gVal++;
            if (bVal < b) bVal++;
            analogWrite(rgbPin[0], rVal);        // new R val
            analogWrite(rgbPin[1], gVal);        // new G val
            analogWrite(rgbPin[2], bVal);        // new B val
            delay(10);
        }
        // Save new components
        rVal = r;
        gVal = g;
        bVal = b;
    }
    delay(delMult*colorDelay);
    watchdog = watchdog - delMult;

    return 0;
}


// Read command string: E_B_P_S_N_T_
int mailControl(String command)
{
   char readLabel = command.charAt(0);                      // Read label char
   if (readLabel != 'E') return -1;                         // "E"rror
   int readVal = command.charAt(1) - '0';                   // get E value (0-1)
   if (readVal < 0 || readVal > 1) return -2;               // sanity check
   else mailErr = readVal;
   
   readLabel = command.charAt(2);                           // Read label char
   if (readLabel != 'B') return -1;                         // "B"ank
   readVal = command.charAt(3) - '0';                       // get B value (0-3)* Special range
   if (readVal < 0 || readVal > 3) return -3;               // sanity check
   else bankVal = readVal;

   readLabel = command.charAt(4);                           // Read label char
   if (readLabel != 'P') return -1;                         // "P"ersonal
   readVal = command.charAt(5) - '0';                       // get P value (0-9)
   if (readVal < 0 || readVal > 9) return -4;               // sanity check
   else persVal = readVal;

   readLabel = command.charAt(6);                           // Read label char
   if (readLabel != 'S') return -1;                         // "S"ervice / "S"ocial
   readVal = command.charAt(7) - '0';                       // get S value (0-9)
   if (readVal < 0 || readVal > 9) return -5;               // sanity check
   else servVal = readVal;

   readLabel = command.charAt(8);                           // Read label char
   if (readLabel != 'N') return -1;                         // "N"otifications
   readVal = command.charAt(9) - '0';                       // get N value (0-9)
   if (readVal < 0 || readVal > 9) return -6;               // sanity check
   else notiVal = readVal;

   readLabel = command.charAt(10);                          // Read label char
   if (readLabel != 'T') return -1;                         // s"T"ar
   readVal = command.charAt(11) - '0';                      // get N value (0-9)
   if (readVal < 0 || readVal > 9) return -7;               // sanity check
   else starVal = readVal;

   watchdog = resetdog;                                     // Reset watchdog
   digitalWrite(led_blue, LOW);
   delay(100);
   digitalWrite(led_blue, HIGH);
   delay(100);
   digitalWrite(led_blue, LOW);
   delay(100);
   digitalWrite(led_blue, HIGH);

   return mailErr;                                          // Return "E" value as status
}

// Read (one label) command string: X_ (possible labels: E_B_P_S_N_T_)
int labelControl(String command)
{
    // Read char
    char readLabel = command.charAt(0);
    char readVal = command.charAt(1) - '0';
    switch (readLabel) {
        case 'E':                                               // "E"rror
            if (readVal < 0 || readVal > 1) return -2;          // sanity check
            else mailErr = readVal;
            break;
        case 'B':                                               // "B"ank
            if (readVal < 0 || readVal > 3) return -3;          // sanity check - special range
            else bankVal = readVal;
            break;
        case 'P':                                               // "Personal"
            if (readVal < 0 || readVal > 9) return -4;          // sanity check
            else persVal = readVal;
            break;
        case 'S':                                               // "S"ervice / "S"ocial
            if (readVal < 0 || readVal > 9) return -5;          // sanity check
            else servVal = readVal;
            break;
        case 'N':                                               // "N"otifications
            if (readVal < 0 || readVal > 9) return -6;          // sanity check
            else notiVal = readVal;
            break;
        case 'T':                                               // s"T"ar
            if (readVal < 0 || readVal > 9) return -7;          // sanity check
            else starVal = readVal;
            break;
    default: 
        return -1;
    }
    watchdog = resetdog;                                        // Reset watchdog
    digitalWrite(led_blue, HIGH);
}

Linux bash script (to be run on an 'always on' PC)

SH
Bash script
No preview (download only).

Command line operation

SH
Encode your Gmail password
   pi@raspberry ~ $ echo your_gmail_password | openssl enc -aes-128-cbc -a -salt -pass pass:local_encoding_password

U2FsdGVkX1+VaAnuBaYJOmqJVWn6l9HOD/2pBbShJrs+xfCN50oHaxJVdOLU32/v

Command line operation

SH
Verify your password encoding is correct
   pi@raspberry ~ $ echo U2FsdGVkX1/YCybSOE77JRZR377kYl0MnMy9zVru8q9oljUyGrzMM4vD/cOzLeqv | openssl enc -aes-128-cbc -a -d -salt -pass pass:local_encoding_password

your_gmail_password

Command line operation

SH
I recommend to launch the script at startup (with cron, for example)
   pi@raspberry ~ $ crontab -l

@reboot /home/pi/updatesparkmon.sh U2FsdGVkX1+VaAnuBaYJOmqJVWn6l9HOD/2pBbShJrs+xfCN50oHaxJVdOLU32/v  #Update Spark monitor

Credits

Andrea S.

Andrea S.

3 projects • 10 followers
Technology enthusiast, problem solver, creative thinker and tinkerer!

Comments