maximkulkin / esp-homekit-demo

Demo of Apple HomeKit accessory server library
MIT License
807 stars 233 forks source link

Sonoff Sensor AM2301 Question #350

Closed kiralikbeyin closed 4 years ago

kiralikbeyin commented 4 years ago

@maximkulkin I can not read data from original sonoff sensor am2301.

//tried
gpio_set_pullup(SENSOR_GPIO, false, false);
gpio_set_pullup(SENSOR_GPIO, true, true);

Sensor is working with ewelink (sonoff app)

Any idea?

maximkulkin commented 4 years ago

gpio_set_pullup() is not how you read data. Can you provide full code?

AramVartanyan commented 4 years ago

This is what I am using for Sonoff TH16 and it works perfect over a year: https://github.com/AramVartanyan/esp-homekit-demo/tree/master/examples/switch_th16

kiralikbeyin commented 4 years ago

gpio_set_pullup() is not how you read data. Can you provide full code?

void temperature_sensor_task(void *pvParameters)
{
  gpio_set_pullup(SENSOR_GPIO, false, false);

  while (1) {

    if (dht_read_float_data(DHT_TYPE_DHT22, SENSOR_GPIO, &humidity_value, &temperature_value)) {
      printf(">>> Sensor: temperature %g, humidity %g\n", temperature_value, humidity_value);

      if (temperature_value != old_temperature_value)
      {

        if ( ((temperature_value * 1.018) > old_temperature_value) || ((temperature_value * 0.982) < old_temperature_value) )
        {

          old_temperature_value = temperature_value;
          current_temperature.value = HOMEKIT_FLOAT(temperature_value);
          homekit_characteristic_notify(&current_temperature, current_temperature.value);

          old_humidity_value = humidity_value;
          current_humidity.value = HOMEKIT_FLOAT(humidity_value);
          homekit_characteristic_notify(&current_humidity, current_humidity.value);
          update_state();
        }
      }
    }

    vTaskDelay(POLL_PERIOD / portTICK_PERIOD_MS);
    taskYIELD();
  }
}

DS18B20 works fine. AM2301 (Alsair brand) works fine. Original Sonoff AM2301 sensor makes trouble. (I always push the 2.5mm jack enough and hear click sound)

I think it is related with dht lib because DHT21 & DHT22 are same in esp-open-rtos but AM2301 is DHT21 (see adafruit dht lib https://github.com/adafruit/DHT-sensor-library/blob/master/DHT.cpp )

This is what we have in esp-open-rtos

static inline bool dht_fetch_data(dht_sensor_type_t sensor_type, uint8_t pin, bool bits[DHT_DATA_BITS])
{
    uint32_t low_duration;
    uint32_t high_duration;

    // Phase 'A' pulling signal low to initiate read sequence
    gpio_write(pin, 0);
    sdk_os_delay_us(sensor_type == DHT_TYPE_SI7021 ? 500 : 20000);
    //sdk_os_delay_us(1100); 
-->1100 DIDNT WORK
    gpio_write(pin, 1);

    taskYIELD();

    // Step through Phase 'B', 40us
    if (!dht_await_pin_state(pin, 40, false, NULL)) {
        debug("Initialization error, problem in phase 'B'\n");
        return false;
    }

    // Step through Phase 'C', 88us
    if (!dht_await_pin_state(pin, 88, true, NULL)) {
        debug("Initialization error, problem in phase 'C'\n");
        return false;
    }

    // Step through Phase 'D', 88us
    if (!dht_await_pin_state(pin, 88, false, NULL)) {
        debug("Initialization error, problem in phase 'D'\n");
        return false;
    }

    // Read in each of the 40 bits of data...
    for (int i = 0; i < DHT_DATA_BITS; i++) {
        if (!dht_await_pin_state(pin, 65, true, &low_duration)) {
            debug("LOW bit timeout\n");
            return false;
        }
        if (!dht_await_pin_state(pin, 75, false, &high_duration)) {
            debug("HIGHT bit timeout\n");
            return false;
        }
        bits[i] = high_duration > low_duration;
    }
    return true;
}

i get this in debug:


Initialization error, problem in phase 'B'

This is a standart return if i do not plug the sensor also.

kiralikbeyin commented 4 years ago

@maximkulkin New original sonoff sensor (v1.0) is different. (not am2301) Chip is BB1 0 F 2 G A

Found this: https://electronics.stackexchange.com/questions/443364/itead-sonoff-rf-wifi-433mhz-switch-identifying-an-unknown-chip-on-the-433mhz

https://au.mouser.com/datasheet/2/368/EFM8BB1-DataSheet-958779.pdf

Is there any way to get temp-hum from new sensor?

image

Old one is blue v0.03 which works - it is really am2301

image image

maximkulkin commented 4 years ago

i think this is a place where you pull out your logic analyzer device and start recording what's happening on a wire.

kiralikbeyin commented 4 years ago

i think this is a place where you pull out your logic analyzer device and start recording what's happening on a wire.

I bought normal ds18b20 sensors and give up. I will try with logic analyzer later.

Thank you for the idea.