Pico Pi - Two Sided Communication

Hello everyone,
I've created an app by using python, now i want to create values on that .py and send to raspberry pico then get response back to my computer. Example pc should send i="x" value could be char then raspberry should response like "if i==1 print("hello"), if i==2("good bye") back to the computer. I saw some examples for C but my app is written by py file so i'm trying to solve by using serial.read, serial.writeline, uart.read etc. Any suggestions please?

PC.py file

import serial

ser = serial.Serial('COM7', baudrate=9600, timeout=1)

try:
    while True:
        data_to_send = input("input a char: ")
        ser.write(data_to_send.encode('utf-8'))
        response = ser.readline().decode('utf-8').strip()
        print("response from device:", response)

except KeyboardInterrupt:
    ser.close()

Board codes

import machine

# Initialize an output pin to control the LED
led = machine.Pin(25, machine.Pin.OUT)

# Initialize the UART communication with a baud rate of 9600
uart = machine.UART(0, baudrate=9600, tx=machine.Pin(0), rx=machine.Pin(1))

while True:
    # Check if there is any data available to read from the UART
    if uart.any():
        received_data = uart.read(1)

        # Check if data was received
        if received_data:
            print("Received data: ", received_data.decode("utf-8"))
            
            # Perform actions based on the received data to turn the LED on/off
            # For example, if the character 'k' is received, turn on the LED,
            # and if any other character is received, turn off the LED
            if received_data == b'k':
                # Turn on the LED
                led.value(1)
                # Send "OK" response to confirm that the character 'k' was received
                uart.write("OK")
            else:
                # Turn off the LED
                led.value(0)
                # Send "UNKNOWN" response to indicate that an unknown character was received
                uart.write("UNKNOWN")

Show us your best try at the Arduino code - please use CODE TAGS.
if you can show exactly the python messages including any framing etc, then we can see why the Arduino may be having trouble.

I've added codes which i've tried recently. Whenever i run app on Visual Code after input any char it comes back but "k" doesnt control any pin for led also im not getting any error. I'm trying to turn led on after sending input which "k"

post deleted by author

@meqira I tried this on an esp32 so there are a few subtle differences you will need to adjust so that it will work with the Pi mainly the uart initialization and the led.

The PC side is your code basically untouched.

import serial

ser = serial.Serial('COM7', baudrate=9600)

try:
    while True:
        data_to_send = input("input a char: ")
        ser.write(data_to_send.encode('utf-8'))
        response = ser.readline().decode('utf-8').strip()
        print("response from device:", response)

except KeyboardInterrupt:
    ser.close()

The micropython reports the status of your PC commands and uses a bytearray so it is possible to send command strings instead of single characters if you so wished.

from machine import UART , Pin

uart = UART(2, baudrate=9600, tx=17, rx=16)
led = Pin(25, Pin.OUT, value=0)

while True:
    read_count=uart.any()
    input_data=bytearray(read_count)
    if read_count>0:
        uart.readinto(input_data,read_count)
        mystring = input_data.decode('utf-8')
        
        if mystring=='k':
            uart.write('ON\n')
            led.on()
        elif mystring=='h':
            uart.write('OFF\n')
            led.off()
        else:
            uart.write('UNKNOWN CMD\n')

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.