5 changed files with 188 additions and 0 deletions
@ -0,0 +1,13 @@ |
|||
import board |
|||
import pulseio |
|||
|
|||
alm = pulseio.PWMOut(board.GP1,frequency=2200,duty_cycle=0) |
|||
|
|||
def alarm(action=True): |
|||
if action: |
|||
alm.duty_cycle = 6500 |
|||
else: |
|||
alm.duty_cycle = 0 |
|||
|
|||
if __name__ == "__main__": |
|||
alarm() |
@ -0,0 +1,2 @@ |
|||
import storage |
|||
storage.remount("/", readonly=False) |
@ -0,0 +1,141 @@ |
|||
import time |
|||
from smoke import mesure_smoke |
|||
from drop import mesure_drop |
|||
from temperature import mesure_temp |
|||
from electricity import mesure_electricity |
|||
from erequest import check_netstatus,postdata,rt,eth_init,check_time,get_data,post_data |
|||
from alarm import alarm |
|||
from light import flash_led |
|||
from led import show_num |
|||
from config import SERVER_URL,MAX_ALM,TIMESPAN,A_FIRE,A_TEMP,A_HUM,SN |
|||
import board |
|||
import digitalio |
|||
import microcontroller |
|||
|
|||
btn = digitalio.DigitalInOut(board.GP0) |
|||
btn.direction = digitalio.Direction.INPUT |
|||
|
|||
def make_alarm(errcode): |
|||
global MAX_ALM |
|||
if MAX_ALM and errcode < 7: |
|||
alarm(True) |
|||
flash_led('alarm') |
|||
else: |
|||
flash_led('alarm') |
|||
alarm(False) |
|||
|
|||
def stay_normal(): |
|||
alarm(False) |
|||
flash_led('normal') |
|||
|
|||
def button_handle(): |
|||
global MAX_ALM |
|||
if btn.value == True and MAX_ALM == True: |
|||
MAX_ALM = False |
|||
elif btn.value == True and MAX_ALM == False: |
|||
# recover to alarm |
|||
MAX_ALM = True |
|||
|
|||
def update_config(cmd, param): |
|||
try: |
|||
with open('config.py','r') as f: |
|||
cfgstr = f.read() |
|||
cmdbegin = cfgstr.find(cmd) |
|||
cmdend = cfgstr[cmdbegin:].find('\r\n') |
|||
if cmdend < 0: |
|||
cmdend = cfgstr[cmdbegin:].find('\n') |
|||
newstr = cfgstr.replace(cfgstr[cmdbegin:cmdbegin+cmdend],cmd+'='+param) |
|||
with open('config.py','w') as f2: |
|||
f2.write(newstr) |
|||
return True |
|||
except: |
|||
return False |
|||
|
|||
def env_detect(): |
|||
global rt |
|||
errno = 0 # error code |
|||
conn = True |
|||
if not eth_init(): |
|||
time.sleep(5) |
|||
microcontroller.reset() |
|||
if check_netstatus == 1: |
|||
errono = 1 |
|||
show_num(errono) |
|||
conn = False |
|||
time.sleep(600) |
|||
microcontroller.reset() |
|||
return None |
|||
try: |
|||
check_time() |
|||
except: |
|||
errono = 1 |
|||
show_num(errno) |
|||
em = sm = dm = 0 |
|||
tm = [0,0] |
|||
sm = mesure_smoke() |
|||
if sm > A_FIRE: |
|||
show_num(10) |
|||
time.sleep(180) # wait smoke sensor to get ready |
|||
begin_time = time.monotonic() # for counting timespan |
|||
while True: |
|||
i = 0 |
|||
button_handle() |
|||
try: |
|||
em = mesure_electricity() |
|||
sm = mesure_smoke() |
|||
dm = mesure_drop() |
|||
tm = mesure_temp() |
|||
while tm[0] == 0 and i < 5: |
|||
tm = mesure_temp() |
|||
i += 1 |
|||
except: |
|||
errno = 8 # measure fail |
|||
if sm > A_FIRE: |
|||
errno = 6 # fire alarm |
|||
elif dm == True: |
|||
errno = 5 # water drop alarm |
|||
elif em == False: |
|||
errno = 4 # power off |
|||
elif tm[0] > A_TEMP: |
|||
errno = 2 # hot temperature |
|||
elif tm[1] > A_HUM: |
|||
errno = 3 # high humidity |
|||
elif conn == False: |
|||
errno = 7 |
|||
else: |
|||
errno = 0 |
|||
data = {"sn":SN, "errno":errno, "electricity":em, "fire":sm, "water":dm, "temperature":tm[0], "humidity":tm[1]} |
|||
print(data) |
|||
if (time.monotonic()-begin_time)/60 >= TIMESPAN: |
|||
try: |
|||
post_data(SERVER_URL+'/env/api/',data) |
|||
conn = True |
|||
except: |
|||
errno = 7 # Sending data fail |
|||
time.sleep(2) |
|||
conn = False |
|||
microcontroller.reset() |
|||
begin_time = time.monotonic() |
|||
show_num(errno) |
|||
if errno == 0: |
|||
stay_normal() |
|||
else: |
|||
make_alarm(errno) |
|||
if rt.datetime[4] == 0 and rt.datetime[5] < 5: |
|||
microcontroller.reset() |
|||
time.sleep(3) |
|||
try: |
|||
cmddata = {'spot':SN} |
|||
cmd = get_data(SERVER_URL+'/env/cmd/',cmddata) |
|||
if cmd and cmd.count('$')==1: |
|||
cmdlist = cmd.split('$') |
|||
if not cmdlist[1]: |
|||
microcontroller.reset() |
|||
if update_config(cmdlist[0],cmdlist[1]): |
|||
microcontroller.reset() |
|||
except: |
|||
pass |
|||
|
|||
if __name__ == '__main__': |
|||
env_detect() |
|||
|
@ -0,0 +1,13 @@ |
|||
SERVER_URL='http://172.18.17.136:8000' |
|||
IP_ADDR='2.50.41.115' |
|||
MAC_ADDR='de:ad:be:82:31:10' |
|||
NET_MASK='255.255.255.128' |
|||
GATEWAY='2.50.41.1' |
|||
DNS='114.114.114.114' |
|||
SN='WJSPJ001' |
|||
SECRET='@This is a Secret $tr!' |
|||
MAX_ALM=True # alarm sound |
|||
TIMESPAN=5 # minute |
|||
A_FIRE=18000 # fire alarm trigger number |
|||
A_TEMP=32.0 |
|||
A_HUM=75.0 |
@ -0,0 +1,19 @@ |
|||
import board |
|||
import digitalio |
|||
import time |
|||
|
|||
dropio = digitalio.DigitalInOut(board.GP9) |
|||
dropio.direction = digitalio.Direction.INPUT |
|||
|
|||
def mesure_drop(): |
|||
try: |
|||
# print(dropio.value) |
|||
return dropio.value |
|||
except: |
|||
return 0 |
|||
|
|||
if __name__ == "__main__": |
|||
while True: |
|||
# print(dropio.value) |
|||
time.sleep(1) |
|||
print(mesure_drop()) |
Loading…
Reference in new issue