From 14c2fd256653b6f7c657c93303183090cf211922 Mon Sep 17 00:00:00 2001 From: dakkar Date: Fri, 20 Apr 2018 14:55:53 +0100 Subject: factor out sleep times --- sensor/thermostat.ino | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) (limited to 'sensor/thermostat.ino') diff --git a/sensor/thermostat.ino b/sensor/thermostat.ino index d90404b..243c80b 100644 --- a/sensor/thermostat.ino +++ b/sensor/thermostat.ino @@ -9,7 +9,8 @@ static BLEUUID thermo_service_uuid("11111111-2222-3333-4444-000000000000"); static BLEUUID thermo_temp_uuid("11111111-2222-3333-4444-000000000001"); static BLEUUID thermo_time_uuid("11111111-2222-3333-4444-000000000002"); -#define DEFAULT_SLEEP_TIME 60 +#define NO_SERVER_SLEEP_TIME 60 +#define ON_ERROR_SLEEP_TIME 30 /* Conversion factor for micro seconds to seconds */ #define uS_TO_S_FACTOR 1000000 @@ -74,7 +75,7 @@ uint32_t send_data_and_get_time(BLEAddress* pAddress,String* data) { bool ok = pClient->connect(*pAddress); if (!ok) { show("connect fail"); - return 10; + return ON_ERROR_SLEEP_TIME; } String status = "SC"; status += pClient->getPeerAddress().toString().c_str(); @@ -107,7 +108,7 @@ uint32_t send_data_and_get_time(BLEAddress* pAddress,String* data) { catch (...) { show("Fail"); pClient->disconnect(); - return 10; + return ON_ERROR_SLEEP_TIME; } } @@ -127,13 +128,13 @@ void loop() { pBLEScan->setActiveScan(true); show("s"); BLEScanResults foundDevices = pBLEScan->start(30); + int next_time; if (cb->pServerAddress) { show("S"); float humidity, temperature; 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); @@ -142,21 +143,18 @@ void loop() { } else { show("no thermo"); - next_time=10; + next_time=ON_ERROR_SLEEP_TIME; } - String nt = "Next time:";nt+=next_time; - display.drawString(0,20,nt);display.display(); - delay(2000); - esp_sleep_enable_timer_wakeup(next_time * uS_TO_S_FACTOR); } else { display.drawString(0,0,"s!");display.display(); - String nt = "Next time:";nt+=DEFAULT_SLEEP_TIME; - display.drawString(0,20,nt);display.display(); - delay(2000); - esp_sleep_enable_timer_wakeup(DEFAULT_SLEEP_TIME * uS_TO_S_FACTOR); + next_time = NO_SERVER_SLEEP_TIME; } + String nt = "Next time:";nt+=next_time; + display.drawString(0,20,nt);display.display(); + delay(2000); + esp_sleep_enable_timer_wakeup(next_time * uS_TO_S_FACTOR); esp_deep_sleep_start(); /* display.clear(); */ -- cgit v1.2.3