|
Forum Index : Microcontroller and PC projects : WebMite: How to read enegyprice from a website
| Author | Message | ||||
| lizby Guru Joined: 17/05/2016 Location: United StatesPosts: 3437 |
Terrific insight and clue. With an ESP32-C3Supermini running Annex, in the "immediate" window, I did r$="http://www.sahkohinta-api.fi/api/v1/halpa?tunnit=1&tulos=haja&aikaraja=2024-11-15" a$=wget$(r$,80) wlog a$ wlog json$(a$,"hinta") And got back: HTTP/1.1 200 OK Date: Sat, 16 Nov 2024 14:17:51 GMT Server: Apache/2.4.57 (Unix) OpenSSL/3.0.2 X-Powered-By: PHP/7.4.33 Upgrade: h2 Connection: Upgrade, Keep-Alive Keep-Alive: timeout=5, max=100 Transfer-Encoding: chunked Content-Type: application/json [{"aikaleima_suomi":"2024-11-15T23:00","aikaleima_utc":"2024-11-15T21:00","hinta":"-0.08700"}] -0.08700 Does "hinta":"-0.08700" provide the information desired? (After googling, I see that "hinta" translates as "price". Is the price in Euros?) ~ Edited 2024-11-17 00:39 by lizby PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed |
||||
| JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 4098 |
Similarly, this: telnet sahkohinta-api.fi 80 GET /api/v1/halpa?tunnit=2&tulos=haja&aikaraja=2024-11-15 HTTP/1.1 Host: www.sahkohinta-api.fi Accept: */* (and an empty line) got me: bb [{"aikaleima_suomi":"2024-11-15T23:00","aikaleima_utc":"2024-11-15T21:00","hinta":"-0.08700"},{"aikaleima_suomi":"2024-11-15T22:00","aikaleima_utc":"2024-11-15T20:00","hinta":"-0.02500"}] 0 The bb is probably a hex char count John |
||||
| Pluto Guru Joined: 09/06/2017 Location: FinlandPosts: 380 |
@Iizby, The price is in eurocents/kWh. The result for that point in time is correct. The request you made actually looks up the cheapest hour on that date. r$="http://www.sahkohinta-api.fi/api/v1/halpa?tunnit=1&tulos=haja&aikaraja=2024-11-15" hints: halpa?: cheapest, tunnit=1: return 1 answer, tulos=haja: results in price order not timeorder So ESP32 with Annex would be one possible solution, if WebMite can't make it. Thanks a lot! |
||||
| Pluto Guru Joined: 09/06/2017 Location: FinlandPosts: 380 |
@John, your request asked for the two cheapest hours on 2024-11-15. See my revious post for explanation/translation of the request. Do you think that it is somehow possible to transform your telnet testing into WebMite or will the HTTPS/HTTP issue be a definite stop? Thank you so much! Fred |
||||
| JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 4098 |
All I did is http so will work on webmite. Just be sure to use both CR and LF at the end of each line and you need an extra blank line to finish the query. The site doesn't seem to like square brackets so either the API doesn't want them or something else. John |
||||
| Pluto Guru Joined: 09/06/2017 Location: FinlandPosts: 380 |
John, I have never used TELNET on WebMite. Have tried to search in the documentation, but unfortunately I did not find anything that could help me forward. I have set WebMite MMBasic Version 6.00.00RC15 OPTION WIFI HuaHuaNisse, ***********, PICOE6616408435 OPTION TCP SERVER PORT 80 OPTION TELNET CONSOLE ON but how to proceed? Fred |
||||
| JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 4098 |
No need. Just do the kind of thing you started out doing with that TCP stuff. All you need is to change what you send to be like the strings I sent on the lines following the telnet command. Your b$ needs changing a bit. Start by taking out the square brackets and add the extra lines (with CR LF between them) ( the lines being the Host and Accept). John Edited 2024-11-17 04:18 by JohnS |
||||
| Pluto Guru Joined: 09/06/2017 Location: FinlandPosts: 380 |
'Find cheapest electricity hour 'https://www.sahkohinta-api.fi/api/v1/halpa?tunnit=2&tulos=haja&aikaraja=2022-11-20 ' 'Warm Thanks to JohnS and Iizby for absolutely essential support. ' OPTION EXPLICIT OPTION DEFAULT NONE Dim buff%(4096/8) DIM a% = 1000 DIM url$="sahkohinta-api.fi" Dim s1$,s2$,s3$,s4$ 'parts of the request string s1$="GET /api/v1/halpa?tunnit=1&tulos=haja&aikaraja=2024-11-17 HTTP/1.1"+Chr$(13)+Chr$(10) s2$="Host: www.sahkohinta-api.fi"+Chr$(13)+Chr$(10) s3$="Accept: */*"+Chr$(13)+Chr$(10) s4$=""+Chr$(13)+Chr$(10) WEB open TCP CLIENT "www.sahkohinta-api.fi",80 WEB TCP CLIENT REQUEST s1$+s2$+s3$+s4$+s4$,buff%(),10000 WEB close TCP CLIENT a%=LinStr(buff%(),"{")-1: print "a%=";a% If a% <> -1 Then LongString trim buff%(),a% LongString print buff%() Print Json$(buff%(),"hinta");" cent/kWh at ";Json$(buff%(),"aikaleima_suomi") endif end RUN Connected a%= 225 {"aikaleima_suomi":"2024-11-17T02:00","aikaleima_utc":"2024-11-17T00:00","hinta":"-0.00100"}] 0 -0.00100 cent/kWh at 2024-11-17T02:00 > After some trials it works. I am so glad that it could be done with WebMite and that I can avoid the ESPs. Thanks again for your unlimited support. No way I could have solved it myself! I have learned a few new things; Now it just remains to remember. Fred |
||||
| JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 4098 |
Excellent - I'm so glad you got it working! John |
||||
| Pluto Guru Joined: 09/06/2017 Location: FinlandPosts: 380 |
The electricityprice-meter from last year has been in constant operation since Jan 2025. Placed in the kitchen it gives a good and easy overwiew of cheap and expensive hours. Better to heat the sauna when close to 0 c/kWh than at 60 c/kWh. ![]() (Don't understand why is it upside down. Tried to turn it and still... To make it a bit more complicated the tariffs are now changing each 15 minute. Got some new Pico 2W and started to work on a new meter. The problem today is that I am not able to receive the full datastring for all of the 96 15minutes data without errors. I had to divide the data into 2 12hour parts, which are received by 2 separate WEB TCP CLIENT REQUEST (which is not a problem). The stringlenght for 12h is LLEN(buff%())=4493 and 4470. I have now set the buffer to Dim buff%(4096*4/8), which should be enough. My question is: What is the max lenght of data that WEBmite's buffer can handle? At best I got about 90 of the 96 datapoints in one shot. The error when trying to get all points was some extra nonsence "eg. 1 10c" in the received textstring. That ruined the Json$ parsing of the data. option list WebMite MMBasic RP2350A Edition V6.01.00RC0 OPTION AUTORUN ON OPTION FLASH SIZE 4194304 OPTION COLOURCODE ON OPTION CPUSPEED (KHz) 150000 OPTION WIFI HuaHuaNisse, ***********, PICO8F2205DCB04 The received data looks like this: Connected LLEN(buff%(): 4493 a%= 227 buff%()= HTTP/1.1 200 OK Date: Mon, 13 Oct 2025 14:22:01 GMT Server: Apache/2.4.57 (Unix) OpenSSL/3.0.2 X-Powered-By: PHP/8.1.18 Upgrade: h2 Connection: Upgrade Transfer-Encoding: chunked Content-Type: application/json 10a4 (this is also something not belonging to the data, but it does not disturb) [{"aikaleima_suomi":"2025-10-14T12:00","aikaleima_utc":"2025-10-14T09:00","hinta":20.49},{"aikaleima_suomi":"2025-10-14T12:15","aikaleima_utc":"2025-10-14T09:15","hinta":19.709},.......etc, etc.... Pluto |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |