Debuging my code in PC without REPL

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
schvic
Posts: 15
Joined: Wed Jul 09, 2014 2:45 pm

Debuging my code in PC without REPL

Post by schvic » Sat Aug 23, 2014 4:55 pm

Greetings,

I am currently developping a weather monitoring station with my pyboard. I have been using the REPL tool to debug some parts of my code.
The next step would be to debug the hole code working together by outputting a lot of specific values (sensor readings, relay states) to my pc via a serial port. By debuging I mean to see the values that my sensors and/or functions generate in order to see if they are correct.

For those who have used Arduino before, I am looking for a solution like "Serial.println("values that I want to see") with a console in my PC listening to the serial port.

I guess I should use a serial port since I haven't been able to paste all the code in the REPL tool (problem with indentations).

My question is: What are the options to see values generated by the pyboard in my pc? It's a lot of values so the LCDs are not an option.

Just for informaiton: My temporary solution for now is by connecting the uart(3) port to one of the arduino mega serial ports, send the messages from my pyboard and then the arduino prints the message to the usb serial port. It works almost perfectly but when I send a string to the arduino from my pyboard, the arduino outputs the decimal ascii value of each character and one line for each ascii value/character. I will maybe work on the arduino code but I would like to have another solution in orther to have most of the pyboard pins available.

Thank you for your support.

Best reagrds,

Schvic

fma
Posts: 164
Joined: Wed Jan 01, 2014 5:38 pm
Location: France

Re: Debuging my code in PC without REPL

Post by fma » Sat Aug 23, 2014 5:13 pm

You definilty don't want to see output through usb connection? Because you get all print() you do on your pyboard, even when running a script in flash...
Frédéric

Turbinenreiter
Posts: 288
Joined: Sun May 04, 2014 8:54 am

Re: Debuging my code in PC without REPL

Post by Turbinenreiter » Sat Aug 23, 2014 5:40 pm

Amusing the code you want to debug looks like this:
(Warning - not tested, using Accleromter as example)

Code: Select all

import pyb

accel = pyb.Accel()

while True:

    ax, ay ,az = accel.filtered_xyz()
    print(ax, ay, az)

1. Save that script on the pyboard, either flash or sd, doesn't matter. Name it test.py
2. Open the REPL.
3. Type

Code: Select all

import test
to run your script on the REPL.
4. You will see the values printed to your screen. It will be hard to read tough, because it will run really fast. You can add

Code: Select all

pyb.delay(100)
after the print to slow it down.

Hope that helps, keep asking if not.

schvic
Posts: 15
Joined: Wed Jul 09, 2014 2:45 pm

Re: Debuging my code in PC without REPL

Post by schvic » Sat Aug 23, 2014 9:55 pm

Many thanks for your answers, it works perfectly by creating a file and printing what I want. :)

I jsut saw there is also a function called pyb.repl_uart(), I will see how it works.

I am now wondering if there is a way to give inputs to the pyboard via the REPL tool when it is executing the code in the main.py file. What do you think?

Many thanks again.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Debuging my code in PC without REPL

Post by dhylands » Sat Aug 23, 2014 10:22 pm

There are a couple of ways of waiting for input.

You can poll the USB to see if data is available: see http://micropython.org/doc/module/pyb/USB_VCP

I wrote a sample program to query what keys are being pressed:
https://github.com/dhylands/upy-example ... er/keys.py

You can use the input command:

Code: Select all

>>> x = input("Enter some data> ")
Enter some data> Hello
>>> x
'Hello'
>>> 
You could write your logging information to a log file (on the sdcard), which you then collect later.

If you're running on a linux host, I have an auto-connecting terminal program written in python:
https://github.com/dhylands/usb-ser-mon

It creates a log file called usb-ser-mon.log which records everything typed and received over the serial port.

schvic
Posts: 15
Joined: Wed Jul 09, 2014 2:45 pm

Re: Debuging my code in PC without REPL

Post by schvic » Sun Aug 24, 2014 12:02 pm

I will experiment with this functions to see how they perform for my application. Concerning the logging function I will stick for now with the REPL solution since it is real time debuging while the application is executing so I can test different configuraitons. The logging function is interesting for analysing the measurements after long periods of acquiring data.

I have an additional question regarding the states of the pins: If a pin is an output and I want to see its state, I should just execute: mypin.value()? I have tried it and it works but apparently it blocks its state. It works in the hardware side but in the software side it keeps teling it is activated even if in the hardware side it is not.

Thank you very much.

Best regards.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Debuging my code in PC without REPL

Post by dhylands » Sun Aug 24, 2014 5:53 pm

schvic wrote:I have an additional question regarding the states of the pins: If a pin is an output and I want to see its state, I should just execute: mypin.value()? I have tried it and it works but apparently it blocks its state. It works in the hardware side but in the software side it keeps teling it is activated even if in the hardware side it is not.
I'm not quite sure I understand. I did the following:

Code: Select all

>>> pin = pyb.Pin('LED_GREEN', pyb.Pin.OUT_PP)
>>> pin.value(1)
>>> pin.value() 
1
>>> pin.value(0)
>>> pin.value() 
0
It's important to configure the pin using the pyb.Pin.OUT_PP, otherwise the pin will be configured as an input (which is the default state of all pins). And writing a value to a pin configured as an input won't do anything.

schvic
Posts: 15
Joined: Wed Jul 09, 2014 2:45 pm

Re: Debuging my code in PC without REPL

Post by schvic » Mon Aug 25, 2014 2:37 pm

Oh, yes, sorry I just saw the error in my code. Thank you for the information, before I used the pin.high() or pin.low() to set the output values but it's better pin.value(1or0).

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Debuging my code in PC without REPL

Post by dhylands » Mon Aug 25, 2014 3:49 pm

pin.high and pin.low should be fine as well:

Code: Select all

>>> pin = pyb.Pin('LED_GREEN', pyb.Pin.OUT_PP)
>>> pin.high()
>>> pin.value()
1
>>> pin.low()
>>> pin.value()
0
pin.high() and pin.value(1) are doing exactly the same thing. Similarly pin.low() and pin.value(0) are also doing exactly the same thing.

schvic
Posts: 15
Joined: Wed Jul 09, 2014 2:45 pm

Re: Debuging my code in PC without REPL

Post by schvic » Mon Aug 25, 2014 8:04 pm

Thank you, the error was because of the conditions for changing the value of the variable. The pin.value() method is more practical tu use :)

Post Reply