#Post json data error
1 messages · Page 1 of 1 (latest)
code to get the status
import ssl
import wifi
import socketpool
import adafruit_requests
import microcontroller
import time
# connect to your SSID
print("Try to connect to SSID:", os.getenv('WIFI_SSID'))
wifi.radio.connect(os.getenv('WIFI_SSID'), os.getenv('WIFI_PWD'))
print("My IP address is", wifi.radio.ipv4_address)
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
url = "http://192.168.8.158:8000/status"
while True:
try:
response = requests.get(url)
response_as_json = response.json()
print()
print(response_as_json)
print()
time.sleep(10)
except Exception as e:
print("Error:\n", str(e))
print("Resetting microcontroller in 10 seconds")
time.sleep(10)
microcontroller.reset()
code to post my data
import ssl
import wifi
import socketpool
import adafruit_requests
import microcontroller
import time
# connect to your SSID
print("Try to connect to SSID:", os.getenv('WIFI_SSID'))
wifi.radio.connect(os.getenv('WIFI_SSID'), os.getenv('WIFI_PWD'))
print("My IP address is", wifi.radio.ipv4_address)
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
url = "http://192.168.8.158:8000/sensor_values"
fake_data = { "device": "chest", "temperature": 22, "humidity": 60, "pressure": 1100, "gas": 1340 }
response = requests.post(url, data=fake_data)
print()
print(response.status_code)
print()
and this fails with:
Try to connect to SSID: GLTC2.4
My IP address is 192.168.8.123
Traceback (most recent call last):
File "adafruit_requests.py", line 534, in _get_socket
OSError: [Errno 104] ECONNRESET
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "code.py", line 19, in <module>
File "adafruit_requests.py", line 732, in post
File "adafruit_requests.py", line 717, in request
File "adafruit_requests.py", line 668, in request
File "adafruit_requests.py", line 515, in _get_socket
RuntimeError: Sending request failed```
I made some progress.
my fastapi was answering "307 Temporary Redirect" because the POST url did not ended with a "/" character.
Now I have 422 Unprocessable Entity so I guess I need to figure out how to properly send json data
🎉 it worked.
Here is the code to send json data
import ssl
import wifi
import socketpool
import adafruit_requests
import microcontroller
import time
import json
# connect to your SSID
print("Try to connect to SSID:", os.getenv('WIFI_SSID'))
wifi.radio.connect(os.getenv('WIFI_SSID'), os.getenv('WIFI_PWD'))
print("My IP address is", wifi.radio.ipv4_address)
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
url = "http://192.168.8.158:8000/sensor_values/"
fake_data = json.dumps({ "device": "chest", "temperature": 22, "humidity": 60, "pressure": 1100, "gas": 1340 })
headers = { 'accept': 'application/json' }
response = requests.post(url, data=fake_data, headers=headers)
print()
print(response.status_code)
print()
All this is part of a conference I'll give next week at Devoxx UK and I'll post the repository with all the code right after
So, your issue is completely resolved?