Recording and graphing temperature data using Matplotlib and edge computing

Arnav Shah
The Startup
Published in
3 min readMay 21, 2020

--

Photo by Isaac Smith on Unsplash

A case for edge computing

Edge computing is often frowned upon by Internet of Things (IoT) developers. Although the practice reduces latency of data aggregation, it is extremely resource-demanding and prevents centralized data processing and collection.

That being said, edge computing allows front-end programmers to perform data processing with the tools that they are already comfortable with. Simply put, edge computing drastically facilitates the data pipeline for a much broader range of developers than status-quo methods.

This is all the more true for novice developers that are just beginning to dip their feet into the field. For people who haven’t mastered full stack development, edge computing is a lot more forgiving and almost always the desirable path.

On top of providing an easier production experience, edge computing allows developers to utilize libraries and packages that they’re already familiar with, giving a whole new level of power and functionality to code.

For the reasons above, I decided to build a simple IoT edge computing application that utilizes a Raspberry Pi and Matplotlib to graph temperature data. Without any further ado, let’s dive into how I did it.

The code

import os
import glob
import time
from matplotlib import pyplot as plt
import re
x = []
y = []
for number in range(1, 61):
x.append(number)
os.system(‘modprobe w1-gpio’)
os.system(‘modprobe w1-therm’)
base_dir = ‘/sys/bus/w1/devices/’
device_folder = glob.glob(base_dir + ‘28*’)[0]
device_file = device_folder + ‘/w1_slave’
pattern = re.compile(r'\d\d\d\d\d')
def read_temp():
with open(device_file, 'r') as file:
sentence = file.read()
mo = re.search(pattern, sentence)
return int(mo.group()) / 1000
while len(y) <= 60:
y.append(read_temp())
plt.plot(x, y)
plt.xlabel('Last 60 checks')
plt.ylabel('Temperature (°C)')
plt.title('Temperature')
plt.show()

note that I only recorded 60 readings for demo purposes. If you have enough RAM, it’s possible to collect more than a week’s worth of data without sacrificing performance.

Initializing our thermal sensor

Firstly, we declare our variables x and y. These are lists that will later be used by Matplotlib to plot a broken line graph. Next, we call os.system and we declare a bunch of variables that point to various directories and files. Don’t worry too much about these, all they do is basically initialize thermal reading capabilities for our temperature sensor. With that out of the way, we can move onto the actual logic.

Reading the temperature

We commence by declaring a regex pattern variable that looks for 5 consecutive digits. Next, we define a function that opens our w1_slave file from before, stores the bytes of its output in a variable called sentence , and then searches the file for our pattern that we defined earlier as pattern. The five digit number is simply a highly precise Celsius reading. By dividing it by 1000, we get the normal Celsius temperature as a decimal value.

Creating our graph

Next, we call read_temp and append the Celsius temperature value to our y list until it has an amount of readings equivalent to our x list (60 in my case). Finally, we plot the graph using our x and y axes, and give the graph its title and subtitles.

Well, that’s all from me. I post articles about Augmented Reality, the Internet of Things and all exponential technologies in between. If you’re interested in that sort of thing, please consider following me here. Thank you for reading!

--

--

Arnav Shah
The Startup

14 y/o working on PCV13 distribution in low-income countries