djhuk1
Posts: 4
Joined: Thu Jan 02, 2020 7:49 pm

Reading /dev/ttyACM0 outputs rubbish after reboot

Sat Jan 23, 2021 2:08 pm

I have this really simple program that reads the internal temperature sensor and blinks an LED. It also prints the temperature

Code: Select all

from machine import Pin, Timer
import machine

led1 = Pin(15, Pin.OUT)
led2 = Pin(13, Pin.OUT)
led3 = Pin(25, Pin.OUT)
tim = Timer()

sensor_temp = machine.ADC(4)
conversion_factor = 3.3 / (65535)

def tick(timer):
    global led1
    global led2
    global led3
    global uart
    led3.toggle()
    reading = sensor_temp.read_u16() * conversion_factor
    temperature = 27 - (reading - 0.706)/0.001721
    print("{}".format(temperature))
    if temperature > 15:
        led1.value(1)
        led2.value(0)
    elif temperature < 15:
        led1.value(0)
        led2.value(1)

tim.init(freq=1, mode=Timer.PERIODIC, callback=tick)
This works, and using "rshell" and putting the file under /pyboard/main.py means it starts up fine when you do a reboot

However on reading /dev/ttyACM0 I just get a few temperature sensor readings and then a bunch of exceptions (the LEDS keep on blinking so the program is running!)

Code: Select all

~|▶ cat /dev/ttyACM0
17.68152

17.68152

17.68152

>>>

>>> 17.68152

17.68152

>>>

>>> 17.68152

17.68152

>>>

>>> >>>

Traceback (most recent call last):

  File "<stdin>", line 1

SyntaxError: invalid syntax

>>>

>>> >7.2

Traceback (most recent call last):

  File "<stdin>", line 1

SyntaxError: invalid syntax

>>> 17.685

17.685

>>>

>>> >
I think it's because on reading /dev/ACM0 it's probably trying to start the REPL - but I don't know how to disable that, any help?

hippy
Posts: 9158
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Reading /dev/ttyACM0 outputs rubbish after reboot

Sun Jan 24, 2021 3:58 pm

Could it be that after the "tim.init()" the program exits, returns to the REPL, but the 'timer interrupt' keeps on running in the background ?

My 'print' commands inside a 'while True:' seem to be okay. I don't get any REPL ">>>" prompts, presumably because it never exits to the REPL.

You could try adding a "while True : time.sleep(1)" or similar at the end of your code.

JumpZero
Posts: 1215
Joined: Thu Mar 28, 2013 7:35 pm
Location: Arcachon, France

Re: Reading /dev/ttyACM0 outputs rubbish after reboot

Sun Jan 24, 2021 6:28 pm

Hi
Long ago I connected an arduino to an ESP8266 running MicroPython via serial link
So I needed to disable the repl on the uart. To do it I used:

Code: Select all

import uos, machine
uos.dupterm(None, 1) 
see here : https://docs.micropython.org/en/latest/library/uos.html
And in my use case I needed to set the uart I did:

Code: Select all

uart = machine.UART(0, baudrate=115200, bits=8, parity=None, stop=1, rxbuf=100, timeout=5, timeout_char=1)

To be able to communicate with the ESP8266 I used the webrepl (nice MicroPython functionality but available with wifi..)
I expect my Pico to be delivered tomorrow... looking forward, I'm excited

hippy
Posts: 9158
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Reading /dev/ttyACM0 outputs rubbish after reboot

Mon Jan 25, 2021 12:46 am

djhuk1 wrote:
Sat Jan 23, 2021 2:08 pm
~|▶ cat /dev/ttyACM0
17.68152
I was testing my code using a Python-basd terminal emulator. When I tried "cat /dev/ttyACM0" to see my Pico's output that immediately returned to the Pi command prompt. So I am a bit intrigued why that worked for you but not for me.

Return to “MicroPython”