Added running average methods to determine if we should send temperature / humidity reports

This commit is contained in:
Thomas Mørch
2015-05-12 21:23:09 +02:00
parent 941afb30e9
commit 7fcdbb32d8
8 changed files with 431 additions and 41 deletions
@@ -14,19 +14,22 @@
#include <EEPROM.h>
#include <sha204_lib_return_codes.h>
#include <sha204_library.h>
#include <RunningAverage.h>
#include <avr/power.h>
// Define a static node address, remove if you want auto address assignment
//#define NODE_ADDRESS 3
#define RELEASE "1.1"
#define RELEASE "1.2"
#define AVERAGES 2
// Child sensor ID's
#define CHILD_ID_TEMP 1
#define CHILD_ID_HUM 2
#define CHILD_ID_BATT 199
// How many milli seconds between each measurement
#define MEASURE_INTERVAL 60000
#define MEASURE_INTERVAL 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
@@ -34,7 +37,7 @@
// When MEASURE_INTERVAL is 60000 and FORCE_TRANSMIT_INTERVAL is 30, we force a transmission every 30 minutes.
// Between the forced transmissions a tranmission will only occur if the measured value differs from the previous measurement
//Pin definitions
// Pin definitions
#define TEST_PIN A0
#define LED_PIN A2
#define ATSHA204_PIN 17 // A3
@@ -50,10 +53,10 @@ MySensor gw;
// Sensor messages
MyMessage msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
MyMessage msgBattery(CHILD_ID_BATT, V_VOLTAGE);
// Global settings
int measureCount = 0;
int sendBattery = 0;
boolean isMetric = true;
// Storage of old measurements
@@ -61,14 +64,21 @@ float lastTemperature = -100;
int lastHumidity = -100;
long lastBattery = -100;
bool highfreq = true;
RunningAverage raHum(AVERAGES);
RunningAverage raTemp(AVERAGES);
/****************************************************
*
* Setup code
*
****************************************************/
void setup() {
// clock_prescale_set(clock_div_8); // Switch to 1Mhz right after powerup.
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
Serial.begin(115200);
Serial.begin(19200);
Serial.print(F("Sensebender Micro FW "));
Serial.print(RELEASE);
Serial.flush();
@@ -79,6 +89,10 @@ void setup() {
digitalWrite(TEST_PIN, HIGH); // Enable pullup
if (!digitalRead(TEST_PIN)) testMode();
// Make sure that ATSHA204 is not floating
pinMode(ATSHA204_PIN, INPUT);
digitalWrite(ATSHA204_PIN, HIGH);
digitalWrite(TEST_PIN,LOW);
digitalWrite(LED_PIN, HIGH);
@@ -99,19 +113,22 @@ void setup() {
gw.present(CHILD_ID_HUM,S_HUM);
isMetric = gw.getConfig().isMetric;
Serial.print("isMetric: "); Serial.println(isMetric);
Serial.print(F("isMetric: ")); Serial.println(isMetric);
raHum.clear();
raTemp.clear();
sendTempHumidityMeasurements(false);
}
// Main loop function
/***********************************************
*
* Main loop function
*
***********************************************/
void loop() {
measureCount ++;
sendBattery ++;
bool forceTransmit = false;
// When we wake up the 5th time after power on, switch to 1Mhz clock
// This allows us to print debug messages on startup (as serial port is dependend on oscilator settings).
if ((measureCount == 5) && highfreq) switchClock(1<<CLKPS2); // Switch to 1Mhz for the reminder of the sketch, save power.
if (measureCount > FORCE_TRANSMIT_INTERVAL) { // force a transmission
forceTransmit = true;
@@ -119,32 +136,52 @@ void loop() {
}
gw.process();
sendBattLevel(forceTransmit);
sendTempHumidityMeasurements(forceTransmit);
if (sendBattery > 60)
{
sendBattLevel(forceTransmit); // Not needed to send battery info that often
sendBattery = 0;
}
gw.sleep(MEASURE_INTERVAL);
}
/*
/*********************************************
*
* Sends temperature and humidity from Si7021 sensor
*
* Parameters
* - force : Forces transmission of a value (even if it's the same as previous measurement)
*/
*
*********************************************/
void sendTempHumidityMeasurements(bool force)
{
if (force) {
lastHumidity = -100;
lastTemperature = -100;
}
bool tx = force;
si7021_env data = humiditySensor.getHumidityAndTemperature();
float oldAvgTemp = raTemp.getAverage();
float oldAvgHum = raHum.getAverage();
float temperature = (isMetric ? data.celsiusHundredths : data.fahrenheitHundredths) / 100.0;
int humidity = data.humidityPercent;
raTemp.addValue(data.celsiusHundredths / 100);
raHum.addValue(data.humidityPercent);
float diffTemp = abs(oldAvgTemp - raTemp.getAverage());
float diffHum = abs(oldAvgHum - raHum.getAverage());
if ((lastTemperature != temperature) | (lastHumidity != humidity)) {
Serial.println(diffTemp);
Serial.println(diffHum);
if (isnan(diffTemp)) tx = true;
if (diffTemp > 0.2) tx = true;
if (diffHum > 0.5) tx = true;
if (tx) {
measureCount = 0;
float temperature = (isMetric ? data.celsiusHundredths : data.fahrenheitHundredths) / 100.0;
int humidity = data.humidityPercent;
Serial.print("T: ");Serial.println(temperature);
Serial.print("H: ");Serial.println(humidity);
@@ -155,12 +192,14 @@ void sendTempHumidityMeasurements(bool force)
}
}
/*
* Sends battery information (both voltage, and battery percentage)
/********************************************
*
* Sends battery information (battery percentage)
*
* Parameters
* - force : Forces transmission of a value
*/
*
*******************************************/
void sendBattLevel(bool force)
{
if (force) lastBattery = -1;
@@ -176,6 +215,11 @@ void sendBattLevel(bool force)
}
}
/*******************************************
*
* Internal battery ADC measuring
*
*******************************************/
long readVcc() {
// Read 1.1V reference against AVcc
// set the reference to Vcc and the measurement to the internal 1.1V reference
@@ -202,18 +246,11 @@ long readVcc() {
return result; // Vcc in millivolts
}
void switchClock(unsigned char clk)
{
cli();
CLKPR = 1<<CLKPCE; // Set CLKPCE to enable clk switching
CLKPR = clk;
sei();
highfreq = false;
}
// Verify all peripherals, and signal via the LED if any problems.
/****************************************************
*
* Verify all peripherals, and signal via the LED if any problems.
*
****************************************************/
void testMode()
{
uint8_t rx_buffer[SHA204_RSP_SIZE_MAX];