Pico Wireless Pack - HTTP POST

Hi,

I’m trying to get my “Pico Wireless Pack” to send the data to my instance of Thingsboard database. I went through the examples and managed to connect to my Wi-Fi. Current set of MicroPython examples are useful for learning but somehow less relevant to, I guess, most of applications where Pico can be used as IoT node to capture the data from the sensors and send it to the Internet based database.

I have currently working set up with Pi Zero when I push my data with the following code:

data['temp'] = avg_T
data['pres'] = avg_P
data['humi'] = avg_H

r = requests.post(thingsboard_url, data=json.dumps(data))

I am certainly not an expert in network programming and I am very new to MicroPython. So, after two days of trying to implement above with picowireless module I am close to giving up :-)

There few examples around that use urequests and usocket modules but those are not available in pimoroni-pico-v1.18.6-micropython.uf2 implementation I currently use.

Can somebody point me into the right direction and advice how to implement above on Pico and POST the data over secured with SSL http protocol?

I got similarly blocked on usocket support when trying to work out how to get MQTT messaging working with a Wireless Pack. I’m afraid I’ve nothing much helpful to contribute other than ‘I share your pain.’

This does seem to be a significant limitation of the Wireless Pack at present.

@jjsanderson thank you for your response. I had a close look at this and found the solution at least to my problem. I suspect this can also help to solve your problem but I am not familiar with MQTT inner workings.

Basically, I used CircuitPython with relevant libraries. I am new to Pico and I am still confused why Pimoroni MicroPython is cut down in this respect. Their drivers are fantastic but there must be a good reason why the socket module is not included.

For everybody’s benefit I included below my code. In essence, I measure T, P, RH and calculate IAQ ratio. All then is sent to my external ThingsBoard server via HTTPS that sits on Linode.

The h/w list is as follows:

import time
import board
import busio
import json
from digitalio import DigitalInOut
import adafruit_requests as requests
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp33spi import adafruit_esp32spi
from bme680IAQ import *
import adafruit_bme680
from adafruit_bus_device.i2c_device import I2CDevice

# Get wifi details and more from a secrets.py file
try:
    from esp32spi_secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise

DEVICE_ADDRESS   = 0x76  # BME688 I2C address
API_KEY          = "put_your_api_key_here"
THINGSBOARD_HOST = "put_your_server_address_here"
THINGSBOARD_URL  = "https://{0}/api/v1/{1}/telemetry".format(THINGSBOARD_HOST, API_KEY)

# Setting up BME688 sensor
comm_port = busio.I2C(board.GP5, board.GP4)
sensor = adafruit_bme680.Adafruit_BME680_I2C(comm_port, DEVICE_ADDRESS)

# Setting up WiFi module
esp32_cs = DigitalInOut(board.GP7)
esp32_ready = DigitalInOut(board.GP10)
esp32_reset = DigitalInOut(board.GP11)
spi = busio.SPI(board.GP18, board.GP19, board.GP16)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)

# Set client socket
requests.set_socket(socket, esp)

# Check WiFi module
if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
    print("ESP32 found and in idle mode")
print("Firmware vers.", str(esp.firmware_version, "utf-8"))

# Connecto to WiFi
print("Connecting to {}...".format(secrets["ssid"]))
while not esp.is_connected:
    try:
        esp.connect_AP(secrets["ssid"], secrets["password"])
    except RuntimeError as e:
        print("{0}, retrying..".format(e.args[0]))
        continue
print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
print("My IP address is", esp.pretty_ip(esp.ip_address))

data = {}
trigger = 0
iaq_tracker = IAQTracker()
response = None
attempts = 3

while True:
    if (trigger < 600):
        data['temp'] = sensor.temperature
        data['pres'] = sensor.pressure
        data['humi'] = sensor.humidity
        data['iaq'] = iaq_tracker.getIAQ(sensor)
        
        print("Dataset {}: {:0.2f}C, {:0.2f}Pa, {:0.2f}%, {} IAQ \r".format(
            trigger+1, data['temp'], data['pres'], data['humi'], data['iaq']), end='')
        trigger += 1
    else:
        while not response:
            try:
                response = requests.post(THINGSBOARD_URL, data=json.dumps(data))
                failure_count = 0
            except AssertionError as error:
                print("\nRequest failed, retrying...\n", error)
                failure_count += 1
                if failure_count >= attempts:
                    raise AssertionError("Failed to resolve hostname, please check your router's DNS configuration.") from error
                continue
        
        if response.status_code == 200:
            print("\nDataset sent to Thingsboard server!")
        response.close()
        
        response = None    
        trigger = 0

    time.sleep(1.0)

Thanks @Chmielar, that does look useful. I’m a bit baffled by the CircuitPython/Micropython split, and it does seem like more stuff in general works in the CP world. Meh.

I might see if I can get what I need working over on that side of the great divide. Good thought.