Jump to content

NLCbanner2024.jpg.2478be509670e60c2d6efd04834b8b47.jpg

Gina

Beyond the Event Horizon
  • Posts

    45,326
  • Joined

  • Last visited

  • Days Won

    120

Posts posted by Gina

  1. To save keep going in and out to upload and test, I've arranged the code to work with the Serial Monitor but not needing the Serial Monitor.

    This is the result :-  First line is from the Serial.println, the second shows the String DateTime.

    1379106509_Screenshotfrom2020-08-0315-25-52.png.a6fb2e05e033b4a413742645db2f0ac0.png

    This is the code :-

    int year, month, day, hour, minute;
    void showLocalTime()
      {
      struct tm timeinfo;
      if(!getLocalTime(&timeinfo)){
        DateTime = "Failed to obtain time";
        return;}
        else {
          year = timeinfo.tm_year + 1900;
          month = timeinfo.tm_mon + 1;
          day = timeinfo.tm_mday;
          hour = timeinfo.tm_hour;
          minute = timeinfo.tm_min;
          DateTime = "Date & Time is ";
          DateTime += year;
          DateTime += "-";
          if (month < 10) {DateTime += "0";}
          DateTime += month;
          DateTime += "-";
          if (day < 10) {DateTime += "0";}
          DateTime += day;
          DateTime += "  ";
          if (hour < 10) {DateTime += "0";}
          DateTime += hour;
          DateTime += ":";
          if (minute < 10) {DateTime += "0";}
          DateTime += minute;
          }
      }

     

  2. Hmm... that uses an array of chars rather than a String type.  DateTime is declared as a String type.  This is not a simple as first thought!!

    I don't need any of the date/time in words - something like "2020-08-03 14:24" would be fine.  Or maybe "14:24 03/08/2020"  I use the first format in filenames and data logging.

  3. Still not right!  Seems there's something wrong with this code:-

    void showLocalTime()
      {
      struct tm timeinfo;
      if(!getLocalTime(&timeinfo)){
        DateTime = "Failed to obtain time";
        return;}
        else {DateTime = (&timeinfo, "%A, %B %d %Y %H:%M:%S");}
      }

    And yet this works with the Serial Monitor :-

    void printLocalTime()
    {
      struct tm timeinfo;
      if(!getLocalTime(&timeinfo)){
        Serial.println("Failed to obtain time");
        return;
      }
      Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
    }

    241214904_Screenshotfrom2020-08-0313-17-22.png.7ab3a8930bdeef7aa22ddd13618c8c11.png

    Anyone know what I'm doing wrong, please?

  4. Found the problem - me!!  Left some code out!!! :iamwithstupid:

    void loop() {
      server.handleClient();
      showLocalTime();         //    <-------------------------  Left this line out!!
      temperatureThen = temperatureNow;
      humidityThen = humidityNow;
      pressureThen = pressureNow;
      ReadBME();
      if (pressureNow > (pressureThen + 0.1)) {pressureDir = "Rising";}
        else if (pressureNow < (pressureThen - 0.1)) {pressureDir = "Falling";}
        else {pressureDir = "No Change";}
      scopeH = dht.readHumidity();
      scopeT = dht.readTemperature();
      delay(2000);
    }

     

  5. Keeping WiFi open fixed the EN problem but I'm still not getting the Date/Time - string is empty.

    Added HTML code for paragraph and title of the data in case the HTML was causing the problem and which I should have done anyway but it wasn't that.  Now I can see where the DateTime string should be but it isn't there.

      ptr +="<h1>Gina's ESP32 Weather Station</h1>\n";
      ptr +="<p>Date/Time: ";
      ptr +=DateTime;
      ptr +="</p>";
      ptr +="<h1>Outside Conditions</h1>\n";

    333473842_Screenshotfrom2020-08-0311-47-37.png.0f234ef82568317fb945d669ceb962de.png

  6. The sketch compiles and after upload works with the Serial Display but needed EN twice.  When installed in the obsy the WiFi wouldn't connect.  I have WiFi disconnecting after NTP then reconnecting - I think this may be the problem and don't think it's needed.

    Here's the code with SSID and PW removed.

    #include <DHT.h>
    
    #include <WiFi.h>
    #include <WebServer.h>
    #include <Wire.h>
    #include <Adafruit_Sensor.h>
    #include <Adafruit_BME280.h>
    #include "DHT.h"
    #include "time.h"
    
    const char* ntpServer = "pool.ntp.org";
    const long  gmtOffset_sec = 0;
    const int   daylightOffset_sec = 3600;
    
    
    #define SEALEVELPRESSURE_HPA (1013.25)
    #define DHTPIN 4     // Digital pin connected to the DHT sensor
    #define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
    
    
    void printLocalTime()
    {
      struct tm timeinfo;
      if(!getLocalTime(&timeinfo)){
        Serial.println("Failed to obtain time");
        return;
      }
      Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
    }
    
    Adafruit_BME280 bme;
    
    float temperature, humidity, pressure, altitude, temperatureNow, humidityNow, pressureNow, 
      temperatureThen, humidityThen, pressureThen, scopeT, scopeH;
    String pressureDir, DateTime;
    
    /*Put your SSID & Password*/
    const char* ssid = "--------";  // Enter SSID here
    const char* password = "--------";  //Enter Password here
    
    void showLocalTime()
    {
      struct tm timeinfo;
      if(!getLocalTime(&timeinfo)){
        Serial.println("Failed to obtain time");
        return;
      }
      DateTime = (&timeinfo, "%A, %B %d %Y %H:%M:%S");
    }
    
    WebServer server(80);             
    DHT dht(DHTPIN, DHTTYPE);
     
    void setup() {
      Serial.begin(115200);
      delay(100);
    
    //connect to WiFi for NTP
      Serial.printf("Connecting to %s ", ssid);
      WiFi.begin(ssid, password);
      while (WiFi.status() != WL_CONNECTED) {
          delay(500);
          Serial.print(".");
      }
      Serial.println(" CONNECTED");
      
      //init and get the time
      configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
      printLocalTime();
    
      //disconnect WiFi as it's no longer needed
      WiFi.disconnect(true);
      WiFi.mode(WIFI_OFF);
    // End of NTP setup
    
      bme.begin(0x76);   
    
      Serial.println("Connecting to ");
      Serial.println(ssid);
    
      //connect to your local wi-fi network
      WiFi.begin(ssid, password);
    
      //check wi-fi is connected to wi-fi network
      while (WiFi.status() != WL_CONNECTED) {
      delay(1000);
      Serial.print(".");
      }
      Serial.println("");
      Serial.println("WiFi connected..!");
      Serial.print("Got IP: ");  Serial.println(WiFi.localIP());
    
      server.on("/", handle_OnConnect);
      server.onNotFound(handle_NotFound);
    
      server.begin();
      Serial.println("HTTP server started");
      dht.begin();
    }
    void loop() {
      server.handleClient();
      temperatureThen = temperatureNow;
      humidityThen = humidityNow;
      pressureThen = pressureNow;
      ReadBME();
      if (pressureNow > (pressureThen + 0.1)) {pressureDir = "Rising";}
        else if (pressureNow < (pressureThen - 0.1)) {pressureDir = "Falling";}
        else {pressureDir = "No Change";}
      scopeH = dht.readHumidity();
      scopeT = dht.readTemperature();
      delay(2000);
    }
    
    void ReadBME() {
      temperatureNow = bme.readTemperature();
      humidityNow = bme.readHumidity();
      pressureNow = bme.readPressure() / 100.0F;
    }
    
    void handle_OnConnect() {
      temperature = temperatureNow;
      humidity = humidityNow;
      pressure = pressureNow;
      server.send(200, "text/html", SendHTML(temperature,humidity,pressure,altitude)); 
    }
    
    void handle_NotFound(){
      server.send(404, "text/plain", "Not found");
    }
    
    String SendHTML(float temperature,float humidity,float pressure,float altitude){
      String ptr = "<!DOCTYPE html> <html>\n";
      ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
      ptr +="<title>Gina's ESP32 Weather Station</title>\n";
      ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
      ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
      ptr +="p {font-size: 24px;color: #444444;margin-bottom: 10px;}\n";
      ptr +="</style>\n";
      ptr +="</head>\n";
      ptr +="<body>\n";
      ptr +="<div id=\"webpage\">\n";
      ptr +="<h1>Gina's ESP32 Weather Station</h1>\n";
      ptr +=DateTime;
      ptr +="<h1>Outside Conditions</h1>\n";
      ptr +="<p>Temperature: ";
      ptr +=temperature;
      ptr +="&deg;C</p>";
      ptr +="<p>Humidity: ";
      ptr +=humidity;
      ptr +="%</p>";
      ptr +="<p>Pressure: ";
      ptr +=pressure;
      ptr +="hPa</p>";
      ptr +="<p>Trend: ";
      ptr +=pressureDir;
    //  ptr +="m</p>";
      ptr +="<h1>Scope Room</h1>\n";
      ptr +="<p>Temperature: ";
      ptr +=scopeT;
      ptr +="&deg;C</p>";
      ptr +="<p>Humidity: ";
      ptr +=scopeH;
      ptr +="%</p>";
    
    
      ptr +="</div>\n";
      ptr +="</body>\n";
      ptr +="</html>\n";
      return ptr;
    }

     

  7. I was planning to use the RTC for two things, getting date and time and displaying it and to interrupt the ESP32 every minute (or 5) to read sensors and store the results from which pressure trends could be derived and maybe provide a log.  For the wind sensors there would be more processing - computing mean and gust wind speed and average wind direction.  I don't know how this could be done by reading NTP every few hours.  I guess the current date and time could be computed from the elapsed loop time interval count but this seems a complicated method.

  8. I don't know about NTP - I'll look for information.

    I have a BME280 outside in Stevenson screen with a 250mm ribbon cable from the ESP32 inside and the DHT22 inside (scope room).  I don't need two pressure readings.  Are you saying you would recommend using two BME280s for better reliability?  I take it then that there's no problem distinguishing which is which.

  9. Added the DHT22 code to the current sketch for the outside sensors to read conditions in the scope room.  Seem to be using up program storage space rather fast and this is only a small sketch!!  I'm rather worried that I may not have room for the more complicated time coded data processing with the RTC mocule.  Time will tell.

    Quote

    Sketch uses 732250 bytes (55%) of program storage space. Maximum is 1310720 bytes.
    Global variables use 40352 bytes (12%) of dynamic memory, leaving 287328 bytes for local variables. Maximum is 327680 bytes.

     

  10. I'm still wondering if magnet and reed switches is the best for sensing wind direction.  I'm having a problem getting the magnet to give the right response from the reed switches.  viz. to get a suitable overlap and not get 3 reed switches on at the same time.  For the Consensus Averaging to work properly I think the overlap representing 8 of the 16 directions needs to be about the same as the "one on" directions.  I'm going do do some more thinking about optical sensing.

  11. When I was looking for other things I came across several DHT22s but now I want one they've all gone into hiding!!  Ordered another, due to arrive tomorrow.  Of course, I don't know if the ones lying about would be any good - they're donkey's years old!

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. By using this site, you agree to our Terms of Use.