update examples
This commit is contained in:
parent
6628c26c91
commit
dd9a08cce5
|
|
@ -6,11 +6,8 @@
|
||||||
// motion event happened (days, hours, minutes, seconds ago) and the battery level
|
// motion event happened (days, hours, minutes, seconds ago) and the battery level
|
||||||
// In sleep mode, Moteino + PIR motion sensor use about ~78uA
|
// In sleep mode, Moteino + PIR motion sensor use about ~78uA
|
||||||
// Make sure you adjust the settings in the configuration section below !!!
|
// Make sure you adjust the settings in the configuration section below !!!
|
||||||
|
|
||||||
// **********************************************************************************
|
// **********************************************************************************
|
||||||
// Copyright Felix Rusu, LowPowerLab.com
|
// Copyright Felix Rusu 2016, http://www.LowPowerLab.com/contact
|
||||||
// Library and code by Felix Rusu - felix@lowpowerlab.com
|
|
||||||
// Get the RFM69 and SPIFlash library at: https://github.com/LowPowerLab/
|
|
||||||
// **********************************************************************************
|
// **********************************************************************************
|
||||||
// License
|
// License
|
||||||
// **********************************************************************************
|
// **********************************************************************************
|
||||||
|
|
@ -26,45 +23,50 @@
|
||||||
// PARTICULAR PURPOSE. See the GNU General Public
|
// PARTICULAR PURPOSE. See the GNU General Public
|
||||||
// License for more details.
|
// License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General
|
|
||||||
// Public License along with this program.
|
|
||||||
// If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
//
|
|
||||||
// Licence can be viewed at
|
// Licence can be viewed at
|
||||||
// http://www.gnu.org/licenses/gpl-3.0.txt
|
// http://www.gnu.org/licenses/gpl-3.0.txt
|
||||||
//
|
//
|
||||||
// Please maintain this license information along with authorship
|
// Please maintain this license information along with authorship
|
||||||
// and copyright notices in any redistribution of this code
|
// and copyright notices in any redistribution of this code
|
||||||
// **********************************************************************************
|
// **********************************************************************************
|
||||||
|
|
||||||
#include <RFM69.h> //get it here: https://github.com/LowPowerLab/RFM69
|
#include <RFM69.h> //get it here: https://github.com/LowPowerLab/RFM69
|
||||||
#include <SPI.h> //get it here: https://github.com/LowPowerLab/SPIFlash
|
#include <RFM69_ATC.h>//get it here: https://github.com/lowpowerlab/RFM69
|
||||||
|
#include <SPIFlash.h> //get it here: https://github.com/lowpowerlab/spiflash
|
||||||
|
#include <SPI.h> //included with Arduino IDE (www.arduino.cc)
|
||||||
#include <LowPower.h> //get library from: https://github.com/LowPowerLab/LowPower
|
#include <LowPower.h> //get library from: https://github.com/LowPowerLab/LowPower
|
||||||
//writeup here: http://www.rocketscream.com/blog/2011/07/04/lightweight-low-power-arduino-library/
|
//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 *************
|
//**** IMPORTANT RADIO SETTINGS - YOU MUST CHANGE/CONFIGURE TO MATCH YOUR HARDWARE TRANSCEIVER CONFIGURATION! ****
|
||||||
//*********************************************************************************************
|
//****************************************************************************************************************
|
||||||
#define NODEID 55 //unique for each node on same network
|
#define NODEID 55 //unique for each node on same network
|
||||||
#define NETWORKID 250 //the same on all nodes that talk to each other
|
#define NETWORKID 100 //the same on all nodes that talk to each other
|
||||||
#define GATEWAYID 1
|
#define GATEWAYID 1
|
||||||
//Match frequency to the hardware version of the radio on your Moteino (uncomment one):
|
//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_868MHZ
|
//#define FREQUENCY RF69_868MHZ
|
||||||
#define FREQUENCY RF69_915MHZ
|
#define FREQUENCY RF69_915MHZ
|
||||||
|
//#define FREQUENCY_EXACT 917000000
|
||||||
#define IS_RFM69HW //uncomment only for RFM69HW! Leave out if you have RFM69W!
|
#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 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 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 MOTION_PIN 3 // 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 MOTION_IRQ 1 // hardware interrupt 1 (D3) - where motion sensors OUTput is connected, this will generate an interrupt every time there is MOTION
|
||||||
#define LED 9 // Moteinos have LEDs on D9
|
#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 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
|
//#define SERIAL_EN //uncomment this line to enable serial IO debug messages, leave out if you want low power
|
||||||
#ifdef SERIAL_EN
|
#ifdef SERIAL_EN
|
||||||
|
#define SERIAL_BAUD 115200
|
||||||
#define DEBUG(input) {Serial.print(input); delay(1);}
|
#define DEBUG(input) {Serial.print(input); delay(1);}
|
||||||
#define DEBUGln(input) {Serial.println(input); delay(1);}
|
#define DEBUGln(input) {Serial.println(input); delay(1);}
|
||||||
#else
|
#else
|
||||||
|
|
@ -72,8 +74,21 @@
|
||||||
#define DEBUGln(input);
|
#define DEBUGln(input);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
RFM69 radio;
|
#ifdef ENABLE_ATC
|
||||||
|
RFM69_ATC radio;
|
||||||
|
#else
|
||||||
|
RFM69 radio;
|
||||||
|
#endif
|
||||||
|
|
||||||
volatile boolean motionDetected=false;
|
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() {
|
void setup() {
|
||||||
#ifdef SERIAL_EN
|
#ifdef SERIAL_EN
|
||||||
|
|
@ -84,43 +99,53 @@ void setup() {
|
||||||
radio.setHighPower(); //uncomment only for RFM69HW!
|
radio.setHighPower(); //uncomment only for RFM69HW!
|
||||||
#endif
|
#endif
|
||||||
radio.encrypt(ENCRYPTKEY);
|
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];
|
char buff[50];
|
||||||
sprintf(buff, "\nTransmitting at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
|
sprintf(buff, "\nTransmitting at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
|
||||||
DEBUGln(buff);
|
DEBUGln(buff);
|
||||||
|
checkBattery();
|
||||||
}
|
}
|
||||||
|
|
||||||
void motionIRQ()
|
void motionIRQ()
|
||||||
{
|
{
|
||||||
motionDetected=true;
|
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() {
|
void loop() {
|
||||||
now = millis();
|
now = millis();
|
||||||
int batteryReading = analogRead(BATTERYSENSE);
|
checkBattery();
|
||||||
if (motionDetected && time-MLO > 20000) //avoid duplicates in 20second intervals
|
|
||||||
|
if (motionDetected && (time-MLO > DUPLICATE_INTERVAL))
|
||||||
{
|
{
|
||||||
|
DEBUG("MOTION");
|
||||||
MLO = time; //save timestamp of event
|
MLO = time; //save timestamp of event
|
||||||
sprintf(sendBuf, "MOTION BAT:%i", batteryReading);
|
sprintf(sendBuf, "MOTION LO:0s BAT:%sv", BATstr);
|
||||||
sendLen = strlen(sendBuf);
|
sendLen = strlen(sendBuf);
|
||||||
if (radio.sendWithRetry(GATEWAYID, sendBuf, sendLen))
|
if (radio.sendWithRetry(GATEWAYID, sendBuf, sendLen))
|
||||||
{
|
{
|
||||||
DEBUGln(" ok!");
|
DEBUGln("..OK");
|
||||||
#ifdef BLINK_EN
|
#ifdef BLINK_EN
|
||||||
Blink(LED,3);
|
Blink(LED,3);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
else DEBUGln(" nothing...");
|
else DEBUGln("..NOK");
|
||||||
radio.sleep();
|
radio.sleep();
|
||||||
motionDetected=false;
|
|
||||||
}
|
}
|
||||||
else sendLoops++;
|
else sendLoops++;
|
||||||
|
|
||||||
|
|
@ -128,13 +153,9 @@ void loop() {
|
||||||
if (sendLoops>=SENDEVERYXLOOPS)
|
if (sendLoops>=SENDEVERYXLOOPS)
|
||||||
{
|
{
|
||||||
sendLoops=0;
|
sendLoops=0;
|
||||||
|
|
||||||
char periodO='X', periodC='X';
|
char periodO='X', periodC='X';
|
||||||
unsigned long lastOpened = (time - MLO) / 1000; //get seconds
|
unsigned long lastOpened = (time - MLO) / 1000; //get seconds
|
||||||
unsigned long LO = lastOpened;
|
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
|
if (lastOpened <= 59) periodO = 's'; //1-59 seconds
|
||||||
else if (lastOpened <= 3599) { periodO = 'm'; lastOpened/=60; } //1-59 minutes
|
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);
|
sprintf(MLOstr, "LO:%ldh%ldm", lastOpened, (LO%3600)/60);
|
||||||
else sprintf(MLOstr, "LO:%ld%c", lastOpened, periodO);
|
else sprintf(MLOstr, "LO:%ld%c", lastOpened, periodO);
|
||||||
|
|
||||||
//sprintf(BATstr, "BAT:%i", batteryReading);
|
sprintf(sendBuf, "%s BAT:%sv", MLOstr, BATstr);
|
||||||
float battV = ((float)batteryReading * 3.3 * 9)/(1023*2.976);
|
|
||||||
dtostrf(battV, 3,2, BATactual);
|
|
||||||
sprintf(sendBuf, "%s BAT:%sv", MLOstr, BATactual);
|
|
||||||
sendLen = strlen(sendBuf);
|
sendLen = strlen(sendBuf);
|
||||||
radio.send(GATEWAYID, sendBuf, sendLen);
|
radio.send(GATEWAYID, sendBuf, sendLen);
|
||||||
radio.sleep();
|
radio.sleep();
|
||||||
|
|
@ -159,12 +177,12 @@ void loop() {
|
||||||
#ifdef BLINK_EN
|
#ifdef BLINK_EN
|
||||||
Blink(LED, 5);
|
Blink(LED, 5);
|
||||||
#endif
|
#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
|
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);
|
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
|
||||||
|
DEBUGln("WAKEUP");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Blink(byte PIN, int DELAY_MS)
|
void Blink(byte PIN, int DELAY_MS)
|
||||||
|
|
@ -174,3 +192,25 @@ void Blink(byte PIN, int DELAY_MS)
|
||||||
delay(DELAY_MS);
|
delay(DELAY_MS);
|
||||||
digitalWrite(PIN,LOW);
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,17 +1,48 @@
|
||||||
// Sample RFM69 random temperature sender at fixed intervals (F)
|
// 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/
|
// Copyright Felix Rusu 2016, http://www.LowPowerLab.com/contact
|
||||||
#include <RFM69.h> //get it here: https://www.github.com/lowpowerlab/rfm69
|
// **********************************************************************************
|
||||||
#include <SPI.h>
|
// 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 <RFM69.h> //get it here: https://github.com/lowpowerlab/rfm69
|
||||||
|
#include <RFM69_ATC.h> //get it here: https://github.com/lowpowerlab/RFM69
|
||||||
|
#include <RFM69_OTA.h> //get it here: https://github.com/lowpowerlab/RFM69
|
||||||
|
#include <SPIFlash.h> //get it here: https://github.com/lowpowerlab/spiflash
|
||||||
|
#include <SPI.h> //included with Arduino IDE (www.arduino.cc)
|
||||||
|
|
||||||
|
//****************************************************************************************************************
|
||||||
|
//**** IMPORTANT RADIO SETTINGS - YOU MUST CHANGE/CONFIGURE TO MATCH YOUR HARDWARE TRANSCEIVER CONFIGURATION! ****
|
||||||
|
//****************************************************************************************************************
|
||||||
#define NODEID 98 //unique for each node on same network
|
#define NODEID 98 //unique for each node on same network
|
||||||
#define NETWORKID 100 //the same on all nodes that talk to each other
|
#define NETWORKID 100 //the same on all nodes that talk to each other
|
||||||
#define GATEWAYID 1
|
#define GATEWAYID 1
|
||||||
//Match frequency to the hardware version of the radio on your Moteino (uncomment one):
|
//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 FREQUENCY RF69_915MHZ
|
||||||
#define IS_RFM69HW //uncomment only for RFM69HW! Leave out if you have RFM69W!
|
#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 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
|
#define ACK_TIME 30 // max # of ms to wait for an ack
|
||||||
|
|
||||||
#ifdef __AVR_ATmega1284P__
|
#ifdef __AVR_ATmega1284P__
|
||||||
|
|
@ -24,10 +55,15 @@
|
||||||
|
|
||||||
#define SERIAL_BAUD 115200
|
#define SERIAL_BAUD 115200
|
||||||
|
|
||||||
int TRANSMITPERIOD = 10000; //transmit a packet to gateway so often (in ms)
|
int TRANSMITPERIOD = 2000; //transmit a packet to gateway so often (in ms)
|
||||||
char buff[35];
|
char buff[50];
|
||||||
byte sendSize=0;
|
byte sendSize=0;
|
||||||
RFM69 radio;
|
|
||||||
|
#ifdef ENABLE_ATC
|
||||||
|
RFM69_ATC radio;
|
||||||
|
#else
|
||||||
|
RFM69 radio;
|
||||||
|
#endif
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(SERIAL_BAUD);
|
Serial.begin(SERIAL_BAUD);
|
||||||
|
|
@ -37,7 +73,10 @@ void setup() {
|
||||||
#endif
|
#endif
|
||||||
radio.encrypt(ENCRYPTKEY);
|
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);
|
sprintf(buff, "\nTransmitting at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
|
||||||
Serial.println(buff);
|
Serial.println(buff);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue