Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sendtohttp] Add Open-Meteo events + JSON event #5207

Open
wants to merge 32 commits into
base: mega
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
514a140
Update Networking.cpp
chromoxdor Jan 2, 2025
a553671
a tiny bit better formatting
chromoxdor Jan 2, 2025
86812a1
changes for OPENMETEO_EVENT and THINGSPEAK_EVENT
chromoxdor Jan 2, 2025
3bc16d8
Update Networking.cpp
chromoxdor Jan 2, 2025
aaa938e
combining two functions
chromoxdor Jan 2, 2025
94ec6b0
wrong use of concat times two :)
chromoxdor Jan 2, 2025
7325ed8
update
chromoxdor Jan 3, 2025
2bc5810
Update Networking.cpp
chromoxdor Jan 3, 2025
0f5b5f2
put thingspeak and openmeteo events into extra file
chromoxdor Jan 3, 2025
fc6ccaa
added inverter event
chromoxdor Jan 3, 2025
0ca2d75
fix for not building on minimal_core_274_esp8266...
chromoxdor Jan 3, 2025
daafc7d
#ifdef to #if. m(
chromoxdor Jan 3, 2025
56e2acf
more universal json event approach
chromoxdor Jan 4, 2025
171cbeb
Update HTTPResponseParser.h
chromoxdor Jan 4, 2025
6d2215b
added variable decimals for floating point numbers
chromoxdor Jan 4, 2025
fe8786f
some changes to the json-event
chromoxdor Jan 4, 2025
1500f3b
made the eventQueue even more complex :P
chromoxdor Jan 4, 2025
0bf7b66
minor changes
chromoxdor Jan 4, 2025
c7a23c2
moved variable to cpp file
chromoxdor Jan 4, 2025
2b571a4
updates
chromoxdor Jan 6, 2025
6083741
Update HTTPResponseParser.cpp
chromoxdor Jan 6, 2025
89a029f
Update define_plugin_sets.h
chromoxdor Jan 6, 2025
57569ca
Update HTTPResponseParser.cpp
chromoxdor Jan 6, 2025
bd6523a
Update HTTPResponseParser.cpp
chromoxdor Jan 6, 2025
229037b
Merge branch 'letscontrolit:mega' into openmeteo-events
chromoxdor Jan 15, 2025
768d4c2
documentation and slight reduction of code
chromoxdor Jan 15, 2025
68dba8c
Update define_plugin_sets.h
chromoxdor Jan 15, 2025
72b49bb
corrected json examples
chromoxdor Jan 15, 2025
390974f
Docs: Formatting and linking
chromoxdor Jan 16, 2025
35ed195
Merge branch 'mega' into openmeteo-events
chromoxdor Jan 21, 2025
40f83cc
fixed typo
chromoxdor Jan 21, 2025
97aab88
Merge branch 'openmeteo-events' of https://github.com/chromoxdor/ESPE…
chromoxdor Jan 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Custom-sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
#define FEATURE_RULES_EASY_COLOR_CODE 1 // Use code highlighting, autocompletion and command suggestions in Rules
#define FEATURE_ESPEASY_P2P 1 // (1/0) enables the ESP Easy P2P protocol
#define FEATURE_ARDUINO_OTA 1 // enables the Arduino OTA capabilities
#define FEATURE_THINGSPEAK_EVENT 1 // generate an event when requesting last value of a field in thingspeak via SendToHTTP(e.g. sendToHTTP,api.thingspeak.com,80,/channels/1667332/fields/5/last)
#define FEATURE_THINGSPEAK_EVENT 0 // Generates an event when requesting last value of a field in thingspeak via SendToHTTP(e.g. sendToHTTP,api.thingspeak.com,80,/channels/1667332/fields/5/last)
#define FEATURE_OPENMETEO_EVENT 0 // Generates an event with the response of a open-meteo request (https://open-meteo.com/en/docs)
#define FEATURE_INVERTER_EVENT 0 // Generates an event with the response of an inverter example URL: http://192.168.1.199/solar_api/v1/GetInverterRealtimeData.cgi?Scope=System
// #define FEATURE_SD 1 // Enable SD card support
// #define FEATURE_DOWNLOAD 1 // Enable downloading a file from an url

Expand Down
26 changes: 22 additions & 4 deletions src/src/CustomBuild/define_plugin_sets.h
Original file line number Diff line number Diff line change
Expand Up @@ -3620,14 +3620,32 @@ To create/register a plugin, you have to :
*/


#ifndef FEATURE_THINGSPEAK_EVENT
#ifdef LIMIT_BUILD_SIZE
#define FEATURE_THINGSPEAK_EVENT 0
#ifndef FEATURE_THINGSPEAK_EVENT
#if defined(PLUGIN_BUILD_MAX_ESP32)
#define FEATURE_THINGSPEAK_EVENT 1
#else
#define FEATURE_THINGSPEAK_EVENT 0
#endif
#endif

#ifndef FEATURE_OPENMETEO_EVENT
#if defined(PLUGIN_BUILD_MAX_ESP32)
#define FEATURE_OPENMETEO_EVENT 1
#else
#define FEATURE_THINGSPEAK_EVENT 1
#define FEATURE_OPENMETEO_EVENT 0
#endif
#endif

#ifndef FEATURE_INVERTER_EVENT
#define FEATURE_INVERTER_EVENT 0
#endif

#if FEATURE_THINGSPEAK_EVENT || FEATURE_OPENMETEO_EVENT || FEATURE_INVERTER_EVENT
#define RESPONSE_PARSER_SUPPORT 1
#else
#define RESPONSE_PARSER_SUPPORT 0
#endif

#if !(defined(SOC_DAC_SUPPORTED) && SOC_DAC_SUPPORTED)
#ifdef USES_P152
#undef USES_P152
Expand Down
243 changes: 243 additions & 0 deletions src/src/Helpers/HTTPResponseParser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
#include "../Helpers/Networking.h"
chromoxdor marked this conversation as resolved.
Show resolved Hide resolved
#include "../Globals/EventQueue.h"
#include "../Helpers/StringConverter.h"
#include "../Helpers/StringProvider.h"

#include <ArduinoJson.h>

void eventFromResponse(const String& host, const int& httpCode, const String& uri, HTTPClient& http) {

// -------------------------------------------------------------------------------------------Thingspeak
#if FEATURE_THINGSPEAK_EVENT

// Generate event with the response of a
// thingspeak request (https://de.mathworks.com/help/thingspeak/readlastfieldentry.html &
// https://de.mathworks.com/help/thingspeak/readdata.html)
// e.g. command for a specific field: "sendToHTTP,api.thingspeak.com,80,/channels/1637928/fields/5/last.csv"
// command for all fields: "sendToHTTP,api.thingspeak.com,80,/channels/1637928/feeds/last.csv"
// where first eventvalue is the channel number and the second to the nineth event values
// are the field values
// Example of the event: "EVENT: ThingspeakReply=1637928,5,24.2,12,900,..."
// ^ ^ └------┬------┘
// channel number ┘ | └ received values
// field number (only available for a "single-value-event")
// In rules you can grep the reply by "On ThingspeakReply Do ..."
// -----------------------------------------------------------------------------------------------------------------------------
// 2024-02-05 - Added the option to get a single value of a field or all values of a channel at a certain time (not only the last entry)
// Examples:
// Single channel: "sendtohttp,api.thingspeak.com,80,channels/1637928/fields/1.csv?end=2024-01-01%2023:59:00&results=1"
// => gets the value of field 1 at (or the last entry before) 23:59:00 of the channel 1637928
// All channels: "sendtohttp,api.thingspeak.com,80,channels/1637928/feeds.csv?end=2024-01-01%2023:59:00&results=1"
// => gets the value of each field of the channel 1637928 at (or the last entry before) 23:59:00
// -----------------------------------------------------------------------------------------------------------------------------

if ((httpCode == 200) && equals(host, F("api.thingspeak.com")) &&
(uri.endsWith(F("/last.csv")) || ((uri.indexOf(F("results=1")) >= 0) && (uri.indexOf(F(".csv")) >= 0)))) {
String result = http.getString();

result.replace(' ', '_'); // if using a single field with a certain time, the result contains a space and would break the code
const int posTimestamp = result.lastIndexOf(':');

if (posTimestamp >= 0) {
result = parseStringToEndKeepCase(result.substring(posTimestamp), 3);

if (uri.indexOf(F("fields")) >= 0) { // when there is a single field call add the field number
// before the value
result = parseStringKeepCase(uri, 4, '/').substring(0, 1) + "," + result; // since the field number is always the fourth part of the
// url and is always a single digit, we can use this to
// extact the fieldnumber
}
eventQueue.addMove(strformat(
F("ThingspeakReply=%s,%s"),
parseStringKeepCase(uri, 2, '/').c_str(),
result.c_str()));
}
}
#endif // if FEATURE_THINGSPEAK_EVENT

// ------------------------------------------------------------------------------------------- OpenMeteo
#if FEATURE_OPENMETEO_EVENT

// Generate an event with the response of an open-meteo request.
// Example command:
// sendtohttp,api.open-meteo.com,80,"/v1/forecast?latitude=52.52&longitude=13.41¤t=temperature_2m,weather_code,wind_speed_10m&daily=temperature_2m_max,temperature_2m_min&forecast_days=1"
// No need for an api key and it is free (daily requests are limited to 10,000 in the free version)
// Visit the URL (https://open-meteo.com/en/docs) and build your personal URL by selecting the location and values you want to receive.
// Supported variable kinds are current, hourly, daily!
// In rules you can grep the reply by the kind of weather variables with "On Openmeteo#<type> Do ..."
// e.g. "On Openmeteo#current Do ..."
// Note: hourly and daily results are arrays which can become very long.
// Best to make seperate calls. Especially for hourly results.

// define limits
# define WEATHER_KEYS_MAX 10
# define URI_MAX_LENGTH 5000

if ((httpCode == 200) && equals(host, F("api.open-meteo.com"))) {
const String str = http.getString();

if (str.length() > URI_MAX_LENGTH) {
addLog(LOG_LEVEL_ERROR, strformat(F("Response exceeds %d characters which could cause instabilities or crashes!"), URI_MAX_LENGTH));
}

auto processAndQueueParams = [](const String& url, const String& str, const String& eventName) {
// Extract the parameters from the URL
int start = url.indexOf(eventName + '=');

if (start == -1) {
return; // No parameters found for the given eventName
}
start += eventName.length() + 1;
const int end = url.indexOf('&', start);
const String params = (end == -1) ? url.substring(start) : url.substring(start, end);

if (!params.isEmpty()) {
String keys[WEATHER_KEYS_MAX];
int keyCount = 0;
int startIndex = 0;
int commaIndex = params.indexOf(',');

// Split and add keys to the array
while (commaIndex != -1) {
if (keyCount >= WEATHER_KEYS_MAX) {
// Stop adding keys if array is full
addLog(LOG_LEVEL_ERROR,
strformat(F(
"More than %d keys in the URL, this could cause instabilities or crashes! Try to split up the calls.."),
WEATHER_KEYS_MAX));
break;
}
String key = params.substring(startIndex, commaIndex);
keys[keyCount++] = key;
startIndex = commaIndex + 1;
commaIndex = params.indexOf(',', startIndex);
}

// Add the last key
if (keyCount < WEATHER_KEYS_MAX) {
const String lastKey = params.substring(startIndex);
keys[keyCount++] = lastKey;
}

String csv;
const int startStringIndex = str.indexOf(strformat(F("\"%s\":"),
eventName.c_str())) +
eventName.length() + 4;

// example( },"current":{"time":... ) we want to start after current":{

const int endStringIndex = str.indexOf('}', startStringIndex);

// ...and want to end before }

for (int i = 0; i < keyCount; i++) // Use keyCount to limit the iteration
{
String key = keys[i];
String value;
int startIndex = str.indexOf(strformat(F("%s\":"), key.c_str()), startStringIndex);

if (startIndex == -1) {
// Handle case where key is not found
value = F("-256"); // Placeholder value
} else {
int endIndex = 0;

if (!equals(eventName, F("current"))) {
// In daily and hourly, the values are stored in an array
startIndex += key.length() + 3; // Move index past the key
endIndex = str.indexOf(']', startIndex);
} else {
startIndex += key.length() + 2; // Move index past the key
endIndex = str.indexOf(',', startIndex);
}

// Find the index of the next comma
if ((endIndex == -1) || (endIndex > str.indexOf("}", startIndex))) {
endIndex = str.indexOf('}', startIndex); // If no comma is found or comma comes after },
// take the rest of the string
}

value = str.substring(startIndex, endIndex);
value.trim(); // Remove any surrounding whitespace
}

if (!csv.isEmpty()) {
csv += ',';
}
csv += value;
}
eventQueue.addMove(strformat(F("OpenMeteo#%s=%s"), eventName.c_str(), csv.c_str()));
}
};

processAndQueueParams(uri, str, F("current"));
processAndQueueParams(uri, str, F("hourly"));
processAndQueueParams(uri, str, F("daily"));
}

#endif // if FEATURE_OMETEO_EVENT

// ------------------------------------------------------------------------------------------- Inverter
#if FEATURE_INVERTER_EVENT

if ((httpCode == 200) && (uri.endsWith(F("GetInverterRealtimeData.cgi?Scope=System"))))
{
DynamicJsonDocument *root = nullptr;
uint16_t lastJsonMessageLength = 512;

const String message = http.getString();

// Cleanup lambda to deallocate resources
auto cleanupJSON = [&root]() {
if (root != nullptr) {
root->clear();
delete root;
root = nullptr;
}
};

// Check if root needs resizing or cleanup
if ((root != nullptr) && (message.length() * 1.5 > lastJsonMessageLength)) {
cleanupJSON();
}

// Resize lastJsonMessageLength if needed
if (message.length() * 1.5 > lastJsonMessageLength) {
lastJsonMessageLength = message.length() * 1.5;
}

// Allocate memory for root if needed
if (root == nullptr) {
# ifdef USE_SECOND_HEAP
HeapSelectIram ephemeral;
# endif // ifdef USE_SECOND_HEAP
root = new (std::nothrow) DynamicJsonDocument(lastJsonMessageLength); // Dynamic allocation
}

if (root != nullptr) {
// Parse the JSON
DeserializationError error = deserializeJson(*root, message);

if (!error) {
int dayEnergy = (*root)["Body"]["Data"]["DAY_ENERGY"]["Values"]["1"].as<int>();
int pac = (*root)["Body"]["Data"]["PAC"]["Values"]["1"].as<int>();
int totalEnergy = (*root)["Body"]["Data"]["TOTAL_ENERGY"]["Values"]["1"].as<int>();
int yearEnergy = (*root)["Body"]["Data"]["YEAR_ENERGY"]["Values"]["1"].as<int>();

addLog(LOG_LEVEL_INFO, strformat(F("Inverter: Day Energy: %d, PAC: %d, Total Energy: %d, Year Energy: %d"),
dayEnergy,
pac,
totalEnergy,
yearEnergy));
eventQueue.addMove(strformat(F("InverterReply=%d,%d,%d,%d"), dayEnergy, pac, totalEnergy, yearEnergy));
} else {
Serial.println("Failed to parse JSON");
}

// Cleanup JSON resources
cleanupJSON();
}
}

#endif // ifdef FEATURE_INVERTER_EVENT
}
18 changes: 18 additions & 0 deletions src/src/Helpers/HTTPResponseParser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef HELPERS_HTTPRESPONSEPARSER_H
#define HELPERS_HTTPRESPONSEPARSER_H

#include "../../ESPEasy_common.h"
#include "../../_Plugin_Helper.h"

#ifdef PLUGIN_BUILD_MINIMAL_OTA
# include <ESP8266HTTPClient.h>
chromoxdor marked this conversation as resolved.
Show resolved Hide resolved
#endif // ifdef ESP8266


// Function declarations
void eventFromResponse(const String& host,
const int & httpCode,
const String& uri,
HTTPClient & http);

#endif // ifndef HELPERS_HTTPRESPONSEPARSER_H
47 changes: 8 additions & 39 deletions src/src/Helpers/Networking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
#include "../Helpers/StringConverter.h"
#include "../Helpers/StringProvider.h"

#if RESPONSE_PARSER_SUPPORT
#include "../Helpers/HTTPResponseParser.h"
#endif

#include "../../ESPEasy-Globals.h"

#include <IPAddress.h>
Expand All @@ -43,6 +47,7 @@

#include <lwip/dns.h>


// Generic Networking routines

// Syslog
Expand Down Expand Up @@ -1643,45 +1648,9 @@ int http_authenticate(const String& logIdentifier,
// Generate event with the HTTP return code
// e.g. http#hostname=401
eventQueue.addMove(strformat(F("http#%s=%d"), host.c_str(), httpCode));

#if FEATURE_THINGSPEAK_EVENT
// Generate event with the response of a
// thingspeak request (https://de.mathworks.com/help/thingspeak/readlastfieldentry.html &
// https://de.mathworks.com/help/thingspeak/readdata.html)
// e.g. command for a specific field: "sendToHTTP,api.thingspeak.com,80,/channels/1637928/fields/5/last.csv"
// command for all fields: "sendToHTTP,api.thingspeak.com,80,/channels/1637928/feeds/last.csv"
// where first eventvalue is the channel number and the second to the nineth event values
// are the field values
// Example of the event: "EVENT: ThingspeakReply=1637928,5,24.2,12,900,..."
// ^ ^ └------┬------┘
// channel number ┘ | └ received values
// field number (only available for a "single-value-event")
// In rules you can grep the reply by "On ThingspeakReply Do ..."
//-----------------------------------------------------------------------------------------------------------------------------
// 2024-02-05 - Added the option to get a single value of a field or all values of a channel at a certain time (not only the last entry)
// Examples:
// Single channel: "sendtohttp,api.thingspeak.com,80,channels/1637928/fields/1.csv?end=2024-01-01%2023:59:00&results=1"
// => gets the value of field 1 at (or the last entry before) 23:59:00 of the channel 1637928
// All channels: "sendtohttp,api.thingspeak.com,80,channels/1637928/feeds.csv?end=2024-01-01%2023:59:00&results=1"
// => gets the value of each field of the channel 1637928 at (or the last entry before) 23:59:00
//-----------------------------------------------------------------------------------------------------------------------------

if (httpCode == 200 && equals(host, F("api.thingspeak.com")) && (uri.endsWith(F("/last.csv")) || (uri.indexOf(F("results=1")) >= 0 && uri.indexOf(F(".csv")) >= 0))){
String result = http.getString();
result.replace(' ', '_'); // if using a single field with a certain time, the result contains a space and would break the code
const int posTimestamp = result.lastIndexOf(':');
if (posTimestamp >= 0){
result = parseStringToEndKeepCase(result.substring(posTimestamp), 3);
if (uri.indexOf(F("fields")) >= 0){ // when there is a single field call add the field number before the value
result = parseStringKeepCase(uri, 4, '/').substring(0, 1) + "," + result; // since the field number is always the fourth part of the url and is always a single digit, we can use this to extact the fieldnumber
}
eventQueue.addMove(strformat(
F("ThingspeakReply=%s,%s"),
parseStringKeepCase(uri, 2, '/').c_str(),
result.c_str()));
}
}
#endif
#if RESPONSE_PARSER_SUPPORT
eventFromResponse(host, httpCode, uri, http);
#endif
}

#ifndef BUILD_NO_DEBUG
Expand Down
Loading