从设备(Google Cloud IoT Core)向 HTTP Cloud Function 发出 HTTP 请求?

HTTP request to HTTP Cloud Function from Device(Google Cloud IoT Core)?

如何从设备(具有云 IOT 核心授权)将 "HTTP requests" 转换为 Google 云功能?

例如现在我有一个已经在 Cloud IoT Core 中注册的设备。为了进行通信,我通过 Pub/Sub Mqtt 将数据发送到云功能。它运行良好,但我无法从云功能向设备发送确认消息,无论响应是否成功。

但现在我想,例如从设备向云功能发出 HTTP POST/GET 请求。这样这个 HTTP 请求就会得到一个确认消息,例如"Your data has reached" 从云功能回来。

我现在在我的设备上尝试的是..

def post_data(payload):
    URL='https://cloudiotdevice.googleapis.com/v1/projects/{project-id}/locations/{cloud-region}/registries/{registry-id}/devices/{device-id}:publishEvent'
    payload = json.dumps(payload)
    headers = {
        'Content-Type': 'application/json; charset=utf-8',
        'Authorization': JWT
    }
    try:
        requests.post(URL, json=payload, headers=headers)
    except requests.exceptions.ConnectionError:
        logging.error('Error posting data to Cloud Function!')
    except requests.exceptions.MissingSchema:
        logging.error('Error posting data to Cloud Function! Are Environment Variables set?')

但是它给我返回 400 错误。我也有子文件夹作为主题,但我也不知道应该在哪里定义它?

payload格式如下

payload = {'device': device_id,
           'type': TYPE,
           'timestamp': time.time(),
           'data': {'temperature': temperature,
                    'humidity': humidity}}

当您使用 HTTP 将遥测事件从您的设备发送到 Cloud IoT 时,有效负载必须 JSON 并且 它必须具有描述的结构 here.具体来说,REST POST 请求的负载必须是:

{
  "binaryData": string
}

您要发布的实际数据是 base 64 编码表示。查看您的示例代码,我没有看到您使用该有效负载发送 REST 请求。