Last active
January 30, 2021 14:47
-
-
Save DanielMerzo/c6db029e845776fe20a3e964ecfc7d3d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ArduinoJson.h> | |
#include <WiFiManager.h> | |
#include <FastLED.h> | |
#include <ArduinoJson.h> | |
#include <math.h> | |
#include <WiFiManager.h> | |
#define BRIGHTNESS 64 | |
#define DATA_PIN D3 | |
#define NUM_LEDS 8 | |
CRGB leds[NUM_LEDS]; | |
WiFiManager wifiManager; | |
WiFiClient client; | |
// ======================== hier deinen API-Key eintragen!!! ============================================================================================================ | |
const String city = "Oldenburg"; | |
const String api_key = "xxx"; // dein Open-Weather-Map-API-Schluessel, kostenlos beziehbar ueber https://openweathermap.org/ | |
// ======================================================================================================================================================================== | |
int weatherID = 0; | |
int weatherID_shortened = 0; | |
String weatherforecast_shortened = " "; | |
int temperature_Celsius_Int = 0; | |
unsigned long lastcheck = 0; | |
void setup() { // Damit der Microcontroller angesteruert werden kann | |
Serial.begin(115200); | |
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); | |
FastLED.setMaxPowerInVoltsAndMilliamps(5, 400); | |
FastLED.clear(); | |
FastLED.show(); | |
for (int i = 0; i < NUM_LEDS; i++) { | |
leds[i] = CRGB( 253, 184, 19); | |
} | |
FastLED.setBrightness(BRIGHTNESS); | |
FastLED.show(); | |
wifiManager.autoConnect("deineWetterLampe"); // SSID des Zugriffpunkt werden abgefragt | |
for (int i = 0; i < NUM_LEDS; i++) { | |
leds[i] = CRGB( 0, 0, 0); | |
} | |
FastLED.show(); | |
delay(2000); | |
getCurrentWeatherConditions(); // Wetter ID wird abgerufen | |
} | |
void loop() { | |
if (millis() - lastcheck >= 1800000) { //Alle 30 Min die Wetter ID abfragen | |
getCurrentWeatherConditions(); | |
lastcheck = millis(); | |
} | |
if (weatherID == 800){ // Animation Klares Wetter Abspielen | |
LED_effect_clearSky(); | |
} | |
else { | |
switch (weatherID_shortened) { //Animation Bewölkt Abspielen | |
case 8: LED_effect_cloudy(); break; | |
} | |
}else{ | |
if (weatherID == 211) { //Animation Gewitter Abspielen | |
LED_effect_thunderstorm(); | |
} | |
}else{ | |
if (weatherID == 311) { //Animation Nieselregen Abspielen | |
LED_effect_drizzleRain(); | |
} | |
}else{ | |
if (weatherID == 501) { //Animation mäßiger Regen Abspielen | |
LED_effect_moderateRain(); | |
} | |
}else{ | |
if (weatherID == 511) { //Animation Gefrierregen Abspielen | |
LED_effect_freezingRain(); | |
} | |
}else{ | |
if (weatherID == 611) { //Animation Graupel Abspielen | |
LED_effect_sleet(); | |
} | |
} | |
} | |
void getCurrentWeatherConditions() { // Verbindung aufbauen um Wetter ID abzurufen | |
int WeatherData; | |
Serial.print("connecting to "); Serial.println("api.openweathermap.org"); | |
if (client.connect("api.openweathermap.org", 80)) { | |
client.println("GET /data/2.5/weather?q=" + city + ",DE&units=metric&lang=de&APPID=" + api_key); | |
client.println("Host: api.openweathermap.org"); | |
client.println("Connection: close"); | |
client.println(); | |
} else { | |
Serial.println("connection failed"); | |
} | |
const size_t capacity = JSON_ARRAY_SIZE(2) + 2 * JSON_OBJECT_SIZE(1) + 2 * JSON_OBJECT_SIZE(2) + 2 * JSON_OBJECT_SIZE(4) + JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(6) + JSON_OBJECT_SIZE(14) + 360; | |
DynamicJsonDocument doc(capacity); | |
deserializeJson(doc, client); | |
client.stop(); | |
weatherID = doc["weather"][0]["id"]; | |
int temperature_Celsius = doc["main"]["temp"]; | |
temperature_Celsius_Int = (int)temperature_Celsius; | |
weatherID_shortened = weatherID / 100; | |
switch (weatherID_shortened) { // Hier werden die Wetterbedingungen festgelegt | |
//... | |
case 8: weatherforecast_shortened = "Wolken"; break; | |
default: weatherforecast_shortened = ""; break; | |
} if (weatherID == 800) weatherforecast_shortened = "klar"; | |
}else{ | |
if (weatherID == 211) weatherforecast_shortened = "gewitter"; | |
}else{ | |
if (weatherID == 311) weatherforecast_shortened = "nieselregen"; | |
}else{ | |
if (weatherID == 501) weatherforecast_shortened = "mäßiger regen"; | |
}else{ | |
if (weatherID == 511) weatherforecast_shortened = "gefrierregen"; | |
}else{ | |
if (weatherID == 611) weatherforecast_shortened = "graupel"; | |
} | |
} | |
void fade(int led_position, uint16_t duration, uint16_t delay_val, uint16_t startR, uint16_t startG, uint16_t startB, uint16_t endR, uint16_t endG, uint16_t endB) { // Animation für die LEDS werden zugeteilt | |
int16_t redDiff = endR - startR; | |
int16_t greenDiff = endG - startG; | |
int16_t blueDiff = endB - startB; | |
int16_t steps = duration * 1000 / delay_val; | |
int16_t redValue, greenValue, blueValue; | |
for (int16_t i = 0 ; i < steps - 1 ; ++i) { | |
redValue = (int16_t)startR + (redDiff * i / steps); | |
greenValue = (int16_t)startG + (greenDiff * i / steps); | |
blueValue = (int16_t)startB + (blueDiff * i / steps); | |
leds[0] = CRGB(redValue, greenValue, blueValue); | |
FastLED.show(); | |
delay(delay_val); | |
} | |
leds[led_position] = CRGB(endR, endG, endB); | |
} | |
void LED_effect_clearSky() { // Effekt für Klaren Himmel | |
FastLED.setBrightness(255); | |
leds[0] = CRGB::Yellow; | |
FastLED.show(); | |
delay(500); | |
leds[0] = CRGB::Black; | |
FastLED.show(); | |
delay(500); | |
} | |
void LED_effect_cloudy() { // Effekt für Bewölkten Himmel | |
} | |
void LED_effect_thunderstorm() { // Effekt für Gewitter | |
FastLED.setBrightness(255); | |
leds[0] = CRGB::Blue; | |
FastLED.show(); | |
delay(500); | |
leds[0] = CRGB::Yellow; | |
FastLED.show(); | |
delay(500); | |
leds[0] = CRGB::Black; | |
FastLED.show(); | |
delay(500); | |
} | |
void LED_effect_drizzleRain() { // Effekt für Nieselregen | |
FastLED.setBrightness(200); | |
leds[0] = CRGB::Blue; | |
FastLED.show(); | |
delay(500); | |
leds[0] = CRGB::Black; | |
FastLED.show(); | |
delay(500); | |
} | |
void LED_effect_moderateRain() { // Effekt für mäßiger Regen | |
FastLED.setBrightness(100); | |
leds[0] = CRGB::Blue; | |
FastLED.show(); | |
delay(500); | |
leds[0] = CRGB::Black; | |
FastLED.show(); | |
delay(500); | |
} | |
void LED_effect_freezingRain() { // Effekt für Gefrierregen | |
FastLED.setBrightness(255); | |
leds[0] = CRGB::Blue; | |
FastLED.show(); | |
delay(500); | |
leds[0] = CRGB::Blue; | |
FastLED.show(); | |
delay(500); | |
leds[0] = CRGB::Black; | |
FastLED.show(); | |
delay(500); | |
} | |
void LED_effect_sleet() { // Effekt für Graupel | |
FastLED.setBrightness(100); | |
leds[0] = CRGB::Blue; | |
FastLED.show(); | |
delay(500); | |
leds[0] = CRGB::Green; | |
FastLED.show(); | |
delay(500); | |
leds[0] = CRGB::Black; | |
FastLED.show(); | |
delay(500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment