-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmq_temp.py
More file actions
64 lines (54 loc) · 1.73 KB
/
mq_temp.py
File metadata and controls
64 lines (54 loc) · 1.73 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
#!/usr/bin/python
import subprocess, time, sys
import Adafruit_DHT
import paho.mqtt.client as mqtt
from time import gmtime,strftime
### custom action ###
def on_connect(client, userdata, flags, rc):
print("Connected code: {}".format(rc))
client.subscribe(topic = "test")
def on_disconnect(client, userdata, rc):
print("Disconnected From Broker")
def on_message(client, userdata, message):
print("message received: " ,str(message.payload.decode("utf-8")))
print("message topic:\t ",message.topic)
print("message qos:\t ",message.qos)
print("retain flag:\t ",message.retain)
### action appointed ###
print("creating new instance")
client = mqtt.Client()
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_message = on_message
print("connecting to broker")
client.connect("140.123.107.170",1883,60)
time.sleep(0.5)
client.loop_start()
###------------------###
sensor = Adafruit_DHT.AM2302
pin = 4
cmd = "(vcgencmd measure_temp)"
while True:
try:
out = subprocess.check_output(cmd,shell=True)
out = float(out[5:9])
print("CPU temp: ",out)
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
print('Temp : {0:.2f}'.format(temperature))
print('Humidity: {0:.2f}%'.format(humidity))
humidity = round(humidity,2)
temperature = round(temperature,2)
datt = strftime("%Y%m%d")
timm = strftime("%H%M%S")
timee = (datt + timm)
#timee = timee.replace(' ','')
sendy = "'EE' {} {} {} {}".format(out,temperature,humidity,timee)
client.publish("sen/temp",sendy)
print("{} {}".format(datt,timm))
print(timee)
print("------------------")
time.sleep(5)
except KeyboardInterrupt:
client.loop_stop()
break