-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathWebServiceSubmit.cpp
126 lines (101 loc) · 4.09 KB
/
WebServiceSubmit.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
#include "WebServiceSubmit.h"
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
extern uint8_t DEFAULT_SLAVE_ADDR_START_RANGE;
extern int balance_status;
extern bool InverterMon;
extern double AmpsRMS;
//Implements EmonCMS WebServiceSubmit abstract/interface class
void EmonCMS::postData(eeprom_settings myConfig, cell_module (&cell_array)[24], int cell_array_max) {
if (!myConfig.emoncms_enabled) return;
String url = myConfig.emoncms_url;// "/emoncms/input/bulk?data=";
url += "[";
for (int a = 0; a < cell_array_max; a++) {
//Ensure its a sensible value to avoid filling emonCMS graph with high values
url += "[0," + String(myConfig.emoncms_node_offset + cell_array[a].address) + ",{\"V\":"
+ String(cell_array[a].valid_values ? cell_array[a].voltage : 0)
+ "},{\"T\":"
//Ensure temperature is in a sensible range
+ String(cell_array[a].valid_values ? cell_array[a].temperature : 0)
+ "},{\"B\":"
//Ensure Bypass is in a sensible range
+ String(cell_array[a].valid_values ? cell_array[a].bypass_status : 0)
+ "}]";
if (a < cell_array_max - 1) {
url += ",";
}
}
url += "]&offset=0&apikey=" + String(myConfig.emoncms_apikey);
WiFiClient client;
if (!client.connect(myConfig.emoncms_host, myConfig.emoncms_httpPort)) {
Serial.println("connection failed");
} else {
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\nHost: " + myConfig.emoncms_host + "\r\nConnection: close\r\n\r\n");
unsigned long timeout = millis() + 2500;
// Read all the lines of the reply from server and print them to Serial
while (client.connected())
{
yield();
if (millis() > timeout) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
if (client.available())
{
String line = client.readStringUntil('\n');
Serial.println(line);
}
}
client.stop();
}
}
//Implements Influxdb WebServiceSubmit abstract/interface class
void Influxdb::postData(eeprom_settings myConfig, cell_module (&cell_array)[24], int cell_array_max) {
if (!myConfig.influxdb_enabled) return;
String poststring;
String poststring1;
HTTPClient http;
//Construct URL for the influxdb
String url = "http://" + String(myConfig.influxdb_host) + ":" + myConfig.influxdb_httpPort + "/write?db=" + String(myConfig.influxdb_database) ;
//Cycle through each module and push the voltage to grafana using a http post.
for (int a = 0; a < cell_array_max; a++) {
//Ensure its a sensible value to avoid filling influxdb db with high values
if (cell_array[a].valid_values==true) {
poststring = poststring + ("Cells,Cell=" + String(a+1) + " " + "Voltage=" + String(cell_array[a].voltage) + ",Temp=" + String(cell_array[a].temperature) + ",Bypass=" + int(cell_array[a].bypass_status) + "\n");
}
}
poststring = poststring + ("Cells Battery-Balancing=" + String(balance_status) + "\n");
if (myConfig.invertermon_enabled == true) poststring = poststring + ("Cells Inverter-Usage=" + String(AmpsRMS) + "\n");
http.begin(url);
http.addHeader("Content-Type", "data-binary");
int httpCode = http.POST(poststring);
String payload = http.getString();
Serial.println (payload);
WiFiClient client;
if (!client.connect(myConfig.influxdb_host, myConfig.influxdb_httpPort)) {
Serial.println("connection failed");
} else {
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\nHost: " + myConfig.influxdb_host + "\r\nConnection: close\r\n\r\n");
unsigned long timeout = millis() + 2500;
// Read all the lines of the reply from server and print them to Serial
while (client.connected())
{
yield();
if (millis() > timeout) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
if (client.available())
{
String line = client.readStringUntil('\n');
//Serial.println(line);
}
}
client.stop();
}
}