@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)