Raspberry Pico W - Can't make a POST Request

June 22, 2024, 23:32

bourdain

Hey, I'm trying to code my Pico to make a POST with a server with an API I created, however, I can't seem to get it to send a post, here's what I have
import network
import socket
from time import sleep
import machine
import urequests as requests  # Use urequests as an alternative to requests in MicroPython
import usocket as socket
import ssl

# Wi-Fi credentials
ssid = 'WifiName'
password = 'abc123abc123'

# Humidity sensor configuration
sensor_pin = machine.ADC(26)
dry_value = 65535  # Adjust this based on completely dry sensor reading
wet_value = 0  # Adjust this based on completely wet sensor reading

# Function to connect to Wi-Fi
def connect_wifi(ssid, password):
    print("Connecting to Wi-Fi...")
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.connect(ssid, password)
    while not wlan.isconnected():
        print("Waiting for connection...")
        sleep(1)
    print('Connected to Wi-Fi:', wlan.ifconfig())
    return wlan

# Function to send HTTP POST request
def send_humidity(humidity):
    print(f"Sending humidity data: {humidity}%")
    url = f'https://ThisIsNotARealURL.zapto.org/api/humidity?humidity=0.0'
    try:
        print(f"Attempting to connect to {url}...")
        response = requests.post(url)
        print('POST response status code:', response.status_code)
        print('Response text:', response.text)
        response.close()
    except Exception as e:
        print("Failed to send HTTP POST request:", e)

# Main loop
while True:
    print("Reading sensor value...")
    sensor_value = sensor_pin.read_u16()
    print(f"Raw sensor value: {sensor_value}")

    dryness_percent = 100 - ((sensor_value - wet_value) / (dry_value - wet_value)) * 100
    print(f"Calculated dryness percent: {dryness_percent}")

    # Connect to Wi-Fi
    wlan = connect_wifi(ssid, password)
    
    # Send HTTP POST request
    send_humidity(dryness_percent)
    
    # Disconnect from Wi-Fi
    print("Disconnecting from Wi-Fi...")
    wlan.disconnect()
    wlan.active(False)
    print("Disconnected from Wi-Fi.")
    
    # Deep sleep for 1 minute (60000 milliseconds)
    print("Entering deep sleep for 1 minute...")
    
Reading sensor value... Raw sensor value: 65535 Calculated dryness percent: 0.0 Connecting to Wi-Fi... Connected to Wi-Fi: ('192.168.1.174', '255.255.255.0', '192.168.1.1', '192.168.1.1') Sending humidity data: 0.0% Attempting to connect to https://ThisIsNotARealURL.zapto.org/api/humidity?humidity=0.0... Failed to send HTTP POST request: no module named 'ussl' Disconnecting from Wi-Fi... Disconnected from Wi-Fi. Entering deep sleep for 1 minute... I've been going all around trying to figure this out, these are the packages I have on my build of MicroPython v1.23.0 on 2024-06-02; Raspberry Pi Pico W with RP2040 using
help('modules')
__main__          asyncio/__init__  hashlib           rp2
_asyncio          asyncio/core      heapq             select
_boot             asyncio/event     io                socket
_boot_fat         asyncio/funcs     json              ssl
_onewire          asyncio/lock      lwip              struct
_rp2              asyncio/stream    machine           sys
_thread           binascii          math              time
_webrepl          bluetooth         micropython       tls
aioble/__init__   builtins          mip/__init__      uasyncio
aioble/central    cmath             neopixel          uctypes
aioble/client     collections       network           urequests
aioble/core       cryptolib         ntptime           vfs
aioble/device     deflate           onewire           webrepl
aioble/l2cap      dht               os                webrepl_setup
aioble/peripheral ds18x20           platform          websocket
aioble/security   errno             random
aioble/server     framebuf          re
array             gc                requests/__init__
Why doesn't my build have USSL 😦 It's supposed to be the latest one, can anyone help me figure this out

bourdain

I tried installing nightlybuilds of MIcropython but didn't have it, tried installing an odler version and it also didn't have it I'm a bit at a loss here

bourdain

This was fixed by downgrading the Firmware to 1.20 from 1.23