DHT11 reporting accurately then -5 in Spark.Publish

Is there anything that would cause a DHT11 to report accurately (as accurate as it can) then the next update cycle to be at -5. Does -5 mean no data? I have three functions pollDHT11, pollDS18B20 and publish. Is this a timing issue or a code problem? It showed up when I got the DS18B20 working but that is also the same time I broke everything out of the loop into functions that called based on the SampleInterval defined.
Sample Interval for DHT and DS18B20 are both 2 Seconds and the metricpublish rate is once per minute


void metricpush(){
  Serial.print("Metric Push!");
  //sprintf(strwatertemp, "Water Temperature: %2.2f Fahrenheit (Chip Name: %s)", waterfahrenheit, ds18b20.getChipName());
  sprintf(strairtemp, "Air Temp %2.2f, Water Temperature: %2.2f", (DHT.getFahrenheit()), waterfahrenheit);
  //Serial.print(strairtemp); //Output Publish String for Verification
  Spark.publish("tempdata", strairtemp, PRIVATE);
  MetricnextPublishTime = millis() + Metric_Publish_Rate;
  } //End of metricpush function

//Updates DHT11 Air Temp/Humidity Sensor Data
void getairtemp(){
  if (!bDHTstarted) {		// start the sample
      Serial.print("\n");
      Serial.print(n);
      Serial.print(": Retrieving information from sensor: ");
      DHT.acquire();
      bDHTstarted = true;
      airtemp = (DHT.getFahrenheit(), 2);
      } //End of DHTstarted IF

  if (!DHT.acquiring()) {		// has sample completed?

      // get DHT status
      int result = DHT.getStatus();

      Serial.print("Read sensor: ");
      switch (result) {
    case DHTLIB_OK:
        Serial.println("OK");
        break;
    case DHTLIB_ERROR_CHECKSUM:
        Serial.println("Error\n\r\tChecksum error");
        break;
    case DHTLIB_ERROR_ISR_TIMEOUT:
        Serial.println("Error\n\r\tISR time out error");
        break;
    case DHTLIB_ERROR_RESPONSE_TIMEOUT:
        Serial.println("Error\n\r\tResponse time out error");
        break;
    case DHTLIB_ERROR_DATA_TIMEOUT:
        Serial.println("Error\n\r\tData time out error");
        break;
    case DHTLIB_ERROR_ACQUIRING:
        Serial.println("Error\n\r\tAcquiring");
        break;
    case DHTLIB_ERROR_DELTA:
        Serial.println("Error\n\r\tDelta time to small");
        break;
    case DHTLIB_ERROR_NOTSTARTED:
        Serial.println("Error\n\r\tNot started");
        break;
    default:
        Serial.println("Unknown error");
        break;

      }

      Serial.print("Humidity (%): ");
      Serial.println(DHT.getHumidity(), 2);

      Serial.print("Temperature (oC): ");
      Serial.println(DHT.getCelsius(), 2);

      Serial.print("Air Temperature: ");
      Serial.println(DHT.getFahrenheit(), 2);

      Serial.print("Temperature (K): ");
      Serial.println(DHT.getKelvin(), 2);

      Serial.print("Dew Point (oC): ");
      Serial.println(DHT.getDewPoint());

      Serial.print("Dew Point Slow (oC): ");
      Serial.println(DHT.getDewPointSlow());

      n++;  // increment DHT Sample counter
      bDHTstarted = false;  // reset the sample flag so we can take another
      DHTnextSampleTime = millis() + DHT_SAMPLE_INTERVAL;  // set the time for next sample
      }  // End of Air Temp/Humidity
    } //End of getairtemp Function

//Updates DS18B20 Water Temp Sensor Data
void getwatertemp(){
    if(!ds18b20.search()){
      Serial.println("No more addresses.");
      Serial.println();
      ds18b20.resetsearch();
      watercelsius = ds18b20.getTemperature();
      waterfahrenheit = ds18b20.convertToFahrenheit(watercelsius);
      DS18B20nextSampleTime = millis() + DS18B20_SAMPLE_INTERVAL;
      Serial.print(watercelsius);
    }
  }//End of Water Temp Collection

@LukeUSMC, which DHT library are you using?

The PietteTech_DHT lib, I think you posted it. I solved the problem though. This line was the issue.

 airtemp = (DHT.getFahrenheit(), 2);

I pulled the ,2) off and am just formatting it to no decimal when I post it to the display and Spark.Publish.

Is that because it is declared as a float? I am VERY new C and code in general, I am an infrastructure “IT Guy” by trade and most of the “code” I have written has been in proprietary scripting languages like SYMCLI and PowerShell.

@LukeUSMC, the format you used is only applicable to a print() statement and will not work in that context as expected, if at all. :smile:

That makes sense, I have my first three pieces in place now and displaying on my OLED. Next I’ll be trying to adapt your webhook for pulling forecast data from open weather. All I want is the current day’s forecasted MaxTemp, MinTemp and windspeed.

1 Like

Oh and Thank You! Your posts and tutorials have proven to be invaluable as I’ve been cobbling together my own device.

1 Like

@LukeUSMC, I’m using that webhook on my RGBPongClock running on my new RGB Matrix Shield (shameless plug) that I have started selling (as a kit). I display a 7-day forecast from the webhook on a 16x32 RGB Matrix Panel. Lots of fun :stuck_out_tongue:

I am going to be borrowing from your code for sure, already pulled it from GitHub. To change what values are returned do I just modify the weather.json response template?

Yup! Then create your webhooks and off ya go!