Skip to content

Week 14: Embedded Networking and Communications

Tasks
- Send a message between two projects

UART

For this task we will be sending a message via UART (Universal Asynchronous Receiver Transmitter). We will be using it to send a message between Liam and Jaryd's projects. We will be connecting the ESP32 on Liam's plant hub. To do so, we repurposed some code that we have previously written.

And the motor controller Pico on Jaryd's robot.

We started by connecting Rx to Tx and Tx to Rx of the boards, as well as ensuring that both boards had a common ground.

We then wrote some code to get the 2 boards talking to each other Via UART which is incredibly easy in a MicroPython Environment. The plan is that Liam's board says hello, and Jaryd's board will reply when it hears it.

Here is Liams code which stars by checking the UART buffer for with the "if uart.any()", and if there is it will simply decode the message into a nice Python string and print it. Then it will transmit a message over UART and sleep for 2 seconds.

from machine import UART, Pin
import time

# Initialize UART 1 on Pico A, TX pin is GP4 and RX pin is GP5
uart = UART(1, baudrate=9600, tx=Pin(43), rx=Pin(44))

while True:
    if uart.any():
        message = uart.read().decode().strip()
        print(message)

    uart.write("Hello!")  # Send 'ON' message to Pico B
    time.sleep(2)    # Wait for 2 seconds
And here is Jaryd's code, it checks the UART buffer for any messages, if there is it prints it, then sends back a hello!
from machine import Pin, UART
import time

uart = UART(1, baudrate=9600, tx=Pin(4), rx=Pin(5))  # Using UART 1 for Pico B
print("Listening for UART messages...")


while True:
    if uart.any(): # listen for uart messages and send a hello back if heard
        message = uart.read().decode().strip() 
        print(message)
        uart.write("Hi there!")

And if we run both of these (in 2 instances of Thonny0), we can see the messages being sent to each other.