This project was inspired by the impact of COVID-19 on my personal life. Due to online learning, I spend most of my day in my basement office, and the lack of a window makes the passage of time difficult to gauge. From this I came up with an idea to make a project that shows me whether the sun will set earlier or later each day, to help myself grasp how much of the day is really left when I've woken up at odd hours.
Here is a demonstration of my project in action.
The setup of my project is very simple. The dial is a servo with some colored cardboard hot glued onto it. The routing of my servo to my Particle Argon is as follows:
There are two things to setup before we paste/modify the code! The first one is including the ArduinoJson library. Without it our code wont compile. To do this, click the bookmark labelled libraries in the left bar of build.particle.io, and search ArduinoJson, then select and click include.
The second thing we need to do is implement the webhook for the Sunset API
This is the link the site I used with documentation of how it works! Basically I'm putting in a latitude and longitude and it returns a Json object and one of the pieces of info is the time of sunset in UTC time. To implement it in particle you want to use the bottom left buttons and go to console and integrations. That will take you to a page like this
My code is setup to work for the CST timezone. The API I use uses UTC time, so I did some manual calculations visible in the code. Click New Integration and select Webhook, Then fill out the following info:
You can change the event name to whatever you want as long as you change my code to change every sunset to whatever you've chosen. Make sure you also choose the device you intend to use for the project.
Then, click > Advanced Settings and fill in the following custom parameters. Put the latitude and longitude of your location for the best accuracy. Finally, under response template fill in the custom template with this, and click save. What this does is simplify the process of getting the sunset in the actual code.
Now to look at the code.
#include <ArduinoJson.h>
StaticJsonDocument<512> doc;
// create servo object to control a servo
Servo myservo;
// variable to store the servo position
int pos = 0;
void setup()
{
// Subscribe to the webhook response event
Particle.subscribe("hook-response/sunset", myHandler, MY_DEVICES);
myservo.attach(D2); // attaches the servo on the D2 pin to the servo object
Serial.begin(9600);
}
void loop() {
// Get some data
String data = String(180);
// Trigger the integration
Particle.publish("sunset", data, PRIVATE);
// Wait 6 seconds
delay(6000);
}
void myHandler(const char *event, const char *data)
{
//retrieving only the hour from the char returned by my webhook and making it into an int
String colin (":");
String WordSearch (data);
int colinPos = WordSearch.indexOf(colin, 0);
String calcTime = WordSearch.substring(0, colinPos);
int intCalcTime = calcTime.toInt();
int finalTime = 0;
// Converting UTC time to Central time
if( intCalcTime == 10)
finalTime = 4;
else if( intCalcTime == 11)
finalTime = 5;
else if( intCalcTime == 12)
finalTime = 6;
else if( intCalcTime == 1)
finalTime = 7;
else if( intCalcTime == 2)
finalTime = 8;
else if( intCalcTime == 3)
finalTime = 9;
else if( intCalcTime == 4)
finalTime = 10;
else if( intCalcTime == 5)
finalTime = 11;
//uncomment line below to test different times
//finalTime = 7;
//calculate Servo Angle
double ServoAngle;
if (finalTime > 7)
ServoAngle= 180;
else
ServoAngle = 180-((double)(finalTime-4)/3)*180;
//Uncomment these printlines to troubleshoot any timezone conversion issues and check variables
Serial.print("Webhook Sunset ");
Serial.println(data);
Serial.println("Sunset String " + WordSearch);
Serial.println("Colin Position " + colinPos);
Serial.println("Hours String " + calcTime);
Serial.println(finalTime);
Serial.print("ServoAngle ");
Serial.println(ServoAngle);
Serial.println(Time.hour(Time.now()));
//uncomment to have code run automatically at the set time everyday
//if (Time.hour(Time.now())==13)
//{
//myservo.write((int)ServoAngle);
//}
//
//comment the line below if intending to run automatically as described above
myservo.write((int)ServoAngle); //this code actually sets the servo angle
}
You can go ahead and copy and paste the code in, make sure to reference the comments. There is a part where I mention time conversion. The API runs on UTC time, so when I get the hour, it is 6 hours ahead of my timezone. I did some manual conversions from the hours of 4 pm to 11 pm (my timezone), the range of time the sun sets. If you are not in US central time, you should calculate different conversions.
If you want to test values there are commented lines of code to change, and at the bottom there's something else to consider depending on how you want to do this project.
My project works two ways, manually and automatically. I can either have an immediate response of when the sun will set by having the servo write as soon as my argon is powered on, or I can have it keep running checks in the background 24/7, and only write a new servo angle when the hour is 7 in the morning. If you want the former, leave the code as is. If you want the latter, comment out the last line of code and uncomment the specified lines above.
Here's a video showing that the latter method works when the time is 7 am.
The next part of my project was the crafty bit. I was initially going to have my dial point to pixel art suns made of perlor beads as is shown in the concept below
However, I did not have enough black and orange beads, so I instead worked with what I had, using construction paper, a sharpie, glue, and a shoebox.
I cut some holes with scissors for my wires to go through, and hotglued some lace from a shopping bag and extra cardboard to make a place for my servo to slide into.
Then, to setup the final product, I fed the wires through the holes, connected the argon to a portable charger, taped my breadboard and charger to the wall, and hung the shoebox on a nail on my wall. And that gets us here!
Comments
Please log in or sign up to comment.