[PYTHON] AWS IoT device life and death monitoring

Introduction

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.

Preparation

Since we will use the thing created in Previous article, please implement the contents.

Device side (Python) implementation

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.

API Reference for MQTClient

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

Operation check using AWS IoT console

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.

スクリーンショット 2017-01-15 18.33.17.png

Then send a message to the 1234 / notification topic that the device is waiting for:

スクリーンショット 2017-01-15 18.32.50.png

The device receives the topic message and replies to 1234 / event as follows:

スクリーンショット 2017-01-15 18.38.19.png

in conclusion

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

AWS IoT device life and death monitoring
Easy and easy IoT life using Micropython!
crond is dead, so talk about monitoring life and death with zabbix
I tried Linux (CentOS 7) life and death monitoring (Ping) with monitoring server Zabbix
Easy connection between Raspberry Pi and AWS IoT
Python mock to try AWS IoT Device Shadow
Raspberry Pi and AWS IoT connection program example
[blackbird-dynamodb] Monitoring AWS DynamoDB
[blackbird-rds] Monitoring AWS RDS
[blackbird-sqs] Monitoring AWS SQS
[blackbird-elb] Monitoring AWS ElasticLoadBalancing
I tried AWS Iot
Run AWS IoT Device SDK for Python on Raspberry Pi
Site monitoring and alert notification with AWS Lambda + Python + Slack