ESP8266 contents of a string getting lost (comfortStatus)

This is only my second post so please excuse my obvious(but not to me) error.
There are no errors during compile


the value is correct in module DHT22.cpp
but when it goes to WebPages the content of comfortStatus is blank.
Also can you please tell me how to post blocks of code that look as nice as everybody else’s(I must be missing something again)

Sorry to use up your time.

DHT22.h

#ifndef DHT22_H
#define DHT22_H

#include <Arduino.h>
#include <DHTesp.h>
#include <Adafruit_BMP280.h>

extern int dhtPin;
extern int pressure_offset;
extern Adafruit_BMP280 bme;
extern float dht_tv, dht_hv, bmp_tv, bmp_pv, bmp_av;
extern float heatIndex;
extern float dewPoint;
extern float cr;
extern String comfortStatus;

void DHT22_Setup();
void DHT22_Get();
#endif


DHT22.cpp

#include <Arduino.h>
#include "DHT22.h"

int dhtPin = D3;
int pressure_offset = 1000;
float dht_tv, dht_hv, bmp_tv, bmp_pv, bmp_av;
float heatIndex = 0;
float dewPoint = 0;
float cr = 0;
String comfortStatus;

DHTesp dht;
Adafruit_BMP280 bme;

void DHT22_Setup()
{
    dht.setup(dhtPin, DHTesp::DHT22);
    while (!bme.begin())
    {
        Serial.println("Could not find a sensor, check wiring!");
    }
}

void DHT22_Get()
{
    for (int i = 1; i <= 20; i++)
    {
        dht_tv = dht_tv + dht.getTemperature();
        dht_hv = dht_hv + dht.getHumidity();
        bmp_tv = bmp_tv + bme.readTemperature();
        bmp_pv = bmp_pv + bme.readPressure() / 100.0F + pressure_offset;
        bmp_av = bmp_av + bme.readAltitude(1019.66);
        //Serial.println(dht_tv);
        if (isnan(dht_tv))
        { // if DHT in error put esp to sleep turn off power to DHT
            Serial.println("Going To Sleep Back in 15 Sec");
        }
    }

    dht_tv = dht_tv / 20;
    dht_hv = dht_hv / 20;

    bmp_tv = bmp_tv / 20;
    bmp_pv = bmp_pv / 20;
    bmp_av = bmp_av / 20;

    ComfortState cf;
    TempAndHumidity newValues = dht.getTempAndHumidity();

    float heatIndex = dht.computeHeatIndex(newValues.temperature, newValues.humidity);
    float dewPoint = dht.computeDewPoint(newValues.temperature, newValues.humidity);
    float cr = dht.getComfortRatio(cf, newValues.temperature, newValues.humidity);

    String comfortStatus;

    switch (cf)
    {
    case Comfort_OK:
        comfortStatus = "Comfort_OK";
        break;
    case Comfort_TooHot:
        comfortStatus = "Comfort_TooHot";
        break;
    case Comfort_TooCold:
        comfortStatus = "Comfort_TooCold";
        break;
    case Comfort_TooDry:
        comfortStatus = "Comfort_TooDry";
        break;
    case Comfort_TooHumid:
        comfortStatus = "Comfort_TooHumid";
        break;
    case Comfort_HotAndHumid:
        comfortStatus = "Comfort_HotAndHumid";
        break;
    case Comfort_HotAndDry:
        comfortStatus = "Comfort_HotAndDry";
        break;
    case Comfort_ColdAndHumid:
        comfortStatus = "Comfort_ColdAndHumid";
        break;
    case Comfort_ColdAndDry:
        comfortStatus = "Comfort_ColdAndDry";
        break;
    default:
        comfortStatus = "Unknown:";
        break;
    };
    
    Serial.println("");
    Serial.println(dht_tv);
    Serial.println(dht_hv);
    Serial.println(bmp_tv);
    Serial.println(bmp_pv);
    Serial.println(bmp_av);
    Serial.println(comfortStatus);
    Serial.println("");
}

WebPages.cpp

#include <Arduino.h>
#include "WebPages.h"
#include "Wifi_Setup.h"
#include "Power.h"
#include "TimeSetup.h"
#include "DHT22.h"

/////////////////////////////////  HTML BUTTON PRESSED  /////////////////////////////////////////////
void Web_Listen()
{
    server.handleClient();
}

//////////////////////////////////  HTML HEADER PAGE  ///////////////////////////////////////////////
void Header()
{
    server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
    server.sendHeader("Pragma", "no-cache");
    server.sendHeader("Connection", "Keep-Alive");
    server.sendHeader("Expires", "-1");
    server.setContentLength(CONTENT_LENGTH_UNKNOWN);
    server.send(200, "text/html", ""); // Empty content inhibits Content-length header
}

// For each new page you add or remove, make sure there is a menu item to call it or remove it when not used
void Site_Navbar()
{
    webpage += F(" <a href='/homepage'>Home</a>");
    //webpage +=F(" <a href='/chart'>Chart</a>");
    //webpage +=F(" <a href='/Gauge'>Gauge</a>");
    //webpage += F(" <a href='/dht'>DHT Reading</a>");
    webpage += F("</div>");
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void SendHTML_Content()
{
    server.sendContent(webpage);
    webpage = "";
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void SendHTML_Stop()
{
    server.sendContent("");
    server.client().stop(); // Stop is needed because no content length was sent
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void homepagePL()
{
    Header();
    //Site_Navbar();
    CallTime();
    webpage += "<p>This page was displayed on : " + String(Timet) + " Hr</p>";
    //homepage_uptime();
    homepage();

    SendHTML_Content(); // Send footer
    SendHTML_Stop();
    webpage = "";
}

void homepage()
{
    webpage += ("<!DOCTYPE html><html>");
    webpage += ("<head><meta name=\"viewport\"width=device-width, initial-scale=1\">");
    //webpage +=F("<html charset=UTF-8><head><meta http-equiv='refresh' content='60'");
    webpage += ("<link rel=\"icon\" href=\"data:,\">");
    webpage += ("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
    webpage += (".button { background-color: #4CAF50; border: 4px solid #4CAF50;; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; }");
    webpage += ("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
    // Web Page Heading
    webpage += ("</style></head>");
    webpage += ("<center><h2>Runtime: ");
    webpage += (Day);
    webpage += (" : ");
    webpage += (Hour);
    webpage += (" : ");
    webpage += (Min);
    webpage += (" : ");
    webpage += (Sec);
    webpage += ("</h2></center>");
    webpage += ("<center><h2></h3></center>");
    webpage += ("<form>");
    webpage += ("<center>");
    webpage += ("<html><head><meta http-equiv=\"Content-Language\" content=\"en-us\"><meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1252\";>");
    webpage += ("<title>Power Moniter 8266</title></head><body><p><b><font face=\"Arial Black\" size=\"6\" color=\"#0000FF\">Power Moniter 8266 </font>");
    webpage += ("</b></p>");
    webpage += ("<table border=\"4\" width=\"420\" bordercolorlight=\"#000000\" bordercolordark=\"#008000\" height=\"137\">");
    webpage += ("<tr><td style=\"border-style: solid; border-width: 1px\" width=\"370\" bgcolor=\"#CC6600\" align=\"center\">");

    webpage += ("<b><font face=\"Verdana\" size=\"2\" color=\"#FFFF99\">Device</font></b>");
    webpage += ("</td><td style=\"border-style: solid; border-width: 1px\" bgcolor=\"#CC6600\" align=\"center\">");
    webpage += ("<b><font face=\"Verdana\" size=\"2\" color=\"#FFFF99\">Voltage</font></b>");
    webpage += ("</td><td style=\"border-style: solid; border-width: 1px\" bgcolor=\"#CC6600\" align=\"center\">");

    webpage += ("<b><font face=\"Verdana\" size=\"2\" color=\"#FFFF99\">Current</font></b>");
    webpage += ("</td><td style=\"border-style: solid; border-width: 1px\" bgcolor=\"#CC6600\" align=\"center\">");
    webpage += ("<b><font face=\"Verdana\" size=\"2\" color=\"#FFFF99\">Power</font></td></tr></b>");
    webpage += ("<tr><td style=\"border-style: solid; border-width: 1px\" width=\"20% \" align=\"center\">");
    webpage += ("Bus Voltage_A  ");
    webpage += ("</font></a></td><td style=\"border-style: solid; border-width: 1px\" width=\"10%\" align=\"center\">");
    webpage += (loadvoltage_A);
    webpage += ("</font></a></td><td style=\"border-style: solid; border-width: 1px\" width=\"10%\" align=\"center\">");
    webpage += (current_mA_A);
    webpage += ("</font></a></td><td style=\"border-style: solid; border-width: 1px\" width=\"10%\" align=\"center\">");
    webpage += (power_mW_A);

    webpage += ("<tr><td style=\"border-style: solid; border-width: 1px\" width=\"20%\" align=\"center\">");
    webpage += ("<center>"
                "Bus Voltage_B   ");
    webpage += ("</font></a></td><td style=\"border-style: solid; border-width: 1px\" width=\"10%\" align=\"center\">");
    webpage += (loadvoltage_B);
    webpage += ("</font></a></td><td style=\"border-style: solid; border-width: 1px\"  width=\"10%\"align=\"center\">");
    webpage += (current_mA_B);
    webpage += ("</font></a></td><td style=\"border-style: solid; border-width: 1px\" width=\"10%\" align=\"center\">");
    webpage += (power_mW_B);
    //////////////////////////////////////////////////
    //webpage +=("<tr><td style=\"border-style: solid; border-width: 1px\" width=\"20%\" align=\"center\">");

    webpage += ("<tr><td style=\"border-style: solid; border-width: 1px\" width=\"370\" bgcolor=\"#CC6600\" align=\"center\">");
    webpage += ("<b><font face=\"Verdana\" size=\"2\" color=\"#FFFF99\">Device</font></b>");
    webpage += ("</td><td style=\"border-style: solid; border-width: 1px\" bgcolor=\"#CC6600\" align=\"center\">");
    webpage += ("<b><font face=\"Verdana\" size=\"2\" color=\"#FFFF99\">Temperature</font></b>");
    webpage += ("</td><td style=\"border-style: solid; border-width: 1px\" bgcolor=\"#CC6600\" align=\"center\">");

    webpage += ("<b><font face=\"Verdana\" size=\"2\" color=\"#FFFF99\">Humidity</font></b>");
    webpage += ("</td><td style=\"border-style: solid; border-width: 1px\" bgcolor=\"#CC6600\" align=\"center\">");
    webpage += ("<b><font face=\"Verdana\" size=\"2\" color=\"#FFFF99\">Comfort</font></td></tr></b>");
    webpage += ("<tr><td style=\"border-style: solid; border-width: 1px\" width=\"20% \" align=\"center\">");
    /////////////////////////////////////
    webpage += ("DHT22 ");
    webpage += ("</font></a></td><td style=\"border-style: solid; border-width: 1px\" width=\"10%\" align=\"center\">");
    webpage += (dht_tv);
    webpage += ("</font></a></td><td style=\"border-style: solid; border-width: 1px\" width=\"10%\" align=\"center\">");
    webpage += (dht_hv);

    **webpage += ("</font></a></td><td style=\"border-style: solid; border-width: 1px\" width=\"10%\" align=\"center\">");
    ****webpage += (comfortStatus);
    **Serial.println("*************");
    **Serial.println(comfortStatus);
    ****Serial.println("*************");
    **
     //////////////////////////////////////////////////
     //webpage +=("<tr><td style=\"border-style: solid; border-width: 1px\" width=\"20%\" align=\"center\">");

     webpage += ("<tr><td style=\"border-style: solid; border-width: 1px\" width=\"370\" bgcolor=\"#CC6600\" align=\"center\">");
    webpage += ("<b><font face=\"Verdana\" size=\"2\" color=\"#FFFF99\">Device</font></b>");
    webpage += ("</td><td style=\"border-style: solid; border-width: 1px\" bgcolor=\"#CC6600\" align=\"center\">");
    webpage += ("<b><font face=\"Verdana\" size=\"2\" color=\"#FFFF99\">Temperature</font></b>");
    webpage += ("</td><td style=\"border-style: solid; border-width: 1px\" bgcolor=\"#CC6600\" align=\"center\">");

    webpage += ("<b><font face=\"Verdana\" size=\"2\" color=\"#FFFF99\">Altitude</font></b>");
    webpage += ("</td><td style=\"border-style: solid; border-width: 1px\" bgcolor=\"#CC6600\" align=\"center\">");
    webpage += ("<b><font face=\"Verdana\" size=\"2\" color=\"#FFFF99\">Pressure</font></td></tr></b>");
    webpage += ("<tr><td style=\"border-style: solid; border-width: 1px\" width=\"20% \" align=\"center\">");
    /////////////////////////////////////
    webpage += ("BMP280 ");
    webpage += ("</font></a></td><td style=\"border-style: solid; border-width: 1px\" width=\"10%\" align=\"center\">");
    webpage += (bmp_tv);
    webpage += ("</font></a></td><td style=\"border-style: solid; border-width: 1px\" width=\"10%\" align=\"center\">");
    webpage += (bmp_av);
    webpage += ("</font></a></td><td style=\"border-style: solid; border-width: 1px\" width=\"10%\" align=\"center\">");
    webpage += (bmp_pv);
    webpage += ("</b></font></a></td></tr></table>");
    webpage += ("</center></form></body></html>");
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void TimeP()
{
    CallTime();
    webpage += F(" <p>");
    webpage += ("<td><b>" + String(Timet) + "</td>");
    webpage += F(" </p>");
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void homepage_uptime()
{
    //webpage += "<P class='style2'></p>";
    //webpage += "<p class='style2'>";
    //webpage += "</p>This is the server home page<br>";
    webpage += "<p>This page was displayed on : " + String(Timet) + " Hr</p>";
    String Uptime = (String(millis() / 1000 / 60 / 60)) + ":";
    Uptime += (((millis() / 1000 / 60 % 60) < 10) ? "0" + String(millis() / 1000 / 60 % 60) : String(millis() / 1000 / 60 % 60)) + ":";
    Uptime += ((millis() / 1000 % 60) < 10) ? "0" + String(millis() / 1000 % 60) : String(millis() / 1000 % 60);
    webpage += "<p>Uptime: " + Uptime + "</p>";
}

For code formatting, put triple backticks (```) before and after where you’re going to paste your code, and then paste it in.

In your DHT22_Get() function in DHT22.cpp, you’re re-declaring (that might not be the right word, but lets roll with that) heatIndex , dewPoint, cr and comfortStatus - i.e. there was no need to put float before the first three, and String before the last. I would not be surprise if the compiler is treating those variables as local variables to that function rather than shoving the values into the global variable. Remove String comfortStatus from that function, and remove float from the front of those other variables, and see if that fixes it.

Edit: Thinking that through a bit more, I don’t think that DHT22.h is right - it doesn’t need the extern references to the varaiables at all. Instead, they should be in your WebPage.cpp, as they are declared in your DHT22.cpp, but shared with WebPage.cpp.

Another handy thing since this is the ESP8266, is that it supports printf, so you can do nice things like

Serial.printf("\ndht_tv=%f, dht_hv=%f, bmp_tv=%f, bmp_pv=%f, bmp_av, comfortStatus=%s\n",dht_tv, dht_hv, bmp_tv, bmp_pv, bmp_av, comfortStatus.c_str());

instead of

    Serial.println("");
    Serial.println(dht_tv);
    Serial.println(dht_hv);
    Serial.println(bmp_tv);
    Serial.println(bmp_pv);
    Serial.println(bmp_av);
    Serial.println(comfortStatus);
    Serial.println("");

And if you want to do fancy things with the formatting, like restrict the floats to two decimal places, do %.2f instead of %f. :wink:

Thank you for your suggestions pfeerick.
Any pointers and suggestions are very welcome indeed, thank you for explaining.
unfortunately following your suggestion produces even more errors.
image

If you have any further thoughts I will be happy to try them.
Again thank you for your time.

1 Like

Sorry I forgot to show my changes

#include "DHT22.h"

int dhtPin=D3;
int pressure_offset=1000;
float dht_tv,dht_hv,bmp_tv,bmp_pv,bmp_av;
heatIndex=0;
dewPoint=0;
cr=0;
comfortStatus="";


DHTesp dht;
Adafruit_BMP280 bme;

void DHT22_Setup(){
  dht.setup(dhtPin, DHTesp:: DHT22);
  while (!bme.begin()) {
    Serial.println("Could not find a sensor, check wiring!");
  }
}

void DHT22_Get(){
      for(int i =1;i<=20;i++){

    dht_tv = dht_tv + dht.getTemperature();
    dht_hv = dht_hv + dht.getHumidity();
    bmp_tv = bmp_tv + bme.readTemperature();
    bmp_pv = bmp_pv + bme.readPressure()/100.0F + pressure_offset;
    bmp_av = bmp_av + bme.readAltitude(1019.66);

    //Serial.println(dht_tv);

    if(isnan(dht_tv)){    // if DHT in error put esp to sleep turn off power to DHT
      Serial.println("Going To Sleep Back in 15 Sec");
        
    }
  }
  dht_tv = dht_tv /20;
  dht_hv = dht_hv /20;
  bmp_tv = bmp_tv /20;
  bmp_pv = bmp_pv /20;
  bmp_av = bmp_av /20;
  ComfortState cf;
  TempAndHumidity newValues = dht.getTempAndHumidity();
  float heatIndex = dht.computeHeatIndex(newValues.temperature, newValues.humidity);
  float dewPoint = dht.computeDewPoint(newValues.temperature, newValues.humidity);
  float cr = dht.getComfortRatio(cf, newValues.temperature, newValues.humidity);

  String comfortStatus;
  switch(cf) {
    case Comfort_OK:
      comfortStatus = "Comfort_OK";
      break;
    case Comfort_TooHot:
      comfortStatus = "Comfort_TooHot";
      break;
    case Comfort_TooCold:
      comfortStatus = "Comfort_TooCold";
      break;
    case Comfort_TooDry:
      comfortStatus = "Comfort_TooDry";
      break;
    case Comfort_TooHumid:
      comfortStatus = "Comfort_TooHumid";
      break;
    case Comfort_HotAndHumid:
      comfortStatus = "Comfort_HotAndHumid";
      break;
    case Comfort_HotAndDry:
      comfortStatus = "Comfort_HotAndDry";
      break;
    case Comfort_ColdAndHumid:
      comfortStatus = "Comfort_ColdAndHumid";
      break;
    case Comfort_ColdAndDry:
      comfortStatus = "Comfort_ColdAndDry";
      break;
    default:
      comfortStatus = "Unknown:";
      break;
  };

  Serial.println("");
  Serial.println(dht_tv);
  Serial.println(dht_hv);
  Serial.println(bmp_tv);
  Serial.println(bmp_pv);
  Serial.println(bmp_av);
  Serial.println(comfortStatus);
  Serial.println("");
}
1 Like

Wrong lines… :open_mouth:

Up the top should be…

int dhtPin=D3;
int pressure_offset=1000;
float dht_tv,dht_hv,bmp_tv,bmp_pv,bmp_av;
float heatIndex=0;
float dewPoint=0;
float cr=0;
String comfortStatus="";

In your DHT22_Get() function

delete String confortStatus;
delete float from

  float heatIndex = dht.computeHeatIndex(newValues.temperature, newValues.humidity);
  float dewPoint = dht.computeDewPoint(newValues.temperature, newValues.humidity);
  float cr = dht.getComfortRatio(cf, newValues.temperature, newValues.humidity);

In other words, if you’ve declared the variables ‘globaly’ within a given c++ unit/file, don’t declare them again locally within a function.

Thank you again for your reply. Sometimes you stare so long your mind goes blank.

And indeed it has got rid of most of the errors except for comfortStatus now showing as having no effect


I thought it better if I place the whole code again. Just in case I had done something else wrong. Thank you again for your very quick reply and time.

#include "DHT22.h"

int dhtPin=D3;
int pressure_offset=1000;
float dht_tv,dht_hv,bmp_tv,bmp_pv,bmp_av;
float heatIndex=0;
float dewPoint=0;
float cr=0;
String comfortStatus="";


DHTesp dht;
Adafruit_BMP280 bme;

void DHT22_Setup(){
  dht.setup(dhtPin, DHTesp:: DHT22);
  while (!bme.begin()) {
    Serial.println("Could not find a sensor, check wiring!");
  }
}

void DHT22_Get(){
      for(int i =1;i<=20;i++){

    dht_tv = dht_tv + dht.getTemperature();
    dht_hv = dht_hv + dht.getHumidity();
    bmp_tv = bmp_tv + bme.readTemperature();
    bmp_pv = bmp_pv + bme.readPressure()/100.0F + pressure_offset;
    bmp_av = bmp_av + bme.readAltitude(1019.66);

    //Serial.println(dht_tv);

    if(isnan(dht_tv)){    // if DHT in error put esp to sleep turn off power to DHT
      Serial.println("Going To Sleep Back in 15 Sec");
        
    }
  }
  dht_tv = dht_tv /20;
  dht_hv = dht_hv /20;
  bmp_tv = bmp_tv /20;
  bmp_pv = bmp_pv /20;
  bmp_av = bmp_av /20;
  ComfortState cf;
  TempAndHumidity newValues = dht.getTempAndHumidity();
  heatIndex = dht.computeHeatIndex(newValues.temperature, newValues.humidity);
  dewPoint = dht.computeDewPoint(newValues.temperature, newValues.humidity);
  cr = dht.getComfortRatio(cf, newValues.temperature, newValues.humidity);

  comfortStatus;
  switch(cf) {
    case Comfort_OK:
      comfortStatus = "Comfort_OK";
      break;
    case Comfort_TooHot:
      comfortStatus = "Comfort_TooHot";
      break;
    case Comfort_TooCold:
      comfortStatus = "Comfort_TooCold";
      break;
    case Comfort_TooDry:
      comfortStatus = "Comfort_TooDry";
      break;
    case Comfort_TooHumid:
      comfortStatus = "Comfort_TooHumid";
      break;
    case Comfort_HotAndHumid:
      comfortStatus = "Comfort_HotAndHumid";
      break;
    case Comfort_HotAndDry:
      comfortStatus = "Comfort_HotAndDry";
      break;
    case Comfort_ColdAndHumid:
      comfortStatus = "Comfort_ColdAndHumid";
      break;
    case Comfort_ColdAndDry:
      comfortStatus = "Comfort_ColdAndDry";
      break;
    default:
      comfortStatus = "Unknown:";
      break;
  };

  Serial.println("");
  Serial.println(dht_tv);
  Serial.println(dht_hv);
  Serial.println(bmp_tv);
  Serial.println(bmp_pv);
  Serial.println(bmp_av);
  Serial.println(comfortStatus);
  Serial.println("");
}

Remove the whole thing @ line 50! :stuck_out_tongue: Getting warmer! :smiley:

Well done that seems to work, all I can say is thank you very much.
Think I need to start reading some more lessons feeling very much like an iceberg:

a little bit warmer.

Thank you again for your time effort and patience.

1 Like