-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSensor.cpp
More file actions
124 lines (93 loc) · 3.22 KB
/
Sensor.cpp
File metadata and controls
124 lines (93 loc) · 3.22 KB
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
//
// Sensor.cpp - Library for handling readings from the temp sensor.
//
#include <DHT.h>
#include "Sensor.h"
#include "defaults.h"
//Sensor::Sensor(uint8_t pin, uint8_t type) {
// Serial.println("[Sensor] DHT(" + String(pin) + ", " + String(type) + ")");
// _dht = DHT(pin, type);
//};
void Sensor::begin( Config *config ) {
// Keep a reference to the config
_config = config;
_poll_sensor_interval = SENSOR_POLL_INTERVAL * 1000;
_next_sensor_poll = millis() + _poll_sensor_interval;
sensor_on();
_dht.begin();
}
void Sensor::loop() {
if (millis() > _next_sensor_poll) { // Time to read the sensors
read_sensor();
_next_sensor_poll = millis() + _poll_sensor_interval;
}
}
// Main Sensor Turn On
void Sensor::sensor_on() {
pinMode(DHTPWR, OUTPUT);
digitalWrite(DHTPWR, 1);
}
// Try to reset the DHT sensor. This only works if your
// sensor is wired to use the DHTPWR pin
void Sensor::reset_sensor() {
Serial.println("Resetting Sensor...");
digitalWrite(DHTPWR, 0);
delay(500);
digitalWrite(DHTPWR, 1);
// Give the sensor a cooling off by skipping the next interval
_next_sensor_poll = millis() + _poll_sensor_interval*2;
// delay(SENSOR_DELAY_AFTER_RESET);
_dht.begin();
}
// Reading Getters
float Sensor::get_temp() { return _cur_temp; }
float Sensor::get_humidity() { return _cur_humidity; }
float Sensor::get_hindex() { return _cur_hindex; }
float Sensor::get_analog() { return _cur_analog; }
float Sensor::get_pressure() {
if (_cur_analog)
return ((_cur_analog / 1024) * 200) - 3.35; // dirty offset removal
return NAN;
}
// Attempt to read sensor values from the DHT22
void Sensor::read_sensor() {
Serial.println("[Sensor] read_sensor");
// Subtract the temperature offset due to heating from the MCU
float temp = _dht.readTemperature(true) - _config->conf.t_offset;
float humidity = _dht.readHumidity();
if (isnan(temp) || isnan(humidity)) {
// Successfully failed to get a readout from the DHT22
Serial.println("[Sensor] Failed to get reading from DHT22");
// If we haven't got a reading in SENSOR_RESET_INTERVAL, reset the sensor
if (millis() - _last_sensor_read > SENSOR_RESET_INTERVAL * 1000) {
reset_sensor(); // may the odds be ever in your favor.
_last_sensor_read = millis(); // not really, but set the counters based off now
// Clear out current readings
_cur_temp = NAN;
_cur_humidity = NAN;
_cur_hindex = NAN;
}
} else {
// Successfully got a readout
_last_sensor_read = millis();
_cur_temp = temp;
_cur_humidity = humidity;
_cur_hindex = _dht.computeHeatIndex(temp, humidity);
// Write out to serial
Serial.println("[Sensor] Temp: " + String(_cur_temp, 2) + "F "
"Humidity: " + String(_cur_humidity, 2) + "% "
"Heat Index: " + String(_cur_hindex, 2));
}
// Calculate Pressure
read_analog();
}
// Sample the analog sensor
void Sensor::read_analog() {
uint16_t sum = 0;
for(int i=0; i < 64; i++) {
delay(5);
sum += analogRead(PRESSURE_PIN);
}
_cur_analog = float(sum) / 64.0;
Serial.println("[Sensor] Analog Sensor: " + String(_cur_analog));
}