Skip to content

Instantly share code, notes, and snippets.

@ServOKio
Created January 28, 2024 09:22
Show Gist options
  • Save ServOKio/429d105a74b53051457d06bdc4ada69a to your computer and use it in GitHub Desktop.
Save ServOKio/429d105a74b53051457d06bdc4ada69a to your computer and use it in GitHub Desktop.
ESP Weather A-LED
#define LED_PIN 0
#define LED_NUM 8
#include "FastLED.h"
#include <ESP8266TrueRandom.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "WiFi-sssd";
const char* password = "-";
const String server = "api.openweathermap.org";
const String openWeatherMapApiKey = "key";
const String city = "City";
const String countryCode = "RU";
const String appid = "wtf";
const String url = "http://"+server+"/data/2.5/weather?q=" + city + "," + countryCode + "&APPID=" + openWeatherMapApiKey;
unsigned long lastConnectionTime = 0;
unsigned long postingInterval = 0;
String httpData;
struct weather_structure {
unsigned int id;
const char* main;
const char* icon;
const char* descript;
float temp;
float pressure;
byte humidity;
float speed;
float deg;
};
weather_structure weather;
CRGB leds[LED_NUM];
//visual
//clouds
int bri[LED_NUM];
int active = 0;
bool ub = false;
//strice
int strice_led = 4;
//bool
bool strice_active = false;
bool strice_on = false;
bool strice_pro = false;
unsigned long last_strice = 0;
//mist
bool mist_active = false;
unsigned long last_mist = 0;
void setup() {
//pre
for(int i=0;i<LED_NUM;i++) bri[i] = 0;
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, LED_NUM);
// Wifi
Serial.begin(115200);
Serial.print("\nConnecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected\nIP address: ");
Serial.println(WiFi.localIP());
Serial.println();
}
void loop(){
//clouds
for (int i = 0; i < LED_NUM; i++) {
leds[i] = mist_active && active != i ? CHSV(0,0,30) : CHSV(0,0,bri[i]);
}
//mist
bool mc = mist_active ? false : true;
if (millis() - last_mist > 10L) {
mc = true;
last_mist = millis();
}
if(!mist_active and random(0,50) == random(0,50)) active = random(0,LED_NUM-1);
if(ub){
if(bri[active] > (strice_active ? 1 : (mist_active ? 30 : 100))){
if(mc) bri[active]--;
} else {
ub = !ub;
if(mist_active and random(0,2) == random(0,2)) active = random(0,LED_NUM-1);
}
} else {
if(bri[active] < (strice_active ? 10 : 255)){
if(mc) bri[active]++;
} else ub = !ub;
}
//strice
if(strice_active){
if(strice_on){
if (millis() - last_strice > 40L) {
if(random(0,2) == random(0,2)) strice_pro = !strice_pro;
leds[strice_led] = CHSV(213, 128, strice_pro ? 0 : 255);
last_strice = millis();
if(random(0,20) == random(0,20)){
leds[strice_led] = CHSV(0, 0, 0);
strice_on = !strice_on;
}
}
if(random(0,100) == random(0,100)){
leds[strice_led] = CHSV(0, 0, 0);
strice_led = random(0,LED_NUM-1);
}
} else if(random(0,500) == random(0,500)) strice_on = !strice_on;
}
FastLED.show();
// delay(1000);
if (WiFi.status() == WL_CONNECTED) {
if (millis() < lastConnectionTime) lastConnectionTime = 0;
if (millis() - lastConnectionTime > postingInterval or lastConnectionTime == 0) {
if (httpRequest() and parseData()) {
Serial.println("\nWeather");
Serial.printf("id: %d\n", weather.id);
Serial.printf("main: %s\n", weather.main);
Serial.printf("description: %s\n", weather.descript);
Serial.printf("icon: %s\n", weather.icon);
Serial.printf("temp: %d celsius\n", round(weather.temp));
Serial.printf("humidity: %d %\n", round(weather.humidity));
Serial.printf("pressure: %d hPa or %d mmHg\n", round(weather.pressure), round(weather.pressure * 0.75));
Serial.printf("wind's speed: %d\n", round(weather.speed));
Serial.printf("wind's direction: %d\n", round(weather.deg));
Serial.println();
String myString = weather.icon;
myString.replace("d", "");
int val = myString.toInt();
switch (val) {
case 1:
case 2:
case 3:
case 4:
case 9:
case 10:
case 13: //clear sky
strice_active = mist_active = false;
break;
case 11:
strice_active = true;
mist_active = false;
break;
case 50:
strice_active = false;
mist_active = true;
break;
default:
// if nothing else matches, do the default
// default is optional
break;
}
}
}
}
}
bool httpRequest() {
WiFiClient client;
HTTPClient http;
bool find = false;
//client.setTimeout(1000);
Serial.print("Connecting ");
http.begin(client, url);
int httpCode = http.GET();
if (httpCode > 0) {
Serial.printf("successfully, code: %d\n", httpCode);
if (httpCode == HTTP_CODE_OK) {
httpData = http.getString();
if (httpData.indexOf(F("\"main\":{\"temp\":")) > -1) {
lastConnectionTime = millis();
find = true;
}
else Serial.println("Failed, json string is not found");
}
}
else Serial.printf("failed, error: %s\n", httpCode);
postingInterval = find ? 600L * 1000L : 60L * 1000L;
http.end();
return find;
}
bool parseData() {
Serial.println(httpData);
DynamicJsonDocument root(1024);
DeserializationError error = deserializeJson(root, httpData);
if (error) {
Serial.println("Json parsing failed!");
return false;
}
weather.id = root["weather"][0]["id"];
weather.main = root["weather"][0]["main"];
weather.descript = root["weather"][0]["description"];
weather.icon = root["weather"][0]["icon"];
weather.temp = root["main"]["temp"];
weather.humidity = root["main"]["humidity"];
weather.pressure = root["main"]["pressure"];
weather.speed = root["wind"]["speed"];
weather.deg = root["wind"]["deg"];
httpData = "";
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment