-
Notifications
You must be signed in to change notification settings - Fork 0
/
IoTClient.py
82 lines (68 loc) · 2.36 KB
/
IoTClient.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!python3
import Adafruit_DHT as DHT
import time
import datetime
import paho.mqtt.client as mqtt
import json
import configparser
import sys
# from multiprocessing import Pool
def OnLog(client, userdata, level, buf=0):
print("[Info] " + buf + "\n")
def OnConnect(client, userdata, flags, rc):
if rc == 0:
print("Connection succeeded\n")
else:
print("Connection failure, returned code = " + str(rc) + "\n")
def OnDisconnect(client, userdata, flags, rc=0):
print("Disconnected, result code was " + str(rc) + "\n")
def OnMessage(client, userdata, msg):
print("Message received: " + str(msg.payload.decode("utf-8")) + "\n")
def isReadingValid(humidity, temperature):
return humidity is not None and temperature is not None
# Read config file
config = configparser.ConfigParser()
config.read(["config.ini", "config.app.ini"])
# Initialize sensor parameters
if config["DEFAULT"]["Sensor"] == "AM2302":
sensor = DHT.AM2302
elif config["DEFAULT"]["Sensor"] == "DHT22":
sensor = DHT.DHT22
elif config["DEFAULT"]["Sensor"] == "DHT11":
sensor = DHT.DHT11
else:
sys.exit('Invalid sensor! Valid sensors are "DHT11", "DHT22" or "AM2302".')
pin = config["DEFAULT"]["Pin"]
sec = int(config["DEFAULT"]["TimeBetweenReadings"])
# Gateway configurations
broker = config["DEFAULT"]["GatewayURL"]
port = int(config["DEFAULT"]["GatewayPort"])
keepalive = int(config["DEFAULT"]["KeepAlive"])
topic = config["DEFAULT"]["TopicOfPublish"]
# Client configurations
client = mqtt.Client(config["DEFAULT"]["DeviceID"])
if config["DEFAULT"].getboolean("UseTls"):
client.tls_set()
client.username_pw_set(config["DEFAULT"]["Username"], config["DEFAULT"]["Password"])
client.on_log = OnLog
client.on_connect = OnConnect
client.on_disconnect = OnDisconnect
client.on_message = OnMessage
print ("Connecting to broker " + broker + "...\n")
client.connect(broker, port, keepalive)
client.loop_start()
while True:
humidity, temperature = DHT.read(sensor, pin)
if isReadingValid(humidity, temperature):
payload = {
"deviceId": config["DEFAULT"]["DeviceID"],
"date": str(datetime.datetime.now()),
"temperature": temperature,
"humidity": humidity
}
print(payload)
client.publish(topic, json.dumps(payload))
time.sleep(sec)
time.sleep(4)
client.loop_stop()
client.disconnect()