-
Notifications
You must be signed in to change notification settings - Fork 18
/
mqtt.homie.dhtsensor.py
112 lines (95 loc) · 4.98 KB
/
mqtt.homie.dhtsensor.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/python
# DHT Sensor Data-logging to MQTT Temperature channel
# Requies a Mosquitto Server Install On the destination.
# Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola
# MQTT Encahncements: David Cole (2016)
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import json
import requests
import sys
import argparse
import time
import datetime
import homie
import Adafruit_DHT
#==================================================================================================================================================
#Usage python mqtt.channel.py <temp topic> <humidity topic> <gpio pin> <optional update frequency># Type of sensor, can be
#Adafruit_DHT.DHT11, Adafruit_DHT.DHT22, or Adafruit_DHT.AM2302.
# eg python mqtt.channel.py 'cupboard/temperature1' 'cupboard/humidity1' 4DHT_TYPE = Adafruit_DHT.DHT22 will start an instance using
#'cupboard/temperature1' as the temperature topic, and using gpio port 4 to talk to a DHT22 sensor Example of sensor connected to
#Raspberry Pi pin 23 it will use the default update time of 300 secons.DHT_PIN = sys.argv[3] Example of sensor connected to Beaglebone
#Black pin P8_11 DHT_PIN = 'P8_11'
#==================================================================================================================================================
# Type of sensor, can be Adafruit_DHT.DHT11, Adafruit_DHT.DHT22, or Adafruit_DHT.AM2302.
DHT_TYPE = Adafruit_DHT.DHT22
# Example of sensor connected to Raspberry Pi pin 23
DHT_PIN = 4
# Example of sensor connected to Beaglebone Black pin P8_11
#DHT_PIN = 'P8_11'
# How long to wait (in seconds) between measurements.
FREQUENCY_SECONDS = 200
def main(configfile='homie-dht.json'):
Homie = homie.Homie(configfile)
temperatureNode = Homie.Node("temperature","temperature")
humidityNode = Homie.Node("humidity","humidity")
json_data=open(configfile).read()
data = json.loads(json_data)
FREQUENCY_SECONDS =data["dht"]["frequency"]
DHT_PIN = data["dht"]["pin"]
print('Logging sensor measurements to {0} every {1} seconds.'.format('MQTT', FREQUENCY_SECONDS))
Homie.setFirmware("dht-temperature","1.0.0")
Homie.setup()
while True:
try:
# Attempt to get sensor reading.
print('Performing read on dht')
humidity, temp = Adafruit_DHT.read(DHT_TYPE, DHT_PIN)
print('After dht read')
# Skip to the next reading if a valid measurement couldn't be taken.
# This might happen if the CPU is under a lot of load and the sensor
# can't be reliably read (timing is critical to read the sensor).
if humidity is None or temp is None:
time.sleep(2)
print "Nothing received from sensor"
continue
currentdate = time.strftime('%Y-%m-%d %H:%M:%S')
print('Date Time: {0}'.format(currentdate))
print('Temperature: {0:0.2f} C'.format(temp))
print('Humidity: {0:0.2f} %'.format(humidity))
# Publish to the MQTT channel
print('Connecting to host')
print("Posting Temperature to homie")
Homie.setNodeProperty(temperatureNode,"degrees",temp,True)
Homie.setNodeProperty(humidityNode,"humidity",humidity,True)
except Exception,e:
# Error appending data, most likely because credentials are stale.
# Null out the worksheet so a login is performed at the top of the loop.
print('Append error, logging in again: ' + str(e))
continue
else:
# Wait 30 seconds before continuing
print('Wrote a message tp MQQTT')
time.sleep(FREQUENCY_SECONDS)
if __name__ == '__main__':
try:
parser= argparse.ArgumentParser(description="Homie Based DHT Reader")
parser.add_argument('-c','--configfile', help='Configuration filename (json)',required=True)
args = parser.parse_args()
main(args.configfile)
except (KeyboardInterrupt, SystemExit):
print "quitting."