问题描述
以下是我尝试使用MQTT将图像发送到Google Cloud IoT Core的尝试.我已经阅读了以下对我有所帮助的帖子,但我的代码仍然无法正常工作:
Below is my attempt to send an image to Google Cloud IoT Core with MQTT. I have read the following posts which have helped me somewhat, but my code still isn't working: How Can I Publish File to AWS- IoT using Mosquitto in Python and How can I publish a file using Mosquitto in python?.
我想我的错误与 client.publish
中的 qos
或我使用循环的方式有关,但我担心自己正在尝试这些因素到目前为止对我没有帮助(尝试 qos
= 0/1/2,例如 client.loop_forever()
).我的图片是1.2 Mb,所以据我了解,这应该不是问题.
I would guess my error has to do either with the qos
in the client.publish
or how I have used the looping, but I'm afraid my experimenting with these factors hasn't helped me so far (trying qos
= 0/1/2 and e.g. client.loop_forever()
). My image is 1.2 Mb, so this shouldn't be a problem as far as I understand.
#!/usr/bin/python
from picamera import PiCamera
import datetime
import time
import jwt
import paho.mqtt.client as mqtt
from time import sleep
# Define some project-based variables to be used below. This should be the only
# block of variables that you need to edit in order to run this script
ssl_private_key_filepath = 'FILE1.pem'
ssl_algorithm = 'RS256' # Either RS256 or ES256
root_cert_filepath = 'FILE2.PEM'
project_id = 'PROJECT_ID'
gcp_location = 'LOCATION'
registry_id = 'REG_ID'
device_id = 'DEVICE_ID'
# end of user-variables
run = True
cur_time = datetime.datetime.utcnow()
def create_jwt():
token = {
'iat': cur_time,
'exp': cur_time + datetime.timedelta(minutes=60),
'aud': project_id
}
with open(ssl_private_key_filepath, 'r') as f:
private_key = f.read()
return jwt.encode(token, private_key, ssl_algorithm)
_CLIENT_ID = 'projects/{}/locations/{}/registries/{}/devices/{}'.format(project_id, gcp_location, registry_id, device_id)
_MQTT_TOPIC = '/devices/{}/events'.format(device_id)
client = mqtt.Client(client_id=_CLIENT_ID)
# authorization is handled purely with JWT, no user/pass, so username can be whatever
client.username_pw_set(
username='unused',
password=create_jwt())
def error_str(rc):
return '{}: {}'.format(rc, mqtt.error_string(rc))
def on_connect(unusued_client, unused_userdata, unused_flags, rc):
print('on_connect', error_str(rc))
def on_publish(unused_client, unused_userdata, unused_mid):
print('on_publish')
run = False
client.on_connect = on_connect
client.on_publish = on_publish
client.tls_set(ca_certs=root_cert_filepath) # Replace this with 3rd party cert if that was used when creating registry
client.connect('mqtt.googleapis.com', 8883)
camera = PiCamera()
camera.start_preview()
sleep(5)
camera.capture('/tmp/picture.jpg')
camera.stop_preview()
with open("/tmp/picture.jpg", 'rb') as f:
imagestring = f.read()
byteArray = bytes(imagestring)
try:
client.publish(_MQTT_TOPIC, byteArray, qos=2)
except:
print('Error')
while run:
client.loop()
client.disconnect()
推荐答案
答案由@Aaron提供.
The answer was provided by @Aaron.
遥测事件有效负载限制为256 KB,无法增加
Telemetry event payload limit is 256 KB and cannot be increased
这篇关于为什么我不能使用Python(带有此代码)将图像通过MQTT发送到Google Cloud IoT Core?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!