MySensors Library & Examples  2.3.2-62-ge298769
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-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  * 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 //#define MY_PJON
44 
45 #include <MySensors.h>
46 #include <Wire.h>
47 
48 #define CHILD_ID_VIBRATION 0
49 #define VIBRATION_SENSOR_DIGITAL_PIN 3
50 #define SensorLED 13
51 
52 uint32_t SLEEP_TIME = 10*1000; // Sleep time between reads (in seconds)
53 
54 //VARIABLES
55 int val = 0; // variable to store the value coming from the sensor
56 float valVIBRATION =0.0;
57 float lastVIBRATION =0.0;
58 unsigned char state = 0;
59 
60 MyMessage vibrationMsg(CHILD_ID_VIBRATION, V_LEVEL);
61 
62 void setup()
63 {
64  pinMode(VIBRATION_SENSOR_DIGITAL_PIN, INPUT);
65  attachInterrupt(digitalPinToInterrupt(VIBRATION_SENSOR_DIGITAL_PIN), blink,
66  FALLING); // Trigger the blink function when the falling edge is detected
67  pinMode(SensorLED, OUTPUT);
68 }
69 
71 {
72  // Send the sketch version information to the gateway and Controller
73  sendSketchInfo("VIBRATION Sensor", "1.0");
74 
75  // Register all sensors to gateway (they will be created as child devices)
76  present(CHILD_ID_VIBRATION, S_VIBRATION);
77 }
78 
79 void loop()
80 {
81 
82  if(state>=40) { // basically below 40 so ignire basic level
83  send(vibrationMsg.set(int16_t(state)));
84  state = 0;
85  digitalWrite(SensorLED,HIGH);
86  } else {
87  state = 0;
88  digitalWrite(SensorLED,LOW);
89  }
90 
91 
92  // Power down the radio. Note that the radio will get powered back up
93  // on the next write() call.
94  delay(1000); //delay to allow serial to fully print before sleep
95 
96  sleep(SLEEP_TIME); //sleep for: sleepTime
97 }
98 
99 void blink()//Interrupts function
100 {
101  state++;
102 }
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: VibrationSensor.ino:79
LOW
#define LOW
Definition: bcm2835.h:574
presentation
void presentation()
Node presentation.
Definition: VibrationSensor.ino:70
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: VibrationSensor.ino:62
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