In [27]:
# Import SDK packages
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient

# For certificate based connection
#myMQTTClient = AWSIoTMQTTClient("myClientID")
# For Websocket connection
myMQTTClient = AWSIoTMQTTClient("pynq-board-test", useWebsocket=True)
# Configurations
# For TLS mutual authentication
#myMQTTClient.configureEndpoint("a1y3khtyn0ojiv.iot.eu-west-1.amazonaws.com", 8883)
# For Websocket
myMQTTClient.configureEndpoint("a1c2qwq6b794bg.iot.eu-west-1.amazonaws.com", 443)
#myMQTTClient.configureCredentials("./test_aws/rootCA.pem", "./test_aws/privateKey.pem", "./test_aws/cert.pem")
# For Websocket, we only need to configure the root CA
myMQTTClient.configureIAMCredentials("AKIAIM2GJTGGBVMKUSFQ", "OWgtnHoV2d9IL3/8K/RmKlthJ/lLBMTC9QnC7yQj")
myMQTTClient.configureCredentials("./test_aws/rootCA.pem")
myMQTTClient.configureOfflinePublishQueueing(-1)  # Infinite offline Publish queueing
myMQTTClient.configureDrainingFrequency(2)  # Draining: 2 Hz
myMQTTClient.configureConnectDisconnectTimeout(10)  # 10 sec
myMQTTClient.configureMQTTOperationTimeout(5)  # 5 sec
In [28]:
# Test connection with the AWS server
myMQTTClient.connect()
Out[28]:
True
In [29]:
#connect sensor TEMPERATURE SENSOR TH02
from pynq.iop.grove_th02 import Grove_TH02
from pynq.iop.grove_th02 import PMOD_GROVE_G4
from pynq.iop.grove_th02 import PMODB

sensor = Grove_TH02(PMODB,PMOD_GROVE_G4)

print(sensor.read())
(23.875, 42.125)
In [31]:
# Publish something on a topic
import time
import random
timeStart = time.time()
#read data every second for a minute
while((time.time() - timeStart) < 60):
    #temperature = sensor.read()[0]
    #if sensor not available
    temperature = random.gauss(25,2)
    myMQTTClient.publish("boards/pynq-luca", '{{"temperature": {}}}'.format(temperature), 0)
    print(temperature)
    time.sleep(1)
21.60870072818319
26.48774661828841
24.17025283502069
22.40702698058316
27.485393879607344
24.985232673316055
24.63022597021598
28.246805349414434
26.655401171910697
24.144517869128116
26.07564474178549
27.055029329836707
25.6582019365923
23.73670891805874
24.990228055821163
20.321476444943062
23.74591434806335
23.224890590091604
22.569177994589772
20.67812567983205
25.074899204152977
23.495210304650133
27.46978644991387
25.543907884521452
26.715328243735975
24.66357254170752
23.06958191297559
31.196694578713043
23.728674681691924
25.027696181072216
26.2259491717929
26.216388000305308
25.495108834618296
26.892870028215317
23.48546130194338
28.07405808658609
23.872919672445832
25.262995661066668
24.07508508340749
26.145189705934214
22.73601328217763
21.83084275182484
26.604993070102775
26.529943870394032
27.116254817347798
26.531587808306796
25.231492588377744
23.802796011931818
22.738657569951407
24.32846615516354
27.492207404834343
26.178418756865117
24.43241463363202
23.560197503993592
21.846219342442357
24.600170010618562
25.481533418323394
24.283709064515545
25.605953574575516
25.40895855999786
In [ ]:
# Disconnect from the server
myMQTTClient.disconnect()
In [ ]: