You seem to be reading the sensor more often, and perhaps more quickly, than you want to admit.
During setup -That's four - 4 - accesses to the sensor with no time delay anywhere.
Similarly, within the connection code -Again, four accesses to the sensor with no delay.
There is a comment in the library code -
I don't have such a sensor to test, but I've got 2 related suggestions.
For troubleshooting purposes forget the network handling code. Concentrate on getting reliable sensor data first.
Write a loop that reads the sensor data 10, 50, 100 times. Experiment first with 2 second delays between each access to the sensor, then increase or decrease it until you find the 'sweet spot' for reliability.
Then retro-fit what you have learnt into the network-aware version of your code.
During setup -
Code:
pin = Pin(0, Pin.OUT, Pin.PULL_DOWN)sensor = DHT11(pin)t = sensor.temperatureh = sensor.humidityprint("Temperature: {}".format(sensor.temperature))print("Humidity: {}".format(sensor.humidity))
Similarly, within the connection code -
Code:
else: #request = str(request) pin = Pin(0, Pin.OUT, Pin.PULL_DOWN) sensor = DHT11(pin) #time.sleep(3) t = sensor.temperature h = sensor.humidity print("Temperature: {}".format(sensor.temperature)) print("Humidity: {}".format(sensor.humidity)) response = html % (t, h) print(t, h)
There is a comment in the library code -
Code:
def measure(self): current_ticks = utime.ticks_us() if utime.ticks_diff(current_ticks, self._last_measure) < MIN_INTERVAL_US and ( self._temperature > -1 or self._humidity > -1 ): # Less than a second since last read, which is too soon according # to the datasheet return
I don't have such a sensor to test, but I've got 2 related suggestions.
For troubleshooting purposes forget the network handling code. Concentrate on getting reliable sensor data first.
Write a loop that reads the sensor data 10, 50, 100 times. Experiment first with 2 second delays between each access to the sensor, then increase or decrease it until you find the 'sweet spot' for reliability.
Then retro-fit what you have learnt into the network-aware version of your code.
Statistics: Posted by B.Goode — Tue Jan 30, 2024 5:52 pm