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 19 commits into
base: mega
Choose a base branch
from

Conversation

chromoxdor
Copy link
Contributor

@chromoxdor chromoxdor commented Jan 2, 2025

As being asked here: https://www.letscontrolit.com/forum/viewtopic.php?p=72083#p72083 i do a PR for Open-Meteo events

  1. It parses url for the variable type (current,hourly, daily)
  2. Contrains the response and parses this for the variables
  3. depending on the variable type it generates an event (e.g. On OpenMeteo#current Do... )

It works but responses can get quite long, especially for hourly variables and also when all types combined together.
But for now it was working fine on an esp32 classic even with very long resonses > 6kb

I tried to kind of create a size check but i am no expert for memory constraint systems. Maybe you can help out here with some recommendations.

I guess when calling this it is already to late 🙂 :

 if (uri.length() > 5000) {
        addLog(LOG_LEVEL_ERROR, F("Response exceeds 5000 characters"));
      }
      else {

How to use:

  1. visit https://open-meteo.com/en/docs
  2. click together what you need and scroll down to the API Response section where you find this:
Bildschirmfoto 2025-01-02 um 14 59 15
  1. usesendtohttp api.open-meteo.com,80,/v1/forecast?...

Issues:

  1. ESPEasy throws a "Too many arguments" error but it still works fine
  2. In the log only 25 eventvalues are displayed. But you can still access all of them with rules. I wonder where the limit is?

@chromoxdor
Copy link
Contributor Author

chromoxdor commented Jan 2, 2025

Command:
sendtohttp api.open-meteo.com,80,"/v1/forecast?latitude=52.5244&longitude=13.4105&current=temperature_2m,relative_humidity_2m&daily=temperature_2m_max,temperature_2m_min,uv_index_max&timezone=Europe%2FLondon&forecast_days=3"

Response:

2580494: EVENT: OpenMeteo#current=4.3,76
2580503: EVENT: OpenMeteo#daily=7.5,2.4,1.2,1.5,-1.0,-1.1,1.00,0.95,0.70

@tonhuisman
Copy link
Contributor

tonhuisman commented Jan 2, 2025

  1. ESPEasy throws a "Too many arguments" error but it still works fine

Wrap the (long) url, that contains several commas, in quotes, and this issue should be resolved 😉
Alternative: url-encode the commas to %2C so ESPEasy doesn't see them as commas

@chromoxdor
Copy link
Contributor Author

Wrap the (long) url, that contains several commas, in quotes, and this issue should be resolved 😉

I tried that and it didn't work...but now i think i know what happend. I have this auto correction plugin in my browser. Disabling it helped :)

- added FEATURE_OPENMETEO_EVENT to Custom-sample.h
- disabled FEATURE_THINGSPEAK_EVENT and FEATURE_OPENMETEO_EVENT on default
- both got the same structure in define_plugin_sets.h
@chromoxdor
Copy link
Contributor Author

Thanks for helping me...

@chromoxdor
Copy link
Contributor Author

I combined the two functions because there is actually no need for that

@TD-er
Copy link
Member

TD-er commented Jan 3, 2025

@tonhuisman , @TD-er Another question: Would it make sense to move these "special" parser into another file?

It would at least make the code more readable.
You can place the .h and .cpp files in the Helpers folder.
Make sure to only include those files only if the feature is enabled, so you don't link in unneeded code when the feature is not used.

@tonhuisman
Copy link
Contributor

By the way @tonhuisman I have the feeling that we already have a JSON parser in ESPEasy somewhere... Can this be?

Yes, we have, but it's not easy to use when parsing unknown data (see P037 code... 😞).

@chromoxdor
Copy link
Contributor Author

Yes, we have, but it's not easy to use when parsing unknown data (see P037 code... 😞).

I was thinking of using the Arduino code for parsing, but for now it is not worth it size-wise.

@tonhuisman
Copy link
Contributor

I was thinking of using the Arduino code for parsing, but for now it is not worth it size-wise.

We already use ArduinoJSON library (though not the latest 7.x as it got quite some increase in size), that works very well when having known input.
Maybe it works in this case, as the structure is predefined, only the keys are variable, and AFAICS, you can use element.sub.subsub (dot) notation to retrieve values from the loaded JSON. You probably need to use .as<float>() or something similar (don't have the examples at hand).

@chromoxdor
Copy link
Contributor Author

I added also the inverter event. It seems that a bunch of inverter using the same API.
I also stole the JSON code from P037. 😬
Maybe in the near future i will use this code to iterate through the open-meteo keys as well...

@chromoxdor chromoxdor changed the title [sendtohttp] Add Open-Meteo events [sendtohttp] Add Open-Meteo events + Inverter event Jan 3, 2025
@chromoxdor
Copy link
Contributor Author

I don't understand why the action runs are failing. For me these builds are working just fine fine...

@chromoxdor chromoxdor changed the title [sendtohttp] Add Open-Meteo events + Inverter event [sendtohttp] Add Open-Meteo events + JSON event Jan 4, 2025
@chromoxdor
Copy link
Contributor Author

chromoxdor commented Jan 4, 2025

OK, hopefully this time the action run finishes without errors.
I changed the "inverter" function for a more universal approach.
Therefore i added a function to parse the content of a file called json.keys line by line.

example content:

Body.Data.DAY_ENERGY.Values.1
Body.Data.PAC.Values.1

For the URL: There needs to be &json added to the end of it to tell the parser that there is a JSON-response.
SendToHTTP 192.168.1.199,80,"/solar_api/v1/GetInverterRealtimeData.cgi?Scope=System?json"

Actually i would like to see this as an plugin, where you enter your URL and store your keys in the same manner. But that not only mean to create a new plugin but also to deduplicate code because of P037.
At the moment this is too complicated as the structure is far too complex for a beginner like me.

@chromoxdor
Copy link
Contributor Author

chromoxdor commented Jan 4, 2025

For the URL: There needs to be &json added to the end of it to tell the parser that there is a JSON-response.
SendToHTTP 192.168.1.199,80,"/solar_api/v1/GetInverterRealtimeData.cgi?Scope=System?json"

Usually adding a parameter that is not existent on the server side causes no issues.
I had no choice for that one since i can not pass a parameter

- add array output to the json parser
- add grouping of keys for individual requests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants