I've just recently started learning Python and wrote this code to plot temperature data coming from an Arduino Uno over serial to a Raspberry Pi. I've been researching and trying different methods to try and stop the memory leak, but nothing seems to work. I'm not sure if the issue is with plotting or collecting the data. I've slowed down the accumulation, but at this point I need help figuring out what I have done wrong. Any help that can be provided is great.
import re
import datetime
import pyqtgraph as pg
from PyQt5 import QtCore,QtGui
import numpy as np
import serial
import csv
ser = serial.Serial('/dev/ttyACM0', 9600)
teml = []
numeric_const_pattern = r'[-+]?(\d+\.\d+|\d+)'
def parser(command):
first_bit = re.findall(r'\b[A-Za-z]+',command)
command_value = re.findall(numeric_const_pattern,command)
decision(first_bit, command_value)
def decision(letter, number):
cmd_bits = 'Temp', 'X', 'A'
if cmd_bits[0] in letter:
Temperature_plotting(number[0])
del number[:]
else:
pass
def Temperature_plotting(temperature):
ctim = datetime.datetime.now().time()
resetim = datetime.time(0, 0, 10, 0)
teml.append(float(temperature))
if len(teml) >= 1000:
with open('daily_temp_data.csv', 'a') as csvfile:
fieldnames = ['Date Stamp','Max','Min']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
x = datetime.date.today()
max_temp = max(teml)
min_temp = min(teml)
writer.writerow({'Date Stamp': x,'Max': max_temp,'Min': min_temp})
del teml[:]
return(teml)
pw = pg.plot(clear=True)
pw.setLabel('left', text='Temp', units='F')
pw.setLabel('bottom', text='Time', units='n*5sec')
pw.setTitle('Temperature')
pw.showGrid(True, True)
pw.showLabel('left', True)
pw.showLabel('bottom', True)
pw.showLabel('top')
pw.setYRange(20, 100, padding=0)
while True:
if ser.inWaiting:
cmd = str(ser.readline())
parser(cmd)
ser.flushInput()
pw.plot(teml[:-1], clear=True)
pg.QtGui.QApplication.processEvents()