为了账号安全,请及时绑定邮箱和手机立即绑定

为什么我不能使用 Python(使用此代码)通过 MQTT 将图像发送到

为什么我不能使用 Python(使用此代码)通过 MQTT 将图像发送到

交互式爱情 2022-07-12 17:38:13
下面是我尝试使用 MQTT 将图像发送到 Google Cloud IoT Core。我已阅读以下对我有所帮助的帖子,但我的代码仍然无法正常工作:如何在 Python 中使用 Mosquitto 将文件发布到 AWS-IoT以及如何在 Python 中使用 Mosquitto 发布文件?.我猜我的错误与qosin theclient.publish或我如何使用循环有关,但恐怕到目前为止我对这些因素的试验对我没有帮助(尝试qos= 0/1/2 和 eg client.loop_forever())。我的图像是 1.2 Mb,所以据我所知,这应该不是问题。#!/usr/bin/pythonfrom picamera import PiCameraimport datetimeimport timeimport jwtimport paho.mqtt.client as mqttfrom 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 scriptssl_private_key_filepath = 'FILE1.pem'ssl_algorithm = 'RS256' # Either RS256 or ES256root_cert_filepath = 'FILE2.PEM'project_id = 'PROJECT_ID'gcp_location = 'LOCATION'registry_id = 'REG_ID'device_id = 'DEVICE_ID'# end of user-variablesrun = Truecur_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 whateverclient.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 = Falseclient.on_connect = on_connectclient.on_publish = on_publishclient.tls_set(ca_certs=root_cert_filepath) # Replace this with 3rd party cert if that was used when creating registryclient.connect('mqtt.googleapis.com', 8883)
查看完整描述

1 回答

?
慕雪6442864

TA贡献1812条经验 获得超5个赞

答案由@Aaron 提供。

“1.2Mb 太大了……”

遥测事件有效负载限制为 256 KB,不能增加


查看完整回答
反对 回复 2022-07-12
  • 1 回答
  • 0 关注
  • 326 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号