[英]MQTT Paho Python Client subscriber, how subscribe forever?
Try to simple subscribe without disconnect to a Mosquitto broker to get all messages from devices that are publishing data in a specific topic, save them in a BD and Post them to a php that do "staff". 尝试简单订阅而不断开与Mosquitto代理的连接,以获取来自在特定主题中发布数据的设备的所有消息,将它们保存在BD中并将它们发布到执行“人员”的php。
Here is my subscribe.py 这是我的subscribe.py
import paho.mqtt.client as mqtt
from mqtt_myapp import *
topic="topic/#" # MQTT broker topic
myclient="my-paho-client" # MQTT broker My Client
user="user" # MQTT broker user
pw="pass" # MQTT broker password
host="localhost" # MQTT broker host
port=1883 # MQTT broker port
value="123" # somethin i need for myapp
def on_connect(mqttc, userdata, rc):
print('connected...rc=' + str(rc))
mqttc.subscribe(topic, qos=0)
def on_disconnect(mqttc, userdata, rc):
print('disconnected...rc=' + str(rc))
def on_message(mqttc, userdata, msg):
print('message received...')
print('topic: ' + msg.topic + ', qos: ' +
str(msg.qos) + ', message: ' + str(msg.payload))
save_to_db(msg)
post_data(msg.payload,value)
def on_subscribe(mqttc, userdata, mid, granted_qos):
print('subscribed (qos=' + str(granted_qos) + ')')
def on_unsubscribe(mqttc, userdata, mid, granted_qos):
print('unsubscribed (qos=' + str(granted_qos) + ')')
mqttc = mqtt.Client(myclient)
mqttc.on_connect = on_connect
mqttc.on_disconnect = on_disconnect
mqttc.on_message = on_message
mqttc.on_subscribe = on_subscribe
mqttc.on_unsubscribe = on_unsubscribe
mqttc.username_pw_set(user,pw)
mqttc.connect(host, port, 60)
mqttc.loop_forever()
Here is my mqtt_myapp.py: 这是我的mqtt_myapp.py:
import MySQLdb
import requests # pip install requests
url = "mydomain/data_from_broker.php"
def save_to_db(msg):
with db:
cursor = db.cursor()
try:
cursor.execute("INSERT INTO MQTT_LOGS (topic, payload) VALUES (%s,%s)", (msg.topic, msg.payload))
except (MySQLdb.Error, MySQLdb.Warning) as e:
print('excepttion BD ' + e)
return None
def post_data(payload,value):
datos = {'VALUE': value,'data-from-broker': payload}
r = requests.post(url, datos)
r.status_code
print('response POST' + str(r.status_code))
db = MySQLdb.connect("localhost","user_db","pass_db","db" )
When I run my python script on background with python -t mqtt_subscribe.py &
I get messages publish for other clients, but after some hours of my subscribe.py script running, socket error happens. 当我使用
python -t mqtt_subscribe.py &
在后台运行我的python脚本时python -t mqtt_subscribe.py &
我获得了其他客户端的消息发布,但是在我的subscribe.py脚本运行几个小时后,发生了套接字错误。
Mosquito.log: Mosquito.log:
...
1475614815: Received PINGREQ from my-paho-client
1475614815: Sending PINGRESP to my-paho-client
1475614872: New connection from xxx.xxx.xxx.xxx on port 1883.
1475614872: Client device1 disconnected.
1475614872: New client connected from xxx.xxx.xxx.xxx as device1(c0, k0, u'user1').
1475614872: Sending CONNACK to device1(0, 0)
1475614873: Received PUBLISH from device1(d0, q1, r0, m1, 'topic/data', ... (33 bytes))
1475614873: Sending PUBACK to device1 (Mid: 1)
1475614873: Sending PUBLISH to my-paho-client (d0, q0, r0, m0, 'topic/data', ... (33 bytes))
1475614874: Received DISCONNECT from device1
1475614874: Client device1 disconnected.
...
1475625566: Received PINGREQ from my-paho-client
1475625566: Sending PINGRESP to my-paho-client
1475625626: Received PINGREQ from my-paho-client
1475625626: Sending PINGRESP to my-paho-client
1475625675: New connection from xxx.xxx.xxx.xxx on port 1883.
1475625675: Client device1 disconnected.
1475625675: New client connected from xxx.xxx.xxx.xxx as device1 (c0, k0, u'user1').
1475625675: Sending CONNACK to device1 (0, 0)
1475625677: Received PUBLISH from device1 (d0, q1, r0, m1, 'topic/data', ... (33 bytes))
1475625677: Sending PUBACK to device1 (Mid: 1)
1475625677: Sending PUBLISH to my-paho-client (d0, q0, r0, m0, 'topic/data', ... (33 bytes))
1475625677: Socket error on client my-paho-client, disconnecting.
1475625677: Received DISCONNECT from device1
...
What could be the problem? 可能是什么问题呢? Any idea or suggestion?
有什么想法或建议吗?
Thanks in advance 提前致谢
If your code in the method "on_message" throws an exception and you do not catch it, you will be disconnected. 如果方法“on_message”中的代码抛出异常并且您没有捕获它,则将断开连接。 Try uncommenting all statements except the print statements.
尝试取消注释除print语句之外的所有语句。 Probably one of the following statements is throwing an exception.
可能以下陈述之一是抛出异常。
save_to_db(msg)
post_data(msg.payload,value)
While inserting into DB, use ?
插入数据库时,使用
?
instead of %s
. 而不是
%s
。 link for reference 链接供参考
cursor.execute('''INSERT INTO users(name, phone, email, password)
VALUES(?,?,?,?)''', (name1,phone1, email1, password1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.