Is it possible to communicate values to a microbit through a serial port using python?

158 Views Asked by At

Unlike the other questions that you may recommend, I am not trying to get the microbit to send me information, I am trying to send information to the microbit.

My goal here is to send through three integers in a list to a microbit, specifically through the serial port as I am trying to send these variables as quickly as possible.

I have absolutely no idea what I am doing here, as all the microbit guides I could find only went through taking information from a microbit.

I have tried going into TeraTerm and adjusting variables from there, but the microbit seems to be unable to understand when the variables have been changed through the TeraTerm program. For example:

In microbit:

serial.redirect_to_usb()
colour == 0
if colour == 1:
display.show(display.HAPPY)

In Tera Term, I enter:

colour = 1

Nothing happens. However, when I type:

colour == 1

It responds with "True".

Is there any way to send my variables to my microbit through python? I don't just mean through TeraTerm, but through python actively automating the task.

I am currently using python 3x, and the latest version of TeraTerm.

2

There are 2 best solutions below

1
Oppy On BEST ANSWER

This micropython code enables a micro:bit v1 to respond to key presses (a, b, c) made on a serial terminal on a PC which is connected to the micro:bit. I use the tio serial terminal. I programmed the micro:bit using the online editor here: https://python.microbit.org/v/3

from microbit import *


# Code in a 'while True:' loop repeats forever
display.show(Image.CONFUSED)
while True:
    data = uart.readline()
    if data == b'a':
        display.show(Image.ARROW_E)
    if data == b'b':
        display.show(Image.ARROW_N)
    if data == b'c':
        display.show(Image.ARROW_W)
    sleep(500)

The data from the serial port is received as bytes, so you need to specify using the syntax:

b'a'

to identify the 'a' character sent from the serial terminal.

0
nekomatic On

The responses you got show that you are successfully communicating with the Microbit. You've connected the Microbit's Python prompt, a.k.a. the REPL (read-evaluate-print loop), to your terminal, and anything you type there is executed by the Microbit. Your first command

colour = 1 

set the variable colour to 1, which doesn't return any response, and your second command was the expression colour == 1 which the Microbit evaluated and told you was True.

Your problem is that your code

if colour == 1:
    display.show(display.HAPPY)

only executed once, when you originally ran it. At that point colour was 0, so the if block wasn't called. What you're after is a way of running that code again each time you change the parameter.

The easiest way to do what you want is probably to define a function which you can call:

def update(colour):
    if colour == 1:
        display.show(display.HAPPY)

Now at the terminal you can do

update(1)

and the display will get updated. You can expand this update function to take more parameters and take more actions according to their values, and/or define additional functions to do different things.

Note that the variables in these functions won't be stored between one call and the next. If you need to store something during one call and read it back during a later one then you'll need to use a global variable.

If you want the Microbit to continuously carry out some action, but listen at the serial port for new data and update the action whenever that data arrives, that's a bit more complex and I think you would need to use the Microbit uart object. If that's so, please ask another question describing specifically how you need it to operate.