MySensors Library & Examples  2.3.2-62-ge298769
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-2022 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 //#define MY_PJON
39 
40 #include <MySensors.h>
41 
42 uint32_t SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
43 #define DIGITAL_INPUT_SENSOR 3 // The digital input you attached your motion sensor. (Only 2 and 3 generates interrupt!)
44 #define CHILD_ID 1 // Id of the sensor child
45 
46 // Initialize motion message
47 MyMessage msg(CHILD_ID, V_TRIPPED);
48 
49 void setup()
50 {
51  pinMode(DIGITAL_INPUT_SENSOR, INPUT); // sets the motion sensor digital pin as input
52 }
53 
55 {
56  // Send the sketch version information to the gateway and Controller
57  sendSketchInfo("Motion Sensor", "1.0");
58 
59  // Register all sensors to gw (they will be created as child devices)
60  present(CHILD_ID, S_MOTION);
61 }
62 
63 void loop()
64 {
65  // Read digital motion value
66  bool tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
67 
68  Serial.println(tripped);
69  send(msg.set(tripped?"1":"0")); // Send tripped value to gw
70 
71  // Sleep until interrupt comes in on motion sensor. Send update every two minute.
72  sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), CHANGE, SLEEP_TIME);
73 }
74 
75 
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:63
presentation
void presentation()
Node presentation.
Definition: MotionSensor.ino:54
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:49
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:290