0

Debugging CircuitPython on CPX
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Debugging CircuitPython on CPX

by gregorygraham on Tue Nov 06, 2018 5:40 pm

I've been using the BBC micro:bit with MicroPython in a 6th grade Python programming class for several months with great success. I am now teaching Python to some 12th graders, so I thought I would try CircuitPython on the CPX. We are having some success, but I've had several cases where the student writes code in Mu and saves it to the CPX, but nothing happens. There are no error messages, so they don't know if it didn't save or what is the problem. I got them to try the Check function in Mu, which finds syntax errors, but not other errors. I've found the feedback with the micro:bit to be much better, so I'm wondering if I'm missing something on the CPX. Does anyone have some simple debugging tips for CircuitPython on CPX?

gregorygraham
 
Posts: 2
Joined: Fri Dec 20, 2013 4:03 pm

Re: Debugging CircuitPython on CPX

by adafruit_support_carter on Wed Nov 07, 2018 3:25 pm

There are only a few set names that will auto run when saved. Are they saving to one of these names?
https://learn.adafruit.com/welcome-to-c ... -file-6-30

adafruit_support_carter
 
Posts: 7014
Joined: Tue Nov 29, 2016 2:45 pm

Re: Debugging CircuitPython on CPX

by MikeBarela on Wed Nov 07, 2018 4:21 pm

In Mu, turn on the Serial connection to display the REPL where any error messages will appear, you can restart the program, etc.

Adafruit suggests all running code be named "code.py". The program cannot have an arbitrary name.

MikeBarela
 
Posts: 689
Joined: Sat Mar 24, 2012 8:56 pm
Location: Mid-Atlantic

Re: Debugging CircuitPython on CPX

by kattni on Wed Nov 07, 2018 4:23 pm

As Carter mentioned, first verify that they are saving the files as code.py or one of the other three possible names (though we recommend code.py).

Can you connect to the serial console and the REPL through Mu on the boards that aren't responding to code being saved? To verify, click "Serial" in Mu to open the serial console. Depending on what's running, you may not see anything in this window. Click in the Serial panel. Then hit "ctrl+c" and you should see "Press any key to enter the REPL. Use CTRL-D to reload." Press enter or any key to enter the REPL. You should see something like:
Code: Select all | TOGGLE FULL SIZE
Adafruit CircuitPython 3.0.3 on 2018-10-10; Adafruit CircuitPlayground Express with samd21g18
>>>

To verify the board is working, you can try entering code into the REPL instead of running it from code.py. Try entering the following:
Code: Select all | TOGGLE FULL SIZE
>>> from adafruit_circuitplayground.express import cpx
>>> cpx.red_led = True

See if the red LED next to the USB port turns on.

The REPL can be used for debugging. You can try entering the contents of code.py line by line and see if it errors at some point.

If there are code-stopping errors, I would expect that there would be some kind of error thrown to the serial console which would look something like this:
Code: Select all | TOGGLE FULL SIZE
Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
Traceback (most recent call last):
  File "code.py", line 7
SyntaxError: invalid syntax

My suggestion would be to strip down the code to something super basic and make sure that the setup is working successfully. Try the same board on a different computer where you have already had success saving code to a CPX. Try a board from a successful setup on one of the unsuccessful machines to start to narrow down where the issue is. CircuitPython does provide feedback when things aren't working successfully, and it seems like your students are not getting to that point for some reason. The CPX does not have a status LED on it to tell you when your code isn't running properly, so you have to rely on the serial output to let you know.

Also, be aware, code from code.py, etc. will not run if you have entered the REPL. So if you are seeing the ">>>" prompt in the serial panel of Mu, saving the code won't change the state of the board. Make sure that your students haven't accidentally entered the REPL and are trying to run code while it's active. Pressing ctrl+d will reload the serial console and exit the REPL if it is active.

Hopefully this gives you a place to start!
Kattni

kattni
 
Posts: 29
Joined: Fri Aug 18, 2017 6:33 pm

Re: Debugging CircuitPython on CPX

by gregorygraham on Thu Nov 08, 2018 1:50 pm

Thanks for the help. We are using code.py, and his code does run when he gets the errors corrected, but he appeared to be getting no error messages in the Mu serial console. However, now I've decided that his problem is that the console windows is so small that the error messages scrolled out of the window, and he didn't know to scroll up to find them. I have since tested his CPX, and it does indeed output error messages to the serial console. Thanks again!

gregorygraham
 
Posts: 2
Joined: Fri Dec 20, 2013 4:03 pm

Re: Debugging CircuitPython on CPX

by kattni on Thu Nov 08, 2018 4:39 pm

I've added a section to our CircuitPython Troubleshooting page about this issue. Thank you for bringing it to our attention!

https://learn.adafruit.com/welcome-to-c ... hing-18-13

I should also have mentioned that we have a huge Troubleshooting page that also has things that may help you in the future.

Happy hacking!

kattni
 
Posts: 29
Joined: Fri Aug 18, 2017 6:33 pm

Please be positive and constructive with your questions and comments.