Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 03:53 04 Dec 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : WebMite: How to read enegyprice from a website

     Page 2 of 2    
Author Message
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3158
Posted: 02:22pm 16 Nov 2024
Copy link to clipboard 
Print this post

  JohnS said  I turned on some debugging:

wget -d -v -S 'http://www.sahkohinta-api.fi/api/v1/halpa?tunnit=1&tulos=haja&aikaraja=2024-11-15'


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 Kingdom
Posts: 3820
Posted: 04:15pm 16 Nov 2024
Copy link to clipboard 
Print this post

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: Finland
Posts: 361
Posted: 04:54pm 16 Nov 2024
Copy link to clipboard 
Print this post

@Iizby,
  Quote  Does "hinta":"-0.08700" provide the information desired?

(After googling, I see that "hinta" translates as "price". Is the price in Euros?)

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: Finland
Posts: 361
Posted: 05:02pm 16 Nov 2024
Copy link to clipboard 
Print this post

@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 Kingdom
Posts: 3820
Posted: 05:08pm 16 Nov 2024
Copy link to clipboard 
Print this post

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: Finland
Posts: 361
Posted: 05:31pm 16 Nov 2024
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 3820
Posted: 06:15pm 16 Nov 2024
Copy link to clipboard 
Print this post

  Pluto said  John,
I have never used TELNET on WebMite.

Fred

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: Finland
Posts: 361
Posted: 07:54pm 16 Nov 2024
Copy link to clipboard 
Print this post

   
'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 Kingdom
Posts: 3820
Posted: 02:09pm 17 Nov 2024
Copy link to clipboard 
Print this post

Excellent - I'm so glad you got it working!

John
 
     Page 2 of 2    
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024