diff options
author | dakkar <dakkar@thenautilus.net> | 2018-04-20 14:45:32 +0100 |
---|---|---|
committer | dakkar <dakkar@thenautilus.net> | 2018-04-20 14:45:32 +0100 |
commit | e215b0963a8eb0eb361b626237b586ef88c3c264 (patch) | |
tree | 7f4cb739078ac6372f0035a0cba773f862fdc9f9 /sensor | |
parent | on BLE connect error, print error and retry in a bit (diff) | |
download | thermostat-e215b0963a8eb0eb361b626237b586ef88c3c264.tar.gz thermostat-e215b0963a8eb0eb361b626237b586ef88c3c264.tar.bz2 thermostat-e215b0963a8eb0eb361b626237b586ef88c3c264.zip |
don't stall if dht times out
Diffstat (limited to 'sensor')
-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); |