daily pastebin goal
47%
SHARE
TWEET

Untitled

a guest Jan 18th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
  2. from matplotlib.figure import Figure
  3. import tkinter as tk
  4. import numpy as np
  5. import matplotlib.pyplot as plt
  6. import matplotlib.animation as animation
  7. import sqlite3
  8. from datetime import datetime
  9. from random import randint
  10.  
  11. class MainApplication(tk.Frame):
  12.     def __init__(self, parent, *args, **kwargs):
  13.         tk.Frame.__init__(self, parent, *args, **kwargs)
  14.         self.parent = parent
  15.  
  16.         root.update_idletasks()
  17.  
  18.         f = Figure(figsize=(15,5), dpi=55)        
  19.         x=1
  20.         ax = f.add_subplot(111)
  21.         line, = ax.plot(x, np.sin(x))    
  22.  
  23.         def animate(i):
  24.             # Open Database
  25.             conn = sqlite3.connect('Sensor_Data.db')
  26.             c = conn.cursor()
  27.             # Create some fake Sensor Data    
  28.             NowIs = datetime.now()
  29.             Temperature = randint(0, 100)
  30.             Humidity = randint(0, 100)
  31.             # Add Data to the Database
  32.             c = conn.cursor()
  33.             # Insert a row of data
  34.             c.execute("insert into Sensor_Stream_1 (Date, Temperature, Humidity) values (?, ?, ?)",
  35.                         (NowIs, Temperature, Humidity))
  36.             # Save (commit) the changes
  37.             conn.commit()
  38.             # Select Data from the Database
  39.             c.execute("SELECT Temperature FROM Sensor_Stream_1 LIMIT 30 OFFSET (SELECT COUNT(*) FROM Sensor_Stream_1)-30")
  40.             # Gives a list of all temperature values
  41.             x = 1
  42.             Temperatures = []
  43.  
  44.             for record in c.fetchall():    
  45.                 Temperatures.append(str(x)+','+str(record[0]))
  46.                 x+=1
  47.             # Setting up the Plot with X and Y Values
  48.             xList = []
  49.             yList = []
  50.  
  51.             for eachLine in Temperatures:
  52.                 if len(eachLine) > 1:
  53.                     x, y = eachLine.split(',')
  54.                     xList.append(int(x))
  55.                     yList.append(int(y))          
  56.  
  57.             ax.clear()
  58.  
  59.             ax.plot(xList, yList)
  60.  
  61.             ax.set_ylim(0,100)
  62.             ax.set_xlim(1,30)
  63.  
  64.             ax.tick_params(axis='both', which='major', labelsize=12)
  65.  
  66.             ax.grid(b=None, which='major', axis='both', **kwargs)
  67.  
  68.  
  69.         label = tk.Label(root,text="Temperature / Humidity").pack(side="top", fill="both", expand=True)
  70.  
  71.         canvas = FigureCanvasTkAgg(f, master=root)
  72.         canvas.get_tk_widget().pack(side="left", fill="both", expand=True)
  73.  
  74.         root.ani = animation.FuncAnimation(f, animate, interval=1000)
  75.  
  76. if __name__ == "__main__":
  77.     root = tk.Tk()
  78.     MainApplication(root).pack(side="top", fill="both", expand=True)
  79.     root.mainloop()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top