Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- from machine import Pin # for pin read/write
- import network # for wifi conenction
- import urequests # for http request
- import time # for delay in http request and debouncing
- def wait_pin_change(pin):
- # wait for pin to change value
- # it needs to be stable for a continuous 20ms
- cur_value = pin.value()
- active = 0
- while active < 20:
- if pin.value() != cur_value:
- active += 1
- else:
- active = 0
- time.sleep_ms(1)
- # Button shield connects pin D0, normally HIGH
- button = Pin(0, Pin.IN)
- # Onboard ESP chip led
- onboardLed = Pin(2, Pin.OUT) # define pin and
- onboardLed.value(1)
- # 5mm led on D1
- led = Pin(5, Pin.OUT)
- # Prevents spamming of serial
- pressedMessagePrinted = False
- notPressedMessagePrinted = False
- # wifi connection
- sta = network.WLAN(network.STA_IF)
- sta.active(True)
- sta.connect("xxxxx", "xxxxxx")
- while True:
- # Led indication for Wifi connection
- if sta.isconnected():
- led.value(1)
- else:
- led.value(0)
- if not button.value(): #button pressed
- wait_pin_change(button)
- if not pressedMessagePrinted:
- print('Button pressed')
- onboardLed.value(0)
- pressedMessagePrinted = True
- notPressedMessagePrinted = False
- urequests.get("http://192.168.1.102/control?cmd=GPIO,5,1") # garage door relay on
- #time.sleep_ms(100)
- urequests.get("http://192.168.1.102/control?cmd=GPIO,5,0") # garage door relay off
- else: #button not pressed
- #wait_pin_change(button)
- if not notPressedMessagePrinted:
- print('Button not pressed')
- onboardLed.value(1)
- notPressedMessagePrinted = True
- pressedMessagePrinted = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement