Paho Mqtt与自己的on_message

时间:2019-12-14 13:24:38

标签: python kivy mqtt self paho

我刚刚加入该社区,并希望在任何时候都能提供帮助,我是python的中级入门者。

我正在为此程序使用Kivy + Paho_mqtt,我一直在尝试使此代码正常工作,但我不能。主要问题是我无法在on_message函数/方法中使用self。

这是我的代码:

import kivy
import threading
import time
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.properties import StringProperty
from kivy.uix.widget import Widget
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
from kivy.clock import Clock
from functools import partial
from kivy.config import Config
Config.set('kivy', 'keyboard_mode', 'systemandmulti')

state = False
global label_on
label_on = False
global label_off
label_off = False

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))

    client.subscribe('room/light')
    client.subscribe('room/light/status')





client = mqtt.Client()
client.on_connect = on_connect
#client.on_message = on_message

client.connect('192.168.100.14',  1883,  60)

client.loop_start()



class MainScreen(GridLayout):



    def on_message(client, userdata, msg):
        global waiting_on
        global waiting_off
        global label_on
        global label_off

        print(msg.topic+"  "+str(msg.payload))

        if msg.topic == 'room/light':


            if str(msg.payload) == "b'on'":

                publish.single('room/light/state', 'on', hostname='192.168.100.14', retain=True)
                print("Turning on...")
                waiting_on = True
                waiting_off = False

            if str(msg.payload) == "b'off'":
                publish.single('room/light/state', 'off', hostname='192.168.100.14', retain=True)
                print("Turning off...")
                waiting_off = True
                waiting_on = False


        if msg.topic == 'room/light/status':

            if str(msg.payload) == "b'success'":

                print("Success")
                publish.single('room/light/state', '', hostname='192.168.100.14', retain=True)
                if waiting_on == True:
                    waiting_on = False
                    print("Successss")
                    label_on = True

                elif waiting_off == True:
                    waiting_off = False
                    print("Succes")
                    label_off = True
                else:
                    print('wrong')


        if str(msg.payload) == "b'turnedOn'":

            print("Turned on")
            label_on = True
            #Clock.schedule_interval(update_label, 0.2)


        if str(msg.payload) == "b'turnedOff'":

            print("Turned off")
            label_off = True
            #Clock.schedule_interval(update_label, 0.2)


    def update_label(self, t=0):
        global label_on
        global label_off
        print('updating')
        if label_on == True:
            Clock.unschedule(self.update_label)
            print('changed to on')
            self.ids.state_label.text = "on"
            self.ids.switch1.disabled = False
            label_on = False
        elif label_off == True:
            Clock.unschedule(self.update_label)
            print('changed to off')
            self.ids.state_label.text = "off"
            self.ids.switch1.disabled = False
            label_off = False

    def callback(self, t=0):
        global state
        Clock.schedule_interval(self.update_label, 0.2)
        #Clock.schedule_once(self.callback)
        self.ids.switch1.disabled = True

        if state == False:
            publish.single('room/light', 'on', hostname='192.168.100.14')
            state = not state


        elif state == True:
            publish.single('room/light', 'off', hostname='192.168.100.14')
            state = not state

    client.on_message = on_message
    #Clock.schedule_interval(update_label, 0.5)

#MainScreen.status = 'on'
#Clock.schedule_interval(update_label, 2)
#mqttc = MainScreen()

class testapp(App):
    def build(self):
        return MainScreen()


if __name__ == "__main__":
    testapp().run()

我的目标是根据esp32上led的当前状态更改标签的开/关。我可以在kivy应用中切换按钮时进行更改,因为它运行update_label,但是当我切换esp32上的开关以打开led时,我无法这样做。

如果我可以在on_message中使用self,那么我遇到的所有问题都可以解决。 当我在on_message中包含self时,不会读取任何mqtt消息。

在此先感谢您的帮助。

0 个答案:

没有答案