help with UART and Raspberry Pico

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Donquizote99
Posts: 5
Joined: Thu Aug 05, 2021 6:08 am

help with UART and Raspberry Pico

Post by Donquizote99 » Wed Aug 18, 2021 8:40 am

Hello

I am developing a small personal project, I use raspberry pico and micropython, previously I had requested your help to solve some problems, despite obtaining the desired behavior, after a time "x" of execution (it varies the time) the program fails, however , it does not send an error, and the behavior of other tasks is not affected, the only task that stops working as expected is the one in charge of transmitting the data, I am a little lost, I do not know where I should look ...

comportamiento deseado:
Image

falla:
Image

the function in charge of transmitting the data:

Code: Select all

async def dataout():
    
    uart = UART(0, baudrate=9600, tx=machine.Pin(16), rx=machine.Pin(17))
    swriter = uasyncio.StreamWriter(uart, {})
    
    while True:

        d1 = await (h())
        d2 = await (t())
        d3 = await (humedadsustrato())
        d4 = await (tempico())
        d5 = await (tsustrato())
                
        if d1 == None:
            p1 = "xxxx"
        else:
            p1 = "{0:.3f}".format(d1)

        if d2 == None:
            p2 = "xxxx"
        else:
            p2 = "{0:.3f}".format( d2)
        
        if d3 == None:
            p3 = "xxxx"
        else:
            p3 = "{0:.3f}".format( d3)
        
        if estatus == True:
            p4 = "on"
        else:
            p4 = "of"

        if ledazul.pin.value() == 0:
            p5 = "aon"
        else:
            p5 = "aof"

        if ledblanco.pin.value() == 0:
            p6 = "bon"
        else:
            p6 = "bof"

        if ledrojo.pin.value() == 0:
            p7 = "ron"
        else:
            p7 = "rof"

        if bomba1.pin.value() == 0:
            p8 = "xon"
        else:
            p8 = "xof"

        p9 = "{0:.3f}".format( d4)

        if d5 == None:
            p10 = "xxxx"
        else:
            p10 = "{0:.3f}".format(d5)

        data_send =str("a"+p1+"b"+p2+"c"+p3+"d"+p4+"e"+p5+"g"+p6+"h"+p7+"i"+p8+"j"+p9+"k"+p10+"l")
        
        swriter.write(data_send)
        await swriter.drain()
        await uasyncio.sleep(2)

the function in charge of receiving the data is very simple:

Code: Select all

ser = serial.Serial('/dev/rfcomm99', 9600,timeout=3, parity=serial.PARITY_EVEN, rtscts=1)

while True:

    if recibir == True:

        linem = ser.readline()   
        line = linem.decode()
        
        indice_a = line.find("a")
        indice_b = line.find("b")
        indice_c = line.find("c")
        indice_d = line.find("d")
        indice_e = line.find("e")
        indice_g = line.find("g")
        indice_h = line.find("h")
        indice_i = line.find("i")
        indice_j = line.find("j")
        indice_k = line.find("k")
        indice_l = line.find("l")

        humedad_ambiente = line[indice_a+1:indice_b]
        temperatura_ambiente = line[indice_b+1:indice_c]
        humedad_sustrato = line[indice_c+1:indice_d]
        estatus_temp = line[indice_d+1:indice_e]
        azul = line[indice_e+2:indice_g]
        blanco = line[indice_g+2:indice_h]
        rojo = line[indice_h+2:indice_i]
        bomba = line[indice_i+2:indice_j]
        tempico = line[indice_j+1:indice_k]
        tsustrato = line[indice_k+1:indice_l]

        print("DATA:  "+line)
        print("---------------------")
        print("estaus temporizador: ", estatus_temp)
        print("azul: ", azul)
        print("blanco: ", blanco)
        print("rojo: ", rojo)
        print("bommba: ", bomba)
        print("humedad ambiente: ",humedad_ambiente)
        print("temperatura ambiente: ", temperatura_ambiente)
        print("temperatura sustrato: ", tsustrato)      
        print("tem interna: ",tempico)
        print("humedad sustrato: ",humedad_sustrato)
        print("---------------------")

    else:
        print("++++++++++++++++++++++")
        print("sin data para recibir")
        print("++++++++++++++++++++++")

    time.sleep(2)

Last edited by Donquizote99 on Tue Aug 31, 2021 2:08 pm, edited 1 time in total.

Donquizote99
Posts: 5
Joined: Thu Aug 05, 2021 6:08 am

Re: help with UART and Raspberry Pico

Post by Donquizote99 » Wed Aug 18, 2021 9:01 am

Update:

I got the following error:

Code: Select all

Task exception wasn't retrieved
future: <Task> coro= <generator object 'dataout' at 2000e610>
Traceback (most recent call last):
  File "uasyncio/core.py", line 1, in run_until_complete
  File "<stdin>", line 25, in dataout
  File "t_sustrato.py", line 18, in tsustrato
  File "ds18x20.py", line 40, in read_temp
  File "ds18x20.py", line 30, in read_scratch
Exception: CRC error
However I am just as lost, I suppose the failure in the data transmission is caused by a malfunction of the sensor code, looking a bit I found that it could be due to a false sense, however it causes me a conflict that works and stops working Any ideas where to look or how to fix this behavior?

Code: Select all

import time, ds18x20
from machine import Pin
import onewire
import uasyncio


async def tsustrato():
    
    try:
        
        ow = onewire.OneWire(Pin(2)) 

        ds = ds18x20.DS18X20(ow)
        roms = ds.scan()
        ds.convert_temp()
        for rom in roms:
            await uasyncio.sleep_ms(850)
            aa=(ds.read_temp(rom))
            return aa
        await uasyncio.sleep_ms(2000)
        
    except onewire.OneWireError:
        pass


    except TypeError:
        pass

    except KeyboardInterrupt:
        pass

robertcarell
Posts: 4
Joined: Thu Aug 12, 2021 10:13 pm

Re: help with UART and Raspberry Pico

Post by robertcarell » Thu Aug 19, 2021 1:12 am

So, we can upload micropython in Raspberry Pi Pico as well, I am new to micropython so just trying to understand it. I hope someone else will help you out.

Post Reply