我想使用Mosquitto和python將文件發佈到AWS IoT。我需要發佈的文件是位於本地目錄中的jpg文件。我擁有將數據發佈到物聯網所需的密鑰和訪問密鑰。我對編程非常陌生,不知道如何編寫這個程序。有人能幫助我嗎?我很抱歉,如果這是非常基本的東西。謝謝如何在Python中使用Mosquitto將文件發佈到AWS-IoT
我已經試過這個How can I publish a file using Mosquitto in python?並沒有爲我工作。
這就是我累的事情。
> #!/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()
解釋如何指向例如,如果以任何方式還是修改 – hardillb