Make battery reading more efficient
This commit is contained in:
parent
39f873d1b5
commit
b977159efd
|
|
@ -51,13 +51,14 @@
|
||||||
//#define FREQUENCY RF69_868MHZ
|
//#define FREQUENCY RF69_868MHZ
|
||||||
#define FREQUENCY RF69_915MHZ
|
#define FREQUENCY RF69_915MHZ
|
||||||
#define ENCRYPTKEY "sampleEncryptKey" //exactly the same 16 characters/bytes on all nodes!
|
#define ENCRYPTKEY "sampleEncryptKey" //exactly the same 16 characters/bytes on all nodes!
|
||||||
#define IS_RFM69HW //uncomment only for RFM69HW! Remove/comment if you have RFM69W!
|
//#define IS_RFM69HW //uncomment only for RFM69HW! Remove/comment if you have RFM69W!
|
||||||
//*********************************************************************************************
|
//*********************************************************************************************
|
||||||
|
|
||||||
#define ACK_TIME 30 // max # of ms to wait for an ack
|
#define ACK_TIME 30 // max # of ms to wait for an ack
|
||||||
#define ONBOARDLED 9 // Moteinos have LEDs on D9
|
#define ONBOARDLED 9 // Moteinos have LEDs on D9
|
||||||
#define EXTLED 5 // MotionOLEDMote has an external LED on D5
|
#define EXTLED 5 // MotionOLEDMote has an external LED on D5
|
||||||
#define BATT_MONITOR A7 // Sense VBAT_COND signal (when powered externally should read ~3.25v/3.3v (1000-1023), when external power is cutoff it should start reading around 2.85v/3.3v * 1023 ~= 880 (ratio given by 10k+4.7K divider from VBAT_COND = 1.47 multiplier)
|
#define BATT_MONITOR A7 // Sense VBAT_COND signal (when powered externally should read ~3.25v/3.3v (1000-1023), when external power is cutoff it should start reading around 2.85v/3.3v * 1023 ~= 880 (ratio given by 10k+4.7K divider from VBAT_COND = 1.47 multiplier)
|
||||||
|
#define BATT_CYCLES 30 //read and report battery voltage every this many wakeup cycles (ex 30cycles * 8sec sleep = 240sec/4min)
|
||||||
#define MOTIONPIN 1 //hardware interrupt 1 (D3)
|
#define MOTIONPIN 1 //hardware interrupt 1 (D3)
|
||||||
|
|
||||||
//#define SERIAL_EN //comment this out when deploying to an installed SM to save a few KB of sketch size
|
//#define SERIAL_EN //comment this out when deploying to an installed SM to save a few KB of sketch size
|
||||||
|
|
@ -98,15 +99,12 @@ void motionIRQ()
|
||||||
motionDetected=true;
|
motionDetected=true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
byte batteryReportCycles=0;
|
||||||
void loop() {
|
void loop() {
|
||||||
|
checkBattery();
|
||||||
if (motionDetected)
|
if (motionDetected)
|
||||||
{
|
{
|
||||||
digitalWrite(EXTLED, HIGH);
|
digitalWrite(EXTLED, HIGH);
|
||||||
//TODO: read several samples and take average
|
|
||||||
//TODO: read analog only every N cycles
|
|
||||||
batteryVolts = analogRead(A7) * 0.00322 * 1.42;
|
|
||||||
dtostrf(batteryVolts, 3,2, BATstr);
|
|
||||||
//sprintf(sendBuf, "MOTION ............................................ BAT:%sv", BATstr);
|
|
||||||
sprintf(sendBuf, "MOTION BAT:%sv", BATstr);
|
sprintf(sendBuf, "MOTION BAT:%sv", BATstr);
|
||||||
sendLen = strlen(sendBuf);
|
sendLen = strlen(sendBuf);
|
||||||
|
|
||||||
|
|
@ -114,6 +112,7 @@ void loop() {
|
||||||
{
|
{
|
||||||
DEBUG("MOTION ACK:OK! RSSI:");
|
DEBUG("MOTION ACK:OK! RSSI:");
|
||||||
DEBUG(radio.RSSI);
|
DEBUG(radio.RSSI);
|
||||||
|
batteryReportCycles = 0;
|
||||||
}
|
}
|
||||||
else DEBUG("MOTION ACK:NOK...");
|
else DEBUG("MOTION ACK:NOK...");
|
||||||
|
|
||||||
|
|
@ -121,17 +120,31 @@ void loop() {
|
||||||
DEBUGln(BATstr);
|
DEBUGln(BATstr);
|
||||||
|
|
||||||
radio.sleep();
|
radio.sleep();
|
||||||
//Blink(EXTLED,50); //Blink(ONBOARDLED,3);
|
|
||||||
motionDetected=false;
|
motionDetected=false;
|
||||||
digitalWrite(EXTLED, LOW);
|
digitalWrite(EXTLED, LOW);
|
||||||
}
|
}
|
||||||
|
else if (batteryReportCycles == BATT_CYCLES)
|
||||||
|
{
|
||||||
|
sprintf(sendBuf, "BAT:%sv", BATstr);
|
||||||
|
sendLen = strlen(sendBuf);
|
||||||
|
radio.send(GATEWAYID, sendBuf, sendLen);
|
||||||
|
radio.sleep();
|
||||||
|
batteryReportCycles=0;
|
||||||
|
}
|
||||||
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
|
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
|
||||||
|
batteryReportCycles++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Blink(byte PIN, int DELAY_MS)
|
byte cycleCount=BATT_CYCLES;
|
||||||
|
void checkBattery()
|
||||||
{
|
{
|
||||||
pinMode(PIN, OUTPUT);
|
if (cycleCount++ == BATT_CYCLES) //only read battery every BATT_CYCLES sleep cycles
|
||||||
digitalWrite(PIN,HIGH);
|
{
|
||||||
delay(DELAY_MS);
|
unsigned int readings=0;
|
||||||
digitalWrite(PIN,LOW);
|
for (byte i=0; i<10; i++) //take 10 samples, and average
|
||||||
|
readings+=analogRead(BATT_MONITOR);
|
||||||
|
batteryVolts = (readings / 10.0) * 0.00322 * 1.42;
|
||||||
|
dtostrf(batteryVolts, 3,2, BATstr); //update the BATStr which gets sent every BATT_CYCLES or along with the MOTION message
|
||||||
|
cycleCount = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue