That's why tasmota and espeasy have it as part of their DHT driver.
So I started modifying the existing DHT driver for micropython to work with the Sonoff SI7402 in a first step (doesn't matter to me if the DHT does still work with the changes, it's only proof-of-concept for now. Will think about doing it properly later) but it doesn't quite work. The humidity seems to be ok but the Temperature is not (it's 0.2°C) and the checksum fails. But the temperature is actually read first and the humidity last so I'm not sure what I did wrong. I'm using an esp8266 for testing btw, a Sonoff TH10.
Maybe someone can point me in the right direction?
These are the drivers tasmota https://github.com/arendst/Tasmota/blob ... ht.ino#L64 and espeasy https://github.com/TD-er/ESPEasy/blob/c ... T.ino#L138 use. The actual code for reading the sensors is rather short.
I tried to use the original reading method in the micropyhton DHT driver which is using machine_time_pulse_us but that did not work at all (but maybe I did something else wrong at that time) so I moved closer to the tasmota driver:
Code: Select all
STATIC mp_obj_t dht_readinto(mp_obj_t pin_in, mp_obj_t buf_in) {
mp_hal_pin_obj_t pin = mp_hal_get_pin_obj(pin_in);
mp_hal_pin_open_drain(pin);
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_WRITE);
if (bufinfo.len < 5) {
mp_raise_ValueError(MP_ERROR_TEXT("buffer too small"));
}
// issue start command
mp_hal_pin_od_high_dht(pin);
mp_hal_delay_ms(250);
//mp_hal_delay_ms(18);
mp_uint_t irq_state = mp_hal_quiet_timing_enter();
mp_hal_pin_od_low(pin);
mp_hal_delay_us_fast(500); //si7402
// release the line so the device can respond
mp_hal_pin_od_high_dht(pin);
//mp_hal_delay_us_fast(10);
mp_hal_delay_us_fast(20); //si7402
// wait for device to respond
mp_uint_t ticks = mp_hal_ticks_us();
while (mp_hal_pin_read(pin) != 0) {
if ((mp_uint_t)(mp_hal_ticks_us() - ticks) > 100) {
goto timeout;
}
}
//si7402
ticks = mp_hal_ticks_us();
while (mp_hal_pin_read(pin) != 1) {
if ((mp_uint_t)(mp_hal_ticks_us() - ticks) > 100) {
goto timeout;
}
}
//si7402
ticks = mp_hal_ticks_us();
while (mp_hal_pin_read(pin) != 0) {
if ((mp_uint_t)(mp_hal_ticks_us() - ticks) > 100) {
goto timeout;
}
}
uint8_t *buf = bufinfo.buf;
for (int i = 0; i < 40; ++i) {
//si7402
ticks = mp_hal_ticks_us();
while (mp_hal_pin_read(pin) != 1) {
if ((mp_uint_t)(mp_hal_ticks_us() - ticks) > 100) {
goto timeout;
}
}
mp_hal_delay_us_fast(35);
if (mp_hal_pin_read(pin)) {
buf[i / 8] = (1 << (7 - i % 8));
}
ticks = mp_hal_ticks_us();
while (mp_hal_pin_read(pin) != 0) {
if ((mp_uint_t)(mp_hal_ticks_us() - ticks) > 100) {
goto timeout;
}
}
}
Or, if you want to check out the Diff between the original module and my changes (link will work for 7 days): https://www.diffchecker.com/Tb1ylMjB