Writing to sensor through UART?

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Winfried2022
 
Posts: 6
Joined: Wed Sep 14, 2022 6:36 am

Writing to sensor through UART?

Post by Winfried2022 »

Hello,

I know nothing about CircuitPython and not much about micro-controllers, and would like to run a CP script on an ESP32 micro-controller to talk to a sensor through its UART.

What would be the equivalent of /dev/ttyS0?

Code: Select all

import time
import serial

class SenseairS8:
    
    def __init__(self, port="/dev/ttyS0", baudrate=9600, timeout=.5):
        self.ser = serial.Serial(port, baudrate=baudrate, timeout=timeout)
        self.ser.flushInput()
    
    def co2(self):
        try:
            self.ser.flushInput()
            self.ser.write(b"\xFE\x44\x00\x08\x02\x9F\x25")
            time.sleep(1)
            response = self.ser.read(7)

            high = response[3]
            low = response[4]
            co2 = (high*256) + low
        except Exception as e:
            co2 = None
            print(e)
        
        return co2
Thank you.
Attachments
Lilygo.TTGO.T8.ESP32-S2.V1.1.png
Lilygo.TTGO.T8.ESP32-S2.V1.1.png (227.95 KiB) Viewed 46 times

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

Re: Writing to sensor through UART?

Post by adafruit_support_carter »

Checkout the info here and see if it helps:
https://learn.adafruit.com/circuitpytho ... art-serial

User avatar
Winfried2022
 
Posts: 6
Joined: Wed Sep 14, 2022 6:36 am

Re: Writing to sensor through UART?

Post by Winfried2022 »

Will do, thanks.

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

Return to “Adafruit CircuitPython”