@kuriko any pointers on how to do that? There's nothing about it in the docs:
https://uiflow-micropython.readthedocs.io/en/develop/software/requests2.html
Posts made by streammyevent
-
RE: HTTP requests2: how to deal with server that keeps the connection alive?
-
HTTP requests2: how to deal with server that keeps the connection alive?
I'm trying to extract current power usage information from a P1 device with an API, but the server is poorly written and doesn't respond to the "Connection: close" header, keeping the connection alive no matter what:
* Trying 192.168.10.22:80... * Connected to 192.168.10.22 (192.168.10.22) port 80 > GET /api/v1/data HTTP/1.1 > Host: 192.168.10.22 > User-Agent: curl/8.4.0 > Accept: */* > Connection: close > Content-Type: application/json > < HTTP/1.1 200 OK < Content-Type: application/json < Content-Length: 1178 < Access-Control-Allow-Origin: * < * Connection #0 to host 192.168.10.22 left intact {"wifi_ssid":"(redacted)","wifi_strength":62,"smr_version":50,"meter_model":"ISKRA 2M550T-1011","unique_id":"(redacted)","active_tariff":2,"total_power_import_kwh":118615.868,"total_power_import_t1_kwh":31059.650,"total_power_import_t2_kwh":87556.218,"total_power_export_kwh":0.016,"total_power_export_t1_kwh":0.002,"total_power_export_t2_kwh":0.014,"active_power_w":5921.000,"active_power_l1_w":1424.000,"active_power_l2_w":1449.000,"active_power_l3_w":3074.000,"active_voltage_l1_v":226.400,"active_voltage_l2_v":225.100,"active_voltage_l3_v":226.700,"active_current_a":26.287,"active_current_l1_a":6.290,"active_current_l2_a":6.437,"active_current_l3_a":13.560,"voltage_sag_l1_count":7.000,"voltage_sag_l2_count":6.000,"voltage_sag_l3_count":7.000,"voltage_swell_l1_count":1.000,"voltage_swell_l2_count":1.000,"voltage_swell_l3_count":1.000,"any_power_fail_count":6.000,"long_power_fail_count":11.000,"total_gas_m3":13682.241,"gas_timestamp":241111145004,"gas_unique_id":"(redacted)","external":[{"unique_id":"(redacted)","type":"gas_meter","timestamp":241111145004,"value":13682.241,"unit":"m3"}]}%
This causes request2 to freeze when trying to get the response contents, like when calling 'print(http_req.text)'. Performing a GET request on any other server works fine, so it's pretty clear to me that this is the issue.
If I add a timeout to the request, like so:
http_req = requests2.get('http://192.168.10.22/api/v1/data', headers={'Content-Type': 'application/json', 'Connection': "close"}, timeout=2)
Then that just triggers an error and crashes the application.
Many servers keep the connection alive by default, leading to this issue. Is there a way to work around it?
-
RE: CoreS3 & HTTP Request block: urequests import error
This results in:
Traceback (most recent call last): File "<stdin>", line 5, in <module> ImportError: no module named 'urequests' >>>
-
RE: UIFlow 2.0.1 HTTP GET and parse JSON
Yes, the endpoint responds almost instantly. Also the status code immediately comes back as as '200'.
-
UIFlow 2.0.1 HTTP GET and parse JSON
Hi team,
I'm trying to complete the simplest of all possible projects, and for the life of me after two days of troubleshooting I simply can't figure it out.
Device: Core2
OS: Mac OS 14.2.1
Software: UIFlow v2.0.1I'm trying to HTTP GET a local JSON endpoint with some data.
I started off trying to program this with the visual interface, but found that I quickly had to switch to python because of the missing 'urequests' lib (there's another thread about that on this forum).
Using the 'requests' library, I found that I can perform the GET request and actually get a 200 status OK back. However, when using the response.json() call, the program hangs with no error and no output.
If I run the same requests in python3 on my desktop environment, it all works.
Here's where I got to:
import os, sys, io import M5 from M5 import * import network import requests title0 = None statTitle = None resTitle = None stat = None res = None # http_req = None wlan = None status = None def setup(): global title0, statTitle, resTitle, stat, res, http_req, wlan, status M5.begin() Widgets.fillScreen(0x222222) title0 = Widgets.Title("Powermonitor", 3, 0xffffff, 0x0000FF, Widgets.FONTS.DejaVu18) statTitle = Widgets.Label("Status:", 2, 28, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu18) resTitle = Widgets.Label("Response:", 2, 57, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu18) stat = Widgets.Label("...", 110, 29, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu18) res = Widgets.Label("...", 111, 57, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu18) stat.setText(str('Fetching')) http_req = requests.get('http://192.168.10.22/api/v1/data') status = http_req.status_code stat.setText(str(status)) print(str(http_req.status_code)) print(str(http_req.reason)) print(str(http_req.encoding)) # print(str(http_req.raw)) # print(str(http_req.text())) data = http_req.json() print(str(data)) # print(str(http_req.json())) # http_req.close() # print(dir(responseJson)) # print(responseJson()) # res.setText(str(http_req.json())) def loop(): global title0, statTitle, resTitle, stat, res, http_req, wlan, status M5.update() if __name__ == '__main__': try: setup() while True: loop() except (Exception, KeyboardInterrupt) as e: try: from utility import print_error_msg print_error_msg(e) except ImportError: print("please update to latest firmware")```
-
RE: CoreS3 & HTTP Request block: urequests import error
Same issue here. Also v2.0.1 on a Core2. Putting the WLAN block after the HTTP request also does not help.