static void ICACHE_FLASH_ATTR pollDHTCb(void * arg){ int counter = 0; int laststate = 1; int i = 0; int bits_in = 0; // int bitidx = 0; // int bits[250]; int data[100]; data[0] = data[1] = data[2] = data[3] = data[4] = 0; // Wake up device, 250ms of high GPIO_OUTPUT_SET(DHT_PIN, 1); delay_ms(500); // Hold low for 20ms GPIO_OUTPUT_SET(DHT_PIN, 0); delay_ms(20); // High for 40ms // GPIO_OUTPUT_SET(2, 1); GPIO_DIS_OUTPUT(DHT_PIN); os_delay_us(40); // Set pin to input with pullup // PIN_PULLUP_EN(PERIPHS_IO_MUX_GPIO2_U); // os_printf("Waiting for gpio2 to drop \n"); // wait for pin to drop? while (GPIO_INPUT_GET(DHT_PIN) == 1 && i < DHT_MAXCOUNT) { if (i >= DHT_MAXCOUNT) { goto fail; } i++; } // os_printf("Reading DHT\n"); // read data! for (i = 0; i < MAXTIMINGS; i++) { // Count high time (in approx us) counter = 0; while (GPIO_INPUT_GET(DHT_PIN) == laststate) { counter++; os_delay_us(1); if (counter == 1000) break; } laststate = GPIO_INPUT_GET(DHT_PIN); if (counter == 1000) break; // store data after 3 reads if ((i > 3) && (i % 2 == 0)) { // shove each bit into the storage bytes data[bits_in / 8] <<= 1; if (counter > BREAKTIME) { //os_printf("1"); data[bits_in / 8] |= 1; } else { //os_printf("0"); } bits_in++; } } if (bits_in < 40) { os_printf("Got too few bits: %d should be at least 40", bits_in); goto fail; } int checksum = (data[0] + data[1] + data[2] + data[3]) & 0xFF; //os_printf("DHT: %02x %02x %02x %02x [%02x] CS: %02x\n", data[0], data[1],data[2],data[3],data[4],checksum); if (data[4] != checksum) { os_printf("Checksum was incorrect after %d bits. Expected %d but got %d", bits_in, data[4], checksum); goto fail; } reading.temperature = scale_temperature(data); reading.humidity = scale_humidity(data); //os_printf("Temp = %d*C, Hum = %d%%\n", (int)(reading.temperature * 100), (int)(reading.humidity * 100)); reading.success = 1; return; fail: os_printf("Failed to get DHT reading, dying\n"); reading.success = 0; }
bool DHTRead(DHT_Sensor *sensor, DHT_Sensor_Data* output) { int counter = 0; int laststate = 1; int i = 0; int j = 0; int checksum = 0; int data[100]; data[0] = data[1] = data[2] = data[3] = data[4] = 0; uint8_t pin = GPIO_ID_PIN(sensor->port); // Wake up device, 250ms of high GPIO_OUTPUT_SET(pin, 1); sleepms(250); // Hold low for 20ms GPIO_OUTPUT_SET(pin, 0); sleepms(20); // High for 40ns GPIO_OUTPUT_SET(pin, 1); os_delay_us(40); // Set DHT_PIN pin as an input GPIO_DIS_OUTPUT(pin); // wait for pin to drop? while (GPIO_INPUT_GET(pin) == 1 && i < DHT_MAXCOUNT) { os_delay_us(1); i++; } if(i == DHT_MAXCOUNT) { DHT_DEBUG("DHT: Failed to get reading from GPIO%d, dying\r\n", pin); return false; } // read data for (i = 0; i < DHT_MAXTIMINGS; i++) { // Count high time (in approx us) counter = 0; while (GPIO_INPUT_GET(pin) == laststate) { counter++; os_delay_us(1); if (counter == 1000) break; } laststate = GPIO_INPUT_GET(pin); if (counter == 1000) break; // store data after 3 reads if ((i>3) && (i%2 == 0)) { // shove each bit into the storage bytes data[j/8] <<= 1; if (counter > DHT_BREAKTIME) data[j/8] |= 1; j++; } } if (j >= 39) { checksum = (data[0] + data[1] + data[2] + data[3]) & 0xFF; DHT_DEBUG("DHT%s: %02x %02x %02x %02x [%02x] CS: %02x (GPIO%d)\r\n", sensor->type==DHT11?"11":"22", data[0], data[1], data[2], data[3], data[4], checksum, pin); if (data[4] == checksum) { // checksum is valid output->temperature = scale_temperature(sensor->type, data); output->humidity = scale_humidity(sensor->type, data); //DHT_DEBUG("DHT: Temperature = %d *C, Humidity = %d %%\r\n", (int)(reading.temperature * 100), (int)(reading.humidity * 100)); DHT_DEBUG("DHT: Temperature*100 = %d *C, Humidity*100 = %d %% (GPIO%d)\n", (int) (output->temperature * 100), (int) (output->humidity * 100), pin); } else { //DHT_DEBUG("Checksum was incorrect after %d bits. Expected %d but got %d\r\n", j, data[4], checksum); DHT_DEBUG("DHT: Checksum was incorrect after %d bits. Expected %d but got %d (GPIO%d)\r\n", j, data[4], checksum, pin); return false; } } else { //DHT_DEBUG("Got too few bits: %d should be at least 40\r\n", j); DHT_DEBUG("DHT: Got too few bits: %d should be at least 40 (GPIO%d)\r\n", j, pin); return false; } return true; }
struct dht_sensor_data *ICACHE_FLASH_ATTR DHTCb(void) { int counter = 0; int laststate = 1; int i = 0; int j = 0; int checksum = 0; int data[100]; data[0] = data[1] = data[2] = data[3] = data[4] = 0; // Wake up device, 250ms of high GPIO_OUTPUT_SET(DHT_PIN, 1); sleepms(250); // Hold low for 20ms GPIO_OUTPUT_SET(DHT_PIN, 0); sleepms(20); // High for 40ns GPIO_OUTPUT_SET(DHT_PIN, 1); os_delay_us(40); // Set DHT_PIN pin as an input GPIO_DIS_OUTPUT(DHT_PIN); // wait for pin to drop? while (GPIO_INPUT_GET(DHT_PIN) == 1 && i < DHT_MAXCOUNT) { os_delay_us(1); i++; } if(i == DHT_MAXCOUNT) { reading.success = 0; os_printf("Failed to get reading, dying\r\n"); return; } // read data for (i = 0; i < DHT_MAXTIMINGS; i++) { // Count high time (in approx us) counter = 0; while (GPIO_INPUT_GET(DHT_PIN) == laststate) { counter++; os_delay_us(1); if (counter == 1000) break; } laststate = GPIO_INPUT_GET(DHT_PIN); if (counter == 1000) break; // store data after 3 reads if ((i>3) && (i%2 == 0)) { // shove each bit into the storage bytes data[j/8] <<= 1; if (counter > DHT_BREAKTIME) data[j/8] |= 1; j++; } } if (j >= 39) { checksum = (data[0] + data[1] + data[2] + data[3]) & 0xFF; //os_printf("DHT: %02x %02x %02x %02x [%02x] CS: %02x", data[0], data[1],data[2],data[3],data[4],checksum); if (data[4] == checksum) { // checksum is valid reading.temperature = scale_temperature(data); reading.humidity = scale_humidity(data); //os_printf("Temperature = %d *C, Humidity = %d %%\r\n", (int)(reading.temperature * 100), (int)(reading.humidity * 100)); reading.success = 1; } else { os_printf("Checksum was incorrect after %d bits. Expected %d but got %d\r\n", j, data[4], checksum); reading.success = 0; } } else { os_printf("Got too few bits: %d should be at least 40\r\n", j); reading.success = 0; } return; }