-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultisensor.py
More file actions
65 lines (60 loc) · 1.9 KB
/
multisensor.py
File metadata and controls
65 lines (60 loc) · 1.9 KB
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
#!/usr/bin/python
########################################################
__author__ = "Edward Boal <ed.boal@edwork.org>"
__license__ = "GPL3"
__version__ = "1.0"
### RPI-MQTT-JSON-Multisensor
#
# Setup: Fill in variables under the configuration section
# See following for more information: https://github.com/edwork/RPI-MQTT-JSON-Multisensor
#
########################################################
import time
import json
import paho.mqtt.publish as publish
import Adafruit_DHT as dht
from gpiozero import MotionSensor
## Configuration
# MQTT Server Information
MQTT_HOST = 'ip-of-your-mqtt-server'
MQTT_PORT = 1883
MQTT_USER = 'noobuser'
MQTT_PASSWORD = 'lamepass'
MQTT_CLIENT_ID = 'pi-sensor-1'
MQTT_TOPIC_PREFIX = 'hass/pisensornode'
## Sensor Information
TEMP_SENSOR_PIN = 17 ## GPIO PIN
MOTION_SENSOR_PIN = 4 ## GPIO PIN
## Setup
sensor_data = {}
pir = MotionSensor(MOTION_SENSOR_PIN)
auth_info = {
'username':MQTT_USER,
'password':MQTT_PASSWORD
}
try:
while True:
humidity,temperature = dht.read_retry(dht.DHT22,
TEMP_SENSOR_PIN
)
humidity = round(humidity, 3) ## Round to 3 places
temperature = round(temperature, 3) ## Round to 3 places
if pir.motion_detected:
motion = 1
else:
motion = 0
sensor_data['temperature'] = temperature
sensor_data['humidity'] = humidity
sensor_data['motion'] = motion
## Publish the message to the MQTT Broker
publish.single('hass/testpi/sensor',
json.dumps(sensor_data),
hostname = MQTT_HOST,
client_id = MQTT_CLIENT_ID,
auth = auth_info,
port = MQTT_PORT
)
## Delay before running again
time.sleep(1)
except KeyboardInterrupt:
pass