-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiMon.py
executable file
·144 lines (126 loc) · 3.52 KB
/
piMon.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/python
# does not give complete path: !/usr/bin/python3
#import sys
#print(sys.path)
#to check the path was correct
import atexit
def exit_handler():
print('EXITING')
GPIO.cleanup()
atexit.register(exit_handler)
#for time.sleep()
import time
import datetime as dt
import os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
# Wanted to use BOARD pin numbering for GPIO instead of BCM
# for sake of simplicity, but the Adafruit Nokia LCD Library uses BCM
led = 21
buzzer = 16
GPIO.setup(led, GPIO.OUT)
GPIO.setup(buzzer, GPIO.OUT)
# Callback for button press events
def btnPressed(btn):
GPIO.output(led,1)
GPIO.output(buzzer,1)
time.sleep(0.010)
GPIO.output(buzzer,0)
GPIO.output(led,0)
print('Button press detected on channel %s'%btn)
if btn==19:
time.sleep(2)
if GPIO.input(19)==False:
lcd.Init()
lcd.Print(0,10, 'SHUTTING DOWN.')
time.sleep(3)
os.system("sudo shutdown -h now")
btns = [26,19,13,6]
# Button Numbering= UP, DOWN, LEFT, RIGHT, ENTER
for btn in btns:
GPIO.setup(btn, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(btn,GPIO.FALLING,callback=btnPressed,bouncetime=250)
import threading
import Adafruit_DHT
sensor = Adafruit_DHT.DHT11
pin = 17
class dht11ThreadClass(threading.Thread):
def __init__(self, delay):
threading.Thread.__init__(self)
self.delay = delay
def run(self):
while True:
time.sleep(self.delay)
global humidity, temperature
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
dht11Thread = dht11ThreadClass(15)
dht11Thread.start()
class ds18b20ThreadClass(threading.Thread):
def __init__(self, delay):
threading.Thread.__init__(self)
self.delay = delay
def run(self):
while True:
time.sleep(self.delay)
tfile = open("/sys/bus/w1/devices/28-031574561eff/w1_slave")
text = tfile.read()
tfile.close()
temperature2_data = text.split()[-1]
global temperature2
temperature2 = float(temperature2_data[2:])
temperature2 = temperature2 / 1000
ds18b20Thread = ds18b20ThreadClass(15)
ds18b20Thread.start()
import subprocess
from wireless import Wireless
def getNetworkInfo():
global IP, ssid
wireless = Wireless()
ssid = wireless.current()
p = subprocess.Popen(['hostname', '-I'], stdout=subprocess.PIPE,stderr=subprocess.PIPE)
IP, err = p.communicate()
import lcd
class lcdThreadClass(threading.Thread):
def __init__(self, delay):
threading.Thread.__init__(self)
self.delay = delay
def run(self):
while True:
try:
time.sleep(self.delay)
x=0
y=8
getNetworkInfo()
lcd.Init()
lcd.Print(x,y*0, ssid)
lcd.Print(x,y*1, IP)
lcd.Print(x,y*2, dt.datetime.now().strftime('%d/%m')+' - '+dt.datetime.now().strftime('%H:%M'))
lcd.Print(x,y*3, ' T1=' + "{:.0f}".format(temperature) + '/H1=' + "{:.0f}".format(humidity))
lcd.Print(x,y*4, ' T2=' + "{:.1f}".format(temperature2))
except:
pass
lcdThread = lcdThreadClass(15)
lcdThread.start()
#from time import localtime, strftime
#import time as time
import thingspeak
import thingspeakChannelKey
class thingSpeakThreadClass(threading.Thread):
def __init__(self, delay):
threading.Thread.__init__(self)
self.delay = delay
def run(self):
while True:
time.sleep(self.delay)
channel_id = 122289
write_key = thingspeakChannelKey.getKey()
channel = thingspeak.Channel(id=channel_id,write_key=write_key)
try:
response = channel.update({1:temperature2, 2:temperature, 3:humidity})
#print response
except:
print "connection failed"
thingSpeakThread = thingSpeakThreadClass(30)
thingSpeakThread.start()
while True:
time.sleep(15)