Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
- from matplotlib.figure import Figure
- import tkinter as tk
- import numpy as np
- import matplotlib.pyplot as plt
- import matplotlib.animation as animation
- import sqlite3
- from datetime import datetime
- from random import randint
- class MainApplication(tk.Frame):
- def __init__(self, parent, *args, **kwargs):
- tk.Frame.__init__(self, parent, *args, **kwargs)
- self.parent = parent
- root.update_idletasks()
- f = Figure(figsize=(15,5), dpi=55)
- x=1
- ax = f.add_subplot(111)
- line, = ax.plot(x, np.sin(x))
- def animate(i):
- # Open Database
- conn = sqlite3.connect('Sensor_Data.db')
- c = conn.cursor()
- # Create some fake Sensor Data
- NowIs = datetime.now()
- Temperature = randint(0, 100)
- Humidity = randint(0, 100)
- # Add Data to the Database
- c = conn.cursor()
- # Insert a row of data
- c.execute("insert into Sensor_Stream_1 (Date, Temperature, Humidity) values (?, ?, ?)",
- (NowIs, Temperature, Humidity))
- # Save (commit) the changes
- conn.commit()
- # Select Data from the Database
- c.execute("SELECT Temperature FROM Sensor_Stream_1 LIMIT 30 OFFSET (SELECT COUNT(*) FROM Sensor_Stream_1)-30")
- # Gives a list of all temperature values
- x = 1
- Temperatures = []
- for record in c.fetchall():
- Temperatures.append(str(x)+','+str(record[0]))
- x+=1
- # Setting up the Plot with X and Y Values
- xList = []
- yList = []
- for eachLine in Temperatures:
- if len(eachLine) > 1:
- x, y = eachLine.split(',')
- xList.append(int(x))
- yList.append(int(y))
- ax.clear()
- ax.plot(xList, yList)
- ax.set_ylim(0,100)
- ax.set_xlim(1,30)
- ax.tick_params(axis='both', which='major', labelsize=12)
- ax.grid(b=None, which='major', axis='both', **kwargs)
- label = tk.Label(root,text="Temperature / Humidity").pack(side="top", fill="both", expand=True)
- canvas = FigureCanvasTkAgg(f, master=root)
- canvas.get_tk_widget().pack(side="left", fill="both", expand=True)
- root.ani = animation.FuncAnimation(f, animate, interval=1000)
- if __name__ == "__main__":
- root = tk.Tk()
- MainApplication(root).pack(side="top", fill="both", expand=True)
- root.mainloop()
Add Comment
Please, Sign In to add comment