From 668a5f649b4fee50d4bd7035b7f376cfca2c0c45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=B8rch?= Date: Thu, 30 Jul 2015 22:34:47 +0200 Subject: [PATCH 1/4] Better "control" for determining if humiidty changes should fire off a transmission --- .../examples/SensebenderMicro/SensebenderMicro.ino | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/libraries/MySensors/examples/SensebenderMicro/SensebenderMicro.ino b/libraries/MySensors/examples/SensebenderMicro/SensebenderMicro.ino index 1dc3e77..02b88d3 100644 --- a/libraries/MySensors/examples/SensebenderMicro/SensebenderMicro.ino +++ b/libraries/MySensors/examples/SensebenderMicro/SensebenderMicro.ino @@ -15,7 +15,7 @@ #include #include #include -#include +//#include // Define a static node address, remove if you want auto address assignment //#define NODE_ADDRESS 3 @@ -149,7 +149,7 @@ void loop() { if ((measureCount == 5) && highfreq) { - clock_prescale_set(clock_div_8); // Switch to 1Mhz for the reminder of the sketch, save power. +// clock_prescale_set(clock_div_8); // Switch to 1Mhz for the reminder of the sketch, save power. highfreq = false; } @@ -182,21 +182,20 @@ void loop() { void sendTempHumidityMeasurements(bool force) { bool tx = force; - + si7021_env data = humiditySensor.getHumidityAndTemperature(); - float oldAvgHum = raHum.getAverage(); raHum.addValue(data.humidityPercent); float diffTemp = abs(lastTemperature - (isMetric ? data.celsiusHundredths : data.fahrenheitHundredths)/100); - float diffHum = abs(oldAvgHum - raHum.getAverage()); + float diffHum = abs(lastHumidity - raHum.getAverage()); Serial.print(F("TempDiff :"));Serial.println(diffTemp); Serial.print(F("HumDiff :"));Serial.println(diffHum); if (isnan(diffHum)) tx = true; if (diffTemp > TEMP_TRANSMIT_THRESHOLD) tx = true; - if (diffHum >= HUMI_TRANSMIT_THRESHOLD) tx = true; + if (diffHum > HUMI_TRANSMIT_THRESHOLD) tx = true; if (tx) { measureCount = 0; @@ -246,7 +245,7 @@ void sendBattLevel(bool force) * Internal battery ADC measuring * *******************************************/ -long readVcc() { +long readVcc() {/* // Read 1.1V reference against AVcc // set the reference to Vcc and the measurement to the internal 1.1V reference #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) @@ -270,6 +269,7 @@ long readVcc() { result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000 return result; // Vcc in millivolts + */ } /**************************************************** From f7204cfcab106a079a09606205a223a0d45e697a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=B8rch?= Date: Mon, 3 Aug 2015 22:38:29 +0200 Subject: [PATCH 2/4] Enable OTA wait period after each transmission, if A1 is held low during bootup --- .../SensebenderMicro/SensebenderMicro.ino | 37 ++++++++++++++++--- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/libraries/MySensors/examples/SensebenderMicro/SensebenderMicro.ino b/libraries/MySensors/examples/SensebenderMicro/SensebenderMicro.ino index 9ac53de..64ded83 100644 --- a/libraries/MySensors/examples/SensebenderMicro/SensebenderMicro.ino +++ b/libraries/MySensors/examples/SensebenderMicro/SensebenderMicro.ino @@ -29,6 +29,12 @@ * * Battery voltage is as battery percentage (Internal message), and optionally as a sensor value (See defines below) * + * + * Version 1.3 - Thomas Bowman Mørch + * Improved transmission logic, eliminating spurious transmissions (when temperatuere / humidity fluctuates 1 up and down between measurements) + * + * Added OTA boot mode, need to hold A1 low while applying power. (uses slightly more power as it's waiting for bootloader messages) + * */ @@ -49,7 +55,7 @@ // Uncomment the line below, to transmit battery voltage as a normal sensor value //#define BATT_SENSOR 199 -#define RELEASE "1.2" +#define RELEASE "1.3" #define AVERAGES 2 @@ -60,6 +66,9 @@ // How many milli seconds between each measurement #define MEASURE_INTERVAL 60000 +// How many milli seconds should we wait for OTA? +#define OTA_WAIT_PERIOD 1000 + // FORCE_TRANSMIT_INTERVAL, this number of times of wakeup, the sensor is forced to report all values to the controller #define FORCE_TRANSMIT_INTERVAL 30 @@ -73,6 +82,7 @@ // Pin definitions #define TEST_PIN A0 +#define OTA_ENABLE A1 #define LED_PIN A2 #define ATSHA204_PIN 17 // A3 @@ -97,6 +107,8 @@ int measureCount = 0; int sendBattery = 0; boolean isMetric = true; boolean highfreq = true; +boolean ota_enabled = false; +boolean transmission_occured = false; // Storage of old measurements float lastTemperature = -100; @@ -126,11 +138,17 @@ void setup() { digitalWrite(TEST_PIN, HIGH); // Enable pullup if (!digitalRead(TEST_PIN)) testMode(); + pinMode(OTA_ENABLE, INPUT); + digitalWrite(OTA_ENABLE, HIGH); + if (!digitalRead(OTA_ENABLE)) ota_enabled = true; + // Make sure that ATSHA204 is not floating pinMode(ATSHA204_PIN, INPUT); digitalWrite(ATSHA204_PIN, HIGH); digitalWrite(TEST_PIN,LOW); + digitalWrite(OTA_ENABLE, LOW); // remove pullup, save some power. + digitalWrite(LED_PIN, HIGH); #ifdef NODE_ADDRESS @@ -169,13 +187,14 @@ void setup() { * ***********************************************/ void loop() { + measureCount ++; sendBattery ++; bool forceTransmit = false; - + transmission_occured = false; if ((measureCount == 5) && highfreq) { -// clock_prescale_set(clock_div_8); // Switch to 1Mhz for the reminder of the sketch, save power. + clock_prescale_set(clock_div_8); // Switch to 1Mhz for the reminder of the sketch, save power. highfreq = false; } @@ -192,7 +211,11 @@ void loop() { sendBattLevel(forceTransmit); // Not needed to send battery info that often sendBattery = 0; } - + + if (ota_enabled & transmission_occured) { + gw.wait(OTA_WAIT_PERIOD); + } + gw.sleep(MEASURE_INTERVAL); } @@ -235,6 +258,7 @@ void sendTempHumidityMeasurements(bool force) gw.send(msgHum.set(humidity)); lastTemperature = temperature; lastHumidity = humidity; + transmission_occured = true; } } @@ -263,6 +287,7 @@ void sendBattLevel(bool force) long percent = vcc / 14.0; gw.sendBatteryLevel(percent); + transmission_occured = true; } } @@ -271,7 +296,7 @@ void sendBattLevel(bool force) * Internal battery ADC measuring * *******************************************/ -long readVcc() {/* +long readVcc() { // Read 1.1V reference against AVcc // set the reference to Vcc and the measurement to the internal 1.1V reference #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) @@ -295,7 +320,7 @@ long readVcc() {/* result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000 return result; // Vcc in millivolts - */ + } /**************************************************** From 8e73752a037a4885ffa59d657954386ae22b46e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=B8rch?= Date: Mon, 3 Aug 2015 22:56:49 +0200 Subject: [PATCH 3/4] changed wait period to 300 mili seconds (@tekka recons 100-200mSeconds is enough, just want to be on the safe side, added serial debug string to indicate OTA FW updates enabled --- .../examples/SensebenderMicro/SensebenderMicro.ino | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/libraries/MySensors/examples/SensebenderMicro/SensebenderMicro.ino b/libraries/MySensors/examples/SensebenderMicro/SensebenderMicro.ino index 64ded83..87d0424 100644 --- a/libraries/MySensors/examples/SensebenderMicro/SensebenderMicro.ino +++ b/libraries/MySensors/examples/SensebenderMicro/SensebenderMicro.ino @@ -67,7 +67,7 @@ #define MEASURE_INTERVAL 60000 // How many milli seconds should we wait for OTA? -#define OTA_WAIT_PERIOD 1000 +#define OTA_WAIT_PERIOD 300 // FORCE_TRANSMIT_INTERVAL, this number of times of wakeup, the sensor is forced to report all values to the controller #define FORCE_TRANSMIT_INTERVAL 30 @@ -140,7 +140,9 @@ void setup() { pinMode(OTA_ENABLE, INPUT); digitalWrite(OTA_ENABLE, HIGH); - if (!digitalRead(OTA_ENABLE)) ota_enabled = true; + if (!digitalRead(OTA_ENABLE)) { + ota_enabled = true; + } // Make sure that ATSHA204 is not floating pinMode(ATSHA204_PIN, INPUT); @@ -178,6 +180,8 @@ void setup() { raHum.clear(); sendTempHumidityMeasurements(false); sendBattLevel(false); + if (ota_enabled) Serial.println("OTA FW update enabled"); + } From 3cf4bb65af79f810f2bd1ec9d18c4079718ba7a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=B8rch?= Date: Mon, 3 Aug 2015 23:02:19 +0200 Subject: [PATCH 4/4] don't scale CPU Freq, if OTA FW Updates are enabled --- .../MySensors/examples/SensebenderMicro/SensebenderMicro.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/MySensors/examples/SensebenderMicro/SensebenderMicro.ino b/libraries/MySensors/examples/SensebenderMicro/SensebenderMicro.ino index 87d0424..e14f5a8 100644 --- a/libraries/MySensors/examples/SensebenderMicro/SensebenderMicro.ino +++ b/libraries/MySensors/examples/SensebenderMicro/SensebenderMicro.ino @@ -198,7 +198,7 @@ void loop() { transmission_occured = false; if ((measureCount == 5) && highfreq) { - clock_prescale_set(clock_div_8); // Switch to 1Mhz for the reminder of the sketch, save power. + if (!ota_enabled) clock_prescale_set(clock_div_8); // Switch to 1Mhz for the reminder of the sketch, save power. highfreq = false; }