Aircoookie / WLED

Control WS2812B and many more types of digital RGB LEDs with an ESP8266 or ESP32 over WiFi!
https://kno.wled.ge
MIT License
14.91k stars 3.22k forks source link

Run weathersketch and wled sketch independently over an esp8266 #1248

Closed LAR92 closed 4 years ago

LAR92 commented 4 years ago

Hello, I'm completely new to the world of programming, I have a nodemcu esp8266 2m ws2812b led strip, a 1.3 inch OLED display and a bme280 sensor, I want to build an infinity mirror and have the led strip for it, I already have one open source sketch showing the weather data time date air pressure humidity air temperature, the esp8266 is connected to the sensor and the screen, this sketch runs perfectly, i want the wled to run independently of the weather sketch, my question is how i can do it, i had too already flashed on the esp8266 everything worked as you can see on the picture, but how can the esp8266 control both at the same time?

IMG_20201011_170627 1 IMG_20201011_170623 1

IMG_20201010_211910 1 IMG_20201010_211907 1 IMG_20201010_212104 1

WEATHERSKETCH:

/**The MIT License (MIT)

Copyright (c) 2018 by Daniel Eichhorn - ThingPulse

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

See more at https://thingpulse.com */

include

include

include

include

include

// time

include // time() ctime()

include <sys/time.h> // struct timeval

include // settimeofday_cb()

//#include "SSD1306Wire.h"

include "SH1106Wire.h"

include "OLEDDisplayUi.h"

include

include "OpenWeatherMapCurrent.h"

include "OpenWeatherMapForecast.h"

include "WeatherStationFonts.h"

include "WeatherStationImages.h"

// Create the Lightsensor instance

define BME_SCK 13

define BME_MISO 12

define BME_MOSI 11

define BME_CS 10

define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme; // I2C //DHTesp dht; /***

// WIFI const char WIFI_SSID = "FRITZ!Box 7490"; const char WIFI_PWD = "Password...";

String humi1; String temp1;

define TZ 1 // (utc+) TZ in hours

define DST_MN 60 // use 60mn for summer time in some countries

// Setup const int UPDATE_INTERVAL_SECS = 10 * 60; // Update every 20 minutes unsigned long delayTime; // Display Settings const int I2C_DISPLAY_ADDRESS = 0x3c;

if defined(ESP8266)

const int SDA_PIN = D4; const int SDC_PIN = D3; const int DH1=D5;

else

const int SDA_PIN = 4; //D3; const int SDC_PIN = 3; //D4; const int DH1=14;

endif

// OpenWeatherMap Settings // Sign up here to get an API key: // https://docs.thingpulse.com/how-tos/openweathermap-key/ String OPEN_WEATHER_MAP_APP_ID = "69c6d20603fda43b366a3ec07838137a"; / Go to https://openweathermap.org/find?q= and search for a location. Go through the result set and select the entry closest to the actual location you want to display data for. It'll be a URL like https://openweathermap.org/city/2657896. The number at the end is what you assign to the constant below. / String OPEN_WEATHER_MAP_LOCATION_ID = "2953528";

// Pick a language code from this list: // Arabic - ar, Bulgarian - bg, Catalan - ca, Czech - cz, German - de, Greek - el, // English - en, Persian (Farsi) - fa, Finnish - fi, French - fr, Galician - gl, // Croatian - hr, Hungarian - hu, Italian - it, Japanese - ja, Korean - kr, // Latvian - la, Lithuanian - lt, Macedonian - mk, Dutch - nl, Polish - pl, // Portuguese - pt, Romanian - ro, Russian - ru, Swedish - se, Slovak - sk, // Slovenian - sl, Spanish - es, Turkish - tr, Ukrainian - ua, Vietnamese - vi, // Chinese Simplified - zh_cn, Chinese Traditional - zh_tw. String OPEN_WEATHER_MAP_LANGUAGE = "de"; const uint8_t MAX_FORECASTS = 4;

const boolean IS_METRIC = true;

// Adjust according to your language const String WDAY_NAMES[] = {"SON.", "MON.", "DIE.", "MIT.", "DON.", "FRE.", "SAM."}; const String MONTH_NAMES[] = {"JAN", "FEB", "MÄRZ", "APR", "MAI", "JUN", "JUL", "AUG", "SEPT", "OKT", "NOV", "DEZ"};

/***

OpenWeatherMapCurrentData currentWeather; OpenWeatherMapCurrent currentWeatherClient;

OpenWeatherMapForecastData forecasts[MAX_FORECASTS]; OpenWeatherMapForecast forecastClient;

define TZ_MN ((TZ)*60)

define TZ_SEC ((TZ)*3600)

define DST_SEC ((DST_MN)*60)

time_t now;

// flag changed in the ticker function every 10 minutes bool readyForWeatherUpdate = false;

String lastUpdate = "--";

long timeSinceLastWUpdate = 0;

//declaring prototypes void drawProgress(OLEDDisplay display, int percentage, String label); void updateData(OLEDDisplay display); void drawBME(OLEDDisplay display,OLEDDisplayUiState state,int16_t x, int16_t y); void drawDateTime(OLEDDisplay display, OLEDDisplayUiState state, int16_t x, int16_t y); void drawCurrentWeather(OLEDDisplay display, OLEDDisplayUiState state, int16_t x, int16_t y); void drawForecast(OLEDDisplay display, OLEDDisplayUiState state, int16_t x, int16_t y); void drawForecastDetails(OLEDDisplay display, int x, int y, int dayIndex); void drawHeaderOverlay(OLEDDisplay display, OLEDDisplayUiState* state); void setReadyForWeatherUpdate();

// Add frames // this array keeps function pointers to all frames // frames are the single views that slide from right to left FrameCallback frames[] = { drawDateTime, drawCurrentWeather, drawForecast, drawBME}; int numberOfFrames = 4;

OverlayCallback overlays[] = { drawHeaderOverlay }; int numberOfOverlays = 1;

void setup() { Serial.begin(115200); Serial.println(); Serial.println(F("BME280 test")); bool status; status = bme.begin(0x76);
if (!status) { Serial.println("Could not find a valid BME280 sensor, check wiring!"); while (1); Serial.println("-- Default Test --"); delayTime = 1000;

Serial.println();
}

// initialize dispaly display.init(); display.clear(); display.display();

//display.flipScreenVertically(); display.setFont(ArialMT_Plain_10); display.setTextAlignment(TEXT_ALIGN_CENTER); display.setContrast(255); WiFi.begin(WIFI_SSID, WIFI_PWD); int counter = 0; while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); display.clear(); display.drawString(64, 10, "Verbindung zu WiFi"); display.drawXbm(40, 30, 8, 8, counter % 3 == 0 ? activeSymbole : inactiveSymbole); display.drawXbm(54, 30, 8, 8, counter % 3 == 1 ? activeSymbole : inactiveSymbole); display.drawXbm(68, 30, 8, 8, counter % 3 == 2 ? activeSymbole : inactiveSymbole); display.display();

counter++;

} // Get time from network time service configTime(TZ_SEC, DST_SEC, "pool.ntp.org");

ui.setTargetFPS(30);

ui.setActiveSymbol(activeSymbole); ui.setInactiveSymbol(inactiveSymbole);

// You can change this to // TOP, LEFT, BOTTOM, RIGHT ui.setIndicatorPosition(BOTTOM);

// Defines where the first frame is located in the bar. ui.setIndicatorDirection(LEFT_RIGHT);

// You can change the transition that is used // SLIDE_LEFT, SLIDE_RIGHT, SLIDE_TOP, SLIDE_DOWN ui.setFrameAnimation(SLIDE_LEFT);

ui.setFrames(frames, numberOfFrames);

ui.setOverlays(overlays, numberOfOverlays);

// Inital UI takes care of initalising the display too. ui.init();

Serial.println("");

updateData(&display);

}

void loop() { if (millis() - timeSinceLastWUpdate > (1000L*UPDATE_INTERVAL_SECS)) { setReadyForWeatherUpdate(); timeSinceLastWUpdate = millis(); }

if (readyForWeatherUpdate && ui.getUiState()->frameState == FIXED) { updateData(&display); }

int remainingTimeBudget = ui.update();

if (remainingTimeBudget > 0) { // You can do some work here // Don't do stuff if you are below your // time budget. delay(remainingTimeBudget); }

}

void drawProgress(OLEDDisplay *display, int percentage, String label) { display->clear(); display->setTextAlignment(TEXT_ALIGN_CENTER); display->setFont(ArialMT_Plain_10); display->drawString(64, 10, label); display->drawProgressBar(2, 28, 124, 10, percentage); display->display(); }

void updateData(OLEDDisplay *display) { drawProgress(display, 10, "Aktualisiere Zeit..."); drawProgress(display, 30, "Aktualisiere Wetter..."); currentWeatherClient.setMetric(IS_METRIC); currentWeatherClient.setLanguage(OPEN_WEATHER_MAP_LANGUAGE); currentWeatherClient.updateCurrentById(&currentWeather, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION_ID); drawProgress(display, 50, "Aktualisiere Prognosen..."); forecastClient.setMetric(IS_METRIC); forecastClient.setLanguage(OPEN_WEATHER_MAP_LANGUAGE); uint8_t allowedHours[] = {12}; forecastClient.setAllowedHours(allowedHours, sizeof(allowedHours)); forecastClient.updateForecastsById(forecasts, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION_ID, MAX_FORECASTS);

readyForWeatherUpdate = false; drawProgress(display, 100, "Fertig..."); delay(1000); }

void drawBME(OLEDDisplay display,OLEDDisplayUiState state,int16_t x, int16_t y){

float temp1=bme.readTemperature(); float pres1=bme.readPressure()/100.0F; float humi1=bme.readHumidity(); delay(delayTime); display->setTextAlignment(TEXT_ALIGN_CENTER); display->setFont(ArialMT_Plain_16); String humi=(IS_METRIC ? "H:" : "H:")+String(humi1, 1)+(IS_METRIC ? "%" : "%"); display->drawString(64+x, y, humi); display->setTextAlignment(TEXT_ALIGN_CENTER); display->setFont(ArialMT_Plain_16); String temp=(IS_METRIC ? " T:" : "T:")+String(temp1, 1)+(IS_METRIC ? "°C" : "°C"); display->drawString(64+x, 15+y, temp); display->setTextAlignment(TEXT_ALIGN_CENTER); display->setFont(ArialMT_Plain_16); String pres=(IS_METRIC ? " P:" : "P:")+String(pres1, 1)+(IS_METRIC ? "hPa" : "hPa"); display->drawString(64+x, 30+y, pres); }

void drawDateTime(OLEDDisplay display, OLEDDisplayUiState state, int16_t x, int16_t y) { now = time(nullptr); struct tm* timeInfo; timeInfo = localtime(&now); char buff[16];

display->setTextAlignment(TEXT_ALIGN_CENTER); display->setFont(ArialMT_Plain_10); String date = WDAY_NAMES[timeInfo->tm_wday];

sprintf_P(buff, PSTR("%s, %02d/%02d/%04d"), WDAY_NAMES[timeInfo->tm_wday].c_str(), timeInfo->tm_mday, timeInfo->tm_mon+1, timeInfo->tm_year + 1900); display->drawString(64 + x, 5 + y, String(buff)); display->setFont(ArialMT_Plain_24);

sprintf_P(buff, PSTR("%02d:%02d:%02d"), timeInfo->tm_hour, timeInfo->tm_min, timeInfo->tm_sec); display->drawString(64 + x, 15 + y, String(buff)); display->setTextAlignment(TEXT_ALIGN_LEFT); }

void drawCurrentWeather(OLEDDisplay display, OLEDDisplayUiState state, int16_t x, int16_t y) { display->setFont(ArialMT_Plain_10); display->setTextAlignment(TEXT_ALIGN_CENTER); display->drawString(64 + x, 38 + y, currentWeather.description);

display->setFont(ArialMT_Plain_24); display->setTextAlignment(TEXT_ALIGN_LEFT); String temp = String(currentWeather.temp, 1) + (IS_METRIC ? "C" : "C"); display->drawString(60 + x, 5 + y, temp);

display->setFont(Meteocons_Plain_36); display->setTextAlignment(TEXT_ALIGN_CENTER); display->drawString(32 + x, 0 + y, currentWeather.iconMeteoCon); }

void drawForecast(OLEDDisplay display, OLEDDisplayUiState state, int16_t x, int16_t y) { drawForecastDetails(display, x, y, 0); drawForecastDetails(display, x + 44, y, 1); drawForecastDetails(display, x + 88, y, 2); }

void drawForecastDetails(OLEDDisplay display, int x, int y, int dayIndex) { time_t observationTimestamp = forecasts[dayIndex].observationTime; struct tm timeInfo; timeInfo = localtime(&observationTimestamp); display->setTextAlignment(TEXT_ALIGN_CENTER); display->setFont(ArialMT_Plain_10); display->drawString(x + 20, y, WDAY_NAMES[timeInfo->tm_wday]);

display->setFont(Meteocons_Plain_21); display->drawString(x + 20, y + 12, forecasts[dayIndex].iconMeteoCon); String temp = String(forecasts[dayIndex].temp, 0) + (IS_METRIC ? "°C" : "°C"); display->setFont(ArialMT_Plain_10); display->drawString(x + 20, y + 34, temp); display->setTextAlignment(TEXT_ALIGN_LEFT); }

void drawHeaderOverlay(OLEDDisplay display, OLEDDisplayUiState state) { now = time(nullptr); struct tm* timeInfo; timeInfo = localtime(&now); char buff[14]; sprintf_P(buff, PSTR("%02d:%02d"), timeInfo->tm_hour, timeInfo->tm_min);

display->setColor(WHITE); display->setFont(ArialMT_Plain_10); display->setTextAlignment(TEXT_ALIGN_LEFT); display->drawString(0, 54, String(buff)); display->setTextAlignment(TEXT_ALIGN_RIGHT); String temp = String(currentWeather.temp, 1) + (IS_METRIC ? "°C" : "°C"); display->drawString(128, 54, temp); display->drawHorizontalLine(0, 52, 128); }

void setReadyForWeatherUpdate() { Serial.println("Setting readyForUpdate to true"); readyForWeatherUpdate = true; }

Aircoookie commented 4 years ago

Hi! Just judging from the hardware, it should be possible to run this sketch alongside WLED. This would be possible by converting it to a WLED usermod. Here is an example usermod for WLED to give you an idea. Making an usermod is quite doable, but not trivial. Keep in mind to

Hope it helps! If this is too much work, I would consider using a second esp8266 for WLED, even if you want the light in the same location.

LAR92 commented 4 years ago

Ok thanks, I'll try it, I'll report how it went.

LAR92 commented 4 years ago

When I check the sketch I get the message:

Arduino: 1.8.13 (Windows 10), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, Legacy (new can return nullptr), All SSL ciphers (most compatible), 4MB (FS:2MB OTA:~1019KB), 2, v2 Lower Memory, Disabled, None, Only Sketch, 115200"

In file included from C:\Users\Lauritz\Downloads\WLED-0.10.2\WLED-0.10.2\wled00\wled00.ino:13:0:

wled.h:49:27: fatal error: ESPAsyncTCP.h: No such file or directory

include

                       ^

compilation terminated.

exit status 1

ESPAsyncTCP.h: No such file or directory

I'm not sure if I did everything right.

LAR92 commented 4 years ago

I have ordered a second esp8266 and will operate the weather station and wled independently.

Aircoookie commented 4 years ago

Hi, sorry for not getting back to you earlier! That's the best solution IMO, now you won't have to mess with one system if the other breaks or if you want to update it.

In case you wondered, that compilation error means that the ESPAsyncTCP.h library is missing. You'd need to install all the libraries listed in the main page of the WLED wiki to compile it successfully. I've come to recommend using VS code with platform.io since it saves you that hassle :)

LAR92 commented 4 years ago

Ok thanks 👍