Python Forum

Full Version: Real time graph of the temperatures and storage
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
import serial                         # import Serial Library
import numpy as np                      # Import numpy
import adafruit_max31856
import matplotlib.pyplot as plt               #import matplotlib library
from drawnow import *
import datetime
import math
import csv



T1= []
T2=[]
plt.ion()
cnt=0
 
##def animate(line):
##    pullData = open('C:\\Users\\Alphinity\\Desktop\\python3\\signalSerial.txt','r').read()
##    dataArray = pullData.split('\n')
##    xar=[]
##    yar=[]
##    for eachLine in dataArray:
##        if len(eachLine)>1:
##            x,y = eachLine.split(',')
##            xar.append(float(x))
##            yar.append(float(x))
##
##    ax1.plot(xar,yar)
##ani = animation.FuncAnimation(fig,animate, interval=100)

def makeFig(): #Create a function that makes our desired plot
    plt.ylim(80,90)                                 #Set y min and max values
    plt.title('My Live Streaming Sensor Data')      #Plot the title
    plt.grid(True)                                  #Turn the grid on
    plt.ylabel('Temp T1')                            #Set ylabels
    plt.plot(T1, 'ro-', label='Degrees F')       #plot the temp1
    plt.legend(loc='upper left')                    #plot the legend
    plt2=plt.twinx()                                #Create a second y axis
    plt.ylim(93450,93525)                           #Set limits of second y axis- adjust to readings you are getting
    plt2.plot(T2, 'b^-', label='Degrees F')           #plot temp2 data
    plt2.set_ylabel('Degrees F')                    #label second y axis
    plt2.ticklabel_format(useOffset=False)            
    plt2.legend(loc='upper right')                  #plot the legend

    
## user-defined params

serialPort = 'COM3'      ## the serial device
path = 'C:\\Users\\Alphinity\\Desktop\\python3\\'             ## the output file path
outputFile = "signalSerial.csv"  ## the output file name


ser = serial.Serial('COM3', 115200)
outputFile = path + outputFile


f = open(outputFile,'w')            #create and/or open a file and give the command to write the informations
f.write(outputFile)                 #write inside the file created
f.close()                           

print ("Writing the serial stream into file: " + outputFile)
print (" [to see the stream: tail -f '+outputFile+' ]")
print (" [to exit: ctrl+c (the elegant way :) ]")

while True:                                         # While loop that loops forever
    line = ser.readline()
    string = line
    T1 = float(string[0])                 #Convert first element to floating number and put in T1
    T2 = float(string[1])                   #Convert second element to floating number and put in T2
    T1.append(T1)                            
    T2.append(T2)
    drawnow(makeFig)
    plt.pause(.000001)
    cnt=cnt+1
    if(cnt>50):                             
        T1.pop(0)                       
        T2.pop(0)
    if (string != ''):
      f = open(outputFile, 'a')
      f.write(str(string))
      f.write("\n")
      f.close()
      #plt.show(string.decode())
      #print (string)                      ## uncomment to see the values in the terminal
      print(string.decode())               ## ctrl+c to stop the code

valueRead.decode().strip()
-----------------------------
I'm getting always this error, I need to show in real time a graph and save in a file everytime new.

Error:
T1.append(T1) #Build our tempF array by appending temp readings AttributeError: 'float' object has no attribute 'append' ----------------------
Thank you so much for how can help me
T1 and T2 are floats, not lists. You are trying to append a value to floats, this is why the error raises.
At least, try to replace
T1 = float(string[0])                 #Convert first element to floating number and put in T1
T2 = float(string[1])                   #Convert second element to floating number and put in T2
T1.append(T1)                            
T2.append(T2)
with
T1.append(string[0])                            
T2.append(string[1])
(Aug-28-2019, 05:18 PM)linkxxx Wrote: [ -> ][python]

T1= []
T2=[]
plt.ion()
cnt=0

T1 = float(string[0]) #Convert first element to floating number and put in T1
T2 = float(string[1]) #Convert second element to floating number and put in T2
T1.append(T1)
T2.append(T2)

You are confusing Python compiler by using same variable name. Try using different variable name to store the float value and try append to T1 and T2 list
Thank you so much to all for the answer, I found the problem, and now I'm getting both the temperatures, only the strange thing is that are not the measurements, because the temperature is around 25 degrees, and on the graph appear 40-45, I'm attaching the code if someone maybe know the answer https://pastebin.com/DCiXqcZG .