A small status monitor on STM32

It’s really inconvenient to check your computer status when your application is using full screen. In order to avoid calling task manager in Windows each time I want to see the usage ratio of my CPU and RAM, I decide to make this stuff.

This is how it looks after all:

I have already implemented the display of both CPU ,RAM and GPU usage percentage, together with the temperture of the Graphic Card.


Preparation

The hardware I chose is from 01 Studio. They are selling a board based on STM32F407. For detail, this chip has a Cortex-M4 core with 168 MHz working frequency.

The main reason I choose this board is because it can run Python! Although it is not the full version of Python, it is micro-python. But still, this can reduce massive work if you want to code on embedded device like this.

I highly recommend this board for people who is interesting at coding on embedded device without any experience.


Software Structure

We have to implement two different part of the monitor, where server side is running on your computer and keeps sending data to the client, our tiny monitor in other words.

The communication between the server and the client is depends on the USB line, which is also the power supply for the board.

After the connection is established, we create a loop on server side. The status data will be collected by using the API in Python. Then the data will be packed as a single data packet and send to our board though the USB line. The STM32 board is running a loop as well wo receive data from the open connection and display the data onto our tiny screen.


Code implementation

Easy to say, hard to do. Now let’s create a new file on our computer as server.

Server side

Firstly, we need to established the connection with the STM32 board.

Step one, import the relative library we need.

import serial

Then, announce a global variable for the serial we need for connection.

After preparation, we are ready to open the connection now.

Note: The serial name can be found in device manager in Windows as long as you connect your board with your computer though USB line.

serial = serial.Serial('COM3', 9600)

Check if the connection is valid

if serial.isOpen():
    print('Serial Already Exists')
else:
    print('Serial is invalid')

Now, create a function to get status data from computer.

In this part, we need psutil for CPU and RAM information, GPUtill for GPU status.

import psutil
import GPUtil

The function code:

def get_pc_status():
    for gpu in GPUtil.getGPUs():
        usage = gpu.load
        temp = gpu.temperature
    return "{} {} {} {}".format(psutil.cpu_percent(), psutil.virtual_memory().percent,
                                '%.1f' % (usage * 100), temp)

Now we still need a function to handle the data and send to the serial.

import time


def send_to_stm32(str1):
    serial.write(str1)
    time.sleep(0.9)

Finally, a loop is needed to keep the process executed again and again.

while True:
        send_to_stm32(get_pc_status().encode('utf8'))

Client Side

Now let’s talk about the part for STM32 board. Luckily, this won’t need too much work.

Import the library.

from machine import I2C,Pin
from ssd1306 import SSD1306_I2C

Established the connection to the screen

Note: The protocol used to write data to the screen is I2C

Y8 and Y6 is the pin we select to connect the screen.

i2c = I2C(sda=Pin("Y8"), scl=Pin("Y6"))
oled = SSD1306_I2C(128, 64, i2c, addr=0x3c)

We can check the configuration immediately. Simply call the function show() for the screen.

oled.text("1: LEDS Test",0,0)
oled.show()

If program runs correctly, you will see the text on the tiny screen after you reboot the board

Still, we need to establish the connection to your computer.

vs = pyb.USB_VCP()

Finally, write the loop to handle the data

temp1 = [0,0,0,0]

if len(str(vs.readline())) > 2:
    while 1:
        temp2 = vs.readline()
        if type(temp2) is bytes:
            temp1 = temp2.decode('utf8').split(" ")
            oled.fill(0)
             oled.text("CPU:"+str(temp1[0])+"%", 0, 0)
             oled.text("RAM:"+str(temp1[1])+"%", 0, 15)
             oled.text("GPU0:"+str(temp1[2])+"%", 0, 30)
             oled.text("GPUTemp:"+str(temp1[3])+"C", 0, 45)
             oled.show()
             pyb.delay(500)

2 thoughts on “A small status monitor on STM32”

Leave a Comment

Your email address will not be published.