/
main.py
178 lines (141 loc) · 4.75 KB
/
main.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
###########################################################################
# 1) connect to internet
# 2) get time
# 3) report data to RPi MQTT
###########################################################################
import machine
import utime
import ntptime
import network
import esp
from dht import DHT11
import ubinascii
from umqtt.robust import MQTTClient
UTC_OFFSET = 3 # hours of differenc between UTC and local (Jerusalem) time
COMMANDS_PORT = 5641 # port at RPi to send commands
RPi_HOST = "10.0.0.17"
TEMPERATURE_LOG_DELAY = 5*60 # minimal number of seconds to wait/
# between subsequent temperature/humidity Logging
TEMPERATURE_SAMPLE_DELAY = 5
MOTION_LOG_DELAY = 5*60 # minimal number of seconds to wait/
# between subsequent motion Logging
clientID = b"chipa_ESP8266"
localBroker = RPi_HOST
def toggleGPIO(p):
p.value(not p.value())
def GPIO_On(p):
p.value(1)
def GPIO_Off(p):
p.value(0)
def getHumidityTemp():
try:
dhtSensor.measure()
temper = dhtSensor.temperature()
humid = dhtSensor.humidity()
except:
(temper, humid) = (None, None)
return temper, humid
def getDateTime():
gotTime = False
utime.sleep(5) # Sleep for 5 seconds, to make sure WiFi is connected
if sta_if.isconnected():
print("Connected to internet, setting time from NTP")
ntptime.settime()
t = rtc.datetime()
print("RTC time is: ", t)
year = t[0]
print("year is: ",year)
gotTime = year > 2016
if gotTime:
print(t)
else:
print("Could not get time")
machine.reset()
return gotTime, t
def getRPiTime():
import socket
addr = socket.getaddrinfo(RPi_HOST, COMMANDS_PORT)[0][-1]
s = socket.socket()
print("Connecting to RPi: ", RPi_HOST)
try:
#TODO Something wrong here, the socket does not connect. \
# Important, after failed connection need to open a new socket.
s.connect(addr)
except:
print("Error connecting to RPi server")
s.close()
return None
s.send("time".encode("utf8"))
s.send("time".encode("utf8"))
time.sleep(3)
data = s.recv(1024)
payload = data.decode("utf8")
if data:
print('commad received: ' + payload)
else:
print('No data received')
s.close()
return None
def pin_interrupt():
global motionDetected, lastMotionTime
lastMotionTime = utime.time()
motionDetected = True
def handleDHT(temper, humid):
global lastTemperatureLogTime
currentTime = utime.time()
print ("Temperature[degC]: ", temper, ", Humidity[%]: ", humid,
"time[s]: ", currentTime)
if (lastTemperatureLogTime == 0) or \
currentTime - lastTemperatureLogTime > TEMPERATURE_LOG_DELAY:
print ("LOGGING temperature and humidity at: ", currentTime)
lastTemperatureLogTime = currentTime
pushSample(temper,"/sensor/Chipa/temperature")
pushSample(humid,"/sensor/Chipa/humidity")
def handlePIR():
global lastMotionTime, lastMotionLogTime
currentTime = utime.time()
print ("Movement detected at: ", lastMotionTime)
if (lastMotionLogTime == 0) or \
(currentTime - lastMotionLogTime) > MOTION_LOG_DELAY:
print ("LOGGING motion at: ", currentTime)
lastMotionLogTime = currentTime
pushSample(lastMotionTime, "/sensor/Chipa/motion")
def pushSample(sample, topic):
global client
client.publish(topic, str(sample))
def MQTT_Connect(client):
isMQTT_Connected = False
while not isMQTT_Connected:
try:
utime.sleep(1)
client.connect()
print("Connected to {}".format(localBroker))
isMQTT_Connected = True
return
except:
print("failed to connect to RPi MQTT broker. waiting 3sec and retrying")
utime.sleep(3)
#Generic Init
print ("Initializing...")
rtc = machine.RTC()
sta_if = network.WLAN(network.STA_IF)
motionDetected = False
lastMotionTime = 0
lastMotionLogTime = 0
lastTemperatureLogTime = 0
dhtSensor = DHT11(machine.Pin(4)) # D2 pin on NodeMCU board. DHT signal pin
pirSig = machine.Pin(5, machine.Pin.IN) # D1 pin on NodeMCU. PIR signal pin
pirSig.irq(handler=lambda p: pin_interrupt(), trigger=machine.Pin.IRQ_RISING)
#MQTT configs
client = MQTTClient(clientID, localBroker)
MQTT_Connect(client)
while True:
#gotTime, curr_tm = getDateTime() # get time
utime.sleep(TEMPERATURE_SAMPLE_DELAY) # Sleep at least one second between each measurement
# sample the temperature and humidity sensor
temper, humid = getHumidityTemp()
handleDHT(temper, humid)
# if detected motion on PIR sensor
if motionDetected:
motionDetected = False
handlePIR()