forked from coderbunker/energy-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWriting_Database.ino
342 lines (279 loc) · 9.47 KB
/
Writing_Database.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//WiFi
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
//InfluxDB
#include <influxDB_udp.h>
//Utility
#include <string.h>
#include <math.h>
#include <Ticker.h>
//Sensor Library
#include <Wire.h>
#include <Adafruit_ADS1015.h>
#include <SparkFun_Si7021_Breakout_Library.h>
#include <Adafruit_CCS811.h>
//-----------------------------------------------------------------------------
#define DEVICE_VERSION 0.9
#define DEVICE_COUNT 7
#define TABLE_HEIGHT DEVICE_COUNT
#define TABLE_WIDTH 4
#define INDEX_MAC 0
#define INDEX_LOCATION 1
#define INDEX_ROOM 2
#define INDEX_TYPE 3
#define SparkFun_Si7021 Weather
//WiFi Information -- START ---------------------------------------------------
#define INFLUX_HOST "10.1.0.230"
#define INFLUX_PORT_POWER 6969
#define INFLUX_PORT_QUALITY 6970
const char* ssid = "Agora Space";
const char* password = "getstuffdone";
const String config[DEVICE_COUNT][TABLE_WIDTH]
{
//MAC Address location room type
{"b8:77:10:c2:dd:bc", "building9", "fusebox", "power" },
{"44:39:69:3a:7d:80", "building1", "dummy", "power" },
{"e8:42:69:3a:7d:80", "coderbunker", "artoffice", "power" },
{"cd:36:69:3a:7d:80", "coderbunker", "kitchen", "quality" },
{"3d:78:10:c2:dd:bc", "coderbunker", "artoffice", "quality" },
{"fb:61:69:3a:7d:80", "coderbunker", "classroom", "quality" },
{"49:7a:10:c2:dd:bc", "coderbunker", "meetingroom", "quality" }
};
String nameLocation = "";
String nameRoom = "";
String nameType = "";
bool isPowerType;
bool isQualityType;
String getConfigValue(String mac_address, int index);
String MacToString(const uint8_t* mac);
//InfluxDB initialisation -- START --------------------------------------------
WiFiUDP udp;
InfluxDB_UDP influxPowerPort(udp, INFLUX_HOST, INFLUX_PORT_POWER);
InfluxDB_UDP influxSensorPort(udp, INFLUX_HOST, INFLUX_PORT_QUALITY);
InfluxDB_Data dataPower("power_measurement");
InfluxDB_Data dataAirQuality("quality_measurement");
//InfluxDB initialisation -- END ----------------------------------------------
//Ticker stuff -- START -------------------------------------------------------
Ticker DB_Updater;
bool tickOccured = false;
void timerCallback() {
tickOccured = true;
}
//Ticker stuff -- UDP ---------------------------------------------------------
Adafruit_ADS1115 ADC_1(0x49);//Create ADS1115 object
Adafruit_ADS1115 ADC_2(0x48);//Create ADS1115 object
SparkFun_Si7021 Si7021; //Create Instance of Si7021 temperature and humidity sensor
Adafruit_CCS811 CCS811; //Create CCS811 object
const float SCALEFACTOR = 0.125F; // This is the scale factor for the default +/- 4.096V Range we will use - see ADS1X15 Library
const float BURDEN_RESISTOR = 220; // Burden resistor where measurement voltage gets measured.
const float VOLTAGE_MAINS = 220.00; // line voltage
const float COIL_WINDING = 2000; // ratio of sensor 100:0.05
//int16_t rawADCvalue; // The is where we store the value we receive from the ADS1115
//float MAX_CURRENT_COIL = 0.0707; // Maximum current of sensor rated at Rms 100 A
//float MAX_VOLTAGE_ADC = 15.981; // Maximum Voltage on burden resistor calculated by burden resistor * maxAmps (33 Ohm * 0.0707 A)
float voltage_adc_1 = 0.0; // The result of applying the scale factor to the raw value
float voltage_adc_2 = 0.0;
float voltage_adc_3 = 0.0;
float current_coil = 0.0; // Calculated depending on the Voltage/Burden resistor
float power_1 = 0.0;
float power_2 = 0.0;
float power_3 = 0.0;
float power_total = 0.0;
float humidity = 0;
float temperature = 0;
float eCO2 = 0;
float TVOC = 0;
String line;
String PowerAsString;
void setup(void)
{
Wire.setClock(400000);
Serial.begin(115200);
delay(1000);
// WiFi Connection -- START -----------------------------------------------
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Wifi Connected to " + String(ssid));
// WiFi Connection -- END -------------------------------------------------
// WiFi Mac Address Mapping -- START --------------------------------------
uint8_t mac[6];
WiFi.macAddress(mac);
String mac_address = MacToString(mac);
nameLocation = getConfigValue(mac_address, INDEX_LOCATION);
nameRoom = getConfigValue(mac_address, INDEX_ROOM);
Serial.println("\n----- IMPORTANT INFORMATION -----");
Serial.println(mac_address);
Serial.println(nameLocation);
Serial.println(nameRoom);
Serial.println("---------------------------------\n");
if(getConfigValue(mac_address, INDEX_TYPE) == "power")
{
isPowerType = 1;
isQualityType = 0;
dataPower.addTag("location",nameLocation);
dataPower.addTag("room",nameRoom);
}
else
{
isPowerType = 0;
isQualityType = 1;
dataAirQuality.addTag("location",nameLocation);
dataAirQuality.addTag("room",nameRoom);
}
// WiFi Mac Address Mapping -- END ----------------------------------------
// Sensor Initialisation -- START -----------------------------------------
if(isQualityType)
{
Si7021.begin();
if(!CCS811.begin())
{
Serial.println("Failed to start CCS811! Please check your wiring.");
while(1)
{
delay(200);
}
}
else
{
Serial.println("CCS811 found");
}
while(!CCS811.available())
{
delay(200);
}
float temp;
temp = CCS811.calculateTemperature();
CCS811.setTempOffset(temp - 25.0);
}
if(isPowerType)
{
ADC_1.setGain(GAIN_ONE);
ADC_2.setGain(GAIN_ONE);
ADC_1.begin();
ADC_2.begin();
}
// Sensor Initialisation -- END -------------------------------------------
//activate Ticker
DB_Updater.attach(1, timerCallback);
}
void loop(void)
{
if(tickOccured)
{
tickOccured = false;
if(isQualityType)
{
dataAirQuality.clear(InfluxDB_Data::FIELD);
dataAirQuality.addField("temperature_c",String(temperature));
dataAirQuality.addField("TVOC_ppb",String(TVOC));
dataAirQuality.addField("eCO2_ppm",String(eCO2));
dataAirQuality.addField("humidity_RH",String(humidity));
influxSensorPort.write(dataAirQuality);
}
if(isPowerType)
{
dataPower.clear(InfluxDB_Data::FIELD);
dataPower.addField("power1_W",String(power_1));
dataPower.addField("power2_W",String(power_2));
dataPower.addField("power3_W",String(power_3));
dataPower.addField("powerTotal_W",String(power_total));
influxPowerPort.write(dataPower);
}
}
if(isQualityType)
{
humidity = Si7021.getRH();
temperature = Si7021.getTemp();
if(CCS811.available())
{
if(!CCS811.readData())
{
eCO2=CCS811.geteCO2();
TVOC=CCS811.getTVOC();
}
else
{
Serial.println("ERROR!");
while(1)
{
delay(200);
}
}
}
}
// current_coil = ((voltage_adc_1) * MAX_CURRENT_COIL)/MAX_VOLTAGE_ADC;
// power = current_coil * COIL_WINDING * VOLTAGE_MAINS;
if(isPowerType)
{
voltage_adc_1 = (calcVrms(32,1) )/1000.0;
voltage_adc_2 = (calcVrms(32,2) )/1000.0;
voltage_adc_3 = (calcVrms(32,3) )/1000.0;
power_1= calcPower(voltage_adc_1);
power_2= calcPower(voltage_adc_2);
power_3= calcPower(voltage_adc_3);
power_total=power_1+power_2+power_3;
}
}
double calcVrms(unsigned int Samples, unsigned int phase)
{
float voltage_rms;
float squared_voltage;
float sample_voltage_sum;
float sample_voltage;
for (unsigned int n = 0; n < Samples; n++)
{
switch(phase){
case 1:{
sample_voltage = ADC_1.readADC_Differential_0_1();
break;
}
case 2:{
sample_voltage = ADC_1.readADC_Differential_2_3();
break;
}
case 3:{
sample_voltage = ADC_2.readADC_Differential_0_1();
break;
}
default: break;
}
squared_voltage = sample_voltage * sample_voltage;
sample_voltage_sum += squared_voltage;
}
voltage_rms = (sqrt(sample_voltage_sum / Samples) * SCALEFACTOR) * sqrt(2);
sample_voltage_sum = 0;
return voltage_rms;
}
double calcPower(float voltage)
{
double power;
power = voltage / BURDEN_RESISTOR * COIL_WINDING * VOLTAGE_MAINS;
return power;
}
String getConfigValue(String mac_address, int index)
{
for(int index_counter = 0 ; index_counter < DEVICE_COUNT ; index_counter++)
{
if(config[index_counter][INDEX_MAC] == mac_address)
{
return config[index_counter][index];
}
}
return "";
}
String MacToString(const uint8_t* mac)
{
String s_mac = "";
for(int8_t index = 5 ; index >= 0 ; index--)
{
s_mac += String(mac[index],HEX);
if(index != 0)
{
s_mac += ":";
}
}
return s_mac;
}