The reason for implementing this architecture is that there is already a mechanism to notify the device using SMS, but it is difficult to investigate because communication is not stable and services of other companies are used. There was a problem. Therefore, the aim is to stabilize it by making it manageable using MQTT.
--Based on the message published from IoT Core, execute the process on the device side --Use Soracom Beam for security
--AWS IoT Core (hereafter IoT Core) --SORACOM Beam (hereinafter Beam)
--Mainly about the source code (AWS SDK) on the device side --Notes on implementation details
First of all, set up IoT Core and Beam.
Please refer to the SORACOM guide as it is very easy to understand. If you follow the procedure, you will not make a mistake.
https://dev.soracom.io/jp/docs/aws_iot_guide_console/
When completed, you will be able to communicate with the device side via IoT Core.
Please refer to the following for the setting method https://dev.soracom.io/jp/start/metadata/
mqtt_sub.py
# -*- coding: utf-8 -*-
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
import time
import json
import subprocess
import requests
SORACOM_METADATA_URL = 'http://metadata.soracom.io/v1/subscriber'
def get_imsi(self):
"""get IMSI from SORACOM API"""
subscriber = json.loads(requests.get(SORACOM_METADATA_URL).text)
return subscriber["imsi"]
#It will be executed when MQTT arrives
def customCallback(client, userdata, message):
message = json.loads(message.payload)["message"]
if message == "REBOOT":
reboot = "sudo reboot"
subprocess.call(reboot, shell=True)
else:
print(message)
pass
def main():
try:
#The identifier of the client. Anything is fine this time.
mqttClient = AWSIoTMQTTClient('aws_iot_sub')
#Communication host settings. Setting the beam endpoint.
mqttClient.configureEndpoint('beam.soracom.io', 1883)
#This is the setting when you go offline.
# configureOfflinePublishQueueing(queueSize, dropBehavior=DROP_NEWEST)
# queueSize:1 or more...queue size is set to the argument value
# queueSize: 0 ...queue invalid
# queueSize: -1 ...Unlimited queue size
# dropBehavior:Setting when queue becomes full DROP if not set_NEWEST
mqttClient.configureOfflinePublishQueueing(0)
#Execution frequency
mqttClient.configureDrainingFrequency(2) # Hz
#client timeout time
mqttClient.configureConnectDisconnectTimeout(10) # sec
#Timeout time for QoS 1
mqttClient.configureMQTTOperationTimeout(5) # sec
#Connection with IoT Core
#You can also set keepalive. The default is 600 seconds
mqttClient.connect()
imsi = get_imsi()
except Exception as err:
print(err)
#If the radio wave is bad, subscribe will be an Exception and python will drop, so try/Loop with except
#The reason why err of except is not output is because the message is not thrown by Exception of subcribe, so even if it is output, it will be empty.
#The topic name should be imsi so that only the environment in which it is running can be subscribed.
while True:
try:
mqttClient.subscribe(str(imsi), 1, customCallback)
time.sleep(1)
except Exception as err:
pass
if __name__ == "__main__":
main()
This time, I introduced only restarting, but it is possible to execute various processes by executing sh. The article posted on the 5th day of advent Calendar 2019, which this article has registered, was interesting, so I think it will be helpful. I wanna try...
I used the AWSIoTPython SDK, but I was able to communicate with Beam using paho as well. With this requirement, the merit of using the SDK is small, and I think that the SDK will be more useful when operating shadows instead of using IoT Core as a broker. However, since paho is running internally, we adopted the SDK that is specialized for AWS.
If you know if there is another good way, please let me know.
Recommended Posts