$USD
  • EUR€
  • £GBP
  • $USD
TUTORIALS OBLOQMicroPythonmicro:bit

UART OBLOQ IoT Module Tutorial 12 uPython: Getting firmware version of UART OBLOQ

DFRobot Dec 19 2018 1029

Introduction

In this tutorial we will check how to get the UART OBLOQ firmware version, using the micro:bit board and uPython to send commands to the device.

For an introductory tutorial that contains the wiring diagram between the micro:bit board and the UART OBLOQ, please consult this previous post. For a detailed explanation about the get firmware version command, please check here.

Note that, for this tutorial, I’m using version 1.7.0 of uPython for the micro:bit board. So, like already covered in the previous tutorial where we sent a ping to the UART OBLOQ, the micro:bit will send a dummy byte with the value 0 when we initialize the serial interface.

After receiving the dummy byte, the UART OBLOQ will start building a command that doesn’t exist, which means we have to first flush this wrong command, and only after that send the actual command to get the firmware version.

Nonetheless, in more recent versions of uPython for the micro:bit, this issue is already solved. So, if you are using a version that already has the fix, you can skip the part of the code where we flush the command.

The code

The first thing we are going to do is importing the functionality we will need from the microbit module. We will import the uart object and the sleep function.

from microbit import uart, sleep

Then, we will call the init method on the uart object, in order to initialize the serial communication interface.

We will use a baud rate of 9600, which is the one used by the UART OBLOQ. Additionally, we will set the serial Tx to pin 0 and the Rx to pin 1 of the micro:bit.

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

Then, since micro:bit sends a garbage character to the serial port after we call the init method, we will first send a “\r” character to the UART OBLOQ, so we flush any previous non-valid command it could be trying to build.

After that, we will wait for 1 second and try to read 10 characters from the serial port. Note that these are more characters than the expected answer, which should be |1|-1|, but we do it as a safeguard.

This should cause no problem because the number passed as input of the read method means that it should return at most that many characters [1], but the method call will return the number of bytes available so far after a timeout, even if they are less that the specified. We will store the result of the read method call on a variable, so we can later print it.

Please take in consideration that we are using the delay approach to keep the code simple. In a real scenario application, we should use something more robust, such as implementing a “read until character” function that waits for the “\r” character, which is sent at the end of the UART OBLOQ responses.

uart.write("\r")
sleep(1000)

output1 = uart.read(10)

Now that we have flushed this garbage content, we can take care of sending the command to get the firmware version of the UART OBLOQ. The command to send is |1|2|, as covered here.

As before, we will wait for 1 second and then try to read 10 bytes with the read method of the uart object, storing the result in a variable.

uart.write("|1|2|\r")
sleep(1000)

output2 = uart.read(10)

To finalize, we will re-initialize the micro:bit serial connection without passing the Tx and Rx pin values, in order to bring back the Python console [1]. As baud rate, we should use the value 115200.

uart.init(baudrate=115200)

After that, we can print the values of both output1 and output2 variables. The first one should contain the string |1|-1| because, as already said, it is only used to flush the byte that micro:bit writes to the serial port after we call the init method.

The second variable should have the firmware version, in the following format:

|1|2|version|

The final source code can be seen below.

from microbit import uart, sleep

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

uart.write("\r")
sleep(1000)

output1 = uart.read(10)

uart.write("|1|2|\r")
sleep(1000)

output2 = uart.read(10)

uart.init(baudrate=115200)
print (output1)
print (output2)

Testing the code

To test the code, save the file and upload it to your micro:bit board, assuming that all the connections between this board and the UART OBLOQ are already done. After the execution of the commands, you should get an output similar to figure 1.

As can be seen, the first line corresponds to the |1|-1| expected response, since we are only flushing the byte sent by micro:bit, which corresponds to no command on the UART OBLOQ. Thus, the UART OBLOQ responds back with |1|-1|, simply indicating the command was not recognized.

The second line corresponds to the UART OBLOQ firmware version command response. As can be seen, for my device, the firmware version is 3.0.

Printing the UART OBLOQ firmware version with uPython and micro:bit

Figure 1 – Output of the program, tested on the uPyCraft IDE.

UART OBLOQ module Tutorials:

REVIEW