-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDB.cpp
168 lines (127 loc) · 4.56 KB
/
DB.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
/*
* DB.cpp - Library for sending sensor readings to a database (influx)
*
*/
#include <ESP8266HTTPClient.h>
#include "DB.h"
#include "Config.h"
// Init DB Class
DB::DB() {
_influx_url = String("");
}
// Setup database based on current config values
void DB::begin( Config *config, Sensor *sensor ) {
// Keep a reference to the config & sensor
_config = config;
_sensor = sensor;
// Create the URL we'll be sending influx data to
_influx_url = "";
if (_config->conf.db_type == DB_TYPE_INFLUXDB) {
_influx_url = "http://" +
String(_config->conf.db_host) + ":" + String(_config->conf.db_port) +
"/write?db=" + String(_config->conf.db_name);
// Start the HTTP Client connection
_http.begin( _influx_url );
}
// Print the influx server info
Serial.println( "[DB] Influx Server: " + _influx_url );
}
void DB::loop() {
}
// URL encode a string
String DB::urlencode( String text ) {
String encoded = "";
// Scan through each character, if it's special, encode it.
for (int i=0; i < text.length(); i++) {
char c = text.charAt(i);
if ( c == ' ' ) // Handle spaces
encoded += '+';
else if ( isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~' ) // Unreserved characters..
encoded += c;
else {
// Other characters need to get converted to their hex codes
char enchar[5] = "";
sprintf(enchar, "%%%X", c);
encoded += enchar;
}
}
return encoded;
}
//
// Escape a string based on the InfluxDB Line Protocol
String DB::influx_escape( String text ) {
String encoded = "";
// Scan through each character, if it's special, escape it
for (int i=0; i < text.length(); i++) {
char c = text.charAt(i);
if ( c == ',' || c == '=' || c == ' ' || c == '"' ) {
encoded += "\\";
encoded += c;
} else
encoded += c;
}
return encoded;
}
// Send readings to database
uint16_t DB::influxDBSend( float temp, float humidity, float hindex ) {
// Build the POST
String influxout;
influxout = influx_escape( _config->conf.db_measurement ) + ",host=" + influx_escape( _config->conf.hostname ) +
",location=" + influx_escape( _config->conf.location ) +
" temperature=" + String(temp, 2) +
",humidity=" + String(humidity, 2) +
",heat_index=" + String(hindex, 2);
Serial.println( "[InfluxDB] " + _influx_url );
Serial.println( "[InfluxDB] " + String(influxout) );
// POST the POST and return the result
return _http.POST(influxout);
}
// Send analog readings to database
uint16_t DB::influxDBAnalogSend( String measurement, float reading, float pressure ) {
// Build the POST
String influxout;
influxout = influx_escape( measurement ) + ",host=" + influx_escape( _config->conf.hostname ) +
",location=" + influx_escape( _config->conf.location ) +
" analog=" + String(reading, 2) +
",pressure=" + String(pressure, 2);
Serial.println( "[InfluxDB] " + _influx_url );
Serial.println( "[InfluxDB] " + String(influxout) );
// POST the POST and return the result
return _http.POST(influxout);
}
// Send sensor readings to database
// (if the network is available!)
void DB::send() {
float temp = _sensor->get_temp();
float humidity = _sensor->get_humidity();
float hindex = _sensor->get_hindex();
float analog = _sensor->get_analog();
float pressure = _sensor->get_pressure();
if (_config->conf.db_type == DB_TYPE_INFLUXDB) {
// Send Temp Sensor
if (isnan(temp) || isnan(humidity) || isnan(hindex)) {
Serial.println( "[InfluxDB] No Temp Sensor Readings Available to Send!" );
} else {
uint16_t httpCode = influxDBSend( temp, humidity, hindex );
// Parse the return
// HTTP Code 204 is successful for influxDB.
if (httpCode != HTTP_CODE_OK && httpCode != 204) {
Serial.println( "[DB] INFLUX HTTP ERROR: " + String(httpCode) );
}
}
// Send Analog
if (isnan(analog) || isnan(pressure)) {
Serial.println( "[InfluxDB] No Analog Sensor Readings Available to Send!" );
} else {
uint16_t httpCode = influxDBAnalogSend( "analog", analog, pressure );
// Parse the return
// HTTP Code 204 is successful for influxDB.
if (httpCode != HTTP_CODE_OK && httpCode != 204) {
Serial.println( "[DB] INFLUX HTTP ERROR: " + String(httpCode) );
}
}
} else if (_config->conf.db_type == DB_TYPE_HTTP) {
// TODO HTTP CALL
Serial.println( "[DB] IF AN HTTP CALL WAS CONFIGURED IT WOULD HAPPEN HERE." );
}
}