Skip to content

Commit 74a64f0

Browse files
committed
Initial Commit
1 parent 9d2f999 commit 74a64f0

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// EgSlimReed2
2+
// By m26872, 2015-12-22
3+
// by mrRed, 2016-07-27
4+
// Interrupt driven binary switch for Slim Node with Reed switch and external pull-up (10Mohm)
5+
// Inspired by mysensors example:
6+
// https://github.com/mysensors/Arduino/blob/master/libraries/MySensors/examples/BinarySwitchSleepSensor/BinarySwitchSleepSensor.ino
7+
8+
//////////TODO:////////
9+
//Battery sensor als define
10+
//////////////////////
11+
12+
13+
// Enable debug prints to serial monitor
14+
#define MY_DEBUG
15+
16+
// Enable and select radio type attached
17+
#define MY_RADIO_NRF24
18+
//#define MY_RADIO_RFM69
19+
20+
#include <SPI.h>
21+
#include <MySensors.h>
22+
#include <Vcc.h>
23+
24+
#define BATT_SENSOR 199
25+
26+
//#define NODE_ID 5 //12 var senaste "reed-node"-id // 110 // Use static Node_ID <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27+
#define SKETCH_NAME "EgSlimReed"
28+
#define SKETCH_MAJOR_VER "2.1 2016-27-07"
29+
#define SKETCH_MINOR_VER "0"
30+
31+
#define PRIMARY_CHILD_ID 3
32+
#define SECONDARY_CHILD_ID 4
33+
34+
#define SW_CHILD_ID 5 //necessary
35+
#define SW_PIN 3
36+
37+
38+
#define BATTERY_REPORT_DAY 1 // Desired heartbeat interval when inactive. Maximum heartbeat/report interval is equal to this due to the dayCounter.
39+
#define BATTERY_REPORT_BY_IRT_CYCLE 5 //Count after how many switch trigger a battery report should be done.
40+
#define ONE_DAY_SLEEP_TIME 86400000
41+
#define VCC_MIN 1.9
42+
#define VCC_MAX 3.0
43+
const float VccCorrection = 3.15/3.06; // Measured Vcc by multimeter divided by reported Vcc
44+
45+
46+
#ifdef BATT_SENSOR
47+
MyMessage msgBatt(BATT_SENSOR, V_VOLTAGE);
48+
#endif
49+
50+
51+
52+
int dayCounter = 0;
53+
int irtCounter = 0;
54+
uint8_t value;
55+
uint8_t sentValue = 2;
56+
bool interruptReturn = false;
57+
int oldBatteryPcnt = 0;
58+
59+
Vcc vcc(VccCorrection);
60+
61+
MyMessage msg(SW_CHILD_ID, V_TRIPPED);
62+
63+
void setup()
64+
{
65+
delay(100); // to settle power for radio
66+
pinMode(SW_PIN, INPUT);
67+
digitalWrite(SW_PIN, HIGH);
68+
69+
}
70+
71+
void presentation() {
72+
// Send the sketch version information to the gateway and Controller
73+
sendSketchInfo(SKETCH_NAME, SKETCH_MAJOR_VER "." SKETCH_MINOR_VER);
74+
75+
// Register binary input sensor to sensor_node (they will be created as child devices)
76+
// You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage.
77+
// If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
78+
present(PRIMARY_CHILD_ID, S_DOOR);
79+
}
80+
81+
void loop()
82+
{
83+
uint8_t value;
84+
static uint8_t sentValue = 2;
85+
86+
if (!interruptReturn) { // Woke up by timer (or first run)
87+
dayCounter++;
88+
if (dayCounter >= BATTERY_REPORT_DAY) {
89+
dayCounter = 0;
90+
sendBatteryReport();
91+
}
92+
}
93+
else { // Woke up by pin change
94+
irtCounter++;
95+
sleep(50); // Short delay to allow switch to properly settle
96+
value = digitalRead(SW_PIN);
97+
Serial.println(irtCounter);
98+
if (value != sentValue) {
99+
send(msg.set(value == HIGH ? 1 : 0));
100+
sentValue = value;
101+
}
102+
if (irtCounter >= BATTERY_REPORT_BY_IRT_CYCLE) {
103+
irtCounter = 0;
104+
sendBatteryReport();
105+
106+
}
107+
}
108+
109+
// Sleep until something happens with the sensor, or one sleep_time has passed since last awake.
110+
interruptReturn = sleep(SW_PIN - 2, CHANGE, ONE_DAY_SLEEP_TIME);
111+
112+
}
113+
114+
void sendBatteryReport() {
115+
float p = vcc.Read_Perc(VCC_MIN, VCC_MAX, true);
116+
int batteryPcnt = static_cast<int>(p);
117+
#ifdef BATT_SENSOR
118+
float v = vcc.Read_Volts();
119+
send(msgBatt.set(v,2));
120+
#endif
121+
if (oldBatteryPcnt != batteryPcnt) {
122+
sendBatteryLevel(batteryPcnt);
123+
oldBatteryPcnt = batteryPcnt;
124+
125+
126+
#ifdef MY_DEBUG
127+
Serial.print("Battery: ");
128+
Serial.print(batteryPcnt);
129+
Serial.print("% ");
130+
Serial.print(v);
131+
Serial.println(" V");
132+
#endif
133+
}
134+
135+
136+
}

0 commit comments

Comments
 (0)