Skip to content

Instantly share code, notes, and snippets.

@remisarrailh
Last active August 18, 2018 10:55
Show Gist options
  • Save remisarrailh/f65148da28efb12af9510caea053a8c5 to your computer and use it in GitHub Desktop.
Save remisarrailh/f65148da28efb12af9510caea053a8c5 to your computer and use it in GitHub Desktop.
/**
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <[email protected]>
* Copyright (C) 2013-2015 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
* Support Forum: http://forum.mysensors.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
*******************************
*
* REVISION HISTORY
* Version 1.0 - Henrik Ekblad (http://www.mysensors.org/build/motion)
* Version 1.1 - Fork for Reedswitchs/Lora32u4 - Rémi Sarrailh
* Version 1.2 - Add Static ID - Rémi Sarrailh
* DESCRIPTION
* 2 Reedswitchs with Lora32u4Motion Sensor example
* https://github.com/madnerdorg/doorsensorRFM95
*
*/
#define MY_NODE_ID 2 // Static ID
#define MY_PARENT_NODE_ID 1 // Static ID
#define CHILD_DOOR 1 // Id for Door
#define CHILD_LOCK 2 // Id for lock
// Enable and select radio type attached
#define MY_RADIO_RFM95
#define MY_RFM95_ENABLE_ENCRYPTION
#define MY_RFM95_IRQ_PIN (7)
#define MY_RFM95_IRQ_NUM digitalPinToInterrupt(7)
#define MY_RFM95_CS_PIN 8
const int VBATPIN = 9;
// Enable debug prints
// #define MY_DEBUG
#include <MySensors.h>
uint32_t SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
#define DIGITAL_INPUT_DOOR 2 // The digital input you attached your motion sensor. (Only 2 and 3 generates interrupt!)
#define DIGITAL_INPUT_LOCK 3
// Initialize door message
MyMessage msg_door(CHILD_DOOR, V_TRIPPED);
MyMessage msg_lock(CHILD_LOCK, V_TRIPPED);
void setup()
{
pinMode(DIGITAL_INPUT_DOOR, INPUT); // sets the motion sensor digital pin as input
pinMode(DIGITAL_INPUT_LOCK, INPUT);
//Serial.begin(115200);
}
void presentation()
{
// Send the sketch version information to the gateway and Controller
sendSketchInfo("Door Sensor", "1.2"); //Add ACK (we need to be sure the message is received)
// Register all sensors to gw (they will be created as child devices)
present(CHILD_DOOR, S_DOOR);
present(CHILD_LOCK, S_DOOR);
}
void loop()
{
// Read digital motion value
bool tripped_door = digitalRead(DIGITAL_INPUT_DOOR);
bool tripped_lock = digitalRead(DIGITAL_INPUT_LOCK);
//Serial.print(tripped_door);
//Serial.println(tripped_lock);
send(msg_door.set(tripped_door?"1":"0")); // Send tripped value to gw
send(msg_lock.set(tripped_lock?"0":"1")); // Send inverted tripped value (reedswitch not connected means lock closed)
sendBatteryLevel(battery());
//delay(1000);
// Sleep until interrupt comes in on motion sensor. Send update every two minute.
sleep(digitalPinToInterrupt(DIGITAL_INPUT_DOOR), CHANGE, digitalPinToInterrupt(DIGITAL_INPUT_LOCK), CHANGE, SLEEP_TIME);
}
int battery() {
float measuredvbat = analogRead(VBATPIN);
measuredvbat = analogRead(VBATPIN);
measuredvbat *= 2; // we divided by 2, so multiply back
measuredvbat *= 3.3; // Multiply by 3.3V, our reference voltage
measuredvbat /= 1024; // convert to voltage
return map(measuredvbat, 3.2, 4.2, 0, 100);
}
#define MY_ENCRYPTION_SIMPLE_PASSWD "MyInsecurePassword"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment