/
Sensors.cpp
182 lines (147 loc) · 5.52 KB
/
Sensors.cpp
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
181
182
#include "Sensors.h"
#include "Settings.h"
// Rollng Data Stores
Storage current5vStorage[STORAGE_MAX_ENTRIES];
Storage current12vStorage[STORAGE_MAX_ENTRIES];
Storage powerStorage[STORAGE_MAX_ENTRIES];
Storage temperatureStorage[STORAGE_MAX_ENTRIES];
// Rolling data store counters
int currentStorageCounter = 0;
int temperatureStorageCounter = 0;
// global interface to DHT sensor
SimpleDHT11 DHT11;
// read current consumption (amps), and store it when enought samples have been collected
void readCurrentConsumption(){
static unsigned long lastRead = 0;
static float value_current5v, value_power5v = 0;
static float value_current12v, value_power12v = 0;
static float sum5v,sum12v = 0;
static int count = 0;
float current5v,current12v;
if(millis() - lastRead >= CUR_INTERVAL) {
//Serial.print(F("Current analog read:"));
//Serial.println(analogRead(ACS1_PIN));
sum5v += analogRead(ACS1_PIN);
sum12v += analogRead(ACS2_PIN);
lastRead = millis();
count++;
if(count == CUR_NB_SAMPLES) {
// calculate current in mah
current5v = getCurrentFromACSValue(sum5v, ACS712_NEUTRAL_5V);
current12v = getCurrentFromACSValue(sum12v, ACS712_NEUTRAL_12V);
// effective value : divide current by square root of 2, and x1000 to get Ah
value_current5v = getEffectiveCurrentInAhFromCurrent(current5v);
value_current12v = getEffectiveCurrentInAhFromCurrent(current12v);
// calculate power in Watts
value_power5v = value_current5v * 5;
value_power12v = value_current12v * 12;
//Serial.print(F("Current 5v reads: "));
//Serial.println(value_current5v);
//Serial.print(F("Power 5v reads: "));
//Serial.println(value_power5v);
Serial.print(F("sum 5v reads: "));
Serial.println(sum5v / CUR_NB_SAMPLES);
Serial.print(F("sum 12v reads: "));
Serial.println(sum12v / CUR_NB_SAMPLES);
Serial.print(F("Current 12v reads: "));
Serial.println(value_current12v);
//Serial.print(F("Power 12v reads: "));
//Serial.println(value_power12v);
// clean up values
if(value_current5v < 0) value_current5v = 0;
if(value_current12v < 0) value_current12v = 0;
if(value_power5v < 0) value_power5v = 0;
if(value_power12v < 0) value_power12v = 0;
// store values in history
time_t timestamp = getTimestamp();
storeValue(value_current5v, current5vStorage, ¤tStorageCounter, timestamp);
storeValue(value_current12v, current12vStorage, ¤tStorageCounter, timestamp);
storeValue(value_power5v+value_power12v, powerStorage, ¤tStorageCounter, timestamp);
// reset counters
sum5v=0;
sum12v=0;
count=0;
}
}
}
float getCurrentFromACSValue(float acsvalue, int neutral) {
return (float) ((acsvalue / CUR_NB_SAMPLES) - neutral)/1024*5/ACS_VPERAMP;
}
float getEffectiveCurrentInAhFromCurrent(float current) {
return (current / 1.414) * 1000;
}
// read temperature (celcius dregress)and store it when enought samples have been collected
void readTemperature() {
byte temp = 0;
byte hum = 0; // not used here but needed to read...
static float value = 0;
static unsigned long lastRead = 0;
static float sum = 0;
static int count = 0;
if(millis() - lastRead >= TEMP_INTERVAL) {
int err = SimpleDHTErrSuccess;
if ((err = DHT11.read(DTH11_PIN, &temp, &hum, NULL)) != SimpleDHTErrSuccess) {
Serial.print(F("Read DHT11 failed, err=")); Serial.println(err);
} else {
sum+=temp;
count++;
if(count == TEMP_NB_SAMPLES || value == 0) {
value = sum / float(TEMP_NB_SAMPLES);
time_t timestamp = getTimestamp();
storeValue(value, temperatureStorage, &temperatureStorageCounter, timestamp);
sum=0;
count=0;
Serial.print(F("Temperature reads: "));
Serial.println(value);
}
}
lastRead = millis();
}
}
// Store values for history retreival (rolling buffer)
void storeValue(float value, Storage *store, int *counter, time_t timestamp) {
// if maximum number of entries stored, start from first pos
if(*counter >= STORAGE_MAX_ENTRIES) {
*counter=0;
}
// store value in history rolling buffer
store[*counter].value = value;
store[*counter].timeStamp = timestamp;
*counter+=1;
}
// retreive the lastest stored temperature (= most recent one)
float getLastTemperature() {
return getLastStoredValue(temperatureStorage, &temperatureStorageCounter);
}
// retreive the lastest stored current (= most recent one)
float getLastCurrent5vConsumption() {
return getLastStoredValue(current5vStorage, ¤tStorageCounter);
}
// retreive the lastest stored current (= most recent one)
float getLastCurrent12vConsumption() {
return getLastStoredValue(current12vStorage, ¤tStorageCounter);
}
// retreive the lastest stored power (= most recent one)
float getLastPowerConsumption() {
return getLastStoredValue(powerStorage, ¤tStorageCounter);
}
// retreive the lastest stored value from a given store
float getLastStoredValue(Storage *store, int *counter) {
return store[*counter - 1].value;
}
// retreive a specific Temperature value
Storage getStoredTemperature(int pos) {
return temperatureStorage[pos];
}
// retreive a specific Current value
Storage getStored5vCurrent(int pos) {
return current5vStorage[pos];
}
// retreive a specific Current value
Storage getStored12vCurrent(int pos) {
return current12vStorage[pos];
}
// retreive a specific power value
Storage getStoredPower(int pos) {
return powerStorage[pos];
}