From dd9a08cce585b4516eb096718ad859c11224434f Mon Sep 17 00:00:00 2001 From: LowPowerLab Date: Thu, 17 Nov 2016 22:51:22 -0500 Subject: [PATCH] update examples --- ...tifier4_sender.ino => MailboxNotifier.ino} | 148 +++++++++++------- Examples/RandomNumbers/RandomNumbers.ino | 63 ++++++-- 2 files changed, 145 insertions(+), 66 deletions(-) rename Examples/MailboxNotifier/{MailboxNotifier4_sender.ino => MailboxNotifier.ino} (58%) diff --git a/Examples/MailboxNotifier/MailboxNotifier4_sender.ino b/Examples/MailboxNotifier/MailboxNotifier.ino similarity index 58% rename from Examples/MailboxNotifier/MailboxNotifier4_sender.ino rename to Examples/MailboxNotifier/MailboxNotifier.ino index bbe25db..53cbd40 100644 --- a/Examples/MailboxNotifier/MailboxNotifier4_sender.ino +++ b/Examples/MailboxNotifier/MailboxNotifier.ino @@ -6,11 +6,8 @@ // motion event happened (days, hours, minutes, seconds ago) and the battery level // In sleep mode, Moteino + PIR motion sensor use about ~78uA // Make sure you adjust the settings in the configuration section below !!! - // ********************************************************************************** -// Copyright Felix Rusu, LowPowerLab.com -// Library and code by Felix Rusu - felix@lowpowerlab.com -// Get the RFM69 and SPIFlash library at: https://github.com/LowPowerLab/ +// Copyright Felix Rusu 2016, http://www.LowPowerLab.com/contact // ********************************************************************************** // License // ********************************************************************************** @@ -26,45 +23,50 @@ // PARTICULAR PURPOSE. See the GNU General Public // License for more details. // -// You should have received a copy of the GNU General -// Public License along with this program. -// If not, see . -// // Licence can be viewed at // http://www.gnu.org/licenses/gpl-3.0.txt // // Please maintain this license information along with authorship // and copyright notices in any redistribution of this code // ********************************************************************************** - #include //get it here: https://github.com/LowPowerLab/RFM69 -#include //get it here: https://github.com/LowPowerLab/SPIFlash +#include //get it here: https://github.com/lowpowerlab/RFM69 +#include //get it here: https://github.com/lowpowerlab/spiflash +#include //included with Arduino IDE (www.arduino.cc) #include //get library from: https://github.com/LowPowerLab/LowPower //writeup here: http://www.rocketscream.com/blog/2011/07/04/lightweight-low-power-arduino-library/ -//********************************************************************************************* -// *********** IMPORTANT SETTINGS - YOU MUST CHANGE/ONFIGURE TO FIT YOUR HARDWARE ************* -//********************************************************************************************* -#define NODEID 55 //unique for each node on same network -#define NETWORKID 250 //the same on all nodes that talk to each other -#define GATEWAYID 1 +//**************************************************************************************************************** +//**** IMPORTANT RADIO SETTINGS - YOU MUST CHANGE/CONFIGURE TO MATCH YOUR HARDWARE TRANSCEIVER CONFIGURATION! **** +//**************************************************************************************************************** +#define NODEID 55 //unique for each node on same network +#define NETWORKID 100 //the same on all nodes that talk to each other +#define GATEWAYID 1 //Match frequency to the hardware version of the radio on your Moteino (uncomment one): -//#define FREQUENCY RF69_433MHZ -//#define FREQUENCY RF69_868MHZ -#define FREQUENCY RF69_915MHZ -#define IS_RFM69HW //uncomment only for RFM69HW! Leave out if you have RFM69W! -#define ENCRYPTKEY "sampleEncryptKey" //exactly the same 16 characters/bytes on all nodes! -#define SENDEVERYXLOOPS 4 //each loop sleeps 8 seconds, so send status message every this many loops (default "4" = 32 seconds) +//#define FREQUENCY RF69_433MHZ +//#define FREQUENCY RF69_868MHZ +#define FREQUENCY RF69_915MHZ +//#define FREQUENCY_EXACT 917000000 +#define IS_RFM69HW //uncomment only for RFM69HW! Leave out if you have RFM69W! +#define ENCRYPTKEY "sampleEncryptKey" //exactly the same 16 characters/bytes on all nodes! +#define SENDEVERYXLOOPS 8 //each loop sleeps 8 seconds, so send status message every this many sleep cycles (default "4" = 32 seconds) +//********************************************************************************************* +#define ENABLE_ATC //comment out this line to disable AUTO TRANSMISSION CONTROL +#define ATC_RSSI -75 //********************************************************************************************* -#define MOTIONPIN 1 //hardware interrupt 1 (D3) -#define BATTERYSENSE A7 //through 1Meg+470Kohm and 0.1uF cap from battery VCC - this ratio divides the voltage to bring it below 3.3V where it is scaled to a readable range -#define LED 9 // Moteinos have LEDs on D9 +#define MOTION_PIN 3 // D3 +#define MOTION_IRQ 1 // hardware interrupt 1 (D3) - where motion sensors OUTput is connected, this will generate an interrupt every time there is MOTION +#define BATT_MONITOR A7 //through 1Meg+470Kohm and 0.1uF cap from battery VCC - this ratio divides the voltage to bring it below 3.3V where it is scaled to a readable range +#define BATT_CYCLES 50 // read and report battery voltage every this many sleep cycles (ex 30cycles * 8sec sleep = 240sec/4min). For 450 cyclesyou would get ~1 hour intervals +#define BATT_FORMULA(reading) reading * 0.00322 * 1.47 +#define LED 5 // Moteinos have LEDs on D9 +#define DUPLICATE_INTERVAL 55000 //avoid duplicates in 55second intervals (ie mailman sometimes spends 30+ seconds at mailbox) //#define BLINK_EN //uncomment to make LED flash when messages are sent, leave out if you want low power -#define SERIAL_BAUD 115200 //#define SERIAL_EN //uncomment this line to enable serial IO debug messages, leave out if you want low power #ifdef SERIAL_EN + #define SERIAL_BAUD 115200 #define DEBUG(input) {Serial.print(input); delay(1);} #define DEBUGln(input) {Serial.println(input); delay(1);} #else @@ -72,8 +74,21 @@ #define DEBUGln(input); #endif -RFM69 radio; +#ifdef ENABLE_ATC + RFM69_ATC radio; +#else + RFM69 radio; +#endif + volatile boolean motionDetected=false; +char sendBuf[61]; +byte sendLen; +byte sendLoops=0; +unsigned long MLO=0; //MailLastOpen (ago, in ms) +unsigned long now = 0, time=0, lastSend = 0, temp = 0; +float batteryVolts = 5; +char BATstr[20]; +char MLOstr[20]; void setup() { #ifdef SERIAL_EN @@ -84,57 +99,63 @@ void setup() { radio.setHighPower(); //uncomment only for RFM69HW! #endif radio.encrypt(ENCRYPTKEY); - pinMode(MOTIONPIN, INPUT); - attachInterrupt(MOTIONPIN, motionIRQ, RISING); + +#ifdef FREQUENCY_EXACT + radio.setFrequency(FREQUENCY_EXACT); //set frequency to some custom frequency +#endif + + radio.setPowerLevel(29); +#ifdef ENABLE_ATC + radio.enableAutoPower(ATC_RSSI); +#endif + + radio.sendWithRetry(GATEWAYID, "START", 6); + + radio.sleep(); + pinMode(MOTION_PIN, INPUT); + pinMode(BATT_MONITOR, INPUT); + attachInterrupt(MOTION_IRQ, motionIRQ, RISING); char buff[50]; sprintf(buff, "\nTransmitting at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915); DEBUGln(buff); + checkBattery(); } void motionIRQ() { motionDetected=true; - //DEBUGln("I"); + DEBUGln("IRQ"); } -char sendBuf[32]; -byte sendLen; -byte sendLoops=0; -unsigned long MLO=0; //MailLastOpen (ago, in ms) -unsigned long now = 0, time=0, lastSend = 0, temp = 0; - void loop() { now = millis(); - int batteryReading = analogRead(BATTERYSENSE); - if (motionDetected && time-MLO > 20000) //avoid duplicates in 20second intervals + checkBattery(); + + if (motionDetected && (time-MLO > DUPLICATE_INTERVAL)) { + DEBUG("MOTION"); MLO = time; //save timestamp of event - sprintf(sendBuf, "MOTION BAT:%i", batteryReading); + sprintf(sendBuf, "MOTION LO:0s BAT:%sv", BATstr); sendLen = strlen(sendBuf); if (radio.sendWithRetry(GATEWAYID, sendBuf, sendLen)) { - DEBUGln(" ok!"); + DEBUGln("..OK"); #ifdef BLINK_EN Blink(LED,3); #endif } - else DEBUGln(" nothing..."); + else DEBUGln("..NOK"); radio.sleep(); - motionDetected=false; } else sendLoops++; - + //send readings every SENDEVERYXLOOPS if (sendLoops>=SENDEVERYXLOOPS) { sendLoops=0; - char periodO='X', periodC='X'; unsigned long lastOpened = (time - MLO) / 1000; //get seconds unsigned long LO = lastOpened; - char* MLOstr="LO:99d23h59m"; - char* BATstr="BAT:1024"; - char* BATactual="BATA:5.00v"; if (lastOpened <= 59) periodO = 's'; //1-59 seconds else if (lastOpened <= 3599) { periodO = 'm'; lastOpened/=60; } //1-59 minutes @@ -147,10 +168,7 @@ void loop() { sprintf(MLOstr, "LO:%ldh%ldm", lastOpened, (LO%3600)/60); else sprintf(MLOstr, "LO:%ld%c", lastOpened, periodO); - //sprintf(BATstr, "BAT:%i", batteryReading); - float battV = ((float)batteryReading * 3.3 * 9)/(1023*2.976); - dtostrf(battV, 3,2, BATactual); - sprintf(sendBuf, "%s BAT:%sv", MLOstr, BATactual); + sprintf(sendBuf, "%s BAT:%sv", MLOstr, BATstr); sendLen = strlen(sendBuf); radio.send(GATEWAYID, sendBuf, sendLen); radio.sleep(); @@ -159,12 +177,12 @@ void loop() { #ifdef BLINK_EN Blink(LED, 5); #endif - delay(10); motionDetected=false; //fix PIR false positive glitch } - - DEBUGln("LOOP"); + + motionDetected=false; //do NOT move this after the SLEEP line below or motion will never be detected time = time + 8000 + millis()-now + 480; //correct millis() resonator drift, may need to be tweaked to be accurate LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); + DEBUGln("WAKEUP"); } void Blink(byte PIN, int DELAY_MS) @@ -173,4 +191,26 @@ void Blink(byte PIN, int DELAY_MS) digitalWrite(PIN,HIGH); delay(DELAY_MS); digitalWrite(PIN,LOW); +} + +byte cycleCount=BATT_CYCLES; +void checkBattery() +{ + if (cycleCount++ == BATT_CYCLES) //only read battery every BATT_CYCLES sleep cycles + { + unsigned int reading=0; + //enable battery monitor + pinMode(A3, OUTPUT); + digitalWrite(A3, LOW); + + for (byte i=0; i<10; i++) + reading += analogRead(BATT_MONITOR); + + //disable battery monitor + pinMode(A3, INPUT); //highZ mode will allow p-mosfet to be pulled high and disconnect the voltage divider on the weather shield + batteryVolts = BATT_FORMULA(reading/10); + DEBUG("reading:"); DEBUGln(reading); + dtostrf(batteryVolts, 3,2, BATstr); + cycleCount = 0; + } } \ No newline at end of file diff --git a/Examples/RandomNumbers/RandomNumbers.ino b/Examples/RandomNumbers/RandomNumbers.ino index 85bb27c..86058a9 100644 --- a/Examples/RandomNumbers/RandomNumbers.ino +++ b/Examples/RandomNumbers/RandomNumbers.ino @@ -1,17 +1,48 @@ // Sample RFM69 random temperature sender at fixed intervals (F) -// Library and code by Felix Rusu - felix@lowpowerlab.com -// Get the RFM69 library at: https://github.com/LowPowerLab/ -#include //get it here: https://www.github.com/lowpowerlab/rfm69 -#include +// ********************************************************************************** +// Copyright Felix Rusu 2016, http://www.LowPowerLab.com/contact +// ********************************************************************************** +// License +// ********************************************************************************** +// This program is free software; you can redistribute it +// and/or modify it under the terms of the GNU General +// Public License as published by the Free Software +// Foundation; either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will +// be useful, but WITHOUT ANY WARRANTY; without even the +// implied warranty of MERCHANTABILITY or FITNESS FOR A +// PARTICULAR PURPOSE. See the GNU General Public +// License for more details. +// +// Licence can be viewed at +// http://www.gnu.org/licenses/gpl-3.0.txt +// +// Please maintain this license information along with authorship +// and copyright notices in any redistribution of this code +// ********************************************************************************** +#include //get it here: https://github.com/lowpowerlab/rfm69 +#include //get it here: https://github.com/lowpowerlab/RFM69 +#include //get it here: https://github.com/lowpowerlab/RFM69 +#include //get it here: https://github.com/lowpowerlab/spiflash +#include //included with Arduino IDE (www.arduino.cc) -#define NODEID 98 //unique for each node on same network +//**************************************************************************************************************** +//**** IMPORTANT RADIO SETTINGS - YOU MUST CHANGE/CONFIGURE TO MATCH YOUR HARDWARE TRANSCEIVER CONFIGURATION! **** +//**************************************************************************************************************** +#define NODEID 98 //unique for each node on same network #define NETWORKID 100 //the same on all nodes that talk to each other #define GATEWAYID 1 //Match frequency to the hardware version of the radio on your Moteino (uncomment one): -#define FREQUENCY RF69_433MHZ +//#define FREQUENCY RF69_433MHZ #define FREQUENCY RF69_915MHZ #define IS_RFM69HW //uncomment only for RFM69HW! Leave out if you have RFM69W! #define ENCRYPTKEY "sampleEncryptKey" //exactly the same 16 characters/bytes on all nodes! +//***************************************************************************************************************************** +#define ENABLE_ATC //comment out this line to disable AUTO TRANSMISSION CONTROL +#define ATC_RSSI -75 +//***************************************************************************************************************************** #define ACK_TIME 30 // max # of ms to wait for an ack #ifdef __AVR_ATmega1284P__ @@ -24,10 +55,15 @@ #define SERIAL_BAUD 115200 -int TRANSMITPERIOD = 10000; //transmit a packet to gateway so often (in ms) -char buff[35]; +int TRANSMITPERIOD = 2000; //transmit a packet to gateway so often (in ms) +char buff[50]; byte sendSize=0; -RFM69 radio; + +#ifdef ENABLE_ATC + RFM69_ATC radio; +#else + RFM69 radio; +#endif void setup() { Serial.begin(SERIAL_BAUD); @@ -36,8 +72,11 @@ void setup() { radio.setHighPower(); //uncomment only for RFM69HW! #endif radio.encrypt(ENCRYPTKEY); - - char buff[50]; + +#ifdef ENABLE_ATC + radio.enableAutoPower(ATC_RSSI); +#endif + sprintf(buff, "\nTransmitting at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915); Serial.println(buff); } @@ -90,4 +129,4 @@ void Blink(byte PIN, int DELAY_MS) digitalWrite(PIN,HIGH); delay(DELAY_MS); digitalWrite(PIN,LOW); -} +} \ No newline at end of file