aws iot 使用 php sdk 订阅?

问题描述 投票:0回答:2

我已经为 php / laravel 实现了 aws-sdk。 我可以使用下面的代码毫无问题地发布消息,但我似乎无法在任何地方找到如何实际订阅该主题并查看从物联网设备收到的响应,任何帮助将不胜感激!

我认为“保留消息”函数可能包含该信息,但它只包含发送/发布的数据,而不包含响应。

非常感谢

        $iot = \AWS::createClient('iotdataplane');
        $result = $iot->publish([
            'payload' => $request->message,
            'retain' => false,
            'qos' => 1,//1,
            'topic' => $request->topic // REQUIRED
        ]);
php laravel amazon-web-services iot aws-php-sdk
2个回答
0
投票

如果这是您想要实现的目标,则不是 100%,但希望它有帮助!

设备影子服务http://docs.aws.amazon.com/iot/latest/developerguide/iot-thing-shadows.html

如果您系统中的某个参与者仅限于 PHP,我建议您查看 AWS IoT 的

device shadow service
。您的其他参与者可以将消息发布到由 AWS IoT 保留的影子主题。尽管您的 PHP actor 可以通过 HTTP 轮询影子值来检查更改,但这可能并不理想。

来源https://forums.aws.amazon.com/thread.jspa?threadID=257022

否则,请对您想要执行的操作提供更多说明,我将尝试编辑我的答案!


0
投票

我不确定示例中的 AWS 客户端如何连接到 IOT 数据平面,但如果它在幕后使用 REST API(我怀疑是这样),那么你就无法使用它来订阅主题。这不是受支持的操作之一。

但是,您可以使用 PHP 的 MQTT 客户端来实现此目的,例如 php-mqtt/client。在此示例中,我使用 X509 证书进行身份验证并订阅 $topic:

// We can get this endpoint from the AWS CLI:
// aws iot describe-endpoint --endpoint-type iot:Data-ATS
$base_url = 'a3gmg2blyb94fz-ats.iot.us-east-1.amazonaws.com';
$mqtt = new MqttClient(
  $base_url,
  8883,
  NULL,
  MqttClient::MQTT_3_1_1
);
$connectionSettings = (new ConnectionSettings())
  ->setConnectTimeout(3)
  ->setUseTls(TRUE)
  // Download root cert from https://docs.aws.amazon.com/iot/latest/developerguide/server-authentication.html
  ->setTlsCertificateAuthorityFile('./tmp/AmazonRootCA1.pem')
  // @see https://docs.aws.amazon.com/iot/latest/developerguide/fleet-provision-api.html#create-keys-cert
  ->setTlsClientCertificateKeyFile($private_key)
  ->setTlsClientCertificateFile($cert);
$mqtt->connect($connectionSettings);

$mqtt->subscribe($topic,
  function ($topic, $message, $retained, $matchedWildcards) use ($mqtt) {
    $payload = json_decode($message);
    echo sprintf("Received message on topic [%s]: %s\n", $topic, $message);
    $mqtt->interrupt();
    $mqtt->disconnect();
  });

$mqtt->loop(TRUE);
© www.soinside.com 2019 - 2024. All rights reserved.