Reader small image

You're reading from  MicroPython Projects

Product typeBook
Published inApr 2020
Reading LevelExpert
PublisherPackt
ISBN-139781789958034
Edition1st Edition
Languages
Right arrow
Author (1)
Jacob Beningo
Jacob Beningo
author image
Jacob Beningo

Jacob Beningo is an independent consultant who specializes in microcontroller-based embedded systems. He has advised, coached, and developed systems across multiple industries, including the automotive, defense, industrial, medical, and space sectors. Jacob enjoys working with companies to help them develop and improve their processes and skill sets. He publishes a monthly newsletter, Embedded Bytes, and blogs for publications about embedded system design techniques and challenges. Jacob holds bachelor's degrees in electrical engineering, physics, and mathematics from Central Michigan University and a master's degree in space systems engineering from the University of Michigan.
Read more about Jacob Beningo

Right arrow

A Custom Debugging Tool to Visualize Sensor Data

The greatest challenge that every embedded software developer faces is troubleshooting their embedded system. When I speak at conferences such as Embedded World, the Embedded Systems Conference, and Arm TechCon, or when I've polled registrants and attendees to my courses and newsletter (Embedded Bytes), on average, developers spend 40% of their time debugging their software. If the average project length is 12 months, that's as much as a year being spent on failure work!

The ability to debug and visualize what an embedded system is doing can help to decrease the amount of time we spend debugging by providing developers with critical system information and a way to easily see what the software is doing. In this chapter, we will build a tool in Python that allows us to visualize what our MicroPython-based embedded system...

Technical requirements

The example code for this chapter can be found at the following GitHub location: https://github.com/PacktPublishing/MicroPython-Projects/tree/master/Chapter06.

In order to run the examples, you will want to have the following hardware and software available:

  • A MicroPython supported development board
  • A UART to USB converter
  • A host machine running Python 3.x
  • A terminal application (such as PuTTy, RealTerm, Terminal, and many others)
  • A text editor such as Sublime Text

Debugging and visualizing embedded systems

A picture is worth a thousand lines of code. As developers, we live in a world of 1s and 0s, registers, peripherals, and scripts that are all interacting with each other and the world around them. Understanding what our software is doing, or the sensors that are connected to it, can dramatically help us develop the system faster but also help us see what the system is doing and better understand how our software is executing.

If you do a search on Google for serial communication plotting, you'll find over a million pages that cover tools such as Mbed's Serial Port Plotter, MegunoLink, ArduinoPlot, and so on. Some of these tools are free, while some require a license that costs less than $50. However, you will find that despite so many options, there really isn't one tool that gives you the flexibility and scalability that...

Visualizer requirements

The main purpose of this project is to develop the base code that can be used to receive data coming from a MicroPython board over a serial port and parse that data so that we can then graph it in real time. There are two main areas that we need to consider the requirements for: hardware and software.

Hardware requirements

The visualizer that we are going to be designing is going to be strictly a software development project. There are no hardware requirements for the project. However, there are a few general recommendations.

First, you can use any MicroPython board that you are interested in. While you can select any board, the example project will be written assuming that you are going to use the...

Visualizer design

At this stage in the project, we've discovered what the requirements for the project are. We are now going to develop a hardware and software architecture. The best way to picture an architecture is through a map that is general enough to provide directions on where we need to head but does not provide enough details regarding how to restrict how we get there. The architecture should be flexible so that we can deal with any changing requirements on the fly.

For our purposes, we will use this section to explore the high-level architecture and then develop the detailed design that we can construct the project in the next section

The visualizer hardware architecture

As we mentioned earlier, there are two...

Constructing the visualizer

The visualizer is going to leverage existing libraries in order to minimize the effort that is required to receive and plot data. There are two libraries that are particularly interesting for this project: pySerial and Matplotlib.

pySerial is a Python module that encapsulates all the features and functions that are needed to interact with a serial port. pySerial can run on multiple operating systems, such as Windows, Mac OS X, and Linux, to name a few. It includes a module called serial that provides useful features for interacting with the serial port, such as the following:

  • Open
  • Close
  • Send
  • Receive

Matplotlib is a Python library that provides the functionality for 2D data plotting, which can be used interactively or can be used for publication-quality figures. The number and types of charts that can be created are quite extensive. For our purposes...

Running the visualizer

Now, we are ready to test the visualizer, which will require several steps. These steps include doing the following:

  1. Starting the MicroPython application
  2. Identifying the COM port the data is received on the host
  3. Starting the visualizer on the host

First, set up and run the MicroPython script. When you connect to your MicroPython Terminal, you should see that the Terminal is displaying information similar to what can be seen in the following screenshot. As we can see, the transmission of sensor data packets over UART is successful:

Next, we can identify which communication port the USB to serial adapter is using. The format for the serial port will vary, depending on the operating system that you are using, along with how you found the COM port. I am using a Microsoft Windows machine, and my USB to serial adapter is currently COM5.

This brings us to the...

Summary

In this chapter, we explored how we can use the UART and on-board a MicroPython board to transmit a stream of data that provides us developers with insights into how our system is behaving. We created a stream of known data that represented temperature and humidity data to verify that a host-based visualizer application was able to parse the data stream and then plot the data in real time. With this communication loop closed, we now have the ability to plot important information such as sensor values, but we can also transmit trace data from our MicroPython application and then plot it to get a better understanding of how our applications are behaving and use this information to troubleshoot our applications.

In the next chapter, we are going to look at how we can integrate motion and gesture detection sensors and interact with them using MicroPython to control a robot...

Questions

  1. What files are used to modify what USB classes are supported on startup by the MicroPython board?
  2. What are some reasons we would use generated data in our development rather than a live sensor?
  3. At what chart refresh rate does the user interface start to become sluggish?
  4. What are some reasons for using the MicroPython UART for communication over using the USB?
  5. What Python function is used to convert a floating-point number into a string?
  6. What module is used to create command-line arguments?
  7. What are some new features that could be added to the visualizer to enhance its capabilities?

Further reading

lock icon
The rest of the chapter is locked
You have been reading a chapter from
MicroPython Projects
Published in: Apr 2020Publisher: PacktISBN-13: 9781789958034
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Jacob Beningo

Jacob Beningo is an independent consultant who specializes in microcontroller-based embedded systems. He has advised, coached, and developed systems across multiple industries, including the automotive, defense, industrial, medical, and space sectors. Jacob enjoys working with companies to help them develop and improve their processes and skill sets. He publishes a monthly newsletter, Embedded Bytes, and blogs for publications about embedded system design techniques and challenges. Jacob holds bachelor's degrees in electrical engineering, physics, and mathematics from Central Michigan University and a master's degree in space systems engineering from the University of Michigan.
Read more about Jacob Beningo