-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
123 lines (98 loc) · 3.31 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
from time import sleep_ms, ticks_ms, ticks_diff
from machine import Pin, I2C
from dth import DTH
import utime
import pycom
import machine
from mqtt import MQTTClient
from network import WLAN
import time
# Wireless network
WIFI_SSID = "Mi"
WIFI_PASS = "65yryr9999"
# Adafruit IO (AIO) configuration
AIO_SERVER = "io.adafruit.com"
AIO_PORT = 1883
AIO_USER = "65yryr"
AIO_KEY = "aio_bzPW21cqKB6QuVBrcYgvsm0QmyQP"
AIO_TEMP_FEED = "65yryr/feeds/temp"
AIO_humidity_FEED = "65yryr/feeds/humidity"
AIO_working_FEED = "65yryr/feeds/working"
# RGBLED
# Disable the on-board heartbeat (blue flash every 4 seconds)
# We'll use the LED to respond to messages from Adafruit IO
pycom.heartbeat(False)
time.sleep(0.1) # Workaround for a bug.
# Above line is not actioned if another
# process occurs immediately afterwards
pycom.rgbled(0xff0000) # Status red = not working
# WIFI
# We need to have a connection to WiFi for Internet access
# Code source: https://docs.pycom.io/chapter/tutorials/all/wlan.html
wlan = WLAN(mode=WLAN.STA)
wlan.connect(WIFI_SSID, auth=(WLAN.WPA2, WIFI_PASS), timeout=5000)
while not wlan.isconnected(): # Code waits here until WiFi connects
machine.idle()
print("Connected to Wifi")
pycom.rgbled(0xffd7000) # Status orange: partially working
time.sleep(3)
pycom.heartbeat(False)
#diffinging pins to the sensors
motionsensor = Pin('P23', mode = Pin.IN)
th = DTH(Pin('P3', mode=Pin.OPEN_DRAIN),0)
RANDOMS_INTERVAL = 5000 # milliseconds
last_random_sent_ticks = 0 # milliseconds
last_time = ticks_ms() # save current time
delay = 2
tem = 0 # temperature
hum = 0 # humidity
def send_temp(): #sending temp and var to adafruit
global last_random_sent_ticks
global RANDOMS_INTERVAL
global tem
global hum
if ((time.ticks_ms() - last_random_sent_ticks) < RANDOMS_INTERVAL):
return; # Too soon since last one sent.
else:
try:
client.publish(topic=AIO_TEMP_FEED, msg=str(tem))
client.publish(topic=AIO_humidity_FEED, msg=str(hum))
print("DONE")
except Exception as e:
print("FAILED")
finally:
last_random_sent_ticks = time.ticks_ms()
def tempo(): # measering the humidity and temperature
global tem
global hum
th = DTH(Pin('P3', mode=Pin.OPEN_DRAIN),0)
result = th.read()
if result.is_valid():
tem = result.temperature
hum = result.humidity
print(tem)
print(hum)
print("Starting Detection...")
client = MQTTClient("65yryr", AIO_SERVER,user = AIO_USER,password= AIO_KEY, port = AIO_PORT)
client.connect() # connecting to adafruit
def status(): # checking and alerting if the room is too hot or too humied
global tem
global hum
if tem >= 28 or tem <= 17:
pycom.rgbled(0x7f0000)
elif hum >= 50 or hum <= 20:
pycom.rgbled(0x7f0000)
else:
pycom.rgbled(0x007f00)
while True: # the main loop
if motionsensor() == 1: # if motion is dedected
print("YES")
tempo()
send_temp()
status()
client.publish(topic=AIO_working_FEED, msg="YES")
else:
client.publish(topic=AIO_working_FEED, msg="NO")
status()
print("NO")
time.sleep(2)