Setting up a serial data stream in MicroPython

As we saw in the hardware architecture for this project, we are going to be using a serial port on the MicroPython device to send a stream of known data to the visualizer to plot. Once we've tested and debugged both sets of code, we can update our MicroPython device to send real sensor data. For now, we just need to send known values that we can use for testing:

  1. Before we do anything, we need to make sure that we import the Universal Asynchronous Receiver/Transmitter (UART) module from pyb and set the emergency exception buffer size. Just like we did in previous projects, this can be done using the following code:
import micropython                      # For emergency exception buffer
from pyb import UART
# Buffer for interrupt error messages
micropython.alloc_emergency_exception_buf(100)
  1. Next, we need to determine which UART we selected to communicate with the serial to UART converter. This can be done by reviewing the hardware architecture and board schematics. For the STM32F475 IoT board, the D0 and D1 pins, which are the UART transmit and receive pins, correspond to UART4. To initialize these pins as a UART functionality and to set the baud rate to 115200, the following lines of Python code can be written:
# Create a uart object, uart4, and setup the serial parameters
uart4 = UART(4, 115200)
uart4.init(115200, bits=8, parity=None, stop=1)
  1. Next, we want to create a few variables that will be used to track the sample time and the data that will be sent to the visualizer. In this case, we want to keep track of the following variables:
    • Time
    • Temperature
    • Humidity

These variables can be declared as floating-point variables, as follows:

# Create variables to store time, temperature and humidity
Time = 0.0
Temperature = -20.0
Humidity = 34.5
  1. The main loop for the code has a few steps that it needs to perform, which include the following:
    1. Update the time
    2. Update the temperature
    3. Update the humidity
    4. Create the sensor string data to send
    5. Send the latest sensor data

The test loop should run at a frequency of 1 Hz, which means that the main program loop will look as follows:

while True:
# Update Time
# Update Sensors
# Create string data
# Send sensor data
pyb.delay(1000)
  1. The code necessary to update the time is nothing more than incrementing the time variable by one. However, the temperature and humidity data are going to be a little more complicated. After all, we don't want to just send any old thing. For the temperature data, we'll start incrementing the temperature throughout each loop by 1 degree until it reaches +20, where it will then turn around and return to -20. The code to pull this off is as follows:
# Update Temperature
if TempDir == 1:
Temperature = Temperature + 1
if Temperature >= 20:
TempDir = 0
else:
Temperature = Temperature - 1
if Temperature <= -20:
TempDir = 1
  1. For the humidity, we do something very similar, except that our variable names and the boundary conditions for the humidity will be 25 on the low end and 35 on the high end. The code necessary to generate this behavior is as follows:
  #Update Humidity
if HumidDir == 1:
Humidity = Humidity + 0.5
if Humidity >= 35:
HumidDir = 0
else:
Humidity = Humidity - 0.5
if Humidity <= 25:
HumidDir = 1
  1. There are many different formats that we could use to send the sensor data to the visualizer application. The simplest is to send the data with the following format:
Chart for the chart, time stamp, sensor data
  1. If we wanted to be super robust, we could also wrap this data in a packet format with sync characters, opcode, data packet size, and a checksum. For the most part, this would be overkill for a simple data visualizer that is used for debugging. We can prepare the data and send it as a string by converting the floating-point values into strings and concatenating them together, as follows:
  # Create string data
TemperatureDataString = '1,' + str(Time) + ',' + str(Temperature) +' '
HumidityDataString = '2,' + str(Time) + ',' + str(Humidity) +' '
  1. The strings are then easy to transmit through the uart4 serial object by writing the following:
  # Send sensor data
print(TemperatureDataString)
uart4.write(TemperatureDataString)
print(HumidityDataString)
uart4.write(HumidityDataString)

Notice that right before we transmit each packet, we print what we are sending to the Terminal. This makes it so much easier to see what the application is doing and will provide useful information in case things don't go as expected. At this point, if you were to run the Python code and connect to the terminal, you would see a sequence of strings being printed, which looks similar to what's being shown in the following screenshot:

Here, we are seeing the Terminal output for the MicroPython code, which is simulating sensor data and transmitting it to the visualizer application.

We are now ready to collect this sensor data from the host serial port (COM port) and plot it in real time.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.152.38