MAS.863/4.140/6.9020
Lleyton Elliott

Return to Assignments page | Return to main page

Week 11 Assignment: Design, Build, and Connect Wired or Wireless Node(s) with Network or Bus Addresses and a Local Interface

With a few weeks left to go in the semester, my plan is to try to have my final project's code base finished by the final sprint week. That means I need to have the microcontroller/sensor code, the communication protocol, and the interface/data display done. This week was focused on the first two: I cleaned up my sensor code from the past couple of weeks, and set up a way to stream the sensor data to my laptop to be used in the final interface.

Though I was familiar with microcontrollers and specifically the Arduino IDE coming into the class, I had minimal experience with using them to communicate. Thus, I wanted to have a relatively simple communication protocol, which would be fine given that my project has one sensor that needs to constantly stream data. I knew I wanted to start by using Python, and I knew the pySerial library was pretty much ubiquitous for serial communication with Python. I wrote up a quick bit of Python code based on the pySerial documentation:


			import serial
			import time

			ser = serial.Serial('COM5', 115200, timeout=0)
			s = 0
			count = 0

			while count < 100:
				if ser.in_waiting:
					s = int(ser.readline())
				print(s)
				time.sleep(0.1)
				count += 1
		

There were only a couple decisions to make with this code. My RP2040 sensor code printed to serial port COM5 at 10Hz at a baud rate of 115200, so the serial setup and time delay were easy. Also, I thought I'd have to flush the serial buffer at first, but I learned that Serial.println() in C++ flushes and writes a new line to the serial buffer by itself, which was quite convenient. I made this code run for 10 seconds (100 iterations of a 0.1 second loop), but it will likely be indefinite for my final project.

There was only one problem with my code. Occasionally, my code would throw an error that read "ValueError: invalid literal for int() with base 10: '\r\n'". I looked this error up and found that Serial.println() includes "carriage return" and "line feed" escape sequences in order for it to display data line-by-line in the serial monitor. I didn't want to change my microcontroller code, as I anticipated using the serial monitor much more to calibrate and debug and Serial.println() made it easy to read. So, I included an exception in case my Python program tried to cast a string of serial data without any numbers as an integer:


			import serial
			import time

			ser = serial.Serial('COM5', 115200, timeout=0)
			s = 0
			count = 0

			while count < 100:
				if ser.in_waiting:
					try:
						s = int(ser.readline().decode('utf-8'))
					except:
						s = s
				print(s)
				time.sleep(0.1)
				count += 1

This worked like a charm! I was seeing numbers between 0 and 255 that perfectly corresponded to the amount of force with which I pressed down on my sensor. I also experimented with making my sensor electrodes smaller so as to make the raw data from the analog sensing small enough to fit inside an int, thus making it much easier to just map that number to an analog signal. I will have more on my modified sensor design in my final project page.

Design Files

Serial Communication Python Script: serial_1.py

Thoughts, Lessons, & Takeaways:

I'm glad my communication protocol was a success, albeit basic. As I have multiple other final projects to focus on right now, I'm still definitely glad to be chugging along in HTMAA and reducing the amount of work I need to do in crunch time in a couple weeks. Not too much to take away from this week but I'm definitely more comfortable with serial communication and I'm ready to tackle the remainder of the projects!