changes to only send req when change
This commit is contained in:
@@ -5,17 +5,21 @@ from machine import Pin, time_pulse_us
|
||||
|
||||
DOOR_OPEN_THRESHOLD = 50 # in cm
|
||||
|
||||
echo_pin = Pin(0, Pin.IN)
|
||||
trig_pin = Pin(1, Pin.OUT)
|
||||
echoPin = Pin(0, Pin.IN)
|
||||
trigPin = Pin(1, Pin.OUT)
|
||||
|
||||
led = Pin("LED", Pin.OUT)
|
||||
|
||||
prevDoorStatus = 67
|
||||
|
||||
def getDistanceCM():
|
||||
trig_pin.value(0)
|
||||
trigPin.value(0)
|
||||
time.sleep_us(2)
|
||||
trig_pin.value(1)
|
||||
trigPin.value(1)
|
||||
time.sleep_us(10)
|
||||
trig_pin.value(0)
|
||||
trigPin.value(0)
|
||||
|
||||
pulse_time = time_pulse_us(echo_pin, 1, 30000)
|
||||
pulse_time = time_pulse_us(echoPin, 1, 30000)
|
||||
|
||||
if pulse_time < 0:
|
||||
return -1
|
||||
@@ -29,6 +33,8 @@ def sendDoorStatus(status):
|
||||
#r = urequests.post(requestURL, data={"status", status})
|
||||
|
||||
def checkDoorDistance():
|
||||
global prevDoorStatus # Tell Python to use the global variable
|
||||
|
||||
dist = -1
|
||||
timeoutTime = 10
|
||||
while timeoutTime > 0 and dist == -1:
|
||||
@@ -36,13 +42,26 @@ def checkDoorDistance():
|
||||
timeoutTime -= 1
|
||||
time.sleep(1)
|
||||
|
||||
status = 0;
|
||||
if dist < DOOR_OPEN_THRESHOLD:
|
||||
sendDoorStatus(1) # door open
|
||||
status = 1 # door open
|
||||
led.on()
|
||||
elif dist == -1:
|
||||
sendDoorStatus(-1) # unknown
|
||||
status = -1 # unknown
|
||||
else:
|
||||
sendDoorStatus(0) # door closed
|
||||
status = 0 # door closed
|
||||
led.off()
|
||||
|
||||
if status != prevDoorStatus:
|
||||
sendDoorStatus(status)
|
||||
prevDoorStatus = status
|
||||
|
||||
def flashLED():
|
||||
flashes = 10
|
||||
while flashes > 0:
|
||||
led.toggle()
|
||||
time.sleep_ms(500)
|
||||
flashes -= 1
|
||||
|
||||
def connectToNet():
|
||||
ssid = "ITRnet"
|
||||
@@ -63,6 +82,7 @@ def connectToNet():
|
||||
if wlan.status() != 3:
|
||||
raise RuntimeError('network connection failed')
|
||||
else:
|
||||
flashLED()
|
||||
print('connected!')
|
||||
status = wlan.ifconfig()
|
||||
print('ip = ' + status[0])
|
||||
|
||||
Reference in New Issue
Block a user