├── .DS_Store ├── .gitignore ├── README.md ├── easy_comms.py ├── led_client.py ├── led_server.py ├── pico_comms_a.py └── pico_comms_b.py /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevinmcaleer/easy_comms/7971bffbb1c7f03b16bf340a8a8b3ec2d9eb32eb/.DS_Store -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevinmcaleer/easy_comms/7971bffbb1c7f03b16bf340a8a8b3ec2d9eb32eb/.gitignore -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Easy Comms 2 | 3 | Easy Comms is a simple MicroPython library for easily sending messages over UART connections from one device to another 4 | 5 | ## Demo Apps 6 | 7 | There are two demo applications: 8 | 9 | * [`pico_comms_a.py`](pico_comms_a.py) - Listens for messages and prints them on the console 10 | * [`pico_comms_b.py`](pico_comms_b.py) - Sends messages, also listens and prints to the console 11 | * [`led_server.py`](led_server.py) - Tells the led_client to blink an led based on the contents of a json formatted message 12 | * [`led_client.py`](led_client.py) - Listens for commands over UART for blink on and off commands 13 | 14 | ### How to use the Demo Apps 15 | Load either of the demo code `pico_comms_a.py` and `pico_comms_b.py` or `led_client.py` and `led_server.py` onto a pair of Raspberry Pi Picos, and wire them us such that the GPIO Pin 0 (RX) goes to GPIO Pin 1 (TX) on the other Pico and vice versa. Also make sure both Picos have their grounds connected otherwise you'll get all kind of wierd messages and errors. 16 | 17 | -------------------------------------------------------------------------------- /easy_comms.py: -------------------------------------------------------------------------------- 1 | #Easy comms is a simple class that allows you to send and receive messages over a serial port. 2 | 3 | from machine import UART, Pin 4 | from time import time_ns 5 | 6 | class Easy_comms: 7 | 8 | uart_id = 0 9 | baud_rate = 9600 10 | timeout = 1000 # milliseconds 11 | 12 | def __init__(self, uart_id:int, baud_rate:int=None): 13 | self.uart_id = uart_id 14 | if baud_rate: self.baud_rate = baud_rate 15 | 16 | # set the baud rate 17 | self.uart = UART(self.uart_id,self.baud_rate) 18 | 19 | # Initialise the UART serial port 20 | self.uart.init() 21 | 22 | def send(self, message:str): 23 | print(f'sending message: {message}') 24 | message = message + '\n' 25 | self.uart.write(bytes(message,'utf-8')) 26 | 27 | def start(self): 28 | message = "ahoy\n" 29 | print(message) 30 | self.send(message) 31 | 32 | def read(self)->str: 33 | start_time = time_ns() 34 | current_time = start_time 35 | new_line = False 36 | message = "" 37 | while (not new_line) or (current_time <= (start_time + self.timeout)): 38 | if (self.uart.any() > 0): 39 | message = message + self.uart.read().decode('utf-8') 40 | if '\n' in message: 41 | new_line = True 42 | message = message.strip('\n') 43 | # print(f'received message: {message}') 44 | return message 45 | else: 46 | return None 47 | -------------------------------------------------------------------------------- /led_client.py: -------------------------------------------------------------------------------- 1 | # led_client 2 | 3 | from easy_comms import Easy_comms 4 | from time import sleep 5 | from machine import Pin 6 | import json 7 | 8 | com1 = Easy_comms(0,9600) 9 | led = Pin("LED", Pin.OUT, value=0) 10 | 11 | while True: 12 | message = com1.read() 13 | if message is not None: 14 | print(f'message: {message}') 15 | try: 16 | command = json.loads(message) 17 | print(f'json: {command}') 18 | if command['command'] == 'blink': 19 | if command['args'] == 'on': 20 | led.on() 21 | if command['args'] == 'off': 22 | led.off() 23 | sleep(0.1) 24 | except Exception as e: 25 | print(f'error: {e}') 26 | -------------------------------------------------------------------------------- /led_server.py: -------------------------------------------------------------------------------- 1 | # led_server 2 | 3 | from easy_comms import Easy_comms 4 | from time import sleep 5 | from machine import Pin 6 | import json 7 | 8 | command = {'command':'blink', 'args': 'on'} 9 | 10 | com1 = Easy_comms(0,9600) 11 | led = Pin(25, Pin.OUT) 12 | 13 | while True: 14 | com1.send(str(json.dumps(command))) 15 | if command['args'] == 'on': 16 | command = {'command':'blink', 'args': 'off'} 17 | else: 18 | command = {'command':'blink', 'args': 'on'} 19 | led.toggle() 20 | sleep(1) 21 | -------------------------------------------------------------------------------- /pico_comms_a.py: -------------------------------------------------------------------------------- 1 | # Pico_comms_a 2 | # Sends commands and listens to responses from pico_comms_b 3 | 4 | from easy_comms import Easy_comms 5 | from time import sleep 6 | 7 | com1 = Easy_comms(uart_id=0, baud_rate=9600) 8 | com1.start() 9 | 10 | while True: 11 | message = "" 12 | message = com1.read() 13 | 14 | if message is not None: 15 | print(f"message received: {message.strip('\n')}") 16 | sleep(1) 17 | -------------------------------------------------------------------------------- /pico_comms_b.py: -------------------------------------------------------------------------------- 1 | # Pico_comms_b 2 | # Sends commands and listens to responses from pico_comms_a 3 | 4 | from easy_comms import Easy_comms 5 | from time import sleep 6 | 7 | com1 = Easy_comms(uart_id=0, baud_rate=9600) 8 | com1.start() 9 | 10 | count = 0 11 | while True: 12 | # send a message 13 | com1.send(f'hello, {count}') 14 | 15 | #check for messages 16 | message = com1.read() 17 | 18 | if message is not None: 19 | print(f"message received: {message.strip('\n')}") 20 | sleep(1) 21 | count +=1 22 | --------------------------------------------------------------------------------