MySensors Library & Examples  2.3.2
MotionSensorRS485.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  * This is an example of sensors using RS485 as transport layer
26  *
27  * Motion Sensor example using HC-SR501
28  * http://www.mysensors.org/build/motion
29  *
30  * If your Arduino board has additional serial ports
31  * you can use to connect the RS485 module.
32  * Otherwise, the transport uses AltSoftSerial to handle two serial
33  * links on one Arduino. Use the following pins for RS485 link
34  *
35  * Board Transmit Receive PWM Unusable
36  * ----- -------- ------- ------------
37  * Teensy 3.0 & 3.1 21 20 22
38  * Teensy 2.0 9 10 (none)
39  * Teensy++ 2.0 25 4 26, 27
40  * Arduino Uno 9 8 10
41  * Arduino Leonardo 5 13 (none)
42  * Arduino Mega 46 48 44, 45
43  * Wiring-S 5 6 4
44  * Sanguino 13 14 12 *
45  *
46  */
47 
48 // Enable debug prints to serial monitor
49 #define MY_DEBUG
50 
51 // Enable RS485 transport layer
52 #define MY_RS485
53 
54 // Define this to enables DE-pin management on defined pin
55 #define MY_RS485_DE_PIN 2
56 
57 // Set RS485 baud rate to use
58 #define MY_RS485_BAUD_RATE 9600
59 
60 // Enable this if RS485 is connected to a hardware serial port
61 //#define MY_RS485_HWSERIAL Serial1
62 
63 #include <MySensors.h>
64 
65 uint32_t SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
66 #define DIGITAL_INPUT_SENSOR 3 // The digital input you attached your motion sensor. (Only 2 and 3 generates interrupt!)
67 #define CHILD_ID 1 // Id of the sensor child
68 
69 // Initialize motion message
70 MyMessage msg(CHILD_ID, V_TRIPPED);
71 
72 void setup()
73 {
74  pinMode(DIGITAL_INPUT_SENSOR, INPUT); // sets the motion sensor digital pin as input
75 }
76 
78 {
79  // Send the sketch version information to the gateway and Controller
80  sendSketchInfo("Motion Sensor", "1.0");
81 
82  // Register all sensors to gw (they will be created as child devices)
83  present(CHILD_ID, S_MOTION);
84 }
85 
86 void loop()
87 {
88  // Read digital motion value
89  bool tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
90 
91  Serial.println(tripped);
92  send(msg.set(tripped?"1":"0")); // Send tripped value to gw
93 
94  // Sleep until interrupt comes in on motion sensor. Send update every two minute.
95  sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), CHANGE, SLEEP_TIME);
96 }
97 
98 
MyMessage is used to create, manipulate, send and read MySensors messages.
Definition: MyMessage.h:289
bool sendSketchInfo(const char *name, const char *version, const bool requestEcho=false)
API declaration for MySensors.
bool send(MyMessage &msg, const bool requestEcho=false)
bool present(const uint8_t sensorId, const mysensors_sensor_t sensorType, const char *description="", const bool requestEcho=false)
void setup()
Called after node initialises but before main loop.
int8_t sleep(const uint32_t sleepingMS, const bool smartSleep=false)
void presentation()
Node presentation.
#define HIGH
Definition: bcm2835.h:572
MyMessage & set(const void *payload, const size_t length)
Set entire payload.
void loop()
Main loop.