[Help needed] Sonoff Si7402

Discuss development of drivers for external hardware and components, such as LCD screens, sensors, motor drivers, etc.
Target audience: Users and developers of drivers.
Post Reply
kevinkk525
Posts: 799
Joined: Sat Feb 03, 2018 7:02 pm

[Help needed] Sonoff Si7402

Post by kevinkk525 » Tue Dec 22, 2020 6:09 am

I'm trying to write a C-driver for the Sonoff Si7402 sensor which is used with the TH10/16 and is connected by an audio jack connector. Therefore it has a custom protocol, which is (almost) the same as the DHT22 protocol.
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;
            }
        }
    }
The rest of the code is the same as the original DHT module.
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
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
Frida
Posts: 45
Joined: Sat Jan 30, 2016 2:20 pm
Location: Middelfart, Denmark

Re: [Help needed] Sonoff Si7402

Post by Frida » Tue Dec 29, 2020 7:37 pm

Hi, is this a Turkish error, or is it sonoff si7021.
I have a couple of pieces that work just fine on my Propeller.
I have found that the starting pulse should be approx. 520 microseconds.
And I have the measuring pulses of 48 microseconds.

Hope this can help you further.
Attachments
Skærmbillede fra 2020-12-29 20-10-14.png
Skærmbillede fra 2020-12-29 20-10-14.png (212.92 KiB) Viewed 878 times
Yes Frida is my watchdog!

User avatar
Frida
Posts: 45
Joined: Sat Jan 30, 2016 2:20 pm
Location: Middelfart, Denmark

Re: [Help needed] Sonoff Si7402

Post by Frida » Sun Jan 03, 2021 8:25 pm

An appetizer.
I found a program in WIKI under: DHT22 temperature and humidity sensor
https://github.com/kurik/uPython-DHT22
I only use data_pin which is Y2 and has changed line 73:
while micros.counter () <493: # 25000:

Hope this is something you can use.
Attachments
test9.png
test9.png (288.28 KiB) Viewed 813 times
Yes Frida is my watchdog!

kevinkk525
Posts: 799
Joined: Sat Feb 03, 2018 7:02 pm

Re: [Help needed] Sonoff Si7402

Post by kevinkk525 » Tue Jan 05, 2021 6:24 am

Thanks, I think it is helpful, it's easy to understand. I'll try it hopefully soon.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
Frida
Posts: 45
Joined: Sat Jan 30, 2016 2:20 pm
Location: Middelfart, Denmark

Re: [Help needed] Sonoff Si7402

Post by Frida » Tue Jan 12, 2021 6:03 pm

Finally manages to compile micropython again.
That's probably 4 years since last. Compiler setup, was
not easy, but it succeeded in the end.
So I changed a line in "micropython/drivers/dht/dth.c"
namely line 55 to:
"mp_hal_delay_us_fast (493); // mp_hal_delay_ms (18);"

I have not tried with other times, to find the limits.

Code: Select all

     // issue start command
     mp_hal_pin_od_high_dht (pin);
     mp_hal_delay_ms (250);
     mp_hal_pin_od_low (pin);
     mp_hal_delay_us_fast (493); // mp_hal_delay_ms (18);

I now have it running for several hours.
Yes Frida is my watchdog!

Post Reply