This project was designed to give a different light color based on whether or not the weather/temperature outside was ideal to go skateboarding. A green light meant that it was okay, a yellow light meant that I might want to feel what it was like outside for myself before making a decision, and a red light meant that it was a bad idea. I tailored this project specifically for where I live, but that can be changed fairly easily.
Connecting to the Open Weather APIThe first task was connecting to the API I was using. I signed up for an Open Weather Map account which gave me an API key and access to "Current Weather Data" for free.
I then set up a webhook in the console section of my particle project. I made sure to specify that the weather data I was getting was from my city. I also made sure to add the specific data I wanted to have in my Response Template, as I did not need every piece of data given by the API.
I then fully implemented the API into my code and printed my data points to my serial monitor. This was helpful in checking that the data I had pulled had been correct and that my project was working later in the process.
// This #include statement was automatically added by the Particle IDE.
#include <ArduinoJson.h>
StaticJsonDocument<1308> doc;
void setup()
{
// Subscribe to the webhook response event
Particle.subscribe("hook-response/skatingWeather", multiValueHandler);
Serial.begin(9600);
}
void multiValueHandler(const char *event, const char *data){
String dataStr = String(data);
char strBuffer[40] = "";
dataStr.toCharArray(strBuffer, 40);
String firstToken = strtok(strBuffer, ",");
Serial.println(data);
Serial.println("Temperature: " + firstToken);
String secondToken = strtok(NULL, ",");
Serial.println("Feels like: " + secondToken);
String thirdToken = strtok(NULL, ",");
String fourthToken = strtok(NULL, ",");
Serial.println("The weather today: " + thirdToken + ", " + fourthToken);
}
Above is the code used to implement the API data and print it to the serial monitor. Take note that parts of the code here that were relevant to later parts of the project were omitted.
Hardware SetupNow it was time to set up the actual LED. Here are a few angles of the hardware setup to make the RGB LED work.
Next I connected the LED and the colors that I wanted it to display to my data retrieved from the API. At the end of the multiValueHandler I added two global variables equal to the current temperature and the "feels like".
currentTemp = atof(firstToken);
feelsLike = atof(secondToken);
weatherType = thirdToken;
The code below shows the specific values/parameters and colors I used for my project, but these can be easily changed.
void loop()
{
// Get some data
String data = String(10);
// Trigger the integration
Particle.publish("skatingWeather", data, PRIVATE);
// Wait 10 seconds
delay(10000);
if(currentTemp < 25 || strcmp(weatherType, rain) == 0 || strcmp(weatherType, snow) == 0 || feelsLike < 25){
setColor(255,0,0); //red. no good for skating bro
}
else if(currentTemp > 45){
setColor(0,255,0); //green. you are good to go!
}
else{
setColor(255,255,0); //yellow. you might wanna feel outside before you go. it's up to you.
}
}
Here is a video of the project in proper working order:
Here is a video testing to make sure the "snow" feature of the project works:
Comments