This repository has been archived by the owner on Aug 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
restAPI.ino
202 lines (173 loc) · 7.78 KB
/
restAPI.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
/*
***************************************************************************
** Program : restAPI, part of DSMRloggerWS
** Version : v1.0.4
**
** Copyright (c) 2020 Willem Aandewiel
**
** TERMS OF USE: MIT License. See bottom of file.
***************************************************************************
*/
// global vars
// Moved to main global firmware space, since it's used in more than one place now.
// static DynamicJsonDocument jsonDoc(1024); // generic doc to return, clear() before use!
//=======================================================================
void restAPI()
{
String pName;
String pVal;
for (uint8_t i = 0; i < httpServer.args(); i++)
{
pName = httpServer.argName(i);
pName.toUpperCase();
pVal = httpServer.arg(i);
pVal.toUpperCase();
DebugTf("[%d] [%s] => [%s]\r\n", i, pName.c_str(), pVal.c_str());
if (pName == "GET") i = 99;
}
if (pName == "GET" && pVal == "DEVINFO")
{
sendDeviceInfo();
}
else if (pName == "GET" && (pVal == "ACTUEEL" || pVal == "ACTUAL"))
{
sendActual();
}
} // restAPI()
// some helper functions
void _returnJSON(JsonObject obj);
void _returnJSON(JsonObject obj)
{
String toReturn;
serializeJson(obj, toReturn);
DebugTf("JSON String is %d chars\r\n", toReturn.length());
httpServer.send(200, "application/json", toReturn);
} // _returnJSON()
void _returnJSON400(const char * message)
{
httpServer.send(400, "application/json", message);
} // _returnJSON400()
//=======================================================================
void sendDeviceInfo()
{
jsonDoc.clear();
//-Slimme Meter Info----------------------------------------------------------
jsonDoc["Identification"] = Identification;
jsonDoc["P1_Version"] = P1_Version;
jsonDoc["Equipment_Id"] = Equipment_Id;
jsonDoc["Electricity_Tariff"] = ElectricityTariff;
jsonDoc["Gas_Device_Type"] = GasDeviceType;
jsonDoc["Gas_Equipment_Id"] = GasEquipment_Id;
//-Device Info-----------------------------------------------------------------
jsonDoc["Author"] = "Willem Aandewiel (www.aandewiel.nl)";
jsonDoc["FwVersion"] = String( _FW_VERSION );
jsonDoc["Compiled"] = String( __DATE__ )
+ String( " " )
+ String( __TIME__ );
jsonDoc["FreeHeap"] = ESP.getFreeHeap();
jsonDoc["maxFreeBlock"] = ESP.getMaxFreeBlockSize();
jsonDoc["ChipID"] = String( ESP.getChipId(), HEX );
jsonDoc["CoreVersion"] = String( ESP.getCoreVersion() );
jsonDoc["SdkVersion"] = String( ESP.getSdkVersion() );
jsonDoc["CpuFreqMHz"] = ESP.getCpuFreqMHz();
jsonDoc["SketchSize"] = formatFloat( (ESP.getSketchSize() / 1024.0), 3);
jsonDoc["FreeSketchSpace"] = formatFloat( (ESP.getFreeSketchSpace() / 1024.0), 3);
if ((ESP.getFlashChipId() & 0x000000ff) == 0x85)
sprintf(cMsg, "%08X (PUYA)", ESP.getFlashChipId());
else sprintf(cMsg, "%08X", ESP.getFlashChipId());
jsonDoc["FlashChipID"] = cMsg; // flashChipId
jsonDoc["FlashChipSize_MB"] = formatFloat((ESP.getFlashChipSize() / 1024.0 / 1024.0), 3);
jsonDoc["FlashChipRealSize_MB"]= formatFloat((ESP.getFlashChipRealSize() / 1024.0 / 1024.0), 3);
jsonDoc["FlashChipSpeed_MHz"] = formatFloat((ESP.getFlashChipSpeed() / 1000.0 / 1000.0), 0);
FlashMode_t ideMode = ESP.getFlashChipMode();
jsonDoc["FlashChipMode"] = flashMode[ideMode];
jsonDoc["BoardType"] =
#ifdef ARDUINO_ESP8266_NODEMCU
"ESP8266_NODEMCU";
#endif
#ifdef ARDUINO_ESP8266_GENERIC
"ESP8266_GENERIC";
#endif
#ifdef ESP8266_ESP01
"ESP8266_ESP01";
#endif
#ifdef ESP8266_ESP12
"ESP8266_ESP12";
#endif
jsonDoc["SSID"] = WiFi.SSID();
//jsonDoc["PskKey"] = WiFi.psk(); // uncomment if you want to see this
jsonDoc["IpAddress"] = WiFi.localIP().toString();
jsonDoc["WiFiRSSI"] = WiFi.RSSI();
jsonDoc["Hostname"] = _HOSTNAME;
jsonDoc["upTime"] = upTime();
jsonDoc["TelegramCount"] = telegramCount;
jsonDoc["TelegramErrors"] = telegramErrors;
jsonDoc["lastReset"] = lastReset;
_returnJSON( jsonDoc.as<JsonObject>() );
} // sendDeviceInfo()
//=======================================================================
void sendActual()
{
jsonDoc.clear();
#ifdef HAS_NO_METER
hourData.EDT1 += 0.33;
EnergyDeliveredTariff1 = hourData.EDT1;
hourData.EDT2 += 0.17;
EnergyDeliveredTariff2 = hourData.EDT2;
EnergyDelivered = hourData.EDT1 + hourData.EDT2;
hourData.ERT1 += 0.11;
EnergyReturnedTariff1 = hourData.ERT1;
hourData.ERT2 += 0.07;
EnergyReturnedTariff2 = hourData.ERT2;
EnergyReturned = hourData.ERT1 + hourData.ERT2;
hourData.GDT += 0.08;
GasDelivered = hourData.GDT;
#endif
//-Totalen----------------------------------------------------------
jsonDoc["Timestamp"] = pTimestamp;
jsonDoc["Energy_Delivered"] = formatFloat(EnergyDelivered, 3);
jsonDoc["Energy_Returned"] = formatFloat(EnergyReturned, 3);
jsonDoc["Gas_Delivered"] = formatFloat(GasDelivered, 2);
jsonDoc["Energy_Delivered_Tariff1"] = formatFloat(EnergyDeliveredTariff1, 3);
jsonDoc["Energy_Delivered_Tariff2"] = formatFloat(EnergyDeliveredTariff2, 3);
jsonDoc["Energy_Returned_Tariff1"] = formatFloat(EnergyReturnedTariff1, 3);
jsonDoc["Energy_Returned_Tariff2"] = formatFloat(EnergyReturnedTariff2, 3);
jsonDoc["Energy_Tariff"] = ElectricityTariff;
jsonDoc["Power_Delivered"] = formatFloat(PowerDelivered, 3);
jsonDoc["Power_Returned"] = formatFloat(PowerReturned, 3);
jsonDoc["Voltage_l1"] = formatFloat(Voltage_l1, 1);
jsonDoc["Current_l1"] = Current_l1;
jsonDoc["Voltage_l2"] = formatFloat(Voltage_l2, 1);
jsonDoc["Current_l2"] = Current_l2;
jsonDoc["Voltage_l3"] = formatFloat(Voltage_l3, 1);
jsonDoc["Current_l3"] = Current_l3;
jsonDoc["Power_Delivered_l1"] = PowerDelivered_l1;
jsonDoc["Power_Returned_l1"] = PowerReturned_l1;
jsonDoc["Power_Delivered_l2"] = PowerDelivered_l2;
jsonDoc["Power_Returned_l2"] = PowerReturned_l2;
jsonDoc["Power_Delivered_l3"] = PowerDelivered_l3;
jsonDoc["Power_Returned_l3"] = PowerReturned_l3;
_returnJSON( jsonDoc.as<JsonObject>() );
} // sendActual()
/***************************************************************************
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
***************************************************************************/