Komunikasi antara dua mikropengawal melalui UART

Hari ini kita akan memperkenalkan protokol UART.

komponen yang digunakan

  • Seeduino Xiao RP2040 (Penghantar)
  • Maker Nano RP2040 (Penerima)
  • Grove CO2 Sensor
  • Grive OLED Display SSD1315
  • Grove Shield for Arduino Nano V 1.1
  • Grove Shield for Seeeduino Xiao

Secara ringkasnya, bacaan CO2, Suhu dan Kelembapan dibaca oleh Penghantar, kemudian nilai pembolehubah dihantar kepada penerima, dan dipaparkan di atas OLED.

saya merujuk kepada artikel ini

#ini adalah Penghantar UART dipasang di atas Seeeduino Xiao RP2040 import time import board import busio import digitalio import adafruit_ssd1306 import adafruit_scd30 #Seeduino Xiao RP2040 mempunyai definisi port I2C sedia terbina i2c = board.I2C() #Seeduino Xiao RP2040 mempunyai definisi port TX, RX sedia terbina uart = busio.UART(board.TX, board.RX, baudrate=9600, timeout=0) UPDATE_INTERVAL = 5 last_time_sent = 0 while True: try: #oled.fill(0) now = time.monotonic() if now - last_time_sent >= UPDATE_INTERVAL: scd = adafruit_scd30.SCD30(i2c) temperature = "{:.2f}".format(scd.temperature) relative_humidity = "{:.2f}".format(scd.relative_humidity) co2_ppm_level = "{:.2f}".format(scd.CO2) print("CO2: "+co2_ppm_level+" ,Temperature : "+temperature+" Humidity "+relative_humidity) #send UART message uart.write(bytes("<R,"+co2_ppm_level+","+temperature+","+relative_humidity+">", "ascii")) last_time_sent = now except: print("something wrong somewhere")
Code language: PHP (php)

#ini adalah penerima UART dipasang di atas Maker Nano RP2040 import time import board import busio import digitalio import analogio import adafruit_ssd1306 #Maker Nano RP2040 tiada definisi port I2C sedia ada dalam board LCD_SDA = board.GP12 LCD_SCL = board.GP13 i2c = busio.I2C(scl=LCD_SCL, sda=LCD_SDA) oled = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c) # Use a timeout of zero so we don't delay while waiting for a message. uart = busio.UART(board.GP4, board.GP5, baudrate=9600, timeout=0) message_started = False while True: byte_read = uart.read(1) # baca 1 byte melalu UART if byte_read is None: # Nothing read. continue if byte_read == b"<": # mula kumpulkan mesej tanpa "<". message = [] message_started = True continue if message_started: if byte_read == b">": # Akhir mesej, tanpa ">". # Tukar kepada string dan pecahkan ikut tanda "," message_parts = "".join(message).split(",") message_type = message_parts[0] message_started = False if message_parts[0] == "R": # Received a message telling us a all sensor values co2=message_parts[1] temperature=message_parts[2] humidity=message_parts[3] print(f"CO2 {co2}") print(f"T {temperature}") print(f"H {humidity}") #Display on OLED oled.fill(0) oled.text("https://sidik.my",20,0,1) oled.rect(2, 10, oled.width-2, oled.height-10, True) oled.text("CO2:" , 4, 15,1) oled.text(co2, 20, 15, True, size=3) oled.text("T:"+temperature, 20, 45, True, size=1) oled.text("H:"+humidity, 70, 45, True, size=1) oled.show() else: # Accumulate message byte. message.append(chr(byte_read[0]))
Code language: PHP (php)

Leave a Reply

Alamat e-mel anda tidak akan disiarkan.

Related Post