[英]How do I publish/Subscribe AWS IoT - server side using Java SDK
I'm really new with AWS and IoT, and my goal is to:我是 AWS 和 IoT 的新手,我的目标是:
Use the Java SDK v.2 from my serverless application to create/get/update/attach/... certificates and things.使用我的无服务器应用程序中的 Java SDK v.2 创建/获取/更新/附加/...证书和其他东西。
Create client side MQTT demo application to connect publish and subscribe to messages used by my new certificates and thing created in phase 1.创建客户端 MQTT 演示应用程序以连接发布和订阅由我的新证书和在第 1 阶段创建的事物使用的消息。
Publish/subscribe messages in the server side in order to talk to my things/clients.在服务器端发布/订阅消息以便与我的东西/客户交谈。
1 & 2 I've managed to do perfectly. 1 & 2 我做得很完美。 But I don't understand how should I do the 3rd one.
但是我不明白我应该怎么做第三个。
In order to connect to IoT Core from the server I first configure my SSO connection using the AWSCLI and in the code I simply use my profile name and region to connect.为了从服务器连接到 IoT Core,我首先使用 AWSCLI 配置我的 SSO 连接,在代码中我只使用我的配置文件名称和区域进行连接。
Your serverless Java application needs to be configured as a "Thing" in the same account/region as your IoT devices.您的无服务器 Java 应用程序需要在与您的 IoT 设备相同的帐户/区域中配置为“事物”。 In the console, go to
在控制台中,go 到
AWS IoT -> Manage -> Things AWS IoT -> 管理 -> 事物
and create a thing for your app.并为您的应用创建一个东西。 In this case you shouldn't need a "Device Shadow", and you can select "Auto Generate Certificates".
在这种情况下,您不需要“设备影子”,您可以 select “自动生成证书”。
For the IoT Policy, you will need the following:对于物联网政策,您将需要以下内容:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "iot:Connect",
"Resource": "arn:aws:iot:us-east-1:YOUR_AWS_ACCOUNT_ID:client/*"
},
{
"Effect": "Allow",
"Action": "iot:Subscribe",
"Resource": "arn:aws:iot:us-east-1:YOUR_AWS_ACCOUNT_ID:topicfilter/*"
},
{
"Effect": "Allow",
"Action": "iot:Receive",
"Resource": "arn:aws:iot:us-east-1:YOUR_AWS_ACCOUNT_ID:topic/*"
},
{
"Effect": "Allow",
"Action": "iot:Publish",
"Resource": "arn:aws:iot:us-east-1:YOUR_AWS_ACCOUNT_ID:topic/*"
}
]
}
Your application will communicate with IoTCore using the endpoint shown in the Settings screen in IoTCore for the region where you have created your thing.您的应用程序将使用您创建事物所在区域的 IoTCore 设置屏幕中显示的端点与 IoTCore 通信。 Your application will authenticate using the key/cert you downloaded when creating the thing (username/password auth is not allowed).
您的应用程序将使用您在创建事物时下载的密钥/证书进行身份验证(不允许使用用户名/密码身份验证)。
Once your application connects to the endpoint, you will want to "subscribe" to the same topic your devices use to send messages.一旦您的应用程序连接到端点,您将希望“订阅”您的设备用于发送消息的同一主题。 You can also publish to one or more topics.
您还可以发布到一个或多个主题。
In order to debug communications, you can use the MQTT client in the AWS IoTCore console, just note the console needs to be refreshed periodically when communication times out.为了调试通信,您可以在 AWS IoTCore 控制台中使用 MQTT 客户端,请注意当通信超时时需要定期刷新控制台。 I recommend marking your topics as favorites so they are easy to re-subscribe to on a refresh.
我建议将您的主题标记为收藏夹,以便在刷新时轻松重新订阅。
As for coding in Java, you should be able to leverage examples from the AWS IoT Device SDK here:至于 Java 中的编码,您应该能够在此处利用 AWS IoT 设备 SDK 中的示例:
https://github.com/aws/aws-iot-device-sdk-java-v2/tree/main/samples https://github.com/aws/aws-iot-device-sdk-java-v2/tree/main/samples
Here's a link to the MQTT client class:下面是 MQTT 客户端 class 的链接:
http://aws-iot-device-sdk-java-docs.s3-website-us-east-1.amazonaws.com/com/amazonaws/services/iot/client/AWSIotMqttClient.html http://aws-iot-device-sdk-java-docs.s3-website-us-east-1.amazonaws.com/com/amazonaws/services/iot/client/AWSIotMqttClient.html
Please note that your app will not have access to messages when not in use.请注意,您的应用在不使用时将无法访问消息。 There are a few strategies to deal with message persistence, but that's outside the scope of your question, so I won't cover it here.
有一些策略可以处理消息持久性,但这不在您问题的 scope 范围内,因此我不会在这里介绍。
Hopefully this gets you pointed in the right direction.希望这能让你指出正确的方向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.