2013-10-11 03:52:52 +01:00
|
|
|
// **********************************************************************************************************
|
2016-11-18 02:13:22 +00:00
|
|
|
// GarageMote garage door controller sketch that works with Moteinos equipped with RFM69W/RFM69HW/RFM69CW/RFM69HCW
|
|
|
|
|
// Monitors door position (open, closed, closing, unknown)
|
|
|
|
|
// Can trigger door open/close
|
2015-02-03 02:12:19 +00:00
|
|
|
// http://www.LowPowerLab.com/GarageMote
|
2016-11-18 02:13:22 +00:00
|
|
|
// **********************************************************************************
|
2018-05-01 17:07:02 +01:00
|
|
|
// Copyright Felix Rusu 2018, http://www.LowPowerLab.com/contact
|
2014-09-22 16:17:42 +01:00
|
|
|
// **********************************************************************************
|
2015-02-03 02:12:19 +00:00
|
|
|
// 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
|
2016-11-18 02:13:22 +00:00
|
|
|
// **********************************************************************************
|
2015-02-03 02:12:19 +00:00
|
|
|
//#define WEATHERSHIELD //uncomment if WeatherShield is present to report temp/humidity/pressure periodically
|
2015-03-10 14:02:19 +00:00
|
|
|
//#define WEATHERSENDDELAY 300000 // send WeatherShield data every so often (ms)
|
2015-02-03 02:12:19 +00:00
|
|
|
// ***************************************************************************************************************************
|
2016-11-18 02:13:22 +00:00
|
|
|
#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)
|
2013-10-11 03:52:52 +01:00
|
|
|
|
2017-03-31 17:21:37 +01:00
|
|
|
//For WeatherShield with BME280 see the WeatherNode example
|
2015-02-03 02:12:19 +00:00
|
|
|
#ifdef WEATHERSHIELD
|
2018-05-01 17:07:02 +01:00
|
|
|
#include <SparkFunBME280.h> //get it here: https://github.com/sparkfun/SparkFun_BME280_Arduino_Library/tree/master/src
|
2015-02-03 02:12:19 +00:00
|
|
|
#include <Wire.h>
|
|
|
|
|
#endif
|
2016-11-18 02:13:22 +00:00
|
|
|
//****************************************************************************************************************
|
|
|
|
|
//**** IMPORTANT RADIO SETTINGS - YOU MUST CHANGE/CONFIGURE TO MATCH YOUR HARDWARE TRANSCEIVER CONFIGURATION! ****
|
|
|
|
|
//****************************************************************************************************************
|
2014-09-22 16:17:42 +01:00
|
|
|
#define GATEWAYID 1
|
2015-02-03 02:12:19 +00:00
|
|
|
#define NODEID 11
|
2018-05-01 17:07:02 +01:00
|
|
|
#define NETWORKID 100
|
2014-09-22 16:17:42 +01:00
|
|
|
//#define FREQUENCY RF69_433MHZ
|
|
|
|
|
//#define FREQUENCY RF69_868MHZ
|
|
|
|
|
#define FREQUENCY RF69_915MHZ //Match this with the version of your Moteino! (others: RF69_433MHZ, RF69_868MHZ)
|
|
|
|
|
#define ENCRYPTKEY "sampleEncryptKey" //has to be same 16 characters/bytes on all nodes, not more not less!
|
2017-03-31 17:21:37 +01:00
|
|
|
#define IS_RFM69HW_HCW //uncomment only for RFM69HW/HCW! Leave out if you have RFM69W/CW!
|
2016-11-18 02:13:22 +00:00
|
|
|
//*****************************************************************************************************************************
|
|
|
|
|
#define ENABLE_ATC //comment out this line to disable AUTO TRANSMISSION CONTROL
|
|
|
|
|
#define ATC_RSSI -75
|
|
|
|
|
//*****************************************************************************************************************************
|
2013-10-11 03:52:52 +01:00
|
|
|
#define HALLSENSOR1 A0
|
|
|
|
|
#define HALLSENSOR1_EN 4
|
|
|
|
|
#define HALLSENSOR2 A1
|
|
|
|
|
#define HALLSENSOR2_EN 5
|
|
|
|
|
|
|
|
|
|
#define RELAYPIN1 6
|
|
|
|
|
#define RELAYPIN2 7
|
|
|
|
|
#define RELAY_PULSE_MS 250 //just enough that the opener will pick it up
|
|
|
|
|
|
|
|
|
|
#define DOOR_MOVEMENT_TIME 14000 // this has to be at least as long as the max between [door opening time, door closing time]
|
|
|
|
|
// my door opens and closes in about 12s
|
|
|
|
|
#define STATUS_CHANGE_MIN 1500 // this has to be at least as long as the delay
|
|
|
|
|
// between a opener button press and door movement start
|
|
|
|
|
// most garage doors will start moving immediately (within half a second)
|
|
|
|
|
//*****************************************************************************************************************************
|
|
|
|
|
#define HALLSENSOR_OPENSIDE 0
|
|
|
|
|
#define HALLSENSOR_CLOSEDSIDE 1
|
|
|
|
|
|
|
|
|
|
#define STATUS_CLOSED 0
|
|
|
|
|
#define STATUS_CLOSING 1
|
|
|
|
|
#define STATUS_OPENING 2
|
|
|
|
|
#define STATUS_OPEN 3
|
|
|
|
|
#define STATUS_UNKNOWN 4
|
|
|
|
|
|
|
|
|
|
#define LED 9 //pin connected to onboard LED
|
|
|
|
|
#define LED_PULSE_PERIOD 5000 //5s seems good value for pulsing/blinking (not too fast/slow)
|
|
|
|
|
#define SERIAL_BAUD 115200
|
|
|
|
|
#define SERIAL_EN //comment out if you don't want any serial output
|
|
|
|
|
|
|
|
|
|
#ifdef SERIAL_EN
|
|
|
|
|
#define DEBUG(input) {Serial.print(input); delay(1);}
|
|
|
|
|
#define DEBUGln(input) {Serial.println(input); delay(1);}
|
|
|
|
|
#else
|
|
|
|
|
#define DEBUG(input);
|
|
|
|
|
#define DEBUGln(input);
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-02-03 02:12:19 +00:00
|
|
|
#ifdef WEATHERSHIELD
|
2018-05-01 17:07:02 +01:00
|
|
|
BME280 bme280;
|
2015-02-03 02:12:19 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
//function prototypes
|
2013-10-11 03:52:52 +01:00
|
|
|
void setStatus(byte newSTATUS, boolean reportStatus=true);
|
2015-05-05 20:37:39 +01:00
|
|
|
void reportStatus();
|
2015-02-03 02:12:19 +00:00
|
|
|
boolean hallSensorRead(byte which);
|
|
|
|
|
void pulseRelay();
|
|
|
|
|
|
|
|
|
|
//global program variables
|
2013-10-11 03:52:52 +01:00
|
|
|
byte STATUS;
|
2014-09-22 16:17:42 +01:00
|
|
|
unsigned long lastStatusTimestamp=0;
|
|
|
|
|
unsigned long ledPulseTimestamp=0;
|
2015-02-03 02:12:19 +00:00
|
|
|
unsigned long lastWeatherSent=0;
|
2013-10-11 03:52:52 +01:00
|
|
|
int ledPulseValue=0;
|
|
|
|
|
boolean ledPulseDirection=false; //false=down, true=up
|
2015-05-05 20:37:39 +01:00
|
|
|
char Pstr[10];
|
2018-05-01 17:07:02 +01:00
|
|
|
char Fstr[10];
|
|
|
|
|
char Hstr[10];
|
|
|
|
|
double F,P,H;
|
2015-02-03 02:12:19 +00:00
|
|
|
char sendBuf[30];
|
|
|
|
|
SPIFlash flash(8, 0xEF30); //WINDBOND 4MBIT flash chip on CS pin D8 (default for Moteino)
|
2013-10-11 03:52:52 +01:00
|
|
|
|
2016-11-18 02:13:22 +00:00
|
|
|
#ifdef ENABLE_ATC
|
|
|
|
|
RFM69_ATC radio;
|
|
|
|
|
#else
|
|
|
|
|
RFM69 radio;
|
|
|
|
|
#endif
|
|
|
|
|
|
2013-10-11 03:52:52 +01:00
|
|
|
void setup(void)
|
|
|
|
|
{
|
2015-05-05 20:37:39 +01:00
|
|
|
#ifdef SERIAL_EN
|
2013-10-11 03:52:52 +01:00
|
|
|
Serial.begin(SERIAL_BAUD);
|
2015-05-05 20:37:39 +01:00
|
|
|
#endif
|
2013-10-11 03:52:52 +01:00
|
|
|
pinMode(HALLSENSOR1, INPUT);
|
|
|
|
|
pinMode(HALLSENSOR2, INPUT);
|
|
|
|
|
pinMode(HALLSENSOR1_EN, OUTPUT);
|
|
|
|
|
pinMode(HALLSENSOR2_EN, OUTPUT);
|
|
|
|
|
pinMode(RELAYPIN1, OUTPUT);
|
|
|
|
|
pinMode(RELAYPIN2, OUTPUT);
|
|
|
|
|
pinMode(LED, OUTPUT);
|
|
|
|
|
|
|
|
|
|
radio.initialize(FREQUENCY,NODEID,NETWORKID);
|
2017-03-31 17:21:37 +01:00
|
|
|
#ifdef IS_RFM69HW_HCW
|
|
|
|
|
radio.setHighPower(); //must include this only for RFM69HW/HCW!
|
2013-11-11 23:26:36 +00:00
|
|
|
#endif
|
|
|
|
|
radio.encrypt(ENCRYPTKEY);
|
2013-10-11 03:52:52 +01:00
|
|
|
|
2016-11-18 02:13:22 +00:00
|
|
|
#ifdef ENABLE_ATC
|
|
|
|
|
radio.enableAutoPower(ATC_RSSI);
|
|
|
|
|
#endif
|
|
|
|
|
|
2013-10-11 03:52:52 +01:00
|
|
|
char buff[50];
|
|
|
|
|
sprintf(buff, "GarageMote : %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
|
2014-09-22 16:17:42 +01:00
|
|
|
DEBUGln(buff);
|
2013-10-11 03:52:52 +01:00
|
|
|
|
|
|
|
|
if (hallSensorRead(HALLSENSOR_OPENSIDE)==true)
|
|
|
|
|
setStatus(STATUS_OPEN);
|
|
|
|
|
if (hallSensorRead(HALLSENSOR_CLOSEDSIDE)==true)
|
|
|
|
|
setStatus(STATUS_CLOSED);
|
|
|
|
|
else setStatus(STATUS_UNKNOWN);
|
2015-02-03 02:12:19 +00:00
|
|
|
|
|
|
|
|
#ifdef WEATHERSHIELD
|
2018-05-01 17:07:02 +01:00
|
|
|
Wire.begin();
|
|
|
|
|
Wire.setClock(400000); //Increase to fast I2C speed!
|
|
|
|
|
|
|
|
|
|
//initialize weather shield BME280 sensor
|
|
|
|
|
bme280.setI2CAddress(0x77); //0x76,0x77 is valid.
|
|
|
|
|
bme280.beginI2C();
|
|
|
|
|
bme280.setMode(MODE_SLEEP); //MODE_SLEEP, MODE_FORCED, MODE_NORMAL is valid. See 3.3
|
|
|
|
|
bme280.setStandbyTime(0); //0 to 7 valid. Time between readings. See table 27.
|
|
|
|
|
bme280.setFilter(0); //0 to 4 is valid. Filter coefficient. See 3.4.4
|
|
|
|
|
bme280.setTempOverSample(1); //0 to 16 are valid. 0 disables temp sensing. See table 24.
|
|
|
|
|
bme280.setPressureOverSample(1); //0 to 16 are valid. 0 disables pressure sensing. See table 23.
|
|
|
|
|
bme280.setHumidityOverSample(1); //0 to 16 are valid. 0 disables humidity sensing. See table 19.
|
|
|
|
|
P = bme280.readFloatPressure() * 0.0002953; //read Pa and convert to inHg
|
|
|
|
|
F = bme280.readTempF();
|
|
|
|
|
H = bme280.readFloatHumidity();
|
|
|
|
|
|
2015-02-03 02:12:19 +00:00
|
|
|
#endif
|
2013-10-11 03:52:52 +01:00
|
|
|
}
|
|
|
|
|
|
2014-09-22 16:17:42 +01:00
|
|
|
unsigned long doorPulseCount = 0;
|
2015-05-05 20:37:39 +01:00
|
|
|
char input=0;
|
2013-10-11 03:52:52 +01:00
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
|
{
|
2015-05-05 20:37:39 +01:00
|
|
|
#ifdef SERIAL_EN
|
2013-10-11 03:52:52 +01:00
|
|
|
if (Serial.available())
|
|
|
|
|
input = Serial.read();
|
2015-05-05 20:37:39 +01:00
|
|
|
#endif
|
|
|
|
|
|
2013-10-11 03:52:52 +01:00
|
|
|
if (input=='r')
|
|
|
|
|
{
|
2014-09-22 16:17:42 +01:00
|
|
|
DEBUGln("Relay test...");
|
2013-10-11 03:52:52 +01:00
|
|
|
pulseRelay();
|
|
|
|
|
input = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UNKNOWN => OPEN/CLOSED
|
2014-09-22 16:17:42 +01:00
|
|
|
if (STATUS == STATUS_UNKNOWN && millis()-(lastStatusTimestamp)>STATUS_CHANGE_MIN)
|
2013-10-11 03:52:52 +01:00
|
|
|
{
|
|
|
|
|
if (hallSensorRead(HALLSENSOR_OPENSIDE)==true)
|
|
|
|
|
setStatus(STATUS_OPEN);
|
|
|
|
|
if (hallSensorRead(HALLSENSOR_CLOSEDSIDE)==true)
|
|
|
|
|
setStatus(STATUS_CLOSED);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-22 16:17:42 +01:00
|
|
|
// OPEN => CLOSING
|
|
|
|
|
if (STATUS == STATUS_OPEN && millis()-(lastStatusTimestamp)>STATUS_CHANGE_MIN)
|
2013-10-11 03:52:52 +01:00
|
|
|
{
|
|
|
|
|
if (hallSensorRead(HALLSENSOR_OPENSIDE)==false)
|
|
|
|
|
setStatus(STATUS_CLOSING);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CLOSED => OPENING
|
2014-09-22 16:17:42 +01:00
|
|
|
if (STATUS == STATUS_CLOSED && millis()-(lastStatusTimestamp)>STATUS_CHANGE_MIN)
|
2013-10-11 03:52:52 +01:00
|
|
|
{
|
|
|
|
|
if (hallSensorRead(HALLSENSOR_CLOSEDSIDE)==false)
|
|
|
|
|
setStatus(STATUS_OPENING);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// OPENING/CLOSING => OPEN (when door returns to open due to obstacle or toggle action)
|
|
|
|
|
// => CLOSED (when door closes normally from OPEN)
|
|
|
|
|
// => UNKNOWN (when more time passes than normally would for a door up/down movement)
|
2014-09-22 16:17:42 +01:00
|
|
|
if ((STATUS == STATUS_OPENING || STATUS == STATUS_CLOSING) && millis()-(lastStatusTimestamp)>STATUS_CHANGE_MIN)
|
2013-10-11 03:52:52 +01:00
|
|
|
{
|
|
|
|
|
if (hallSensorRead(HALLSENSOR_OPENSIDE)==true)
|
|
|
|
|
setStatus(STATUS_OPEN);
|
|
|
|
|
else if (hallSensorRead(HALLSENSOR_CLOSEDSIDE)==true)
|
|
|
|
|
setStatus(STATUS_CLOSED);
|
2014-09-22 16:17:42 +01:00
|
|
|
else if (millis()-(lastStatusTimestamp)>DOOR_MOVEMENT_TIME)
|
2013-10-11 03:52:52 +01:00
|
|
|
setStatus(STATUS_UNKNOWN);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (radio.receiveDone())
|
|
|
|
|
{
|
|
|
|
|
byte newStatus=STATUS;
|
|
|
|
|
boolean reportStatusRequest=false;
|
2014-09-22 16:17:42 +01:00
|
|
|
DEBUG('[');DEBUG(radio.SENDERID);DEBUG("] ");
|
2013-10-11 03:52:52 +01:00
|
|
|
for (byte i = 0; i < radio.DATALEN; i++)
|
|
|
|
|
DEBUG((char)radio.DATA[i]);
|
|
|
|
|
|
2014-09-22 16:17:42 +01:00
|
|
|
if (radio.DATALEN==3)
|
2014-09-22 16:12:01 +01:00
|
|
|
{
|
2014-09-22 16:17:42 +01:00
|
|
|
//check for an OPEN/CLOSE/STATUS request
|
|
|
|
|
if (radio.DATA[0]=='O' && radio.DATA[1]=='P' && radio.DATA[2]=='N')
|
|
|
|
|
{
|
|
|
|
|
if (millis()-(lastStatusTimestamp) > STATUS_CHANGE_MIN && (STATUS == STATUS_CLOSED || STATUS == STATUS_CLOSING || STATUS == STATUS_UNKNOWN))
|
|
|
|
|
newStatus = STATUS_OPENING;
|
|
|
|
|
//else radio.Send(requester, "INVALID", 7);
|
|
|
|
|
}
|
|
|
|
|
if (radio.DATA[0]=='C' && radio.DATA[1]=='L' && radio.DATA[2]=='S')
|
|
|
|
|
{
|
|
|
|
|
if (millis()-(lastStatusTimestamp) > STATUS_CHANGE_MIN && (STATUS == STATUS_OPEN || STATUS == STATUS_OPENING || STATUS == STATUS_UNKNOWN))
|
|
|
|
|
newStatus = STATUS_CLOSING;
|
|
|
|
|
//else radio.Send(requester, "INVALID", 7);
|
|
|
|
|
}
|
|
|
|
|
if (radio.DATA[0]=='S' && radio.DATA[1]=='T' && radio.DATA[2]=='S')
|
|
|
|
|
{
|
|
|
|
|
reportStatusRequest = true;
|
|
|
|
|
}
|
2013-10-11 03:52:52 +01:00
|
|
|
}
|
2014-09-22 16:17:42 +01:00
|
|
|
|
|
|
|
|
// wireless programming token check
|
|
|
|
|
// DO NOT REMOVE, or GarageMote will not be wirelessly programmable any more!
|
|
|
|
|
CheckForWirelessHEX(radio, flash, true);
|
2013-10-11 03:52:52 +01:00
|
|
|
|
|
|
|
|
//first send any ACK to request
|
2014-09-22 16:17:42 +01:00
|
|
|
DEBUG(" [RX_RSSI:");DEBUG(radio.RSSI);DEBUG("]");
|
2014-08-13 03:38:58 +01:00
|
|
|
if (radio.ACKRequested())
|
2013-10-11 03:52:52 +01:00
|
|
|
{
|
|
|
|
|
radio.sendACK();
|
|
|
|
|
DEBUG(" - ACK sent.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//now take care of the request, if not invalid
|
|
|
|
|
if (STATUS != newStatus)
|
|
|
|
|
{
|
|
|
|
|
pulseRelay();
|
|
|
|
|
setStatus(newStatus);
|
|
|
|
|
}
|
|
|
|
|
if (reportStatusRequest)
|
|
|
|
|
{
|
|
|
|
|
reportStatus();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DEBUGln();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//use LED to visually indicate STATUS
|
|
|
|
|
if (STATUS == STATUS_OPEN || STATUS == STATUS_CLOSED) //solid ON/OFF
|
|
|
|
|
{
|
|
|
|
|
digitalWrite(LED, STATUS == STATUS_OPEN ? LOW : HIGH);
|
|
|
|
|
}
|
|
|
|
|
if (STATUS == STATUS_OPENING || STATUS == STATUS_CLOSING) //pulse
|
|
|
|
|
{
|
2014-09-22 16:17:42 +01:00
|
|
|
if (millis()-(ledPulseTimestamp) > LED_PULSE_PERIOD/256)
|
2013-10-11 03:52:52 +01:00
|
|
|
{
|
|
|
|
|
ledPulseValue = ledPulseDirection ? ledPulseValue + LED_PULSE_PERIOD/256 : ledPulseValue - LED_PULSE_PERIOD/256;
|
|
|
|
|
|
|
|
|
|
if (ledPulseDirection && ledPulseValue > 255)
|
|
|
|
|
{
|
|
|
|
|
ledPulseDirection=false;
|
|
|
|
|
ledPulseValue = 255;
|
|
|
|
|
}
|
|
|
|
|
else if (!ledPulseDirection && ledPulseValue < 0)
|
|
|
|
|
{
|
|
|
|
|
ledPulseDirection=true;
|
|
|
|
|
ledPulseValue = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
analogWrite(LED, ledPulseValue);
|
|
|
|
|
ledPulseTimestamp = millis();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (STATUS == STATUS_UNKNOWN) //blink
|
|
|
|
|
{
|
2014-09-22 16:17:42 +01:00
|
|
|
if (millis()-(ledPulseTimestamp) > LED_PULSE_PERIOD/20)
|
2013-10-11 03:52:52 +01:00
|
|
|
{
|
|
|
|
|
ledPulseDirection = !ledPulseDirection;
|
|
|
|
|
digitalWrite(LED, ledPulseDirection ? HIGH : LOW);
|
|
|
|
|
ledPulseTimestamp = millis();
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-02-03 02:12:19 +00:00
|
|
|
|
|
|
|
|
#ifdef WEATHERSHIELD
|
|
|
|
|
if (millis()-lastWeatherSent > WEATHERSENDDELAY)
|
|
|
|
|
{
|
|
|
|
|
lastWeatherSent = millis();
|
2018-05-01 17:07:02 +01:00
|
|
|
bme280.setMode(MODE_FORCED); //Wake up sensor and take reading
|
|
|
|
|
P = bme280.readFloatPressure() * 0.0002953; //read Pa and convert to inHg
|
|
|
|
|
F = bme280.readTempF();
|
|
|
|
|
H = bme280.readFloatHumidity();
|
|
|
|
|
|
|
|
|
|
dtostrf(F, 3,2, Fstr);
|
|
|
|
|
dtostrf(H, 3,2, Hstr);
|
2015-02-03 02:12:19 +00:00
|
|
|
dtostrf(P, 3,2, Pstr);
|
2018-05-01 17:07:02 +01:00
|
|
|
|
|
|
|
|
sprintf(sendBuf, "F:%s H:%s P:%s", Fstr, Hstr, Pstr);
|
2015-02-03 02:12:19 +00:00
|
|
|
byte sendLen = strlen(sendBuf);
|
|
|
|
|
radio.send(GATEWAYID, sendBuf, sendLen);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2013-10-11 03:52:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//returns TRUE if magnet is next to sensor, FALSE if magnet is away
|
|
|
|
|
boolean hallSensorRead(byte which)
|
|
|
|
|
{
|
|
|
|
|
//while(millis()-lastStatusTimestamp<STATUS_CHANGE_MIN);
|
|
|
|
|
digitalWrite(which ? HALLSENSOR2_EN : HALLSENSOR1_EN, HIGH); //turn sensor ON
|
|
|
|
|
delay(1); //wait a little
|
|
|
|
|
byte reading = digitalRead(which ? HALLSENSOR2 : HALLSENSOR1);
|
|
|
|
|
digitalWrite(which ? HALLSENSOR2_EN : HALLSENSOR1_EN, LOW); //turn sensor OFF
|
|
|
|
|
return reading==0;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-05 20:37:39 +01:00
|
|
|
void setStatus(byte newSTATUS, boolean reportIt)
|
2013-10-11 03:52:52 +01:00
|
|
|
{
|
|
|
|
|
if (STATUS != newSTATUS) lastStatusTimestamp = millis();
|
|
|
|
|
STATUS = newSTATUS;
|
|
|
|
|
DEBUGln(STATUS==STATUS_CLOSED ? "CLOSED" : STATUS==STATUS_CLOSING ? "CLOSING" : STATUS==STATUS_OPENING ? "OPENING" : STATUS==STATUS_OPEN ? "OPEN" : "UNKNOWN");
|
2015-05-05 20:37:39 +01:00
|
|
|
if (reportIt)
|
2013-10-11 03:52:52 +01:00
|
|
|
reportStatus();
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-05 20:37:39 +01:00
|
|
|
void reportStatus(void)
|
2013-10-11 03:52:52 +01:00
|
|
|
{
|
|
|
|
|
char buff[10];
|
|
|
|
|
sprintf(buff, STATUS==STATUS_CLOSED ? "CLOSED" : STATUS==STATUS_CLOSING ? "CLOSING" : STATUS==STATUS_OPENING ? "OPENING" : STATUS==STATUS_OPEN ? "OPEN" : "UNKNOWN");
|
|
|
|
|
byte len = strlen(buff);
|
2015-05-05 20:37:39 +01:00
|
|
|
radio.sendWithRetry(GATEWAYID, buff, len);
|
2013-10-11 03:52:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pulseRelay()
|
|
|
|
|
{
|
|
|
|
|
digitalWrite(RELAYPIN1, HIGH);
|
|
|
|
|
digitalWrite(RELAYPIN2, HIGH);
|
|
|
|
|
delay(RELAY_PULSE_MS);
|
|
|
|
|
digitalWrite(RELAYPIN1, LOW);
|
|
|
|
|
digitalWrite(RELAYPIN2, LOW);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Blink(byte PIN, byte DELAY_MS)
|
|
|
|
|
{
|
|
|
|
|
pinMode(PIN, OUTPUT);
|
|
|
|
|
digitalWrite(PIN,HIGH);
|
|
|
|
|
delay(DELAY_MS);
|
|
|
|
|
digitalWrite(PIN,LOW);
|
2015-02-03 02:12:19 +00:00
|
|
|
}
|