简体   繁体   English

MQTT物联网智能插件 Python

[英]MQTT IoT smart plug in Python

I am totally new in IoT and I have a code exercise for my university.我是物联网的新手,我有一个针对我大学的代码练习。 I don't know how to connect this script in order to connect to MQTT and run without errors.我不知道如何连接此脚本才能连接到 MQTT 并正常运行。

How can I connect with MQTT?如何与 MQTT 连接? The script is the following:脚本如下:

import paho.mqtt.client as mqtt
import ssl
import os
import sys

import matplotlib.pyplot as plt
from matplotlib.widgets import Button
from threading import Timer
from datetime import datetime

class IoTExample:
    def __init__(self):
        self._establish_mqtt_connection()

    def start(self):
        self.client.loop_forever() 
    
    def disconnect(self, args=None):
        self.client.disconnect()

    def _establish_mqtt_connection(self):
        self.client = mqtt.Client
        self.client.on_log = self._on_log
        client.username_pw_set('iotlesson', 'YGK0tx5pbtkK2WkCBvJlJWCg')
        self.client.connect('phoenix.medialab.ntua.gr', 8883)
        client.subscribe('hscnl/hscnl02/state/ZWaveNode005_Switch/state')
        self.client.publish('hscnl/hscnl02/sendcommand/ZWaveNode005_Switch', 'ON')
        self.client.loop_forever()

    def _on_connect(self, client, userdata, flags, rc):
        self.client.on_connect = self._on_connect

    def _on_message(self, client, userdata, msg):
        self.client.on_message = self.on_message
        print(msg.topic+' '+str(msg.payload))

    def _on_log(self, client, userdata, level, buf):
        self.client.on_log = self._on_log
        print('log: ', buf)

try:
    iot_example = IoTExample()
    iot_example.start()
except KeyboardInterrupt:
    print('Interrupted')
    try:
        iot_example.disconnect()
        sys.exit(0)
    except SystemExit:
        os._exit(0)

I got the following errors:我收到以下错误:

python /home/mina/paho.mqtt.python/iot_example.py
Traceback (most recent call last):
  File "/home/mina/paho.mqtt.python/iot_example.py", line 41, in <module>
    iot_example = IoTExample()
  File "/home/mina/paho.mqtt.python/iot_example.py", line 13, in __init__
    self._establish_mqtt_connection()
  File "/home/mina/paho.mqtt.python/iot_example.py", line 24, in _establish_mqtt_connection
    self.client.connect('phoenix.medialab.ntua.gr', 8883)
TypeError: unbound method connect() must be called with Client instance as first argument (got str instance instead)
   def _establish_mqtt_connection(self):
        self.client = mqtt.Client()

You need to add parentheses when you create the Client instance.您需要在创建Client实例时添加括号。

TypeError: unbound method connect() must be called with Client instance as first argument (got str instance instead) TypeError:未绑定方法 connect() 必须使用 Client 实例作为第一个参数调用(改为获取 str 实例)

The fact that it says unbound is a clue that you didn't create an instance.它说未绑定的事实表明您没有创建实例。 Instead, self.client was just another name for the Client class itself.相反, self.client只是Client class本身的另一个名称。

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

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