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
Changes from 2 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
120 changes: 120 additions & 0 deletions src/src/Helpers/Networking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,126 @@ int http_authenticate(const String& logIdentifier,
}
}
#endif

# if FEATURE_OMETEO_EVENT
chromoxdor marked this conversation as resolved.
Show resolved Hide resolved

// 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&current=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.

if ((httpCode == 200) && equals(host, F("api.open-meteo.com")))
{
// Length of the response is limited to 5000 characters
// ToDo: What would be right size?
if (uri.length() > 5000) {
addLog(LOG_LEVEL_ERROR, F("Response exceeds 5000 characters"));
}
else {
String str = http.getString();
chromoxdor marked this conversation as resolved.
Show resolved Hide resolved

// Process URL to get unique keys
auto getCombinedParams = [](String url, String paramName) {
chromoxdor marked this conversation as resolved.
Show resolved Hide resolved
int start = url.indexOf(paramName + "=");
chromoxdor marked this conversation as resolved.
Show resolved Hide resolved

if (start == -1) { return String(""); }
chromoxdor marked this conversation as resolved.
Show resolved Hide resolved
start += paramName.length() + 1;
int end = url.indexOf('&', start);
chromoxdor marked this conversation as resolved.
Show resolved Hide resolved
return (end == -1) ? url.substring(start) : url.substring(start, end);
};

// Extract current hourly and daily parameters
String currentParams = getCombinedParams(uri, "current");
String hourlyParams = getCombinedParams(uri, "hourly");
String dailyParams = getCombinedParams(uri, "daily");
chromoxdor marked this conversation as resolved.
Show resolved Hide resolved

auto processAndQueueParams = [](String& params, String& str, String eventName) {
chromoxdor marked this conversation as resolved.
Show resolved Hide resolved
if (!params.isEmpty()) {
String keys[20];
int keyCount = 0;
chromoxdor marked this conversation as resolved.
Show resolved Hide resolved

int startIndex = 0;
int commaIndex = params.indexOf(',');

// Split and add keys to the array
while (commaIndex != -1) {
String key = params.substring(startIndex, commaIndex);
keys[keyCount++] = key;
chromoxdor marked this conversation as resolved.
Show resolved Hide resolved
startIndex = commaIndex + 1;
commaIndex = params.indexOf(',', startIndex);
}

// Add the last key
String lastKey = params.substring(startIndex);
chromoxdor marked this conversation as resolved.
Show resolved Hide resolved
keys[keyCount++] = lastKey;
chromoxdor marked this conversation as resolved.
Show resolved Hide resolved

String csv = "";
chromoxdor marked this conversation as resolved.
Show resolved Hide resolved
int startStringIndex = str.indexOf("\"" + eventName + "\":") + eventName.length() + 4;
int endStringIndex = str.indexOf("}", startStringIndex);
chromoxdor marked this conversation as resolved.
Show resolved Hide resolved

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

if (startIndex == -1)
{
// Handle case where key is not found
value = "-256"; // Placeholder value
chromoxdor marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
int endIndex = 0;

if (eventName != "current") { // in daily and hourly the values are stored in an
chromoxdor marked this conversation as resolved.
Show resolved Hide resolved
// array
// so
// get rid of the brackets and put the values in a csv
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 += ",";
chromoxdor marked this conversation as resolved.
Show resolved Hide resolved
}
csv += value;
}
eventName = "OpenMeteo#" + eventName;
chromoxdor marked this conversation as resolved.
Show resolved Hide resolved
eventQueue.addMove(strformat(F("%s=%s"), eventName.c_str(), csv.c_str()));
}
};

processAndQueueParams(currentParams, str, "current");
processAndQueueParams(hourlyParams, str, "hourly");
processAndQueueParams(dailyParams, str, "daily");
chromoxdor marked this conversation as resolved.
Show resolved Hide resolved
}
}
# endif // if FEATURE_OMETEO_EVENT
}

#ifndef BUILD_NO_DEBUG
Expand Down