With either the Arduino or the CircuitPython code running on the Trinkey QT2040, the data is being sent out via a USB CDC serial port. This data can be received by anything that can open and talk to the serial port. Here we provide an example using Python and pySerial.

Install pySerial

The pySerial module is used to open and read from the serial port in Python. If this module is not already installed on your setup, go here:

Which Serial Port?

The Arduino example sends out data using the typical Serial.print() command. This serial port should show up in the place you'd look for the Arduino Serial Monitor.

The CircuitPython example uses a secondary serial port and data is sent using usb_cdc.data.write(). This is different than the serial port where print() output shows up. There should be two serial ports that show up when running the CircuitPython example. The data is most likely on the second one.

The serial port location will likely be different between the Arduino and CircuitPython code.
Be sure to change receiving code as needed to point to proper serial port location.

Read CSV String

Here is an example of how to open the serial port and read the incoming CSV data:

# SPDX-FileCopyrightText: 2021 Carter Nelson for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import serial

# open serial port (NOTE: change location as needed)
ss = serial.Serial("/dev/ttyACM0")

# read string
_ = ss.readline() # first read may be incomplete, just toss it
raw_string = ss.readline().strip().decode()

# create list of floats
data = [float(x) for x in raw_string.split(',')]

# print them
print("CO2 =", data[0])
print("pressure =", data[1])
print("temperature =", data[2])
print("humidity =", data[3])

Read JSON String

Here is an example of how to open the serial port and read the incoming JSON data:

# SPDX-FileCopyrightText: 2021 Carter Nelson for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import json
import serial

# open serial port (NOTE: change location as needed)
ss = serial.Serial("/dev/ttyACM0")

# read string
_ = ss.readline() # first read may be incomplete, just toss it
raw_string = ss.readline().strip().decode()

# load JSON
data = json.loads(raw_string)

# print data
print("CO2 =", data['CO2'])
print("pressure =", data['pressure'])
print("temperature =", data['temperature'])
print("humidity =", data['humidity'])

This guide was first published on Dec 22, 2021. It was last updated on Apr 17, 2024.

This page (Receiving Data) was last updated on Apr 17, 2024.

Text editor powered by tinymce.