--In this article, Raspberry Pi and AWS IoT are already connectable. --For the connection method, refer to This Qiita article. --Write a Pub / Sub Python program using the MQTT protocol.
--Program to publish messages: MQTTpub_test1_qiita.py --Store the time stamp in a message and send it. --For the time stamp, use the UNIX time (epoch seconds) that can be obtained with the time () function. --Program to subscribe to messages: MQTTsub_test1_qiita.py --See the difference between the time stamp stored in the message and the time stamp when it was received. Measure the time lag.
――The following is a program to be executed on Raspberry Pi. --Publish program
MQTTpub_test1_qiita.py
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
import os
import json
import time
CLIENT_ID = "mqtt_pub1"
IOT_ENDPOINT_URL = "XXX-XXX.XXX.ap-northeast-1.amazonaws.com"
IOT_ENDPOINT_PORT = 8883
PATH = os.getcwd()
ROOT_CA_PATH = PATH + "/XXX-CA1.pem"
PRIVATE_KEY_PATH = PATH + "/XXX-private.pem.key"
CERTIFICATE_PATH = PATH + "/XXX-certificate.pem.crt"
KEEP_ALIVE_TIME = 60
TOPIC = "rasp3-mono/timestamp"
myMQTTClient = AWSIoTMQTTClient(CLIENT_ID)
myMQTTClient.configureEndpoint(IOT_ENDPOINT_URL, IOT_ENDPOINT_PORT)
myMQTTClient.configureCredentials(ROOT_CA_PATH, PRIVATE_KEY_PATH, CERTIFICATE_PATH)
myMQTTClient.configureOfflinePublishQueueing(-1)
myMQTTClient.configureDrainingFrequency(2)
myMQTTClient.configureConnectDisconnectTimeout(10)
myMQTTClient.configureMQTTOperationTimeout(5)
def GetTimeStamp():
return time.time()
if __name__ == '__main__':
myMQTTClient.connect(KEEP_ALIVE_TIME)
num = 0
while num < 10:
message = {}
message['num'] = num
message['time'] = GetTimeStamp()
payload = json.dumps(message)
print("payload=", payload)
myMQTTClient.publish(TOPIC, payload, 1)
num += 1
time.sleep(5)
--Subscribe program
MQTTsub_test1_qiita.py
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
import os
import json
import time
CLIENT_ID = "mqtt_sub1"
IOT_ENDPOINT_URL = "XXX-XXX.XXX.ap-northeast-1.amazonaws.com"
IOT_ENDPOINT_PORT = 8883
PATH = os.getcwd()
ROOT_CA_PATH = PATH + "/XXX-CA1.pem"
PRIVATE_KEY_PATH = PATH + "/XXX-private.pem.key"
CERTIFICATE_PATH = PATH + "/XXX-certificate.pem.crt"
KEEP_ALIVE_TIME = 60
TOPIC = "rasp3-mono/timestamp"
myMQTTClient = AWSIoTMQTTClient(CLIENT_ID)
myMQTTClient.configureEndpoint(IOT_ENDPOINT_URL, IOT_ENDPOINT_PORT)
myMQTTClient.configureCredentials(ROOT_CA_PATH, PRIVATE_KEY_PATH, CERTIFICATE_PATH)
myMQTTClient.configureOfflinePublishQueueing(-1)
myMQTTClient.configureDrainingFrequency(2)
myMQTTClient.configureConnectDisconnectTimeout(10)
myMQTTClient.configureMQTTOperationTimeout(5)
def customCallback(client, userdata, message):
dict_message = json.loads(message.payload)
print("num = {}, Time Difference = {}".format(dict_message['num'], time.time() - dict_message['time']))
if __name__ == '__main__':
myMQTTClient.connect(KEEP_ALIVE_TIME)
num = 0
while num < 12:
myMQTTClient.subscribe(TOPIC, 1, customCallback)
num += 1
time.sleep(5)
--Subscribe program execution example --Time lag is displayed
--Program considerations --In the Publish program, the message to be sent used the json format. --The json format received by the Subscribe program is returned to the dictionary type. --Please match XXX to the usage environment.
--Tested with two connection methods. (The program is the same)
――When I ran Pub / Sub 30 times, the result of the time lag was as follows.
――The time lag depends on the communication environment, so it is just for reference. ――As mentioned above, the connection test was completed using the Pub / Sub program.
Recommended Posts