Connect to AWS IoT from mqttcli
mqttcli that, to develop a MQTT client running in the CLI, to the public.
Let's connect to AWS IoT from this mqttcli.
Download mqttcli
mqttcli files from,
- Linux (arm / amd64)
- FreeBSD (arm / amd64)
- Darwin (amd64)
- Windows (amd64)
There are prepared, please download the binary suitable for your own architecture. Then, chmod u+x mqttcli and please grant execute permissions.
Make things with AWS IoT
- From AWS console AWS IoT Open.
- Create Resource from Create Thing select the
- Enter the Name, Create press
- Since the Thing of the name that you just entered in the list below comes out, select, from the right of the tag Connect a Device click.
- Connect a Device since coming out of the screen that, NodeJS press, Generate Certificate and Policy Press
- After about 10 seconds,
- Download Public Key
- Download Private Key
- Download Certificate
You are instructed to download three files, so download them all.
Confirm & Start Connecting press. Then, the following JSON is displayed, so copy it and save it in a file.
Code-Block .. :: json
- {
"Host" : "A3HVHEAALED.Iot.Ap-northeast-1.Amazonaws.Com", "Port" : 8883, "clientId" : "Something", "ThingName" : "Something", "CaCert" : "Root-CA .Crt "," ClientCert " : " 2A338xx2xxf-Certificate.Pem.Crt "," privateKey " : " Aad380efffx-Private.Pem.Key "
}
root-CA.crt というファイルを AWS IoT SDKのここ _ as it is written on, `こちらのSymantecのページ Authority-G5.pem> `_ please be obtained from.
Please put the three files you downloaded earlier, the JSON file and root - CA.crt in the same directory.
This is the end of the preparation.
Connect to AWS IoT
Go to the directory containing the file and start mqttcli as follows. -t to specify the topic in but, $ you might need to escape. --conf is a JSON file that you just saved to specify in. -d is for debugging.
$ mqttcli sub -t "\$aws/things/something/shadow/update" --conf something.json -d
INFO[0000] Broker URI: ssl://A3HVHEAALED.iot.ap-northeast-1.amazonaws.com:8883
INFO[0000] Topic: $aws/things/something/shadow/update
INFO[0000] connecting...
INFO[0000] client connected
With success it is successful. I can connect with MQTT.
Update Thing Shadow
To update Thing Shadow send JSON as shown below.
{
"state": {
"reported": {
"from": "mqttcli"
}
}
}
Let's send it with mqttcli
echo '{"state": {"reported": {"from": "mqttcli"} } }"' | mqttcli pub -t "\$aws/things/something/shadow/update" --conf something.json -d -s
Now, from the AWS Console, the state should be rewritten.
In this way, I was able to touch AWS IoT with mqttcli and. I also confirm that the same thing can be done by mosquitto_sub.
At last
Let's use AWS IoT's SDK instead of hitting MQTT directly. Then you do not have to be conscious of MQTT like this.