The micro:bit code

We will start the code by importing the uart object and the sleep function from the microbit module.

from microbit import uart, sleep

Then, like we did on the previous tutorial, we will define a readUntil function that reads characters from the serial port until a specific character is received. As output, the function returns the content read until the character was found.

This function will help us handling the response to commands sent to the UART OBLOQ.

After this, we will initialize the serial interface to work with pins 0 and 1 from the micro:bit board. After this, the MicroPython prompt will become unavailable until we re-enable it again.

uart.init(baudrate=9600, tx = pin0, rx = pin1)

Then, we will take care of flushing the dummy byte that is sent to the serial port after we initialize the serial interface. Note however that this issue only happens in older versions of MicroPython, such as 1.7.0 (the one I’m using). In newer versions that no longer have this issue, we can skip this procedure.

uart.write("\r")
readUntil(uart, '\r')

Next, we will send the command to connect the UART OBLOQ to the WiFi network and wait for the procedure to finish, leveraging again the readUntil function. You can read in more detail about the WiFi connection procedure here.

Finally, after the UART OBLOQ is connected to the WiFi network, we will send the GET request. You can read more about the details of the command we need to send on this tutorial. To sum up, the command takes the format shown below.

|3|1|destinationURL|

The destination URL has the following format, where you should change #yourFlaskMachineIp# by the IP of the machine that is hosting the Flask server:

http://#yourFlaskMachineIp#:8090/get

You can check below the MicroPython code to send this command:

uart.write("|3|1|http://192.168.1.83:8090/get|\r")

The UART OBLOQ answers to the command in the following format:

|3|HTTPResponseCode|ResponseBody|

So, we will leverage again the readUntil function to wait for the ‘3‘ character to be sent to the serial port. We don’t need to store the result of this function call in a variable since this part of the command response doesn’t contain any useful information.

Then, we will call the function again, now to wait for the “\r” character, which signals the end of the UART OBLOQ command response. This time, we will store the readUntil function response, which contains the HTTP response code and body.

readUntil(uart, '3')
result = readUntil(uart, '\r')

To finalize, we will re-enable the Python prompt and then print the request response we have obtained and stored in a variable. The final code can be seen below.

Testing the code

To test the whole system, first run the Flask server using a Python tool of your choice. I’m using IDLE, a Python IDE.

Then, after connecting the micro:bit to the UART OBLOQ and powering both devices, simply run the previous MicroPython script. You should get an output similar to figure 1 after the script finishes the execution.

As can be seen, the code 200 was returned by the server, thus indicating success. Also, the body of the response corresponds to the “Received” string we have defined on Flask.

Figure 1 – Response of the GET request.

If you go back to the tool where you are running the Flask server, you should see the headers of the request getting printed.