Merge pull request #122 from tbowmo/master

Added battery reporting as a sensor value
This commit is contained in:
Henrik Ekblad
2015-06-07 21:02:57 +02:00
2 changed files with 52 additions and 8 deletions
+29 -1
View File
@@ -1,5 +1,8 @@
menu.cpu=Processor
######################################
## Sensebender Micro
MysensorsMicro.name=Sensebender Micro
MysensorsMicro.upload.tool=arduino:avrdude
@@ -26,4 +29,29 @@ MysensorsMicro.menu.cpu.8Mhz=Atmega328 8Mhz
MysensorsMicro.menu.cpu.8Mhz.build.f_cpu=8000000L
MysensorsMicro.menu.cpu.1Mhz=Atmega328 1Mhz
MysensorsMicro.menu.cpu.1Mhz.build.f_cpu=1000000L
MysensorsMicro.menu.cpu.1Mhz.build.f_cpu=1000000L
########################################
## Gateway settings
MysensorsGW.name=Sensebender Gateway
MysensorsGW.build.core=arduino:arduino
MysensorsGW.build.variant=GW
MysensorsGW.build.board=AVR_1284
MysensorsGW.build.mcu=atmega1284p
MysensorsGW.build.f_cpu=20000000L
MysensorsGW.upload.tool=arduino:avrdude
MysensorsGW.upload.protocol=arduino
MysensorsGW.upload.maximum_size=123720
MysensorsGW.upload.maximum_data_size=16384
MysensorsGW.upload.speed=115200
MysensorsGW.bootloader.tool=arduino:avrdude
MysensorsGW.bootloader.unlock_bits=0x3F
MysensorsGW.bootloader.lock_bits=0x0F
MysensorsGW.bootloader.low_fuses=0xE2
MysensorsGW.bootloader.high_fuses=0xD2
MysensorsGW.bootloader.extended_fuses=0xFE
MysensorsGW.bootloader.file=DualOptiboot/optiboot_atmega1284.hex
@@ -3,7 +3,7 @@
//
// If A0 is held low while powering on, it will enter testmode, which verifies all on-board peripherals
//
// Battery voltage is repported as child sensorId 199, as well as battery percentage (Internal message)
// Battery voltage is as battery percentage (Internal message), and optionally as a sensor value (See defines below)
#include <MySensor.h>
@@ -20,6 +20,9 @@
// Define a static node address, remove if you want auto address assignment
//#define NODE_ADDRESS 3
// Uncomment the line below, to transmit battery voltage as a normal sensor value
//#define BATT_SENSOR 199
#define RELEASE "1.2"
#define AVERAGES 2
@@ -54,6 +57,10 @@ MySensor gw;
MyMessage msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
#ifdef BATT_SENSOR
MyMessage msgBatt(BATT_SENSOR, V_VOLTAGE);
#endif
// Global settings
int measureCount = 0;
int sendBattery = 0;
@@ -112,6 +119,10 @@ void setup() {
gw.present(CHILD_ID_TEMP,S_TEMP);
gw.present(CHILD_ID_HUM,S_HUM);
#ifdef BATT_SENSOR
gw.present(BATT_SENSOR, S_POWER);
#endif
isMetric = gw.getConfig().isMetric;
Serial.print(F("isMetric: ")); Serial.println(isMetric);
raHum.clear();
@@ -171,18 +182,18 @@ void sendTempHumidityMeasurements(bool force)
float oldAvgTemp = raTemp.getAverage();
float oldAvgHum = raHum.getAverage();
raTemp.addValue(data.celsiusHundredths / 100);
raTemp.addValue(data.celsiusHundredths);
raHum.addValue(data.humidityPercent);
float diffTemp = abs(oldAvgTemp - raTemp.getAverage());
float diffTemp = abs(lastTemperature - data.celsiusHundredths/100);
float diffHum = abs(oldAvgHum - raHum.getAverage());
Serial.println(diffTemp);
Serial.println(diffHum);
Serial.print(F("TempDiff :"));Serial.println(diffTemp);
Serial.print(F("HumDiff :"));Serial.println(diffHum);
if (isnan(diffTemp)) tx = true;
if (diffTemp > 0.2) tx = true;
if (diffHum > 0.5) tx = true;
if (diffTemp > 0.3) tx = true;
if (diffHum >= 0.5) tx = true;
if (tx) {
measureCount = 0;
@@ -213,6 +224,11 @@ void sendBattLevel(bool force)
long vcc = readVcc();
if (vcc != lastBattery) {
lastBattery = vcc;
#ifdef BATT_SENSOR
gw.send(msgBatt.set(vcc));
#endif
// Calculate percentage
vcc = vcc - 1900; // subtract 1.9V from vcc, as this is the lowest voltage we will operate at