Particle Photon Powered Physical Notifications Device

Having just received my photon maker kit I wanted to build a simple project to show case the photon’s capabilities and so I decided to build a photon powered RGB lantern which I’d light up using IFTTT recipes.

image

The Internet Powered Lamp Lighting Up

The components :

  • Particle Photon
  • 3 x 8mm RGB LEDs (common anode)
  • 330ohm resistor
  • Prototyping board
  • Battery Pack
  • Ikea Paper Lantern

Wiring:
image

  • Connect the 3 RGB in parallel with the ¬†the resistor connected to the common anode.
  • Red to A4
  • Green to A3
  • Red to A2
  • Common anode resistor to D0
  • Battery pack positive to photon Vin
  • Battery pack -Ve to photon¬†GND

Code:

I didn’t want the lantern to light up all of a sudden instead I wanted it to gradually light up.

// -----------------------------------
// Controlling RGB LEDs over the Internet
// 23 October 2015
// -----------------------------------
int led_red = A5;
int led_blue = A4;
int led_green = A3;
int led_on = D0;

int led=D7;

int r_val=255;
int g_val=255;
int b_val=255;
int p_val=0;
int t_val=1;
bool is_on=false;
// Last time, we only needed to declare pins in the setup function.
// This time, we are also going to register our Spark function

void setup()
{
    Serial.begin(9600); 
  
  
   // Pin configuration, 3 PINS, plus anode
   pinMode(led_on, OUTPUT);
   pinMode(led_red, OUTPUT);
   pinMode(led_blue, OUTPUT);
   pinMode(led_green, OUTPUT);

   digitalWrite(led,HIGH);
    
    
    //we declare a photon function to use through IFTTT
   Spark.function("led",ledToggle);
  
    //turn the LEDs off 
   digitalWrite(led_on,LOW);
   analogWrite(led_green,255);
   analogWrite(led_red,255);
   analogWrite(led_blue,255);

}

void loop()
{
   // Nothing to do here
}

/*This is the function IFTTT will be talking to, it accepts a string command
*/

int ledToggle(String command) {
    /* Spark function to receive colour and duration RRRGGGBBBTTT example 000255255001 for RED turn on for 1 second, 
    values are reversed so 000 is the strongest illumination and 255 is off
    */

    is_on=true;
    /*debug bit*/
    Serial.println("c : ");
    Serial.println("command recieved "+command);
    
    /*tokenize string and extract colors*/
    String red=command.substring(0,3);
    String green=command.substring(3,6);
    String blue=command.substring(6,9);
    String backoff_t=command.substring(9,12);
    
    /*debug bit*/
    Serial.println("R"+red);
    Serial.println("G"+green);
    Serial.println("B"+blue);
    Serial.println("t"+backoff_t);

    /*cast string colors*/
    r_val=red.toInt();
    g_val=green.toInt();
    b_val=blue.toInt();
    t_val=backoff_t.toInt();

/*cycle colours on*/
    set_lights();
    
    /*keep lights on for the required duration*/
    delay(1000*t_val);
    
    //reset vars and turn off 
    is_on=false;
    r_val=255;
    g_val=255;
    b_val=255;
    t_val=1;
    
    set_lights();
}

/*
Function used to set lights
*/
int set_lights()
{
    //colour cycle while lighting up
    
    //set colour
    analogWrite(led_red,r_val);
    analogWrite(led_green,g_val);
    analogWrite(led_blue,b_val);
    
    if(is_on)
    {
        // gradual power increase to common anode
        for(int i =0; i<=255; i++)
        {
             delay(5);
             analogWrite(led_on,i);
        }
    }
    else 
    {
        analogWrite(led_on,0);
    }
   
}

IFTTT integration:

Once the code was flashed on my photon, I was able to invoke the photon function over the internet, with the desired RGB colour value and duration sent as a parameter.

IFTTT Action

IFTTT Action, to light up purple for 20 seconds

 

 
**Update: I cleaned up the implementation by using a neopixel stick which greatly reduced the wiring and increased the functionality**
 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s