Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #######Code 1(PLOTTING LIVE ) :
- import os
- import time
- from time import sleep
- from datetime import datetime
- #from scipy import stats
- #CODE FOR TEMP SENSOR DATA PLOTTING USING MATPLOT
- import matplotlib as mpl
- #mpl.use('tkagg')
- import matplotlib.pyplot as plt
- import numpy as np
- import serial
- DATA_SIZE = int(input('enter data size needed')) # Python 3
- temperature = np.zeros(DATA_SIZE, dtype=np.float32)
- index = 0
- samples = range(0, DATA_SIZE)
- sr=0
- fig = plt.figure()
- ax = fig.gca()
- #ax.set_ylim([0, 60])
- #ax.set_xlim([0, 24])
- #ax.set_title('Real-Time Temperature Monitoring')
- #x.set_xlabel('Time->')
- #.set_ylabel('Temperature(C)->')
- print(temperature)
- # Enable Interactive Plotting, use plt.ioff() to turn off
- plt.ion()
- # Open Serial Port
- ser = serial.Serial('/dev/ttyACM0',baudrate=9600,timeout=10
- ,parity=serial.PARITY_NONE,
- stopbits=serial.STOPBITS_ONE,
- bytesize=serial.EIGHTBITS
- )
- print(ser)
- file = open("/home/pi/Desktop/New2.csv","w")
- file.write("Sr.No.,time,Sensor1n")
- try:
- print ('run')
- while True:
- temp = ser.readline()
- print(temp)
- if index < DATA_SIZE:
- # Convert String to a Number
- temperature[index] = float(temp)
- print(temperature)
- # Clear the Old Plot from Screen
- ax.cla()
- #ax.grid(True)
- minor_x=np.arange(0,24,0.5)
- major_x=np.arange(0,24,1)
- ax.set_xticks(major_x)
- ax.set_xticks(minor_x,minor=True)
- ax.grid(which='both')
- ax.set_ylim([0, 60])
- ax.set_title('Real-Time Temperature Monitoring')
- ax.set_xlabel('Time ->')
- ax.set_ylabel('Temperature$^circ$(C) ->')
- ax.plot(samples,temperature,'b')
- plt.pause(0.01)
- index +=1
- sr=sr+1
- now = datetime.now()
- print(index)
- file = open("/home/pi/Desktop/New2.csv","a")
- file.write(str(sr)+","+str(now)+","+str(temp)+"n")
- file.flush()
- time.sleep(1)
- else:
- index = 0
- finally:
- plt.ioff()
- plt.show()
- ser.close()
- import tkinter as tk
- # -----
- import sys
- class StdoutRedirectorLabel(object):
- def __init__(self, widget):
- self.widget = widget
- # clear at start because it will use +=
- self.widget['text'] = ''
- def write(self, text):
- # have to use += because one `print()` executes `sys.stdout` many times
- self.widget['text'] += text
- import subprocess
- def callback1():
- import coderun
- coderun.function1()
- def callback2():
- import coderun
- ## # keep original `sys.stdout
- old_stdout = sys.stdout
- ##
- # redirect to class which will add text to `lbl`
- sys.stdout = StdoutRedirectorLabel(lbl)
- ##
- ## # it will execute only `function2` and assign result to Label (with ending "n")
- coderun.function2()
- # set back original `sys.stdout
- sys.stdout = old_stdout
- def callback3():
- import coderun
- # keep original `sys.stdout
- old_stdout = sys.stdout
- # redirect to class which will add text to `lbl`
- sys.stdout = StdoutRedirectorLabel(lbl)
- # it will execute only `function3` and assign result to Label (with ending "n")
- coderun.function3()
- # set back original `sys.stdout
- sys.stdout = old_stdout
- # --- main ---
- master = tk.Tk()
- master.geometry('1000x800')
- lbl = tk.Label(master, text='')
- lbl.pack()
- btn1 = tk.Button(master, text=" Temperature sensor ", command=callback1)
- btn1.pack()
- btn2 = tk.Button(master, text="Sensor2", command=callback2)
- btn2.pack()
- btn3 = tk.Button(master, text="Sensor3", command=callback3)
- btn3.pack()
- master.mainloop()
Add Comment
Please, Sign In to add comment