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-2015 Sensnology AB
10
+ * Full contributor list: https://github.com/mysensors/Arduino/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: Yveaux
23
+ *
24
+ * DESCRIPTION
25
+ * This sketch provides an example of how to implement a humidity/temperature
26
+ * sensor using a Si7021 sensor.
27
+ *
28
+ * For more information, please visit:
29
+ * http://www.mysensors.org/build/humiditySi7021
30
+ *
31
+ */
32
+
33
+ // Enable debug prints
34
+ #define MY_DEBUG
35
+
36
+ // Enable REPORT_BATTERY_LEVEL to measure battery level and send changes to gateway
37
+ // #define REPORT_BATTERY_LEVEL
38
+
39
+ // Enable and select radio type attached
40
+ #define MY_RADIO_NRF24
41
+ // #define MY_RADIO_RFM69
42
+ // #define MY_RS485
43
+
44
+ #include < MySensors.h>
45
+
46
+ #define CHILD_ID_HUM 0
47
+ #define CHILD_ID_TEMP 1
48
+
49
+ static bool metric = true ;
50
+
51
+ // Sleep time between sensor updates (in milliseconds)
52
+ static const uint64_t UPDATE_INTERVAL = 60000 ;
53
+
54
+ #include < SI7021.h>
55
+ static SI7021 sensor;
56
+
57
+ #ifdef REPORT_BATTERY_LEVEL
58
+ #include < Vcc.h>
59
+ static uint8_t oldBatteryPcnt = 200 ; // Initialize to 200 to assure first time value will be sent.
60
+ const float VccMin = 1.8 ; // Minimum expected Vcc level, in Volts: Brownout at 1.8V -> 0%
61
+ const float VccMax = 2.0 *1.6 ; // Maximum expected Vcc level, in Volts: 2xAA fresh Alkaline -> 100%
62
+ const float VccCorrection = 1.0 ; // Measured Vcc by multimeter divided by reported Vcc
63
+ static Vcc vcc (VccCorrection);
64
+ #endif
65
+
66
+ void presentation ()
67
+ {
68
+ // Send the sketch info to the gateway
69
+ sendSketchInfo (" TemperatureAndHumidity" , " 1.0" );
70
+
71
+ // Present sensors as children to gateway
72
+ present (CHILD_ID_HUM, S_HUM, " Humidity" );
73
+ present (CHILD_ID_TEMP, S_TEMP, " Temperature" );
74
+
75
+ metric = getControllerConfig ().isMetric ;
76
+ }
77
+
78
+ void setup ()
79
+ {
80
+ while (not sensor.begin ())
81
+ {
82
+ Serial.println (F (" Sensor not detected!" ));
83
+ delay (5000 );
84
+ }
85
+ }
86
+
87
+
88
+ void loop ()
89
+ {
90
+ // Read temperature & humidity from sensor.
91
+ const float temperature = float ( metric ? sensor.getCelsiusHundredths () : sensor.getFahrenheitHundredths () ) / 100.0 ;
92
+ const float humidity = float ( sensor.getHumidityBasisPoints () ) / 100.0 ;
93
+
94
+ #ifdef MY_DEBUG
95
+ Serial.print (F (" Temp " ));
96
+ Serial.print (temperature);
97
+ Serial.print (metric ? ' C' : ' F' );
98
+ Serial.print (F (" \t Hum " ));
99
+ Serial.println (humidity);
100
+ #endif
101
+
102
+ static MyMessage msgHum ( CHILD_ID_HUM, V_HUM );
103
+ static MyMessage msgTemp (CHILD_ID_TEMP, V_TEMP);
104
+
105
+ send (msgTemp.set (temperature, 2 ));
106
+ send (msgHum.set (humidity, 2 ));
107
+
108
+ #ifdef REPORT_BATTERY_LEVEL
109
+ const uint8_t batteryPcnt = static_cast <uint8_t >(0.5 + vcc.Read_Perc (VccMin, VccMax));
110
+
111
+ #ifdef MY_DEBUG
112
+ Serial.print (F (" Vbat " ));
113
+ Serial.print (vcc.Read_Volts ());
114
+ Serial.print (F (" \t Perc " ));
115
+ Serial.println (batteryPcnt);
116
+ #endif
117
+
118
+ // Battery readout should only go down. So report only when new value is smaller than previous one.
119
+ if ( batteryPcnt < oldBatteryPcnt )
120
+ {
121
+ sendBatteryLevel (batteryPcnt);
122
+ oldBatteryPcnt = batteryPcnt;
123
+ }
124
+ #endif
125
+
126
+ // Sleep until next update to save energy
127
+ sleep (UPDATE_INTERVAL);
128
+ }
0 commit comments