My python code for interfacing with a DHT11 works fine, but not the C++ "equivalent". I've revised it for hours and run it through ChatGPT countless times to play with the timings, still with readings failing. I cannot see any functional difference between the two sets of code. The error is "Data not good, insufficient bits read". When printing out the readings it does make, it's usually all just 1s.
Code:
import arrayimport timeimport boardfrom adafruit_blinka.microcontroller.bcm283x.pulseio.PulseIn import PulseInclass customDHT11: def __init__(self): self.pulse_in = PulseIn(board.D17, 81, idle_state=True) self.pulse_in.pause() def exit(self) -> None: self.pulse_in.deinit() def _pulses_to_binary(self, pulses: array.array, start: int, stop: int) -> int: binary = 0 hi_sig = False for bit_inx in range(start, stop): if hi_sig: bit = 0 if pulses[bit_inx] > 51: bit = 1 binary = binary << 1 | bit hi_sig = not hi_sig return binary def measure(self) -> None: self.pulse_in.clear() self.pulse_in.resume(18000) # low for 18ms time.sleep(0.25) self.pulse_in.pause() pulses = array.array("H") while self.pulse_in: pulses.append(self.pulse_in.popleft()) if len(pulses) < 80: raise RuntimeError("A full buffer was not returned. Try again.") buf = array.array("B") for byte_start in range(0, 80, 16): buf.append(self._pulses_to_binary(pulses, byte_start, byte_start + 16)) if (buf[0] + buf[1] + buf[2] + buf[3]) & 0xFF != buf[4]: raise RuntimeError("Checksum did not validate. Try again.") print("Temp={0}ºC, Humidity={1}%".format(buf[2], buf[0]))
Code:
import timeimport customDHT11sensor = customDHT11.customDHT11()while True: try: sensor.measure() except RuntimeError as error: print(error.args[0]) time.sleep(2) continue except Exception as error: sensor.exit() raise error time.sleep(2)
Code:
#include <wiringPi.h>#include <stdio.h>#include <stdlib.h>#include <stdint.h>#define PIN 17void measure() { pinMode(PIN, OUTPUT); digitalWrite(PIN, LOW); delay(18); // 18ms delay to start the communication digitalWrite(PIN, HIGH); delayMicroseconds(20); // 20us delay to prepare for response pinMode(PIN, INPUT); int dht11_dat[5] = {0, 0, 0, 0, 0}; uint8_t laststate = HIGH; uint8_t counter = 0; uint8_t j = 0; uint8_t i; // Wait for the DHT11 response for (i = 0; i < 85; i++) { counter = 0; while (digitalRead(PIN) == laststate) { counter++; delayMicroseconds(1); if (counter == 255) { break; } } laststate = digitalRead(PIN); if (counter == 255) { break; } // Ignore first 3 transitions (response signal) if (i >= 4 && (i % 2 == 0)) { dht11_dat[j / 8] <<= 1; if (counter > 16) { // Adjust threshold based on timing dht11_dat[j / 8] |= 1; } j++; } // Print debug information printf("Bit %d: counter = %d, state = %d\n", i, counter, laststate); } // Print raw data for debugging printf("Raw data: "); for (i = 0; i < 5; i++) { printf("%02x ", dht11_dat[i]); } printf("\n"); if (j < 40) { printf("Data not good, insufficient bits read\n"); return; } if (((dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF) != dht11_dat[4]) { printf("Data not good, checksum mismatch\n"); return; } printf("Humidity = %d.%d %% Temperature = %d.%d *C\n", dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3]);}int main() { if (wiringPiSetupGpio() == -1) { printf("wiringPi setup failed\n"); return 1; } while (true) { measure(); delay(2000); // 2 seconds delay between readings } return 0;}
Statistics: Posted by AntiRix — Wed Jul 24, 2024 4:20 pm — Replies 0 — Views 11