diff options
-rw-r--r-- | sensor/thermostat.ino | 28 |
1 files changed, 16 insertions, 12 deletions
diff --git a/sensor/thermostat.ino b/sensor/thermostat.ino index 8c2abb1..0b90053 100644 --- a/sensor/thermostat.ino +++ b/sensor/thermostat.ino @@ -46,15 +46,11 @@ class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks { } // onResult }; // MyAdvertisedDeviceCallbacks -void read_sensor(float* humidity, float* temperature) { - // faking it temporarily, my sensor is not cooperating - *temperature = 25.0; - *humidity = 87.0; - return; - +bool read_sensor(float* humidity, float* temperature) { dht.setup(13,DHTesp::DHT11); - while (1) { + int tries=0; + while (++tries<10) { delay(dht.getMinimumSamplingPeriod()); *humidity = dht.getHumidity(); *temperature = dht.getTemperature(); @@ -67,8 +63,9 @@ void read_sensor(float* humidity, float* temperature) { data += dht.getStatusString(); show(data); - if (dht.getStatus() != DHTesp::ERROR_TIMEOUT) return; + if (dht.getStatus() != DHTesp::ERROR_TIMEOUT) return true; } + return false; } uint32_t send_data_and_get_time(BLEAddress* pAddress,String* data) { @@ -131,11 +128,18 @@ void loop() { show("S"); float humidity, temperature; - read_sensor(&humidity,&temperature); - String data = String(BLEDevice::getAddress().toString().c_str()) + " " + String(humidity) + " " + String(temperature) + "\n"; - show(data); + bool ok = read_sensor(&humidity,&temperature); + int next_time; + if (ok) { + String data = String(BLEDevice::getAddress().toString().c_str()) + " " + String(humidity) + " " + String(temperature) + "\n"; + show(data); - int next_time = send_data_and_get_time(cb->pServerAddress,&data); + next_time = send_data_and_get_time(cb->pServerAddress,&data); + } + else { + show("no thermo"); + next_time=10; + } String nt = "Next time:";nt+=next_time; display.drawString(0,20,nt);display.display(); delay(2000); |