MySensors Library & Examples  2.3.2
VibrationSensor.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  * DESCRIPTION
22  *
23  * Vibration Sensor
24  *
25  * connect the sensor as follows :
26  *
27  * VCC >>> 5V
28  * S >>> D3
29  * GND >>> GND
30  *
31  * Based on: http://www.dfrobot.com/wiki/index.php/DFRobot_Digital_Vibration_Sensor_V2_SKU:DFR0027
32  * Contributor: epierre
33  */
34 
35 // Enable debug prints to serial monitor
36 #define MY_DEBUG
37 
38 // Enable and select radio type attached
39 #define MY_RADIO_RF24
40 //#define MY_RADIO_NRF5_ESB
41 //#define MY_RADIO_RFM69
42 //#define MY_RADIO_RFM95
43 
44 #include <MySensors.h>
45 #include <Wire.h>
46 
47 #define CHILD_ID_VIBRATION 0
48 #define VIBRATION_SENSOR_DIGITAL_PIN 3
49 #define SensorLED 13
50 
51 uint32_t SLEEP_TIME = 10*1000; // Sleep time between reads (in seconds)
52 
53 //VARIABLES
54 int val = 0; // variable to store the value coming from the sensor
55 float valVIBRATION =0.0;
56 float lastVIBRATION =0.0;
57 unsigned char state = 0;
58 
59 MyMessage vibrationMsg(CHILD_ID_VIBRATION, V_LEVEL);
60 
61 void setup()
62 {
63  pinMode(VIBRATION_SENSOR_DIGITAL_PIN, INPUT);
64  attachInterrupt(digitalPinToInterrupt(VIBRATION_SENSOR_DIGITAL_PIN), blink,
65  FALLING); // Trigger the blink function when the falling edge is detected
66  pinMode(SensorLED, OUTPUT);
67 }
68 
70 {
71  // Send the sketch version information to the gateway and Controller
72  sendSketchInfo("VIBRATION Sensor", "1.0");
73 
74  // Register all sensors to gateway (they will be created as child devices)
75  present(CHILD_ID_VIBRATION, S_VIBRATION);
76 }
77 
78 void loop()
79 {
80 
81  if(state>=40) { // basically below 40 so ignire basic level
82  send(vibrationMsg.set(int16_t(state)));
83  state = 0;
84  digitalWrite(SensorLED,HIGH);
85  } else {
86  state = 0;
87  digitalWrite(SensorLED,LOW);
88  }
89 
90 
91  // Power down the radio. Note that the radio will get powered back up
92  // on the next write() call.
93  delay(1000); //delay to allow serial to fully print before sleep
94 
95  sleep(SLEEP_TIME); //sleep for: sleepTime
96 }
97 
98 void blink()//Interrupts function
99 {
100  state++;
101 }
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 LOW
Definition: bcm2835.h:574
#define HIGH
Definition: bcm2835.h:572
MyMessage & set(const void *payload, const size_t length)
Set entire payload.
void loop()
Main loop.