forked from lumapu/ahoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModWebserver.h
151 lines (123 loc) · 4.22 KB
/
ModWebserver.h
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
// ################# WebServer #################
#ifndef __MODWEBSERVER_H
#define __MODWEBSERVER_H
#define MODWEBSERVER
#include <ESP8266WebServer.h>
#include "Debug.h"
#include "Settings.h"
ESP8266WebServer server (WEBSERVER_PORT);
void returnOK () {
//--------------
server.send(200, F("text/plain"), "");
}
void returnFail(String msg) {
//-------------------------
server.send(500, F("text/plain"), msg + "\r\n");
}
void handleHelp () {
//-----------------
String out = "<html>";
out += "<body><h2>Hilfe</h2>";
out += "<br><br><table>";
out += "<tr><td>/</td><td>zeigt alle Messwerte in einer Tabelle; refresh alle 10 Sekunden</td></tr>";
out += "<tr><td>/data</td><td>zum Abruf der Messwerte in der Form Name=wert</td></tr>";
out += "<tr><td>:{port+1}/update</td><td>OTA</td></tr>";
out += "<tr><td>/reboot</td><td>startet neu</td></tr>";
out += "</table></body></html>";
server.send (200, "text/html", out);
}
void handleReboot () {
//-------------------
returnOK ();
ESP.reset();
}
void handleRoot() {
//----------------
String out = "<html><head><meta http-equiv=\"refresh\" content=\"10\":URL=\"" + server.uri() + "\"></head>";
out += "<body>";
out += "<h2>Hoymiles Micro-Inverters</h2>";
char floatString[20];
char line[100];
for (uint8_t wr = 0; wr < anzInv; wr++) {
out += "<h3>" + String(inverters[wr].name) + "</h3>";
out += "<h3>S/N " + String (getSerialNoTxt(wr)) + "</h3>";
out += "<br><br><table border='1'>";
out += "<tr><th>Kanal</th><th>Wert</th><th>Einheit</th></tr>";
for (uint8_t i = 0; i < inverters[wr].anzTotalMeasures; i++) {
dtostrf (getMeasureValue(wr, i),1, getDigits(wr,i), floatString);
sprintf(line, "<tr><td>%s</td><td>%s</td><td>%s</td></tr>", getMeasureName(wr, i), floatString, getUnit(wr, i));
//DEBUG_OUT.println(line);
out += String(line);
/* out += "<tr><td>" + getMeasureName(i) + "</td>";
out += "<td>" + String(getMeasureValue(i)) + "</td></tr>";
out += "<td>" + String(getUnit(i)) + "</td></tr>"; */
}
out += "</table>";
}
int pos = out.indexOf("°");
do {
if (pos>1) {
out = out.substring (0, pos) + "°" + out.substring(pos+2);
}
pos = out.indexOf("°");
} while (pos>1);
out += "</body></html>";
server.send (200, "text/html", out);
//DEBUG_OUT.println (out);
}
void handleData () {
//-----------------
String out = "";
for (uint8_t wr = 0; wr < anzInv; wr++) {
for (int i = 0; i < inverters[wr].anzTotalMeasures; i++) {
out += (anzInv <= 1 ? "" : String (wr) + ".") + String(getMeasureName(wr,i)) + '='
+ String (getMeasureValue(wr,i)) /*+ ' ' + String(getUnit(wr,i))*/ + '\n';
}
}
server.send(200, "text/plain", out);
}
void handleNotFound() {
//--------------------
String message = "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " NAME:" + server.argName(i) + "\n VALUE:" + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
void setupWebServer (void) {
//-------------------------
server.begin();
server.on("/", handleRoot);
server.on("/reboot", handleReboot);
server.on("/data", handleData);
server.on("/help", handleHelp);
//server.onNotFound(handleNotFound); wegen Spiffs-Dateimanager
DEBUG_OUT.println ("[HTTP] installed");
}
void webserverHandle() {
//====================
server.handleClient();
}
// ################# OTA #################
#ifdef WITH_OTA
#include <ESP8266HTTPUpdateServer.h>
ESP8266WebServer httpUpdateServer (UPDATESERVER_PORT);
ESP8266HTTPUpdateServer httpUpdater;
void setupUpdateByOTA () {
//------------------------
httpUpdater.setup (&httpUpdateServer, UPDATESERVER_DIR, UPDATESERVER_USER, UPDATESERVER_PW);
httpUpdateServer.begin();
DEBUG_OUT.println ("[OTA] installed");
}
void checkUpdateByOTA() {
//---------------------
httpUpdateServer.handleClient();
}
#endif
#endif