MySensors Library & Examples  2.3.2
MotionSensor.ino
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  *
24  * DESCRIPTION
25  * Motion Sensor example using HC-SR501
26  * http://www.mysensors.org/build/motion
27  *
28  */
29 
30 // Enable debug prints
31 // #define MY_DEBUG
32 
33 // Enable and select radio type attached
34 #define MY_RADIO_RF24
35 //#define MY_RADIO_NRF5_ESB
36 //#define MY_RADIO_RFM69
37 //#define MY_RADIO_RFM95
38 
39 #include <MySensors.h>
40 
41 uint32_t SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
42 #define DIGITAL_INPUT_SENSOR 3 // The digital input you attached your motion sensor. (Only 2 and 3 generates interrupt!)
43 #define CHILD_ID 1 // Id of the sensor child
44 
45 // Initialize motion message
46 MyMessage msg(CHILD_ID, V_TRIPPED);
47 
48 void setup()
49 {
50  pinMode(DIGITAL_INPUT_SENSOR, INPUT); // sets the motion sensor digital pin as input
51 }
52 
54 {
55  // Send the sketch version information to the gateway and Controller
56  sendSketchInfo("Motion Sensor", "1.0");
57 
58  // Register all sensors to gw (they will be created as child devices)
59  present(CHILD_ID, S_MOTION);
60 }
61 
62 void loop()
63 {
64  // Read digital motion value
65  bool tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
66 
67  Serial.println(tripped);
68  send(msg.set(tripped?"1":"0")); // Send tripped value to gw
69 
70  // Sleep until interrupt comes in on motion sensor. Send update every two minute.
71  sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), CHANGE, SLEEP_TIME);
72 }
73 
74 
HIGH
#define HIGH
Definition: bcm2835.h:572
sendSketchInfo
bool sendSketchInfo(const char *name, const char *version, const bool requestEcho=false)
loop
void loop()
Main loop.
Definition: MotionSensor.ino:62
presentation
void presentation()
Node presentation.
Definition: MotionSensor.ino:53
MyMessage::set
MyMessage & set(const void *payload, const size_t length)
Set entire payload.
send
bool send(MyMessage &msg, const bool requestEcho=false)
present
bool present(const uint8_t sensorId, const mysensors_sensor_t sensorType, const char *description="", const bool requestEcho=false)
setup
void setup()
Called after node initialises but before main loop.
Definition: MotionSensor.ino:48
sleep
int8_t sleep(const uint32_t sleepingMS, const bool smartSleep=false)
MySensors.h
API declaration for MySensors.
MyMessage
MyMessage is used to create, manipulate, send and read MySensors messages.
Definition: MyMessage.h:289