/* * * weitere Ă„nderung in handleCommands.handle_dht dort dht.begin auskommentiert void setupDHT() { if (dht_active == 1) { dht.begin(); } } void checkDHT() { if (timeNow - LastDHTTime > DeltaDHTTime && dht_active == 1) { float h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t)) { Serial.print(gerDate(NTP.getTimeDateString())); Serial.println(" Failed to read from DHT sensor!"); build_wwwStatus2("Failed DHT sensor!"); //LastDHTTime = timeNow - DeltaDHTTime + 1; } else { float hic = dht.computeHeatIndex(t, h, false); dht_humi = h; dht_tmpc = t; dht_tmpi = hic; sendInfosTemp(); reportCCU_DHT(); //LastDHTTime = timeNow; } LastDHTTime = timeNow; } } */ // dhtnolib begin // #define USE_DHT // Default DHT11 sensor needs no external library from sonoff_post.h // from sonoff_template.h enum UserSelectablePins { GPIO_NONE, // Not used GPIO_DHT11, // DHT11 GPIO_DHT22, // DHT21, DHT22, AM2301, AM2302, AM2321 GPIO_SI7021, // iTead SI7021 GPIO_DSB, // Single wire DS18B20 or DS18S20 GPIO_SENSOR_END }; const uint8_t kGpioNiceList[GPIO_SENSOR_END] PROGMEM = { GPIO_NONE, // Not used GPIO_DHT11, // DHT11 GPIO_DHT22, // DHT21, DHT22, AM2301, AM2302, AM2321 GPIO_SI7021, // iTead SI7021 GPIO_DSB, // Single wire DS18B20 or DS18S20 }; const char kSensorNames[] PROGMEM = "NONE" "|" "DHT11" "|" "AM2301" "|" "SI7021" "|" "DS18X20" ; /*********************************************************************************************\ * DHT11, AM2301 (DHT21, DHT22, AM2302, AM2321), SI7021 - Temperature and Humidy * * Reading temperature or humidity takes about 250 milliseconds! * Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) * Source: Adafruit Industries https://github.com/adafruit/DHT-sensor-library \*********************************************************************************************/ #define DHT_MAX_SENSORS 3 #define DHT_MAX_RETRY 8 uint32_t dht_max_cycles; uint8_t dht_data[5]; byte dht_sensors = 0; struct DHTSTRUCT { byte pin; byte type; char stype[12]; uint32_t lastreadtime; uint8_t lastresult; float t = NAN; float h = NAN; } Dht[DHT_MAX_SENSORS]; long uptime = 0; void DhtReadPrep() { for (byte i = 0; i < dht_sensors; i++) { digitalWrite(Dht[i].pin, HIGH); } } int32_t DhtExpectPulse(byte sensor, bool level) { int32_t count = 0; while (digitalRead(Dht[sensor].pin) == level) { if (count++ >= (int32_t)dht_max_cycles) { return -1; // Timeout } } return count; } boolean DhtRead(byte sensor) { int32_t cycles[80]; uint8_t error = 0; dht_data[0] = dht_data[1] = dht_data[2] = dht_data[3] = dht_data[4] = 0; // digitalWrite(Dht[sensor].pin, HIGH); // delay(250); if (Dht[sensor].lastresult > DHT_MAX_RETRY) { Dht[sensor].lastresult = 0; digitalWrite(Dht[sensor].pin, HIGH); // Retry read prep delay(250); } pinMode(Dht[sensor].pin, OUTPUT); digitalWrite(Dht[sensor].pin, LOW); if (GPIO_SI7021 == Dht[sensor].type) { delayMicroseconds(500); } else { delay(20); } noInterrupts(); digitalWrite(Dht[sensor].pin, HIGH); delayMicroseconds(40); pinMode(Dht[sensor].pin, INPUT_PULLUP); delayMicroseconds(10); if (-1 == DhtExpectPulse(sensor, LOW)) { //AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT D_TIMEOUT_WAITING_FOR " " D_START_SIGNAL_LOW " " D_PULSE)); Serial.println("TIMEOUT_WAITING_FOR START_SIGNAL_LOW _PULSE"); error = 1; } else if (-1 == DhtExpectPulse(sensor, HIGH)) { //AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT D_TIMEOUT_WAITING_FOR " " D_START_SIGNAL_HIGH " " D_PULSE)); Serial.println("TIMEOUT_WAITING_FOR START_SIGNAL_HIGH_PULSE"); error = 1; } else { for (int i = 0; i < 80; i += 2) { cycles[i] = DhtExpectPulse(sensor, LOW); cycles[i+1] = DhtExpectPulse(sensor, HIGH); } } interrupts(); if (error) { return false; } for (int i = 0; i < 40; ++i) { int32_t lowCycles = cycles[2*i]; int32_t highCycles = cycles[2*i+1]; if ((-1 == lowCycles) || (-1 == highCycles)) { //AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT D_TIMEOUT_WAITING_FOR " " D_PULSE)); Serial.println("TIMEOUT_WAITING_FOR PULSE"); return false; } dht_data[i/8] <<= 1; if (highCycles > lowCycles) { dht_data[i / 8] |= 1; } } uint8_t checksum = (dht_data[0] + dht_data[1] + dht_data[2] + dht_data[3]) & 0xFF; if (dht_data[4] != checksum) { char log_data[50]; snprintf_P(log_data, sizeof(log_data), "DHT D_CHECKSUM_FAILURE" ," %02X, %02X, %02X, %02X, %02X =? %02X", dht_data[0], dht_data[1], dht_data[2], dht_data[3], dht_data[4], checksum); //AddLog(LOG_LEVEL_DEBUG); Serial.println(log_data); return false; } return true; } void DhtReadTempHum(byte sensor) { if (sensor != 0) { Serial.println("only 1 sensor is implemented"); return; } if ((NAN == Dht[sensor].h) || (Dht[sensor].lastresult > DHT_MAX_RETRY)) { // Reset after 8 misses Dht[sensor].t = NAN; Dht[sensor].h = NAN; Serial.println("Reset sensor after 8 tries"); } if (DhtRead(sensor)) { if (Dht[0].type == GPIO_DHT22) { Serial.println("DHT 22 Read success"); } if (Dht[0].type == GPIO_DHT11) { Serial.println("DHT 11 Read success"); } switch (Dht[sensor].type) { case GPIO_DHT11: Dht[sensor].h = dht_data[0]; Dht[sensor].t = dht_data[2] + ((float)dht_data[3] * 0.1f); // Issue #3164 break; case GPIO_DHT22: case GPIO_SI7021: Dht[sensor].h = ((dht_data[0] << 8) | dht_data[1]) * 0.1; Dht[sensor].t = (((dht_data[2] & 0x7F) << 8 ) | dht_data[3]) * 0.1; if (dht_data[2] & 0x80) { Dht[sensor].t *= -1; } break; } //Dht[sensor].t = ConvertTemp(Dht[sensor].t); Dht[sensor].lastresult = 0; } else { Dht[sensor].lastresult++; } } void DHT11_0_Init(byte pin){ dht_max_cycles = microsecondsToClockCycles(1000); // 1 millisecond timeout for reading pulses from DHT sensor. Dht[0].pin = pin; Dht[0].type = GPIO_DHT11; pinMode(Dht[0].pin, INPUT_PULLUP); Dht[0].lastreadtime = 0; Dht[0].lastresult = 0; dht_sensors = 1; } void DHT22_0_Init(byte pin){ dht_max_cycles = microsecondsToClockCycles(1000); // 1 millisecond timeout for reading pulses from DHT sensor. Dht[0].pin = pin; Dht[0].type = GPIO_DHT22; pinMode(Dht[0].pin, INPUT_PULLUP); Dht[0].lastreadtime = 0; Dht[0].lastresult = 0; dht_sensors = 1; } // dhtnolib end void setupDHT(){ if (dht_active == 1) { DHT11_0_Init(DHTPIN); } } void checkDHT() { if (timeNow - LastDHTTime > DeltaDHTTime && dht_active == 1) { byte sensor = 0; DhtReadTempHum(sensor); //float h = dht.readHumidity(); float h = Dht[0].h; // Read temperature as Celsius (the default) //float t = dht.readTemperature(); float t = Dht[0].t; // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t)) { Serial.print(gerDate(NTP.getTimeDateString())); Serial.println(" Failed to read from DHT sensor!"); build_wwwStatus2("Failed DHT sensor!"); //LastDHTTime = timeNow - DeltaDHTTime + 1; } else { float hic = 0 ; // dht.computeHeatIndex(t, h, false); dht_humi = h; dht_tmpc = t; dht_tmpi = hic; sendInfosTemp(); reportCCU_DHT(); //LastDHTTime = timeNow; } LastDHTTime = timeNow; } } //