-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmqtt_connector.py
executable file
·69 lines (56 loc) · 2.11 KB
/
mqtt_connector.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
#!/usr/bin/python3
import paho.mqtt.client as mqtt
import dht22_sensor_readings as dht22
import logging
import time
import json
import os
import random
from dotenv import load_dotenv
from typing import Any, Dict
from datetime import datetime
load_dotenv()
BROKER_ADDRESS = os.environ.get('BROKER_ADDRESS')
BROKER_PORT = os.environ.get('BROKER_PORT')
CLIENT_ID = os.environ.get('CLIENT_ID')
CLIENT_PSSWD = os.environ.get('CLIENT_PSSWD')
def main():
logging.basicConfig(
filename='logs/application.log',
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.DEBUG,
datefmt='%Y-%m-%d %H:%M:%S'
)
mqtt_client = mqtt.Client(client_id=CLIENT_ID, clean_session=False)
mqtt_client.username_pw_set(CLIENT_ID, CLIENT_PSSWD)
mqtt_client.max_queued_messages_set(0)
mqtt_client.connect(BROKER_ADDRESS, int(BROKER_PORT))
mqtt_client.on_connect = on_connect
mqtt_client.on_disconnect = on_disconnect
periodically_publish_dht22_data(mqtt_client)
mqtt_client.loop_forever()
def on_disconnect(client: mqtt.Client, userdata: Any, flags: Dict, rc: int) -> None:
logging.warning("MQTT Client Disconnecting...")
def on_connect(client: mqtt.Client, userdata: Any, flags: Dict, rc: int) -> None:
logging.debug('{} MQTT Client Connected to {} {}'.format(
"#" * 30, BROKER_ADDRESS, "#" * 30))
logging.debug('Client connected with flags: {}'.format(flags))
logging.debug('Client connected with return code: {}'.format(rc))
def periodically_publish_dht22_data(client: mqtt.Client) -> None:
while(True):
try:
dts = datetime.now()
res = {
'temp': dht22.fetch_temperature(),
'humidity': dht22.fetch_humidity(),
'dts' : dts.strftime("%d/%m/%Y %H:%M:%S")
}
client.publish('sensor/{}'.format(CLIENT_ID), json.dumps(res))
logging.info('Published Data: ')
logging.info(json.dumps(res))
time.sleep(2)
except Exception as e:
logging.error(e)
continue
if __name__ == '__main__':
main()