Week 13. Networking and communications

Group Assignment

Here is our group assignment on Kelleigh's page:

Group Assignment

Individual Assignment

Wired UART Communication using Thonny: Step-by-Step Guide

In this comprehensive guide, I will walk you through the process of establishing wired UART communication between two Raspberry Pi Pico boards using Thonny.

Prerequisites:

Hardware Setup:

Software Installation:

Download and Install Thonny:

Download Thonny

Initial Setup:

Setting Up Pico Boards:

I have never used this board before, so I first wanted to ensure I could get it to work on both computers.

I tried several times to get the pico board connected to my device and another laptop and kept experiencing the error: "User Upload error: Failed uploading: uploading error: exit status 1"

I found a lot of different advice out there to address this issue so I tried all of the following. To be honest, I’m not sure if all of those steps were necessary or not, but hopefully it was not a waste of time.

First I went into preferences and added this URL

https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json

Pico Trouble 1 Pico Trouble 2

I then added RP2040 to the boards manager:

Pico Trouble 3

I also added the following libraries after searching pcf857:

Pico Trouble 4

Bootsel Button

Finally, I think what actually did it, was holding down the Bootsel button.

On the Raspberry Pi Pico, there is a "BOOTSEL" button. Press and hold this button while connecting the Pico to your computer via USB. Release the "BOOTSEL" button after connecting.

Now, my boards are communicating with each device and the LEDs are blinking with this code:


  const int ledPin = 25;  // The built-in LED on Raspberry Pi Pico is connected to pin 25


void setup() {
 pinMode(ledPin, OUTPUT);
}


void loop() {
 digitalWrite(ledPin, HIGH);  // Turn on the LED
 delay(500);                   // Wait for 500 milliseconds
 digitalWrite(ledPin, LOW);   // Turn off the LED
 delay(500);                   // Wait for 500 milliseconds
}

Download Necessary Files:

Download the Required Python Files:

Computer A downloads:

pico_comms_a.py

easy_comms.py

Computer B downloads:

pico_comms_b.py

If Needed, Create Your Own Files Manually (this is what I had to do to create the files above):

Open a Text Editor:

Py file

Configure Thonny:

Open Thonny:

Install MicroPython Plugin:

Plugin

Connect Raspberry Pi Pico:

Select MicroPython Interpreter:

python install interpreter

Check Connection Status:

Wiring:

Use a breadboard and male-male wires to establish the following connections:

Here is what it looks like:

wiring network

Programming Computers:

Use one computer (Computer A) to program Pico A and the other computer (Computer B) to program Pico B.

Programming Pico A (Computer A):

Code: easy_comms.py


      from machine import UART, Pin
      from time import time_ns
  
      class easy_comms:
       
          uart_id = 0
          baud_rate = 9600
          timeout = 1000 # milliseconds
          
          def __init__(self, uart_id:int, baud_rate:int=None):
              self.uart_id = uart_id
              if baud_rate: self.baud_rate = baud_rate
  
              # set the baud rate
              self.uart = UART(self.uart_id,self.baud_rate)
  
              # Initialise the UART serial port
              self.uart.init()
                  
          def send(self, message:str):
              print(f'sending message: {message}')
              message = message + '\n'
              self.uart.write(bytes(message,'utf-8'))
              
          def start(self):
              message = "ahoy\n"
              print(message)
              self.send(message)
  
          def read(self)->str:
              start_time = time_ns()
              current_time = start_time
              new_line = False
              message = ""
              while (not new_line) or (current_time <= (start_time + self.timeout)):
                  if (self.uart.any() > 0):
                      message = message + self.uart.read().decode('utf-8')
                      if '\n' in message:
                          new_line = True
                          message = message.strip('\n')
                          # print(f'received message: {message}')
                          return message
              else:
                  return None
    

Code: pico_comms_a.py


      # Pico_comms_a
      # Sends commands and listens to responses from pico_comms_b
      
      from easy_comms import easy_comms
      from time import sleep
      
      com1 = easy_comms(uart_id=0, baud_rate=9600)
      com1.start()
      
      while True:
      message = ""
      message = com1.read()
      
      if message is not None:
      print(f"message received: {message.strip('\n')}")
      sleep(1)
    

Programming Pico B (Computer B):

Code: pico_comms_b.py


      # Pico_comms_b
      # Sends commands and listens to responses from pico_comms_a
  
      from easy_comms import easy_comms
      from time import sleep
  
      com1 = easy_comms(uart_id=0, baud_rate=9600)
      com1.start()
  
      count = 0
      while True:
          # send a message
          com1.send(f'hello, {count}')
          
          #check for messages
          message = com1.read()
          
          if message is not None:
              print(f"message received: {message.strip('\n')}")
          sleep(1)
          count +=1
    

Running the Programs:

Pico A (Computer A):

Pico B (Computer B):