Below is my first sketch with this awsome 2aa battery powerd board.
i didn't measure the current. but i tryed to be as efficient with the power as possible.
my avr is running on 8Mhz, this is becaus the DTH22 doesn't run on 1Mhz.
#include <SPI.h>
#include <MySensor.h>
#include <DHT.h>
#include <Vcc.h>
#define CHILD_ID_TEMP 1 // Child id for temperatur
#define CHILD_ID_HUM 2 // Child id for humidity
#define CHILD_ID_VOLT 3 // Child id for battery reading
#define HUMIDITY_SENSOR_DIGITAL_PIN 3 // Where is my DHT22 data pin connected to
int node_id=6; // What is my node id
unsigned long SLEEP_TIME =30000UL; // Sleep time between reads (in milliseconds)
int sleepcycle=1; // Counter to count the amout of time not sending data
int humoffset=2; // only data is send if humidity is changed for this amout
int tempoffset=0.5; // only data is if temperature is changed for this amout
int gwsendtimout=20; // each 20*sleep_time (10 minutes) data will be send
const float VccMin = 1.5; // Minimum expected Vcc level, in Volts.
const float VccMax = 3.0; // Maximum expected Vcc level, in Volts.
const float VccCorrection = 1.0/1.0; // Measured Vcc by multimeter divided by reported Vcc
Vcc vcc(VccCorrection);
MySensor gw;
DHT dht;
//Store last values
float lastTemp = 0 ;
float lastHum = 0 ;
float batteryV=0;
int oldBatteryPcnt = 0;
boolean lastTripped = false ;
boolean metric = true;
boolean gwsend = true; // to determin if data has to be send
MyMessage msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
MyMessage msgVolt(CHILD_ID_VOLT, V_VOLTAGE);
void setup()
{
gw.begin(NULL,node_id,false);
dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
// Send the Sketch Version Information to the Gateway
gw.sendSketchInfo("Humidity", "1.1",true);
// Register all sensors to gw (they will be created as child devices)
gw.present(CHILD_ID_HUM, S_HUM,"Humidity douche");
gw.present(CHILD_ID_TEMP, S_TEMP,"Temperatuur douche");
gw.present(CHILD_ID_VOLT, S_MULTIMETER,"Battery voltage");
metric = gw.getConfig().isMetric;
}
void loop()
{
// get the battery Voltage
batteryV = vcc.Read_Volts();
int batteryPcnt = vcc.Read_Perc(VccMin, VccMax);
if (oldBatteryPcnt != batteryPcnt) {
// Power up radio after sleep
gwsend=true;
oldBatteryPcnt = batteryPcnt;
}
delay(dht.getMinimumSamplingPeriod());
float temp1 = dht.getTemperature();
float humidity = dht.getHumidity();
if (isnan(temp1)) {
// Serial.println("Failed reading temperature from DHT");
} else if ((temp1 <= lastTemp-tempoffset)||(temp1 >= lastTemp+tempoffset)) {
lastTemp = temp1;
if (!metric) {
temp1 = dht.toFahrenheit(temp1);
}
gwsend=true;
}
if (isnan(humidity)) {
// Serial.println("Failed reading humidity from DHT");
} else if ((humidity <= lastHum-humoffset)||(humidity >= lastHum+humoffset)) {
lastHum = humidity;
gwsend=true;
}
if (sleepcycle>gwsendtimout){
gwsend=true;
}
if (gwsend){
gw.sendBatteryLevel(oldBatteryPcnt);
gw.send(msgVolt.set(batteryV, 1));
gw.send(msgTemp.set(lastTemp, 1));
gw.send(msgHum.set(lastHum, 1));
gwsend=false;
sleepcycle=1;
}
sleepcycle++;
gw.sleep(SLEEP_TIME);
}