|
Forum Index : Microcontroller and PC projects : PicomiteWeb problem with TCP CLIENT REQUEST
| Author | Message | ||||
| LucV Regular Member Joined: 19/02/2023 Location: NetherlandsPosts: 62 |
I hate to admit it but I have a problem with web requests. I genuinely think it is my fault but I just can not find the fault. I have a test account at Thingspeak and there is a value on a field. You can get that with the next api call: http://api.thingspeak.com/channels/1841798/fields/1.json?results=2 If you paste that in your browsers URL field you will get the data containing fields with test temperatures. So I tried to convert that to PicomiteWeb and I keep getting ar error. Can anyone point me in the right direction ?? Dim buff%(512) WEB ntp Dim report$="/channels/1841798/fields/1.json?results=2" Dim b$= "GET"+report$+Chr$(10)+Chr$(13) WEB open tcp client "api.thingspeak.com",80 WEB TCP CLIENT REQUEST b$,buff%(),10000 WEB close tcp client Print Json$(buff%(),channel()) Print "Weather for " + Json$(buff%(),"name") Print "Temperature is ",Val(Json$(buff%(),"field1")) Print "Pressure is ",Json$(buff%(),"main.pressure") What i did try is to leave the "GET"+ out and that did not work. I also tried to put the / at the end of "api.thingspeak.com" to no avail. BTW I do have a connection as the time is fetched from the NTP server and then ntp address 94.228.220.14 got ntp response: 19/02/2023 14:41:06/ tcp address 52.204.34.129r Connected [9] Print Json$(buff%(),channel{})1 Error : Invalid JSON data This is what I get. Any thoughts ??? EDIT: I am on PicoMiteWEB > print (MM.Ver) 5.070718 Luc Edited 2023-02-20 00:47 by LucV Luc's tech Blog |
||||
| matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 10633 |
You get a space after the GET. That then gets the data successfully. The json decoding isn't right but I'll leave that for you Dim buff%(512) WEB ntp Dim report$="/channels/1841798/fields/1.json?results=2" Dim b$= "GET "+report$+Chr$(10)+Chr$(13) WEB open tcp client "api.thingspeak.com",80 Print b$ WEB TCP CLIENT REQUEST b$,buff%(),10000 WEB close tcp client LongString print buff%() ' good so far Print Json$(buff%(),channel()) Print "Weather for " + Json$(buff%(),"name") Print "Temperature is ",Val(Json$(buff%(),"field1")) Print "Pressure is ",Json$(buff%(),"main.pressure") |
||||
| LucV Regular Member Joined: 19/02/2023 Location: NetherlandsPosts: 62 |
Like I said: I genuinely think it is my fault !!!! Missing a stupid space...... Thank you very much !!!! Works as a charm. For anyone interested I use JSONPATHFINDER for decoding JSON strings: https://jsonpathfinder.com/ Thanks again. Luc Edited 2023-02-20 02:20 by LucV Luc's tech Blog |
||||
| LucV Regular Member Joined: 19/02/2023 Location: NetherlandsPosts: 62 |
Sorry to be a PITA but I still run into some trouble with other sites. Yes: Thingspeak works like a charm and so does Dweet (for those that know the service). If anyone is interesetd I will post how to send data to Dweet and how to retrieve it. Dweet is a free service. However. I presume HTTP and no HTTPS .......... That locks out a lot of api's like Telegram I also presume GET and no Post ........ That also locks out some api's like Pushbullet etc. Any idea why the joke API does not work. It is a stupid service but it gives me a hint on how to adress api's. The general api is: http://official-joke-api.appspot.com/random_joke Put this in your webbrowser URL and you will get a random joke. However: Dim buff%(512) WEB ntp Dim report$="/random_joke" Dim b$= "GET "+report$+Chr$(10)+Chr$(13) WEB open tcp client "official-joke-api.appspot.com",80 WEB TCP CLIENT REQUEST b$,buff%(),10000 WEB close tcp client LongString print buff%() Gives an error. HTTP/1.0 404 Not Found And I have a home automation system: Domoticz Dim buff%(512) WEB ntp Dim report$="/json.htm?type=devices&rid=2662" Dim b$= "GET "+ report$+Chr$(10)+Chr$(13) WEB open tcp client "192.168.1.66",8080 WEB TCP CLIENT REQUEST b$,buff%(),10000 WEB close tcp client LongString print buff%() Here also an error rises. HTTP/1.1 400 Bad Request Would that be because the portnumber is 8080. I know this is difficult if you do not own a Domoticz system yourself...... Typing the URL in the browser gives me a JSON feedback with all data about my local thermometer. I know, I know a lot of questions. I hope someone can give a hand. Luc Luc's tech Blog |
||||
| JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 4147 |
Thanks for contributing. Give Peter time, he's getting there :) Oh, same comment. Yes but your sample effectively connects first to just http://official-joke-api.appspot.com and that site does something reasonable but in this case unwanted (try it in a browser). John |
||||
| LucV Regular Member Joined: 19/02/2023 Location: NetherlandsPosts: 62 |
John, I made a mistake in my program by leaving out a space. I would not be surprised if I made more mistakes. Therefore I was not sure if HTTPS or POST were already implemented. Peter did not mention this in his posts about the new Picomite web version. It was meant as a question but it sounded pushy. I apologise for that. You are right about the Joke site. It does something strange and then shows the JSON data. I did not see that. I am coming a long way and have been programming in C++ and MicroPython on several microcontrollers (ESP8266, ESP32 and Pico). On MicroPyrhon the Joke API did not pose a problem. So I thought it was a problem on my side not understanding the syntax. Thanks. Luc Luc's tech Blog |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |