In the previous article (http://qiita.com/yokobonbon/items/be0da968bba40b396187), I explained that it was possible to easily emulate IoT devices with Python.
This time, I would like to easily monitor the life and death of devices using the Pub / Sub mechanism of AWS IoT.
Since we will use the thing created in Previous article, please implement the contents.
On the device side, it is assumed that the client certificate and the UUID of the device are specified for execution. It is a simple implementation that registers a topic for life and death monitoring on the device side, and if a message enters there, it will reply.
Below is a sample code for Python
class AWSIoTClient:
def __init__(self, endpoint, rootCA, key, cert, uuid):
self.myMQTTClient = AWSIoTMQTTClient("python-thing")
self.myMQTTClient.configureEndpoint(endpoint, 8883)
self.myMQTTClient.configureCredentials(rootCA, key, cert)
self.myMQTTClient.configureOfflinePublishQueueing(-1) # Infinite offline Publish queueing
self.myMQTTClient.configureDrainingFrequency(2) # Draining: 2 Hz
self.myMQTTClient.configureConnectDisconnectTimeout(10) # 10 sec
self.myMQTTClient.configureMQTTOperationTimeout(5)
self.notificationTopic = uuid + "/notification"
self.eventTopic = uuid + "/event"
def start(self):
self.myMQTTClient.connect()
print "Wating for : " + self.notificationTopic
self.myMQTTClient.subscribe(self.notificationTopic, 1, self.customCallback)
def customCallback(self, client, userdata, message):
print message.payload
callbackMessage = "{\n I'm healthy\n}"
client.publish(self.eventTopic, callbackMessage, 1)
def main():
args = Args()
client = AWSIoTClient(args.getEndpoint(), args.getRootCA(), args.getKey(), args.getCert(),
args.getUUID())
client.start()
while True:
time.sleep(1)
The MQTTClient subscribe method monitors the "UUID / notification" topic. When a message is written to the topic, the customCallback method is called to write the message in the "UUID / event" topic. As a result, life and death monitoring is simply realized.
The value 1 in the second argument of the subscribe method represents MQTT's QoS.
The implementation of the python application works as follows
python python-thing.py -e XXXX.iot.ap-northeast-1.amazonaws.com -r root-CA.crt -c test_thing.cert.pem -k test_thing.private.key -u 1234
Now that the device is ready, you can send a message to the topic from the AWS IoT console. In the AWS IoT console, you can select "Test" to send a message to a topic "Publish" or read a message "Subscribe".
First, the 1234 / event topic will be returned with a health check from the device, so subscribe as follows.
Then send a message to the 1234 / notification topic that the device is waiting for:
The device receives the topic message and replies to 1234 / event as follows:
Life and death monitoring can be easily realized by returning a message to another topic when a message comes to a specific topic. AWS IoT is great because this can be achieved by yourself in a few hours.
Recommended Posts