Guest User

Untitled

a guest
Jan 19th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. #######Code 1(PLOTTING LIVE ) :
  2.  
  3.  
  4. import os
  5. import time
  6. from time import sleep
  7. from datetime import datetime
  8. #from scipy import stats
  9.  
  10.  
  11.  
  12. #CODE FOR TEMP SENSOR DATA PLOTTING USING MATPLOT
  13. import matplotlib as mpl
  14. #mpl.use('tkagg')
  15. import matplotlib.pyplot as plt
  16. import numpy as np
  17. import serial
  18. DATA_SIZE = int(input('enter data size needed')) # Python 3
  19. temperature = np.zeros(DATA_SIZE, dtype=np.float32)
  20. index = 0
  21. samples = range(0, DATA_SIZE)
  22. sr=0
  23. fig = plt.figure()
  24. ax = fig.gca()
  25.  
  26. #ax.set_ylim([0, 60])
  27. #ax.set_xlim([0, 24])
  28. #ax.set_title('Real-Time Temperature Monitoring')
  29. #x.set_xlabel('Time->')
  30. #.set_ylabel('Temperature(C)->')
  31. print(temperature)
  32.  
  33. # Enable Interactive Plotting, use plt.ioff() to turn off
  34. plt.ion()
  35.  
  36. # Open Serial Port
  37. ser = serial.Serial('/dev/ttyACM0',baudrate=9600,timeout=10
  38. ,parity=serial.PARITY_NONE,
  39. stopbits=serial.STOPBITS_ONE,
  40. bytesize=serial.EIGHTBITS
  41. )
  42. print(ser)
  43. file = open("/home/pi/Desktop/New2.csv","w")
  44. file.write("Sr.No.,time,Sensor1n")
  45.  
  46. try:
  47. print ('run')
  48. while True:
  49. temp = ser.readline()
  50. print(temp)
  51. if index < DATA_SIZE:
  52. # Convert String to a Number
  53.  
  54. temperature[index] = float(temp)
  55. print(temperature)
  56. # Clear the Old Plot from Screen
  57. ax.cla()
  58. #ax.grid(True)
  59. minor_x=np.arange(0,24,0.5)
  60. major_x=np.arange(0,24,1)
  61. ax.set_xticks(major_x)
  62. ax.set_xticks(minor_x,minor=True)
  63. ax.grid(which='both')
  64. ax.set_ylim([0, 60])
  65. ax.set_title('Real-Time Temperature Monitoring')
  66. ax.set_xlabel('Time ->')
  67. ax.set_ylabel('Temperature$^circ$(C) ->')
  68. ax.plot(samples,temperature,'b')
  69. plt.pause(0.01)
  70. index +=1
  71. sr=sr+1
  72. now = datetime.now()
  73. print(index)
  74. file = open("/home/pi/Desktop/New2.csv","a")
  75. file.write(str(sr)+","+str(now)+","+str(temp)+"n")
  76. file.flush()
  77. time.sleep(1)
  78. else:
  79. index = 0
  80.  
  81.  
  82.  
  83. finally:
  84. plt.ioff()
  85. plt.show()
  86. ser.close()
  87.  
  88. import tkinter as tk
  89.  
  90. # -----
  91. import sys
  92.  
  93. class StdoutRedirectorLabel(object):
  94.  
  95. def __init__(self, widget):
  96. self.widget = widget
  97. # clear at start because it will use +=
  98. self.widget['text'] = ''
  99.  
  100. def write(self, text):
  101. # have to use += because one `print()` executes `sys.stdout` many times
  102. self.widget['text'] += text
  103. import subprocess
  104.  
  105. def callback1():
  106.  
  107. import coderun
  108.  
  109.  
  110. coderun.function1()
  111.  
  112.  
  113. def callback2():
  114.  
  115. import coderun
  116.  
  117. ## # keep original `sys.stdout
  118. old_stdout = sys.stdout
  119. ##
  120. # redirect to class which will add text to `lbl`
  121. sys.stdout = StdoutRedirectorLabel(lbl)
  122. ##
  123. ## # it will execute only `function2` and assign result to Label (with ending "n")
  124. coderun.function2()
  125.  
  126. # set back original `sys.stdout
  127. sys.stdout = old_stdout
  128.  
  129.  
  130. def callback3():
  131.  
  132. import coderun
  133.  
  134. # keep original `sys.stdout
  135. old_stdout = sys.stdout
  136.  
  137. # redirect to class which will add text to `lbl`
  138. sys.stdout = StdoutRedirectorLabel(lbl)
  139.  
  140. # it will execute only `function3` and assign result to Label (with ending "n")
  141. coderun.function3()
  142.  
  143. # set back original `sys.stdout
  144. sys.stdout = old_stdout
  145.  
  146. # --- main ---
  147.  
  148. master = tk.Tk()
  149. master.geometry('1000x800')
  150.  
  151. lbl = tk.Label(master, text='')
  152. lbl.pack()
  153.  
  154. btn1 = tk.Button(master, text=" Temperature sensor ", command=callback1)
  155. btn1.pack()
  156.  
  157. btn2 = tk.Button(master, text="Sensor2", command=callback2)
  158. btn2.pack()
  159.  
  160. btn3 = tk.Button(master, text="Sensor3", command=callback3)
  161. btn3.pack()
  162.  
  163. master.mainloop()
Add Comment
Please, Sign In to add comment