-
Notifications
You must be signed in to change notification settings - Fork 10
/
low_power_sensor_inside.ino
180 lines (147 loc) · 5.3 KB
/
low_power_sensor_inside.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
Low power node - ATMega328p program to send humidity, temperature and battery voltage
This program enables to send sensor data with low power:
- send sensor data to a 433Mhz gateway
- with 3 AA batteries measured at 5V the current consumption in sleep mode is around 5uA
Contributors:
- 1technophile
Based on the libraries:
- RCSwitch
- LowPower
Based on the work of:
Nick Gammon : http://www.gammon.com.au/power
Rocketscream: https://github.com/rocketscream/Low-Power
Tinkerit: https://code.google.com/archive/p/tinkerit/wikis/SecretVoltmeter.wiki
Documentation:
Project home: https://github.com/1technophile/low_power_sensor
Blog: http://1technophile.blogspot.com/2016/12/low-cost-low-power-room-sensor.html
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <DHT.h>
#include "LowPower.h"
#include <RCSwitch.h>
#include <string.h>
#define DHTTYPE DHT22
RCSwitch mySwitch = RCSwitch();
//Time used to wait for an interval before resending data
unsigned long time;
//Pin on which the sensors are connected
const int LedPin = 9;
const int DhtPin = 3;
const int DhtPowerPin = 4;
const int EmitPin = 6;
const int EmitPowerPin = 7;
const int TimeToSleep = 3000; // set time to sleep (approx) in seconds
DHT dht(DhtPin,DHTTYPE);
// define humidity variable to hold the final value
int humidity = 0;
//Do we want to see trace for debugging purposes
#define TRACE 0 // 0= trace off 1 = trace on
/*These values define the RF code value sent if the sensor values are
equals to 0, for example, if the sensor value of temperature is 24°C, the
program is going to send 33240, this resulting value can be interpreted
either at gateway level or better at domotic software level (example openhab)*/
#define HUM "61000"
#define TEMP "63000"
#define VOLT "65000"
#define ERRORCODE "99999"
void setup()
{
Serial.begin(9600);
// initialize the input for presence detection
pinMode(DhtPowerPin,INPUT);
pinMode(EmitPowerPin,INPUT);
// start led signal
pinMode(LedPin,OUTPUT);
digitalWrite(LedPin, HIGH);
delay(500);
digitalWrite(LedPin, LOW);
pinMode(LedPin,INPUT);
// Launch traces for debugging purposes
trc("Start of the program");
}
void loop()
{
// begin emitting
pinMode(EmitPowerPin,OUTPUT);
digitalWrite(EmitPowerPin, HIGH);
mySwitch.enableTransmit(EmitPin); // Using Pin #6
mySwitch.setRepeatTransmit(10); //increase transmit repeat to avoid lost of rf sendings
// send battery voltage
sendData(vccVoltage(), atol(VOLT));
// send temp and hum
pinMode(DhtPowerPin,OUTPUT);
digitalWrite(DhtPowerPin, HIGH);
dht.begin();
TempAndHum();
digitalWrite(DhtPowerPin, LOW);
pinMode(DhtPin,INPUT);//disable the internal pullup resistor enable by dht.begin
pinMode(DhtPowerPin,INPUT);
//deactivate the transmitter
mySwitch.disableTransmit();
pinMode(EmitPowerPin,INPUT);
// sleep for x seconds
sleepSeconds(TimeToSleep);
}
void sleepSeconds(int seconds)
{
for (int i = 0; i < seconds; i++) {
LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF);
}
}
void TempAndHum(){
delay(500);
//retreiving value of temperature and humidity from DHT
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
trc("Failed to read from DHT sensor!");
sendData(atol(ERRORCODE), atol(HUM));//send error code
sendData(atol(ERRORCODE), atol(TEMP));//send error code
} else {
sendData(int(h*10), atol(HUM));
sendData(int(t*10), atol(TEMP));
}
}
void sendData(long dataTosend, long dataType){
long sum = atol(ERRORCODE);
trc(String(dataTosend));
trc(String(dataType));
if (dataTosend == sum) {
// nothing to do sending error code
} else {
sum = dataTosend + dataType; // sending value added to topic offset
}
trc("Sum");
trc(String(sum));
//sending value by RF
mySwitch.send(sum,24);
}
// https://code.google.com/archive/p/tinkerit/wikis/SecretVoltmeter.wiki
long vccVoltage() {
long result = 0;
// Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(10); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA,ADSC));
result = ADCL;
result |= ADCH<<8;
result = 1126400L / result; // Back-calculate AVcc in mV
return result;
}
//trace function
void trc(String msg){
if (TRACE) {
Serial.println(msg);
}
}