|
| 1 | +/* |
| 2 | + * The MySensors Arduino library handles the wireless radio link and protocol |
| 3 | + * between your home built sensors/actuators and HA controller of choice. |
| 4 | + * The sensors forms a self healing radio network with optional repeaters. Each |
| 5 | + * repeater and gateway builds a routing tables in EEPROM which keeps track of the |
| 6 | + * network topology allowing messages to be routed to nodes. |
| 7 | + * |
| 8 | + * Created by Henrik Ekblad <[email protected]> |
| 9 | + * Copyright (C) 2013-2019 Sensnology AB |
| 10 | + * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors |
| 11 | + * |
| 12 | + * Documentation: http://www.mysensors.org |
| 13 | + * Support Forum: http://forum.mysensors.org |
| 14 | + * |
| 15 | + * This program is free software; you can redistribute it and/or |
| 16 | + * modify it under the terms of the GNU General Public License |
| 17 | + * version 2 as published by the Free Software Foundation. |
| 18 | + * |
| 19 | + ******************************* |
| 20 | + * |
| 21 | + * REVISION HISTORY |
| 22 | + * Version 1.0 - Henrik Ekblad |
| 23 | + * Version 1.1 - Ivo Pullens, added battery voltage reporting roughly every 24h |
| 24 | + * |
| 25 | + * DESCRIPTION |
| 26 | + * Motion Sensor example using HC-SR501. Requires at least MySensors 2.3.2 ! |
| 27 | + * http://www.mysensors.org/build/motion |
| 28 | + * Every 24 hours the battery level will be reported. |
| 29 | + * Battery power will be measured as described here. |
| 30 | + * https://www.mysensors.org/build/humidity_si7021 |
| 31 | + */ |
| 32 | + |
| 33 | +// Enable debug prints |
| 34 | +// #define MY_DEBUG |
| 35 | + |
| 36 | +// Enable and select radio type attached |
| 37 | +#define MY_RADIO_RF24 |
| 38 | +//#define MY_RADIO_NRF5_ESB |
| 39 | +//#define MY_RADIO_RFM69 |
| 40 | +//#define MY_RADIO_RFM95 |
| 41 | + |
| 42 | +#include <MySensors.h> |
| 43 | +#include <Vcc.h> // Install from https://github.com/Yveaux/Arduino_Vcc |
| 44 | + |
| 45 | +const float VccMin = 1.8; // Minimum expected Vcc level, in Volts: Brownout at 1.8V -> 0% |
| 46 | +const float VccMax = 2.0*1.6; // Maximum expected Vcc level, in Volts: 2xAA fresh Alkaline -> 100% |
| 47 | +const float VccCorrection = 1.0; // Measured Vcc by multimeter divided by reported Vcc |
| 48 | +static Vcc vcc(VccCorrection); |
| 49 | + |
| 50 | +#define SLEEP_TIME_MS (24ul*60ul*60ul*1000ul) // Sleep time between battery reports (24h, in milliseconds) |
| 51 | +#define DIGITAL_INPUT_SENSOR 3 // The digital input you attached your motion sensor. (Only 2 and 3 generates interrupt!) |
| 52 | +#define CHILD_ID 1 // Id of the sensor child |
| 53 | + |
| 54 | +// Initialize motion message |
| 55 | +MyMessage msg(CHILD_ID, V_TRIPPED); |
| 56 | + |
| 57 | +void setup() |
| 58 | +{ |
| 59 | + pinMode(DIGITAL_INPUT_SENSOR, INPUT); // sets the motion sensor digital pin as input |
| 60 | +} |
| 61 | + |
| 62 | +void presentation() |
| 63 | +{ |
| 64 | + // Send the sketch version information to the gateway and Controller |
| 65 | + sendSketchInfo(F("Motion Sensor"), F("1.1")); |
| 66 | + |
| 67 | + // Register all sensors to gw (they will be created as child devices) |
| 68 | + present(CHILD_ID, S_MOTION, F("Motion detected")); |
| 69 | +} |
| 70 | + |
| 71 | +void loop() |
| 72 | +{ |
| 73 | + static uint32_t sleepTimeMs = SLEEP_TIME_MS; |
| 74 | + |
| 75 | + // Sleep until interrupt comes in on motion sensor, or if sleepTimeMs elapsed. |
| 76 | + sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), RISING, sleepTimeMs); |
| 77 | + |
| 78 | + // Read digital motion value |
| 79 | + const bool tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; |
| 80 | + |
| 81 | + Serial.print(F("Motion tripped ")); |
| 82 | + Serial.println(tripped); |
| 83 | + send(msg.set(tripped?"1":"0")); // Send tripped value to gw |
| 84 | + |
| 85 | + // Request how much of the sleep time is remaining |
| 86 | + sleepTimeMs = getSleepRemaining(); |
| 87 | + |
| 88 | + Serial.print(F("Remaining sleep time [ms] ")); |
| 89 | + Serial.println(sleepTimeMs); |
| 90 | + |
| 91 | + if (0ul == sleepTimeMs) |
| 92 | + { |
| 93 | + // Report battery level and restart the cycle |
| 94 | + const uint8_t batteryPcnt = static_cast<uint8_t>(0.5 + vcc.Read_Perc(VccMin, VccMax)); |
| 95 | + sendBatteryLevel(batteryPcnt); |
| 96 | + |
| 97 | + Serial.print(F("Vbat ")); |
| 98 | + Serial.print(vcc.Read_Volts()); |
| 99 | + Serial.print(F("\tPerc ")); |
| 100 | + Serial.println(batteryPcnt); |
| 101 | + |
| 102 | + sleepTimeMs = SLEEP_TIME_MS; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | + |
0 commit comments