-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublisher.py
64 lines (52 loc) · 2.68 KB
/
publisher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/python
# importing libraries
import paho.mqtt.client as paho
import ssl
from time import sleep
from random import uniform
connflag = False
def on_connect(client, userdata, flags, rc): # func for making connection
global connflag
print("Connected to AWS")
connflag = True
#if connection is successful, rc value will be 0
print("Connection returned result: " + str(rc) )
#print(flags)
def on_message(client, userdata, msg): # Func for Sending msg
print(msg.topic+" "+str(msg.payload))
#def on_log(client, userdata, level, buf):
# print(msg.topic+" "+str(msg.payload))
mqttc = paho.Client()
#create an mqtt client object
#attach call back function
mqttc.on_connect = on_connect
#attach on_connect function written in the
#mqtt class, (which will be invoked whenever
#mqtt client gets connected with the broker)
#is attached with the on_connect function
#written by you.
mqttc.on_message = on_message # assign on_message func
#attach on_message function written inside
#mqtt class (which will be invoked whenever
#mqtt client gets a message) with the on_message
#function written by you
#### Change following parameters ####
awshost = "XXXXXXXXXXX-ats.iot.us-east-1.amazonaws.com" # Endpoint
awsport = 8883 # Port no.
clientId = "IoT_Test" # Thing_Name
thingName = "IoT_Test" # Thing_Name
caPath = "XXXXXXXXXCA1.pem.crt" #Amazon's certificate from Third party # Root_CA_Certificate_Name
certPath = "XXXXXXXXX-certificate.pem.crt" # <Thing_Name>.cert.pem.crt. Thing's certificate from Amazon
keyPath = "XXXXXXXXXX-private.pem.key" # <Thing_Name>.private.key Thing's private key from Amazon
mqttc.tls_set(caPath, certfile=certPath, keyfile=keyPath, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None) # pass parameters
mqttc.connect(awshost, awsport, keepalive=60) # connect to aws server
mqttc.loop_start() # Start the loop
while 1:
sleep(5)
if connflag == True:
tempreading = uniform(20.0,25.0) # Generating Temperature Readings
message = '{"temperature":'+str(tempreading)+'}'
mqttc.publish("temperatureTopic", message, 1) # topic: temperature # Publishing Temperature values
print("msg sent: temperature " + "%.2f" % tempreading ) # Print sent temperature msg on console
else:
print("waiting for connection...")