Megan Altman
Published

Lane Tech HS - PCL- Fitbit and Smart Lamp

Using a relay, a Photon, and a Fitbit, I created a lamp turns on and off according to my sleep schedule.

IntermediateFull instructions provided2 hours1,558
Lane Tech HS - PCL- Fitbit and Smart Lamp

Things used in this project

Hardware components

Photon
Particle Photon
×1
Relay (generic)
×1
extension cord
×1
Flex Fitbit
×1

Software apps and online services

Maker service
IFTTT Maker service

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)

Story

Read more

Custom parts and enclosures

Enclosure CAD

This was a box, made in tinkercad, with openings for the extension cord and power for the photon
Screen shot 2018 02 16 at 12 01 47 pm kqbobprquc

CAD1

Schematics

Relay And Arduino

The picture shows an Arduino, however, a photon can be easily substituted.
Relay 700x467 8w5vrkepnj

Code

Code for Photon

C/C++
used with Photon web based IDE
//relay and onboard led pin

int relayP = D2;
int ledPin = D7;

//times default to waking up at 6 am and fallinf asleep at 10 pm
double wakeUpTime=06.00;
double currentTime=0;
double offTime=22.00;

//functions
int fellAsleepFunction(String command);
int awokeAtFunction(String command);
int switchControl(String command);

//used for converting string data to double hour
String x;
int length1=0;

//master switch boolean
bool buttonControlled = false;

void setup() 
{
    Serial.begin(9600);
    pinMode(relayP, OUTPUT);
    pinMode(ledPin, OUTPUT); 
    
    //create  functions
    Particle.function("FellAsleep", fellAsleepFunction);
    Particle.function("AwokeAt", awokeAtFunction);
    Particle.function("switch", switchFunction);
    
    //test function
    Particle.subscribe("testFunction60618", myHandler);
}


void loop()
{
   
   //retrieve current time, convert to current time zone, convert to military time, convert to a double. 
    currentTime = Time.hour();
   
    currentTime = currentTime +18;
    if(currentTime>23)
    {
        currentTime = currentTime- 24;
    }
   currentTime= currentTime + (double)Time.minute()/60;
    
  
   //checks to see if it is in button controlled or automatic mode
   if(!buttonControlled)
   {
  
        // turn off if it is between bed time and midnight or between midnight and wake up time
        if(currentTime>offTime && currentTime<24 )
        {
            digitalWrite(relayP, HIGH);
            digitalWrite(ledPin, LOW);
        }
        else if(currentTime>0 && currentTime<wakeUpTime )
        {
            digitalWrite(relayP, HIGH);
            digitalWrite(ledPin, LOW);
        }
        else 
        {
            digitalWrite(relayP, LOW);
            digitalWrite(ledPin, HIGH);
        }
   
   }
   else
   {
        digitalWrite(relayP, LOW);
   }
  
   
   
    
    

}
//switches boolean every time button is pressed
int switchFunction(String command)
{
   buttonControlled = !buttonControlled;

    Particle.publish("PUBLISH60618",command); 
}







//These two functions do the exact same thing. They scan a strign for a time, convert it to a number, and publish the data. One of these only responds to the fitbit, and the other is meant to be used for testing.

int fellAsleepFunction(String command)
{
    x=command;
    length1 = x.lastIndexOf(":");
    offTime = x.substring(length1-2, length1).toInt();
    offTime = offTime + (((double)x.substring(length1 + 1, length1+3).toInt())/60);
         
    //offTime = offTime + 12;
   
    Particle.publish("PUBLISH60618",command); 
}

int awokeAtFunction(String command)
{
    x=command;
    length1 = x.lastIndexOf(":");
    wakeUpTime = x.substring(length1-2, length1).toInt();
    wakeUpTime = wakeUpTime + (((double)x.substring(length1 + 1, length1+3).toInt())/60);


    Particle.publish("PUBLISH60618",command); 
}



void myHandler(const char *eventName, const char *data)
{
    x=data;
    length1 = x.lastIndexOf(":");
    offTime = x.substring(length1-2, length1).toInt();
    offTime = offTime + (((double)x.substring(length1 + 1, length1+3).toInt())/60);
 
    offTime = offTime + 12;
  
    Particle.publish("PUBLISH60618",data);

   
}

Credits

Megan Altman

Megan Altman

2 projects • 7 followers
College Freshman. I have been programming for several years with FIRST Robotics.

Comments

Add projectSign up / Login