#include "Weather.h"
#include <Adafruit_NeoPixel.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
unsigned long interval;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, 8, NEO_GRB + NEO_KHZ800);
// Blinks the LED with the specified color.
void blink(uint8_t red, uint8_t green, uint8_t blue)
{
pixels.setPixelColor(0, pixels.Color(red, green, blue));
pixels.show();
delay(250);
pixels.setPixelColor(0, pixels.Color(0, 0, 0));
pixels.show();
delay(250);
}
// Sets the color and brightness of the LED based on wind speed and direction.
void displayResults()
{
int red = 0;
int green = 0;
int blue = 0;
float dir = getWindDirection();
Serial.print(F("Wind direction: "));
Serial.print(dir);
Serial.println(F(" degrees"));
float speed = getWindSpeed();
Serial.print(F("Wind speed: "));
Serial.print(speed);
Serial.println(F(" km/h"));
if (dir < 90) // Red
{
red = map(dir, 0, 90, 0, 255) * (speed/200);
}
if (dir > 0 && dir < 180) // Green
{
green = map(dir, 0, 180, 0, 255) * (speed/200);
}
if (dir > 90 && dir < 270) // Blue
{
blue = map(dir, 90, 270, 0, 255) * (speed/200);
}
if (dir > 180 && dir < 270) // Purple
{
red = map(dir, 180, 270, 0, 255) * (speed/200);
}
if (dir > 270) // Purple
{
red = map(dir, 270, 360, 0, 255) * (speed/200);
blue = map(dir, 270, 360, 0, 255) * (speed/200);
}
char colorArr [64];
snprintf(colorArr, 64, "Color: R = %i, G = %i, B = %i", red, green, blue);
Serial.println(colorArr);
pixels.setPixelColor(0, pixels.Color(red, green, blue));
pixels.show();
}
// Gets weather data from the API. LED blinks red on failure.
void getWindData()
{
updateWeather();
if (!apiError)
{
displayResults();
}
else
{
while(true)
{
blink(64, 0, 0);
}
}
}
void setup()
{
Serial.begin(115200);
pixels.begin();
WiFi.begin(ssid, password, 6);
while (WiFi.status() != WL_CONNECTED)
{
blink(0, 0, 64);
}
getWindData();
}
void loop()
{
unsigned long now = millis();
if (now - interval > 300000)
{
getWindData();
interval = now;
}
}North
South
East
West