forked from tbnobody/OpenDTU
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Power meter support: HTTP(S) + JSON (Shelly 3EM, Tasmota, Volkszä…
…hler etc.) (#153) * Implement HTTP(s) + JSON type Power Meter support --------- Co-authored-by: Bernhard Kaszt <berni@bcserv.eu>
- Loading branch information
Showing
15 changed files
with
639 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
#pragma once | ||
|
||
#include <stdint.h> | ||
|
||
class HttpPowerMeterClass { | ||
public: | ||
void init(); | ||
bool updateValues(); | ||
float getPower(int8_t phase); | ||
bool httpRequest(const char* url, const char* httpHeader, const char* httpValue, uint32_t timeout, | ||
char* response, size_t responseSize, char* error, size_t errorSize); | ||
float getFloatValueByJsonPath(const char* jsonString, const char* jsonPath, float &value); | ||
|
||
private: | ||
float power[POWERMETER_MAX_PHASES]; | ||
}; | ||
|
||
extern HttpPowerMeterClass HttpPowerMeter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
#include "Configuration.h" | ||
#include "HttpPowerMeter.h" | ||
#include "MessageOutput.h" | ||
#include <WiFiClientSecure.h> | ||
#include <HTTPClient.h> | ||
#include <FirebaseJson.h> | ||
|
||
void HttpPowerMeterClass::init() | ||
{ | ||
} | ||
|
||
float HttpPowerMeterClass::getPower(int8_t phase) | ||
{ | ||
return power[phase - 1]; | ||
} | ||
|
||
bool HttpPowerMeterClass::updateValues() | ||
{ | ||
const CONFIG_T& config = Configuration.get(); | ||
|
||
char response[2000], | ||
errorMessage[256]; | ||
bool success = true; | ||
|
||
for (uint8_t i = 0; i < POWERMETER_MAX_PHASES; i++) { | ||
POWERMETER_HTTP_PHASE_CONFIG_T phaseConfig = config.Powermeter_Http_Phase[i]; | ||
|
||
if (!phaseConfig.Enabled || !success) { | ||
power[i] = 0.0; | ||
continue; | ||
} | ||
|
||
if (i == 0 || config.PowerMeter_HttpIndividualRequests) { | ||
if (!httpRequest(phaseConfig.Url, phaseConfig.HeaderKey, phaseConfig.HeaderValue, phaseConfig.Timeout, | ||
response, sizeof(response), errorMessage, sizeof(errorMessage))) { | ||
MessageOutput.printf("[HttpPowerMeter] Getting the power of phase %d failed. Error: %s\r\n", | ||
i + 1, errorMessage); | ||
success = false; | ||
} | ||
} | ||
|
||
if (!getFloatValueByJsonPath(response, phaseConfig.JsonPath, power[i])) { | ||
MessageOutput.printf("[HttpPowerMeter] Couldn't find a value with Json query \"%s\"\r\n", phaseConfig.JsonPath); | ||
success = false; | ||
} | ||
} | ||
|
||
return success; | ||
} | ||
|
||
bool HttpPowerMeterClass::httpRequest(const char* url, const char* httpHeader, const char* httpValue, uint32_t timeout, | ||
char* response, size_t responseSize, char* error, size_t errorSize) | ||
{ | ||
WiFiClient* wifiClient = NULL; | ||
HTTPClient httpClient; | ||
|
||
response[0] = '\0'; | ||
error[0] = '\0'; | ||
|
||
if (String(url).substring(0, 6) == "https:") { | ||
wifiClient = new WiFiClientSecure; | ||
reinterpret_cast<WiFiClientSecure*>(wifiClient)->setInsecure(); | ||
} else { | ||
wifiClient = new WiFiClient; | ||
} | ||
|
||
if (!httpClient.begin(*wifiClient, url)) { | ||
snprintf_P(error, errorSize, "httpClient.begin failed"); | ||
delete wifiClient; | ||
return false; | ||
} | ||
|
||
httpClient.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); | ||
httpClient.setUserAgent("OpenDTU-OnBattery"); | ||
httpClient.setConnectTimeout(timeout); | ||
httpClient.setTimeout(timeout); | ||
httpClient.addHeader("Content-Type", "application/json"); | ||
httpClient.addHeader("Accept", "application/json"); | ||
|
||
if (strlen(httpHeader) > 0) { | ||
httpClient.addHeader(httpHeader, httpValue); | ||
} | ||
|
||
int httpCode = httpClient.GET(); | ||
|
||
|
||
if (httpCode == HTTP_CODE_OK) { | ||
String responseBody = httpClient.getString(); | ||
|
||
if (responseBody.length() > (responseSize - 1)) { | ||
snprintf_P(error, errorSize, "Response too large! Response length: %d Body start: %s", | ||
httpClient.getSize(), responseBody.c_str()); | ||
} else { | ||
snprintf(response, responseSize, responseBody.c_str()); | ||
} | ||
} else if (httpCode <= 0) { | ||
snprintf_P(error, errorSize, "Error: %s", httpClient.errorToString(httpCode).c_str()); | ||
} else if (httpCode != HTTP_CODE_OK) { | ||
snprintf_P(error, errorSize, "Bad HTTP code: %d", httpCode); | ||
} | ||
|
||
httpClient.end(); | ||
delete wifiClient; | ||
|
||
if (error[0] != '\0') { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
float HttpPowerMeterClass::getFloatValueByJsonPath(const char* jsonString, const char* jsonPath, float& value) | ||
{ | ||
FirebaseJson firebaseJson; | ||
firebaseJson.setJsonData(jsonString); | ||
|
||
FirebaseJsonData firebaseJsonResult; | ||
if (!firebaseJson.get(firebaseJsonResult, jsonPath)) { | ||
return false; | ||
} | ||
|
||
value = firebaseJsonResult.to<float>(); | ||
|
||
firebaseJson.clear(); | ||
|
||
return true; | ||
} | ||
|
||
HttpPowerMeterClass HttpPowerMeter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.