Python Forum
Raspberry pi Sensehat auto update
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Raspberry pi Sensehat auto update
#1
Hi all

This is my first post so I apologise if this post is in the wrong place

I have a raspberry pi and sense hat and would like to have Tkinter labels update various sensor readings automatically

I have the orientation sensor data I would like to display that data in real-time on the labels

I've tried many combinations but because I'm new to python I cannot get my head around how to update the labels automatically

hear is what I have so fare

from sense_hat import SenseHat
from tkinter import *

# Create a container for everything
canvas = Tk()

# Create  a path to the sensors
trycorder = SenseHat()


def UpdateOrientationDate():

    #Reaquire the orientation data
    orientationData = trycorder.get_orientation()

    #Update the labels with orientation data from the trycorder
    pitchLabel.config(text=str(round(orientationData["pitch"], 0)))
    rollLabel.config(text=str(round(orientationData["roll"], 0)))
    yowLabel.config(text=str(round(orientationData["yaw"], 0)))


#Set Globals
global pitchLabel
global rollLabel
global yowLabel


#Get intital orientation orientation data from the trycorder
orientationData = trycorder.get_orientation()

# Pre-populate the Labels with data for orientation from the trycorder
pitchLabel = Label(canvas, text=str(round(orientationData["pitch"], 0)))
rollLabel = Label(canvas, text=str(round(orientationData["roll"], 0)))
yowLabel = Label(canvas, text=str(round(orientationData["yaw"], 0)))


#Place the labels on the grid
pitchLabel.grid(row=0, column=0)
rollLabel.grid(row=1, column=0)
yowLabel.grid(row=2, column=0)


#Call the function every 500ms
canvas.after(500, UpdateOrientationDate)

#Ive also tried this
#while True:
#    #Reaquire the orientation data
#    orientationData = trycorder.get_orientation()
#
#    #Update the labels with orientation data from the trycorder
#    pitchLabel.config(text=str(round(orientationData["pitch"], 0)))
#    rollLabel.config(text=str(round(orientationData["roll"], 0)))
#    yowLabel.config(text=str(round(orientationData["yaw"], 0)))


canvas.mainloop()
Any help or pointers would be welcomed
Reply
#2
I don't have a sense hat to work with but I can tell you that after () does not work like you think it would. The first time that you call canvas.after (500) it will wait half a second but the next time you call it, it will not seem to work at all because it only waited the same half a second, not a whole second. To work with this you have to add time each time you call it.

My code below simply increments the values in orientationData instead of getting it from the sense hat but the concept is the same. I hope this helps.

from tkinter import *
 
# Create a container for everything
canvas = Tk()
 
def UpdateOrientationDate():

    #Reaquire the orientation data
    for key, value in orientationData.items () :
        orientationData [key] = value + 1 

    #Update the labels with orientation data from the trycorder
    pitchLabel.config(text=str(round(orientationData["pitch"], 0)))
    rollLabel.config(text=str(round(orientationData["roll"], 0)))
    yowLabel.config(text=str(round(orientationData["yaw"], 0)))
 
#Get intital orientation orientation data from the trycorder
orientationData = {"pitch": 7, "roll": 8, "yaw": 9}

# Pre-populate the Labels with data for orientation from the trycorder
pitchLabel = Label(canvas, width = 9, text="0")
rollLabel = Label(canvas, text="0")
yowLabel = Label(canvas, text="0")

#Place the labels on the grid
pitchLabel.grid(row=0, column=0)
rollLabel.grid(row=1, column=0)
yowLabel.grid(row=2, column=0)

wait_time = 0
for count in range (13) :
    wait_time += 500
    canvas.after (wait_time, UpdateOrientationDate)

canvas.mainloop()
Reply
#3
(Apr-09-2021, 03:25 PM)BashBedlam Wrote: I don't have a sense hat to work with but I can tell you that after () does not work like you think it would. The first time that you call canvas.after (500) it will wait half a second but the next time you call it, it will not seem to work at all because it only waited the same half a second, not a whole second. To work with this you have to add time each time you call it.

My code below simply increments the values in orientationData instead of getting it from the sense hat but the concept is the same. I hope this helps.

from tkinter import *
 
# Create a container for everything
canvas = Tk()
 
def UpdateOrientationDate():

    #Reaquire the orientation data
    for key, value in orientationData.items () :
        orientationData [key] = value + 1 

    #Update the labels with orientation data from the trycorder
    pitchLabel.config(text=str(round(orientationData["pitch"], 0)))
    rollLabel.config(text=str(round(orientationData["roll"], 0)))
    yowLabel.config(text=str(round(orientationData["yaw"], 0)))
 
#Get intital orientation orientation data from the trycorder
orientationData = {"pitch": 7, "roll": 8, "yaw": 9}

# Pre-populate the Labels with data for orientation from the trycorder
pitchLabel = Label(canvas, width = 9, text="0")
rollLabel = Label(canvas, text="0")
yowLabel = Label(canvas, text="0")

#Place the labels on the grid
pitchLabel.grid(row=0, column=0)
rollLabel.grid(row=1, column=0)
yowLabel.grid(row=2, column=0)

wait_time = 0
for count in range (13) :
    wait_time += 500
    canvas.after (wait_time, UpdateOrientationDate)

canvas.mainloop()

Hi BashBedlam

Thanks for the replay I found the answer via trial and error and got the following
from sense_hat import SenseHat
from tkinter import *
from tkinter.ttk import *


# Create a container for everything  
canvas = Tk()

canvas.title("Sense Hat test")

# Create  a path to the sensors 
trycorder = SenseHat()

def UpdatePitchData(): 
    #Reaquire the orientation data
    data = trycorder.get_orientation()
    
    #Update the labels with orientation data from the trycorder
    p = str(round(data["pitch"],0))
    pitchLabel.config(text="Pitch = " + p)
    pitchLabel.after(100, UpdatePitchData)    

def UpdateRollData():
     #Reaquire the orientation data
    data = trycorder.get_orientation()
    
    #Update the labels with orientation data from the trycorder
    r = str(round(data["roll"], 0))
    rollLabel.config(text="Roll = " + r)
    rollLabel.after(100, UpdateRollData)

def UpdateYawData():
     #Reaquire the orientation data
    data = trycorder.get_orientation()
    
    #Update the labels with orientation data from the trycorder
    y = str(round(data["yaw"], 0))
    yowLabel.config(text="Yaw = " + y)
    yowLabel.after(100,UpdateYawData)

# Pre-populate the Labels with data for orientation from the trycorder
pitchLabel = Label(canvas)
rollLabel = Label(canvas)
yowLabel = Label(canvas)

#Place the labels on the grid               
pitchLabel.grid(row=0, column=0)
rollLabel.grid(row=1, column=0)
yowLabel.grid(row=2, column=0)

UpdatePitchData()
UpdateRollData()
UpdateYawData()

mainloop()
Reply
#4
from datetime import datetime
import tkinter as tk

running = False

def update_time():
    """Update the time display"""
    if running:
        time_display.set(datetime.now().strftime('%H:%M:%S'))
        root.after(1000, update_time)  # Scedule myself to run again

def start_stop():
    """Start/stop updating the time display"""
    global running
    running = not running
    if running:
        update_time()

root = tk.Tk()
time_display = tk.StringVar()
tk.Label(root, textvariable=time_display, width=8).pack(padx=10, pady=10)
tk.Button(root, text='Start/Stop', command=start_stop).pack(padx=10, pady=10)
root.mainloop()
This doesn't do anything in you program.
#Set Globals
global pitchLabel
global rollLabel
global yowLabel
Using the "global" keyword only makes sense inside a function. It tells Python to not create a local variable when doing variable assignment.
global_variable=0
def uses_local_variable(value):
    global_variable = value

def uses_global_variable(value):
    global global_variable
    global_variable = value

print(global_variable)
uses_local_variable(5)
print(global_variable)
uses_global_variable(10)
print(global_variable)
Output:
0 0 10
Reply
#5
(Apr-09-2021, 04:26 PM)deanhystad Wrote:
from datetime import datetime
import tkinter as tk

running = False

def update_time():
    """Update the time display"""
    if running:
        time_display.set(datetime.now().strftime('%H:%M:%S'))
        root.after(1000, update_time)  # Scedule myself to run again

def start_stop():
    """Start/stop updating the time display"""
    global running
    running = not running
    if running:
        update_time()

root = tk.Tk()
time_display = tk.StringVar()
tk.Label(root, textvariable=time_display, width=8).pack(padx=10, pady=10)
tk.Button(root, text='Start/Stop', command=start_stop).pack(padx=10, pady=10)
root.mainloop()
This doesn't do anything in you program.
#Set Globals
global pitchLabel
global rollLabel
global yowLabel
Using the "global" keyword only makes sense inside a function. It tells Python to not create a local variable when doing variable assignment.
global_variable=0
def uses_local_variable(value):
    global_variable = value

def uses_global_variable(value):
    global global_variable
    global_variable = value

print(global_variable)
uses_local_variable(5)
print(global_variable)
uses_global_variable(10)
print(global_variable)
Output:
0 0 10

Thank you but as you can see from my reply I worked it out
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  class Update input (Gpio pin raspberry pi) caslor 2 744 Jan-30-2023, 08:05 PM
Last Post: caslor
Question Python + Google Sheet | Best way to update specific cells in a single Update()? Vokofe 1 2,627 Dec-16-2020, 05:26 AM
Last Post: Vokofe

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020