如何使用Python中的Mosquitto将文件发布到AWS- IoT [英] How Can I Publish File to AWS- IoT using Mosquitto in Python

查看:74
本文介绍了如何使用Python中的Mosquitto将文件发布到AWS- IoT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Mosquitto和python将文件发布到AWS IoT.我需要发布的文件是本地目录中的jpg文件.我有将数据发布到IoT所需的秘密密钥和访问密钥.我对编程非常陌生,不知道如何编写该程序.有谁可以帮助我吗?如果这是非常基本的事情,我深表歉意.谢谢

我已经尝试过如何发布一个在python中使用Mosquitto的文件?,对我不起作用.

这就是我累的方式.

 >#!/usr/bin/python导入蚊子导入系统进口ssl将paho.mqtt.client导入为mqttf =打开(数据")imagestring = f.read()byteArray =字节(图像字符串)client.publish("photo",byteArray,0)#在主题收到消息时调用def on_message(mqttc,obj,msg):print(从主题收到消息:" + msg.topic +"| QoS:" + str(msg.qos)+已接收数据:" + str(msg.payload))#使用client-id = mqtt-test创建一个客户端mqttc = mqtt.Client(client_id ="mqtt-test")mqttc.on_connect = on_connectmqttc.on_subscribe = on_subscribemqttc.on_message = on_message#配置网络加密mqttc.tls_set("/home/username/root-CA.crt",certfile ="/home/username/6fdda68178-certificate.pem.crt",keyfile ="/home/username/6fdda68178-private.pem.key",tls_version = ssl.PROTOCOL_TLSv1_2,密码=无)#连接到aws-account-specific-iot-endpointmqttc.connect("A2DL8ZE59089FKF.iot.us-west-2.amazonaws.com",端口= 8883)#要发布到的主题mqttc.subscribe("$ aws/things/mqtt-listener/shadow/update/#",qos = 1)#自动处理重新连接mqttc.loop_forever() 

解决方案

看起来您的代码有些倒退,或者至少很难理解.这是一些将二进制文件上传到AWS IOT的工作代码示例.

 #!/usr/bin/python将paho.mqtt.client导入为paho导入操作系统进口插座进口ssl从进口睡眠开始从随机进口制服connflag =假def on_connect(客户端,用户数据,标志,rc):全局标志connflag =真print(连接返回结果:" + str(rc))mqttc = paho.Client()mqttc.on_connect = on_connectawshost ="YOURAWSHOST.iot.us-west-2.amazonaws.com"awsport = 8883caPath ="root-CA.crt"certPath ="YOURCERT.pem.crt"keyPath ="YOURKEY.pem.key"mqttc.tls_set(caPath,certfile = certPath,keyfile = keyPath,cert_reqs = ssl.CERT_REQUIRED,tls_version = ssl.PROTOCOL_TLSv1_2,ciphers = None)mqttc.connect(awshost,awsport,keepalive = 60)mqttc.loop_start()而1 == 1:睡眠(0.5)f =打开('mybinaryfile')imagestring = bytearray(f.read())f.close()message ='"image":{"bytearray":'+ imagestring +'"}}'mqttc.publish("$ aws/things/rpi/shadow/update",message,qos = 1) 

请记住,您发布的消息必须很小-最大大小为128KB.如果图像较大,则可能需要遍历图像并将其读取为小于128KB(+开销)的块,然后为每个图像将多个图像上传到AWS IOT,直到将整个内容上传为止./p>

-射线

I am trying to publish a file to AWS IoT using Mosquitto and python. The file i need to publish is a jpg file that is in my local directory. I have secret key and access key that are required to publish data to IoT. I am very new to programming and don't know how to write this program. can someone help me please? I apologize if this is something very basic. Thank you

I have already tried this How can I publish a file using Mosquitto in python? and did not work for me.

This is how i tired to do.

> #!/usr/bin/python

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


f = open("data") 
imagestring = f.read() 
byteArray = bytes(imagestring) 
client.publish("photo", byteArray ,0)

#called when a message is received by a topic 
def on_message(mqttc, obj, msg):
print("Received message from topic: "+msg.topic+" | QoS: "+str(msg.qos)+"Data      Received: "+str(msg.payload))

#creating a client with client-id=mqtt-test 
mqttc = mqtt.Client(client_id="mqtt-test")

mqttc.on_connect = on_connect 
mqttc.on_subscribe = on_subscribe 
mqttc.on_message = on_message

#Configure network encryption  
mqttc.tls_set("/home/username/root-CA.crt",
certfile="/home/username/6fdda68178-certificate.pem.crt",
keyfile="/home/username/6fdda68178-private.pem.key",
              tls_version=ssl.PROTOCOL_TLSv1_2,
              ciphers=None)

#connecting to aws-account-specific-iot-endpoint 
mqttc.connect("A2DL8ZE59089FKF.iot.us-west-2.amazonaws.com", port=8883) 


#the topic to publish to 
mqttc.subscribe("$aws/things/mqtt-listener/shadow/update/#", qos=1)


#automatically handles reconnecting 
mqttc.loop_forever()

解决方案

It looks like you have some things backwards in your code, or at least hard to understand. Here's an example of some working code to upload a binary file to AWS IOT.

#!/usr/bin/python

import paho.mqtt.client as paho
import os
import socket
import ssl
from time import sleep
from random import uniform

connflag = False

def on_connect(client, userdata, flags, rc):
   global connflag
   connflag = True
   print("Connection returned result: " + str(rc))

mqttc = paho.Client()
mqttc.on_connect = on_connect

awshost = "YOURAWSHOST.iot.us-west-2.amazonaws.com"
awsport = 8883
caPath = "root-CA.crt"
certPath = "YOURCERT.pem.crt"
keyPath = "YOURKEY.pem.key"

mqttc.tls_set(caPath, certfile=certPath, keyfile=keyPath, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None)

mqttc.connect(awshost, awsport, keepalive=60)

mqttc.loop_start()
while 1==1:
   sleep(0.5)
   f = open('mybinaryfile')
   imagestring = bytearray(f.read())
   f.close()

   message = '"image": { "bytearray": "' + imagestring + '"} } '
   mqttc.publish("$aws/things/rpi/shadow/update", message, qos=1)

Keep in mind that your published messages need to be SMALL - 128KB is the max size. If you have large images, you'll likely need to loop over your image and read it into chunks that are smaller than 128KB (+ overhead), and upload multiple images to AWS IOT for each image until you get the entire thing uploaded.

-Ray

这篇关于如何使用Python中的Mosquitto将文件发布到AWS- IoT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆