Trinkey_QT2040_Enviro_Gadget 2

Adafruit's tiny microcontroller platform. Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
dpuerto
 
Posts: 40
Joined: Wed Feb 01, 2023 12:25 am

Re: Trinkey_QT2040_Enviro_Gadget 2

Post by dpuerto »

Ok, maybe it's just running then?

I can't seem to get it to show the values in Mu editor or in Mac OSX screen...

But I put together a python file on my local and I am trying to connect to the trinkey and print some json using the example and ran it but returned another error that that port is busy:

Code: Select all

# 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/tty.usbmodem14601")

# 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'])
➜ sensors git:(master) ✗ python3 trinkey.py
Traceback (most recent call last):
File "/Library/Python/3.8/site-packages/serial/serialposix.py", line 322, in open
self.fd = os.open(self.portstr, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
OSError: [Errno 16] Resource busy: '/dev/tty.usbmodem14601'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "trinkey.py", line 9, in <module>
ss = serial.Serial("/dev/tty.usbmodem14601")
File "/Library/Python/3.8/site-packages/serial/serialutil.py", line 244, in __init__
self.open()
File "/Library/Python/3.8/site-packages/serial/serialposix.py", line 325, in open
raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg))
serial.serialutil.SerialException: [Errno 16] could not open port /dev/tty.usbmodem14601: [Errno 16] Resource busy: '/dev/tty.usbmodem14601'


Really not sure what I am doing wrong but I think that the Trinkey firmware never properly loaded. I did the flash_nuke.uf2,

Afterwork I'll try again. I'll flash nuke it again and try to insert the trinkey with the Boot being pressed and then apply the u2if_trinkey_qt2040_rp2040.uf2 because ultimately I want to allow direct reading with u2if...

I've been googling a bit trying to find more information how to free up that port resource or kill that process via command line but coming up with nothing ... :-\
Last edited by dastels on Wed May 17, 2023 7:06 pm, edited 1 time in total.
Reason: Use the code tag when embedding code

User avatar
dpuerto
 
Posts: 40
Joined: Wed Feb 01, 2023 12:25 am

Re: Trinkey_QT2040_Enviro_Gadget 2

Post by dpuerto »

I must not know how to properly load these python packages. I am trying to simply import board and it does not import. I believe that I have adafruit-blinka library installed correctly, per documentation and because it repeatedly returns that the resource is satisfied. I reset and renuked after restarting in boot sel mode and then added the latest circuitPython from https://circuitpython.org/board/adafrui ... 0_trinkey/. Then I attempted the test again to simply import board. It does not. I am trying to reconcile while adafruit-blinka is not instantiating

User avatar
dastels
 
Posts: 15817
Joined: Tue Oct 20, 2015 3:22 pm

Re: Trinkey_QT2040_Enviro_Gadget 2

Post by dastels »

Where do you want the code to run? The Trinkey or your Mac? Blinka is (last I checked) for using CircuitPython code/libraries on the Raspberry Pi and similar boards.

The errors about the port may be due to having a REPL/terminal connection to the port already open. Or you might have the wrong port; the code is trying to open /dev/tty.usbmodem14601.

There seems to be confusion as to what code is running (or should run) where.

Dave

User avatar
dpuerto
 
Posts: 40
Joined: Wed Feb 01, 2023 12:25 am

Re: Trinkey_QT2040_Enviro_Gadget 2

Post by dpuerto »

Further complicating things, this talks about any computer running Pico / RP2040 boards...

https://circuitpython.org/blinka/adafru ... 0_trinkey/

then routes me to: https://github.com/execuc/u2if

I want the code to run on the trinkey FIRST and foremost to test that I am reading values from the SCD41, and I want to ultimately listen to the usb port that the trinkey is plugged into with an external application (on my Mac) so that I can take those JSON values and send them up to the cloud and then fetch and render those values from my persistence layer on my website.

I think Busio is the problem. I am cherry picking files and I cherrypicked adafruit-blinka and /board/ and board.py and busio.py and added them to the /lib/ on my circuitPython. I am just trying to test the code on the CIRCUITPY using Mu Editor's Serial.

when I run

Code: Select all

import board

print ("Available pins: ")
for pin in dir(board):
    if pin.startswith("D") or pin.startswith("A"):
        print pin
nothing shows...

similarly I tried

Code: Select all

import time
import board
import busio
import adafruit_scd4x

i2c = busio.I2C(board.SCL, board.SDA)
scd = adafruit_scd4x.SCD4X(i2c)

while True:
    print ("CO2 PPM: ", scd.CO2)
    time.sleep(1)
and it prints out every second with NONE...

I also receive NONE for usb_cdc.data when I tried to run:

Code: Select all

import board
import busio

usb_cdc = busio.UART(board.TX, board.RX, baudrate=115200)

data = usb_cdc.data
if data is not None:
    print(data.decode("utf-8"))
    usb_cdc.data = None
It returns: AttributeError: 'UART' object has no attribute 'data'... (please excuse the lack of indentation)
Last edited by dastels on Wed May 17, 2023 8:34 pm, edited 1 time in total.
Reason: Please use code tags for embedded code.

User avatar
dpuerto
 
Posts: 40
Joined: Wed Feb 01, 2023 12:25 am

Re: Trinkey_QT2040_Enviro_Gadget 2

Post by dpuerto »

Thanks, I didn't notice that code block, Is there a shorthand?

So I can manipulate the board blinking the light on the Trinkey and changing the colors modifying the RGB values. I believe the problem occurs somewhere in the busio i2c and bme280 implementation. I am going to continue to test. I hope that the SCD41 is going to send the values to the Trinkey.

User avatar
dastels
 
Posts: 15817
Joined: Tue Oct 20, 2015 3:22 pm

Re: Trinkey_QT2040_Enviro_Gadget 2

Post by dastels »

OK, you're exploring things I haven't.

I'll look into it.

Dave

User avatar
dpuerto
 
Posts: 40
Joined: Wed Feb 01, 2023 12:25 am

Re: Trinkey_QT2040_Enviro_Gadget 2

Post by dpuerto »

I'm not sure who you are but whoever you may be, I want you to know you're awesome and a hero for jumping into this rabbit hole repeatedly for me. Thank you thank you thank you

User avatar
dastels
 
Posts: 15817
Joined: Tue Oct 20, 2015 3:22 pm

Re: Trinkey_QT2040_Enviro_Gadget 2

Post by dastels »

Thanks. I put it to the support team to see if anyone else can lend a hand.

Dave

User avatar
dpuerto
 
Posts: 40
Joined: Wed Feb 01, 2023 12:25 am

Re: Trinkey_QT2040_Enviro_Gadget 2

Post by dpuerto »

Code: Select all

import board
import busio
print("Hello blinka!")
i2c = busio.I2C(board.SCL, board.SDA)
print("I2C ok!")
print("done!")
This blinka test passes. I am going to have to dabble a little deeper and go line by line. I was just trying to utilize the code from the repo for trinkey+scd41 /// and I even found a github repo with someone from my company where he had success but he leveraged the same code... There's a disconnect somewhere, but I am feeling like it's going to take me going line by line and I'm leaving for a trip this weekend...................

User avatar
dpuerto
 
Posts: 40
Joined: Wed Feb 01, 2023 12:25 am

Re: Trinkey_QT2040_Enviro_Gadget 2

Post by dpuerto »

Circling back, no improvement on my end... I am considering trying the trinkey with the button that broke and see if that board seems to read... it always fails when trying to use the adafruit_bme280 import which is used in this fashion in the example code:(

import adafruit_scd4x from adafruit_bme280
import basic as adafruit_bme280

User avatar
adafruit_support_carter
 
Posts: 29457
Joined: Tue Nov 29, 2016 2:45 pm

Re: Trinkey_QT2040_Enviro_Gadget 2

Post by adafruit_support_carter »

It seems like this is a good summary of the overall goal?
I want the code to run on the trinkey FIRST and foremost to test that I am reading values from the SCD41, and I want to ultimately listen to the usb port that the trinkey is plugged into with an external application (on my Mac) so that I can take those JSON values and send them up to the cloud and then fetch and render those values from my persistence layer on my website.
If you want the code to run on the Trinkey, you do not need to worry about Blinka. You would run CircuitPython on the Trinkey and send data over a data USB serial connection. For reference this discusses how to set that up:
https://learn.adafruit.com/customizing- ... ta-3096590
How you connect to the resulting USB serial connection and read the data on the host PC is up to you. It just needs to be something that can open and read from the serial port. The Trinkey is simply running CircuitPython, connecting to the sensor, and sending data out over the USB serial connection.

The other approach, involves installing the u2if firmware (not CircuitPython) onto the Trinkey and then installing Blinka onto the host PC (your Mac):
https://learn.adafruit.com/adafruit-tri ... on-example
You would then write Python code on the host PC to directly connect to the sensor - through the Trinkey. The Trinkey+u2if+Blinka setup is acting like a bridge and making it seem like your host PC has I2C ports.

User avatar
dpuerto
 
Posts: 40
Joined: Wed Feb 01, 2023 12:25 am

Re: Trinkey_QT2040_Enviro_Gadget 2

Post by dpuerto »

adafruit_support_carter wrote: Thu May 25, 2023 5:45 pm It seems like this is a good summary of the overall goal?
I want the code to run on the trinkey FIRST and foremost to test that I am reading values from the SCD41, and I want to ultimately listen to the usb port that the trinkey is plugged into with an external application (on my Mac) so that I can take those JSON values and send them up to the cloud and then fetch and render those values from my persistence layer on my website.
If you want the code to run on the Trinkey, you do not need to worry about Blinka. You would run CircuitPython on the Trinkey and send data over a data USB serial connection. For reference this discusses how to set that up:
https://learn.adafruit.com/customizing- ... ta-3096590
How you connect to the resulting USB serial connection and read the data on the host PC is up to you. It just needs to be something that can open and read from the serial port. The Trinkey is simply running CircuitPython, connecting to the sensor, and sending data out over the USB serial connection.

The other approach, involves installing the u2if firmware (not CircuitPython) onto the Trinkey and then installing Blinka onto the host PC (your Mac):
https://learn.adafruit.com/adafruit-tri ... on-example
You would then write Python code on the host PC to directly connect to the sensor - through the Trinkey. The Trinkey+u2if+Blinka setup is acting like a bridge and making it seem like your host PC has I2C ports.

So after following the u2if directions and finding some issues with how we were importing I have updated my code and installed libraries locally but I continue to run into this error:
CODE: SELECT ALL

Code: Select all

Traceback (most recent call last):
  File "trinkey.py", line 4, in <module>
    import adafruit_scd4x
  File "/Users/dpuerto/Sites/XXXX.com/sensors/adafruit_scd4x.py", line 31, in <module>
    from adafruit_bus_device import i2c_device
ImportError: cannot import name 'i2c_device' from 'adafruit_bus_device' (/Users/dpuerto/Sites/XXXX.com/sensors/adafruit_bus_device/__init__.py)
I've also tried with microcontroller and board for ic2 instead of busio... hmmm...

I'm using this:

https://github.com/adafruit/Adafruit_Ci ... thon_SCD4x

Here is my code (all libraries are installed)

Code: Select all

import time
import board
import busio
import adafruit_scd4x
from adafruit_bme280 import basic as adafruit_bme280

i2c = busio.I2C(board.SCL, board.SDA)
scd = adafruit_scd4x.SCD4X(i2c)
scd.start_periodic_measurement()

bme = adafruit_bme280.Adafruit_BME280_I2C(i2c)

while True:
    time.sleep(5)
    print("CO2 =", scd.CO2)
    print("Pressure = {:.1f} hPa".format(bme.pressure))
    print("Temperature = {:.1f} degC".format(bme.temperature))
    print("Humidity = {:.1f}%".format(bme.humidity))
    

Does this code look correct to you? I am not a python person but the imports in the adafruit_scd4x.py look off...

Code: Select all

import time
import struct
from adafruit_bus_device import i2c_device
from micropython import const
Attachments
Screenshot 2023-05-25 at 4.53.54 PM.png
Screenshot 2023-05-25 at 4.53.54 PM.png (49.58 KiB) Viewed 187 times

User avatar
adafruit_support_carter
 
Posts: 29457
Joined: Tue Nov 29, 2016 2:45 pm

Re: Trinkey_QT2040_Enviro_Gadget 2

Post by adafruit_support_carter »

OK, sounds like you're going the Blinka/u2if route.

The basic Blinka test is passing.

You mentioned not getting any pins listed. Let's double check that a different way. And the environment variable at the same time.

Launch python interactively and try running these commands at the REPL prompt. Should get the same output as shown here:

Code: Select all

$ python3
Python 3.8.10 (default, Mar 13 2023, 10:26:41) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ.get('BLINKA_U2IF')
'1'
>>> import board
>>> dir(board)
['BUTTON', 'I2C', 'NEOPIXEL', 'SCL', 'SDA', '__blinka__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__repo__', '__spec__', '__version__', 'ap_board', 'board_id', 'detector', 'pin', 'sys']
>>> 
What do you get?

User avatar
dpuerto
 
Posts: 40
Joined: Wed Feb 01, 2023 12:25 am

Re: Trinkey_QT2040_Enviro_Gadget 2

Post by dpuerto »

adafruit_support_carter wrote: Fri May 26, 2023 11:09 am OK, sounds like you're going the Blinka/u2if route.

The basic Blinka test is passing.

You mentioned not getting any pins listed. Let's double check that a different way. And the environment variable at the same time.

Launch python interactively and try running these commands at the REPL prompt. Should get the same output as shown here:

Code: Select all

$ python3
Python 3.8.10 (default, Mar 13 2023, 10:26:41) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ.get('BLINKA_U2IF')
'1'
>>> import board
>>> dir(board)
['BUTTON', 'I2C', 'NEOPIXEL', 'SCL', 'SDA', '__blinka__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__repo__', '__spec__', '__version__', 'ap_board', 'board_id', 'detector', 'pin', 'sys']
>>> 
What do you get?

Code: Select all

➜  sensors git:(master) ✗ python3
Python 3.8.2 (default, Aug 25 2020, 09:23:57) 
[Clang 12.0.0 (clang-1200.0.32.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ.get('BLINKA_U2IF')
>>> import board
>>> dir(board)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
>>> exit()
➜  sensors git:(master) ✗ 

User avatar
adafruit_support_carter
 
Posts: 29457
Joined: Tue Nov 29, 2016 2:45 pm

Re: Trinkey_QT2040_Enviro_Gadget 2

Post by adafruit_support_carter »

You are getting a None return for the environment variable query. Make sure that has been set before launching Python:
https://learn.adafruit.com/circuitpytho ... le-3095615

Locked
Please be positive and constructive with your questions and comments.

Return to “Trinket ATTiny, Trinket M0”