How Can I Publish File to AWS- IoT using Mosquitto in Python

心已入冬 提交于 2021-01-28 21:40:22

问题


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()

回答1:


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



来源:https://stackoverflow.com/questions/35882134/how-can-i-publish-file-to-aws-iot-using-mosquitto-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!