This project was part of the Lane Tech HS Physical Computing Lab course. I created this project as part of the Home Automation / IoT project.
I decided to create a lamp that turns itself on and off according to my sleep schedule. The information on my sleep was gathered using a Fitbit an then communicated to the photon using Google sheets and IFTTT.
CircuitryThe wiring on this project is very simple. I spliced a relay into an extension cord, rather than the light itself, so that the project would be easier to transfer and can be plugged into any lamp.
The enclosure was designed in tinkerCad and 3d printed.
Code and Cloud CommunicationThe fitbit syncs to the internet whenever it connects to a phone, and publishes sleep data. This project only uses the values "AwokeAt" and "FellAsleepAt" but it should be noted that the fitbit collects and publishes a lot of other sleep related statistics.
I created two ifttt applets that retrieve the sleep data and then call the appropriate particle functions. These functions scan the data for a time and convert it to a decimal value. ie 3:45 becomes 3.75. The current time is then constantly compared to the time awake or a asleep and used to determine if the light should be on.
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);
}
The master switch return control of the light to the user, by switch the boolean representing whether or not the automated control is running and can be controlled using a button widget, which IFTTT generates for your phone.
//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);
}
The final applet allows particle to publish an event containing data and log that data to a spreadsheet.
There is also a test function in the code which is subscribed to an event and sets the time asleep to that time, this was meant for demonstative purposes to manuallly set a time. The fitbit is pretty cool and conveinent but since it only publishes the data, and therfore triggers the IFTTT applets, once a day it is not easy to use for presentations.
Comments