简体   繁体   English

Kivy-显示实时传感器数据的标签

[英]Kivy - Label to display real-time Sensor Data

I would like to create two labels in Kivy that update their text with sensor data from temp sensors. 我想在Kivy中创建两个标签,使用来自温度传感器的传感器数据更新其文本。

Temp sensors are connected to an Arduino, which prints their values to serial in the example format every two seconds or so: 温度传感器连接到Arduino,Arduino每两秒钟左右以示例格式将其值打印到串行:

A 82.4 (on line 1) A 82.4 (在线1上)

B 80.6 (on line 2) B 80.6 (第2行)

The A/B is included in each print as an identifier that python could pick up to differentiate between the two. A / B包含在每个打印物中,作为python可以用来区分两者的标识符。

The issue is importing this data into python and attaching it to labels. 问题是将此数据导入python并将其附加到标签。

Here is the existing .py: 这是现有的.py文件:

import kivy

kivy.require('1.10.0')

from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.button import Button
from kivy.clock import Clock
from kivy.properties import StringProperty, NumericProperty, ObjectProperty
from digitalclock import DigitalClock
from kivy.animation import Animation

import serial
import time
import opc


class IntroScreen(Screen):
    pass


class ContScreen(Screen):
    pass


class ScreenManagement(ScreenManager):
    pass


#Disregard this, a timer is included in layout
class Timer(Label):
    a = NumericProperty()  # seconds

    def __init__(self, root, instance, duration, bg_color, **kwargs):
        super(Timer, self).__init__(**kwargs)
        self.obj = instance
        self.a = duration
        self.root = root

        self.obj.disabled = True    # disable widget/button
        self.obj.background_color = bg_color
        self.root.add_widget(self)  # add Timer/Label widget to screen, 'cont'

    def animation_complete(self, animation, widget):
        self.root.remove_widget(widget)  # remove Timer/Label widget to screen, 'cont'
        self.obj.background_color = [1, 1, 1, 1]    # reset to default colour
        self.obj.disabled = False   # enable widget/button

    def start(self):
        Animation.cancel_all(self)  # stop any current animations
        self.anim = Animation(a=0, duration=self.a)
        self.anim.bind(on_complete=self.animation_complete)
        self.anim.start(self)

    def on_a(self, instance, value):
        self.text = str(round(value, 1))


class Status(FloatLayout):
    _change = StringProperty()
    _tnd = ObjectProperty(None)

    def update(self, *args):
        self.time = time.asctime()
        self._change = str(self.time)
        self._tnd.text = str(self.time)
        print (self._change)

#Here is where I start referencing Serial Comms, this line is to identify where
#to *send* commands to via a separate identifier.
bone = serial.Serial('/dev/ttyACM0', 9600)


class XGApp(App):
    time = StringProperty()
    sensor1 = NumericProperty(0)
    sensor2 = NumericProperty(0)

    def update(self, *args):
        self.time = str(time.asctime())
    arduino = self.arduino
    data = arduino.read(arduino.inWaiting())
    for line in data.split('\n'):
        try:
            sensor, value = line.strip().split(' ')
        except:
            print("parse error!")
            continue
        if sensor == 'A':
            self.sensor1 = float(value)
        elif sensor == 'B':
            self.sensor2 = float(value)
        else:
            print("unknown data! {}".format(line))

    def build(self):
        try:
            self.arduino = serial.Serial('/dev/ttyACM0', 9600)
        except Exception as e: print(e)

        Clock.schedule_interval(self.update, 1)
        return Builder.load_file("main.kv")


xApp = XGApp()

if __name__ == "__main__":

    xApp.run()

and the .kv: 和.kv:

<ContScreen>:
    FloatLayout
        orientation: 'vertical'
        padding: [10,50,10,50]
        spacing: 50

        Label:
            id: 'TempLabel1'
            text: str(app.sensor1)
            color: 1,1,1,1
            font_size: 80
            pos_hint: {'center_x':0.2, 'center_y':0.6}

        Label:
            id: 'TempLabel2'
            text: str(app.sensor1)
            color: 1,1,1,1
            font_size: 80
            pos_hint: {'center_x':0.5, 'center_y':0.6}

later in the .kv: 稍后在.kv中:

StackLayout
        orientation: "tb-rl"
        spacing: 15

        Button:
            text: "1"
            size_hint: None, .16
            width: 225
            on_press:
                Timer(root, self, 10, [100, 0, 100, 1.75]).start()
                bone.Write('j'.encode())
                print("One Executed")

TempLabel1 and TempLabel2 are the two labels i'd like updated from the sensors. TempLabel1和TempLabel2是我想从传感器更新的两个标签。

It's totally possible. 完全有可能。 But you are missing a few things. 但是您缺少一些东西。

You are trying to connect to the serial port after running your app, that won't work as your app will be stopped when you arrive there. 您正在尝试运行应用程序连接到串行端口,该操作将无法正常进行,因为您到达该位置时,应用程序将被停止。 Instead, you want to do this part while your app runs. 相反,您想在应用程序运行时执行此部分。 I would do the try/except to connect to arduino in app.build. 我会做尝试/除了连接到app.build中的arduino。

def build (self):
    try:
        self.arduino = serial.Serial('/dev/ttyACM0')
    exept:
        print("unable to connect to arduino :(")

    Clock.schedule_interval(self.update, 1)
    return Builder.load_file("main.kv")

then, you want to check for messages in the update method, but you don't want to block, so you only read the amount of data that is waiting in the buffer. 然后,您要检查update方法中的消息,但不想阻塞,因此只读取缓冲区中等待的数据量。

def update(self, *args):
    arduino = self.arduino
    data = arduino.read(arduino.inWaiting())

then you do your processing, i assume your data is something like: 然后您进行处理,我假设您的数据是这样的:

A value
B value

in such case, you can parse it and update the corresponding variable, something like: 在这种情况下,您可以解析它并更新相应的变量,例如:

def update(self, *args):
    arduino = self.arduino
    data = arduino.read(arduino.inWaiting()) 
    for line in data.split('\n'):
        try:
            sensor, value = line.strip().split(' ')
        except:
            print("parse error!")
            continue
        if sensor == 'A':
            self.sensor1 = float(value)
        elif sensor == 'B':
            self.sensor2 = float(value)
        else:
            print("unknown data! {}".format(line))

would do the job, it's a bit simplistic, as it assumes you always get full lines, but it can be improved later if needed (and it seems enough for a lot of cases in my experience). 会完成这项工作,这有点简单,因为它假定您总是会获得完整的阵容,但是如果需要的话可以在以后进行改进(根据我的经验,这似乎足够了)。

Now, we need to make sure that our labels notice the changes of values, for this, kivy uses properties , which are smarter attributes, you need to declare them on the app class. 现在,我们需要确保标签注意到值的变化,为此,kivy使用properties ,它们是更聪明的属性,您需要在app类上声明它们。

class XGApp(App):
    sensor1 = NumericProperty(0)
    sensor2 = NumericProperty(0)

now, you can make your update display the value directly, through the app instance. 现在,您可以通过应用实例使更新直接显示值。

<ContScreen>:
    FloatLayout
        orientation: 'vertical'
        padding: [10,50,10,50]
        spacing: 50

        Label:
            id: 'TempLabel1'
            text: str(app.sensor1)
            color: 1,1,1,1
            font_size: 80
            pos_hint: {'center_x':0.2, 'center_y':0.6}

        Label:
            id: 'TempLabel2'
            text: str(app.sensor2)
            color: 1,1,1,1
            font_size: 80
            pos_hint: {'center_x':0.5, 'center_y':0.6}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM