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.
lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1702
Posted: 04:26pm 07 Jun 2018
Copy link to clipboard
Print this post
Thanks Peter No it didn't work even using port 443, it's no big deal though because I can always set up a https server on the pi and then call it from within MM
I've been trying to see if I can get Wunderground Api data but for some reason I'm getting
Error: inet_qton() failed at line 19 TCP client a$,80
I've commented all the code out other than TCP OPEN and I'm using 2018-06-05_020428_mmbasicstretch.zip
I've even restarted the Pi, deleted the .option and mmbasic code and reloaded it in case it was corrupted
[code]Option explicit Option default none Dim a$,b$,c$ Dim integer i,j,rlen Dim m%(4000) Dim MYID$="EGCC.json" 'Manchester Airport 'Dim APIKEY$="XXXXXXXXX" 'Dim report$="weather" 'a$=GetIP$("api.wunderground.com/") TCP client a$,80 'b$= "GET /data/2.5/"+report$+"?id="+MYID$+"&APPID="+APIKEY$ 'b$= "GET 23.42.155.25/api/+APIKEY$/geolookup/conditions/q/+MYID$.json 'b$=b$+" HTTP/1.0"+Chr$(13)+Chr$(10)+Chr$(13)+Chr$(10) 'TCP send b$ 'LongString clear m%() 'Do ' TCP receive c$ ' If c$<>"" Then LongString append m%(),c$ 'Loop While c$<>"" 'LongString trim m%(),LInStr(m%(),"{")-1 'rlen=LLen(m%()) 'i=1 'Open report$+".json" For output As #1 'Do While rlen>0 ' If rlen>240 Then ' j=240 ' Else ' j=rlen ' EndIf ' rlen=rlen-j ' Print #1,LGetStr$(m%(),i,j); ' Print LGetStr$(m%(),i,j); ' i=i+j 'Loop 'Print "" 'TCP close 'Close #1 'Print "Weather for "+json$(m%(),"name") 'Print "Temperature is ",val(json$(m%(),"main.temp"))-273 'Print "Pressure is ",json$(m%(),"main.pressure") 'Print json$(m%(),"weather[0].description") [/code]Edited by lew247 2018-06-09
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 10570
Posted: 04:34pm 07 Jun 2018
Copy link to clipboard
Print this post
You have commented out the code that sets a$ for the call to TCP client
lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1702
Posted: 07:10pm 07 Jun 2018
Copy link to clipboard
Print this post
It now throws up the error Invalid URL [quote]LongString trim m%(),LInStr(m%(),"(")-1 Error: -1 is invalid (valid is 1 to 418)[quote]
I know I changed a { to ( in that line but it gave the same error message
IF I change that line to LongString trim m%(),LInStr(m%(),"1") then the program will run but it then gives this error
[code]Option explicit Option default none Dim a$,b$,c$ Dim integer i,j,rlen Dim m%(4000) Dim MYID$="EGCC.json" 'Manchester Airport Dim APIKEY$="XXXXXXXXX" Dim report$="weather" a$=GetIP$("wunderground.com") TCP client a$,80 'b$= "GET /data/2.5/"+report$+"?id="+MYID$+"&APPID="+APIKEY$ b$= "GET /api/+APIKEY$/geolookup/conditions/q/+MYID$.json b$=b$+" HTTP/1.0"+Chr$(13)+Chr$(10)+Chr$(13)+Chr$(10) TCP send b$ LongString clear m%() Do TCP receive c$ If c$<>"" Then LongString append m%(),c$ Loop While c$<>"" LongString trim m%(),LInStr(m%(),"(")-1 rlen=LLen(m%()) i=1 Open report$+".json" For output As #1 Do While rlen>0 If rlen>240 Then j=240 Else j=rlen EndIf rlen=rlen-j ' Print #1,LGetStr$(m%(),i,j); Print LGetStr$(m%(),i,j); i=i+j Loop Print "" TCP close Close #1 'Print "Weather for "+json$(m%(),"name") 'Print "Temperature is ",val(json$(m%(),"main.temp"))-273 'Print "Pressure is ",json$(m%(),"main.pressure") 'Print json$(m%(),"weather[0].description") [/code]
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 10570
Posted: 08:03pm 07 Jun 2018
Copy link to clipboard
Print this post
The program is working perfectly. You are talking to the website and getting an answer back. However, the request you are sending is incorrect and the website is telling you so.
Print B$ before the "TCP send" command and then you can see the string you are creating and correct it for whatever wunderground requires
JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 4126
Posted: 09:37pm 07 Jun 2018
Copy link to clipboard
Print this post
This isn't correct b$= "GET /api/+APIKEY$/geolookup/conditions/q/+MYID$.json
Does it fail to report an error?
John
lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1702
Posted: 06:19am 08 Jun 2018
Copy link to clipboard
Print this post
No it doesn't report an error
IF I use a web browser and type api.wunderground.com/api/APIKEY$/geolookup/conditions/q/EGCC.json
Then this works and produces the report.
I know there was an error in the code I posted Dim MYID$="EGCC.json" it should be Dim MYID$="EGCC" and +.JSON added to the end of B$
No matter how I word B$ I get the same thing - "bad request" Yet it works on the web page
I've even tried using the IP address in B$ but it still says .0 Bad Request
Peter I'm guessing the .0 Bad Request means the .0 at the end of " HTTP/1.0"+Chr$(13)+Chr$(10)+Chr$(13)+Chr$(10) ? I've tried putting " HTTP/1" and " HTTP/1.1" but it still gives the same error
JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 4126
Posted: 09:57am 08 Jun 2018
Copy link to clipboard
Print this post
The part I posted is not valid Basic because you've missed out things like quotes, string + and so on.
It doesn't even have a matching quote for the one near the start!
I don't know what you really meant but it's something more like b$= "GET /api/"+APIKEY$+"/geolookup/conditions/q/"+MYID$+".json"
Are you SURE what you posted does not give an MMBasic error?
If you are then please report it to Geoff as a bug because it's not valid due to it only having one quote (") symbol. It should be refused by MMBasic when entered. (Regardless of it being garbage in terms of what you meant.)
JohnEdited by JohnS 2018-06-09
lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1702
Posted: 10:04am 08 Jun 2018
Copy link to clipboard
Print this post
It's not valid basic it's code for the Raspberry Pi
JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 4126
Posted: 10:54am 08 Jun 2018
Copy link to clipboard
Print this post
It's from your program. You're using MMBasic and the syntax is wrong as I've said.
It won't work in MMBasic (on any platform) because of that.
John
lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1702
Posted: 09:18pm 08 Jun 2018
Copy link to clipboard
Print this post
I've found the problem but don't know how to fix it
I'm using 2018-06-08_190744_mmbasicstretch.zip
When I send the Get request it's sending the wrong data
In the attached picture you can see the GET request - the IP address is correct, the data is correct as I've tried it direct in a browser
In the bottom half in the browser section it says the requested URL is different to what was actually sent
This is the actual code - API key blanked out
[code]Option explicit Option default none Dim a$,b$,c$ Dim integer i,j,rlen Dim m%(9000) Dim MYID$="EGCC" 'Manchester Airport Dim APIKEY$="XXXXXXXXX" Dim report$="weather" a$=GetIP$("api.wunderground.com") TCP client a$,80 PRINT a$ 'b$= "GET "+a$+"/data/2.5/"+report$+"?id="+MYID$+"&APPID="+APIKEY$ b$= "GET "+a$+"/api/+APIKEY$+/geolookup/conditions/q/EGCC.json" b$=b$+" HTTP/1.0"+Chr$(13)+Chr$(10)+Chr$(13)+Chr$(10) PRINT b$ TCP send b$ LongString clear m%() Do TCP receive c$ If c$<>"" Then LongString append m%(),c$ Loop While c$<>"" LongString trim m%(),LInStr(m%(),"1") rlen=LLen(m%()) i=1 Open report$+".json" For output As #1 Do While rlen>0 If rlen>240 Then j=240 Else j=rlen EndIf rlen=rlen-j ' Print #1,LGetStr$(m%(),i,j); Print LGetStr$(m%(),i,j); i=i+j Loop Print "" TCP close Close #1[/code]
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 10570
Posted: 09:37pm 08 Jun 2018
Copy link to clipboard
Print this post
The wunderground docs say the request should be of the form
lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1702
Posted: 07:18am 09 Jun 2018
Copy link to clipboard
Print this post
That's what mine is, but instead of using country and city its the Airport code I know there's a weather station at the airport so it's ideal
it works perfectly pasting the code into a web browser
[quote]The first thing to know about the API is how to specify a location. You can use a latitude and longitude; or you can search by city, ZIP code (for U.S. cities only) or airport code. You can also specify the ID of a Weather Underground weather station if you know it.[/quote]
JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 4126
Posted: 06:48am 10 Jun 2018
Copy link to clipboard
Print this post
One or more of these may matter: 1. you left out http:// (a browser may silently add that) 2. you have an extra /geolookup