(错误模块)python mqtt_Listen_Sensor_Data.py [英] (ERROR MODULE) python mqtt_Listen_Sensor_Data.py
本文介绍了(错误模块)python mqtt_Listen_Sensor_Data.py的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将我的MQTT数据存储在树莓派上的SQLITE数据库中.我要怎么做才能得到下面的错误?
I'm trying to store my MQTT data in SQLITE database on raspberry. What am I doing wrong to get the error below?
代码:
import paho.mqtt.client as mqtt
from store_Sensor_Data_to_DB import sensor_Data_Handler
# MQTT Settings
MQTT_Broker = "tcp://localhost:1883"
MQTT_Port = 1883
Keep_Alive_Interval = 45
MQTT_Topic = "kuca/primanje/kanta01/r"
#Subscribe to all Sensors at Base Topic
def on_connect(mosq, obj, rc):
self.subscribe(MQTT_Topic, 0)
#Save Data into DB Table
def on_message(mosq, obj, msg):
# This is the Master Call for saving MQTT Data into DB
# For details of "sensor_Data_Handler" function please refer "sensor_data_to_db.py"
print "MQTT Data Received..."
print "MQTT Topic: " + msg.topic
print "Data: " + msg.payload
sensor_Data_Handler(msg.topic, msg.payload)
def on_subscribe(mosq, obj, mid, granted_qos):
pass
mqttc = mqtt.Client()
# Assign event callbacks
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_subscribe = on_subscribe
# Connect
mqttc.connect(MQTT_Broker, int(MQTT_Port), int(Keep_Alive_Interval))
# Continue the network loop
mqttc.loop_forever()
错误:
pi@Pi:~/Desktop/SQLITE $ python mqtt_Listen_Sensor_Data.py
Traceback (most recent call last):
File "mqtt_Listen_Sensor_Data.py", line 42, in <module>
mqttc.connect(MQTT_Broker, int(MQTT_Port), int(Keep_Alive_Interval))
File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 768, in connect
return self.reconnect()
File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 895, in reconnect
sock = socket.create_connection((self._host, self._port), source_address=(self._bind_address, 0))
File "/usr/lib/python2.7/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known
推荐答案
Paho Python客户端托管主机&端口号不是connect
方法的URL.
The Paho Python client takes a host & port not a URL for it's connect
method.
connect(host, port=1883, keepalive=60, bind_address="")
因此,您应该在脚本顶部编辑代理定义.
So you should edit broker definition at the top of the script.
MQTT_Broker = "localhost"
这篇关于(错误模块)python mqtt_Listen_Sensor_Data.py的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文