#include #include #include #include #include "DHT.h" #include #include #include "proj_conf.h" #define LED 2 // On board LED #define DHTTYPE DHT22 // DHT 11 uint8_t DHTPin = 12; DHT dht(DHTPin, DHTTYPE); const char *ntpServer = "pool.ntp.org"; const long gmtOffset_sec = 3600; // Replace with your GMT offset (seconds) const int daylightOffset_sec = 0; // Replace with your daylight offset (seconds) // Generally, you should use "unsigned long" for variables that hold time // The value will quickly become too large for an int to store unsigned long previousMillis = 0; // constants won't change : float time_till_update = 10; // define device I2C address: 0x76 or 0x77 (0x77 is library default address) #define BMP280_I2C_ADDRESS 0x76 // initialize Adafruit BMP280 library Adafruit_BMP280 bmp280; float humidity, temperature, temperature_2, pressure; int currZambretti = 26; ESP8266WebServer server(80); // Server on port 80 const char MAIN_page[] PROGMEM = R"=====( Weather

Some Weather

NodeMCU Data Logger

Time Temperature (°C) Temperature 2 (°C) Humidity (%) Pressure (hPa) Zambretti next update in (m)


)====="; void handleRoot() { String s = MAIN_page; // Read HTML contents server.send(200, "text/html", s); // Send web page } void readData() { String data = "{\"Temperature\":\"" + String(temperature) + "\", \"Humidity\":\"" + String(humidity) + "\", \"Temperature_2\":\"" + String(temperature_2) + "\", \"Pressure\":\"" + String(pressure) + "\", \"forcast_num\": " + String(currZambretti) + ", \"time_till_update\": " + String(time_till_update) + " }"; digitalWrite(LED, !digitalRead(LED)); // Toggle LED on data request ajax server.send(200, "text/plane", data); // Send ADC value, temperature and humidity JSON to client ajax request delay(2000); temperature = (dht.readTemperature()); humidity = dht.readHumidity(); // read temperature and pressure from BMP280 sensor temperature_2 = (bmp280.readTemperature()); // get temperature pressure = (bmp280.readPressure() / 100); // get pressure Serial.println("T1: " + String(temperature) + " T2:" + String(temperature_2) + " H:" + String(humidity) + " P: " + String(pressure) + " Z: " + String(currZambretti) + " dT: " + String(time_till_update)); } void setup() { Serial.begin(115200); Serial.println(); pinMode(DHTPin, INPUT); dht.begin(); // initialize the BMP280 sensor // Wire.begin(D1, D2); // set I2C pins [SDA = D2, SCL = D1], default clock is 100kHz // Wire.begin(4, 0); Wire.begin(); while (!bmp280.begin(BMP280_I2C_ADDRESS)) { Serial.println("BPM280 put"); delay(1000); } WiFi.begin(ssid, password); // Connect to your WiFi router Serial.println(""); // Onboard LED port Direction output pinMode(LED, OUTPUT); // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } // If connection successful show IP address in serial monitor Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); // IP address assigned to your ESP server.on("/", handleRoot); // Which routine to handle at root location. This is display page server.on("/readData", readData); // This page is called by java Script AJAX server.begin(); // Start server configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); Serial.println("HTTP server started"); } void loop(void) { server.handleClient(); // Handle client requests unsigned long currentMillis = millis(); time_till_update = ((interval - (currentMillis - previousMillis)) / 60000.0); if (currentMillis - previousMillis >= interval) { // save the last time you blinked the LED previousMillis = currentMillis; zembretti_number_calc(); } } ///////////////////////////////////////// // WEATHER PREDICTION ///////////////////////////////////////// int rise_options[] = {25, 25, 25, 24, 24, 19, 16, 12, 11, 9, 8, 6, 5, 2, 1, 1, 0, 0, 0, 0, 0, 0}; int steady_options[] = {25, 25, 25, 25, 25, 25, 23, 23, 22, 18, 15, 13, 10, 4, 1, 1, 0, 0, 0, 0, 0, 0}; int fall_options[] = {25, 25, 25, 25, 25, 25, 25, 25, 23, 23, 21, 20, 17, 14, 7, 3, 1, 1, 1, 0, 0, 0}; float prev_pressure = 0; void zembretti_number_calc(){ Serial.println("Calc new Zembretti"); float curr_pressure = (bmp280.readPressure() / 100); // get pressure time_t rawtime; struct tm *timeinfo; time(&rawtime); timeinfo = localtime(&rawtime); int mon = timeinfo->tm_mon; float pressureRange = (maxAvgPressure - minAvgPressure); float calculus = (float) curr_pressure; int isRising = 0; // calc if pressure is rising or not, default is equal if (curr_pressure > prev_pressure) isRising = 1; else if (prev_pressure > curr_pressure) isRising = -1; if (isNorthern){ // no wind data so ignore calculation for it if ( mon >= 4 && mon <= 9 ) {// if Summer if (isRising == 1) { calculus += (7 / 100) * pressureRange; } else if (isRising == -1) { calculus -= (7 / 100) * pressureRange; } } } else { // no wind data so ignore calculation for it if ( mon < 4 || mon > 9 ) {// if Summer if (isRising == 1) { calculus += (7 / 100) * pressureRange; } else if (isRising == -1) { calculus -= (7 / 100) * pressureRange; } } } calculus = (curr_pressure - minAvgPressure) / (pressureRange / 22); prev_pressure = curr_pressure; // non expectet values if (calculus < 0){ currZambretti = 26; return; } if (calculus >= 22) { currZambretti = 26; return; } if(isRising > 0) currZambretti = rise_options[(int) calculus]; else if (isRising < 0) currZambretti = fall_options[(int) calculus]; else currZambretti = steady_options[(int) calculus]; return; }