Skip to content

100% nan when using Ticker #103

Closed
Closed
@TinkerFu

Description

@TinkerFu

This is on an esp8266.

Went from polling sensor every 30 seconds via delay(30000) to using a Ticker. 100% failure.
I've even removed all wifi code down to just a ticker.

#include <DHT.h>
//#include <ESP8266WiFi.h>
//#include <ESP8266HTTPClient.h>
#include <Ticker.h>

#define DHTTYPE DHT22   // DHT 11
const int DHTPin = 2;

DHT dht(DHTPin, DHTTYPE);
Ticker sensor1;

void setup () {
 
  Serial.begin(115200);
  delay(10);
  sensor1.attach(30.0, getReadings);
}
 
void loop() {
}

void getReadings() {
      dht.begin();
      delay(50);
      float h = dht.readHumidity();
      float f = dht.readTemperature(true);

      if (isnan(h) || isnan(f)) {
        Serial.println("Failed to read from Sensor!");      
      }
      else{
        Serial.print("Humidity: ");
        Serial.print(h);
        Serial.print(" %\t Temperature: ");
        Serial.print(f);             
        Serial.println(" *F");
      }

}

I enabled debug:

    Max clock cycles: 80000
    Timeout waiting for start signal low pulse.

I've used this #48 as a reference but even after applying fix it still fails every time. Relevant section of my DHT.cpp

    // End the start signal by setting data line high for 40 microseconds.
    //digitalWrite(_pin, HIGH);
    //delayMicroseconds(40);

    // Now start reading the data line to get the value from the DHT sensor.
    pinMode(_pin, INPUT_PULLUP);
    delayMicroseconds(50);  // Delay a bit to let sensor pull data line low.

Same board works fine if the read code is put in main loop.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions