Advertisement
Guest User

Untitled

a guest
Oct 27th, 2018
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.64 KB | None | 0 0
  1. #
  2. from machine import Pin # for pin read/write
  3. import network # for wifi conenction
  4. import urequests # for http request
  5. import time # for delay in http request and debouncing
  6.  
  7. def wait_pin_change(pin):
  8.     # wait for pin to change value
  9.     # it needs to be stable for a continuous 20ms
  10.     cur_value = pin.value()
  11.     active = 0
  12.     while active < 20:
  13.         if pin.value() != cur_value:
  14.             active += 1
  15.         else:
  16.             active = 0
  17.         time.sleep_ms(1)
  18.  
  19. # Button shield connects pin D0, normally HIGH
  20. button = Pin(0, Pin.IN)
  21.  
  22. # Onboard ESP chip led
  23. onboardLed = Pin(2, Pin.OUT) # define pin and
  24. onboardLed.value(1)
  25. # 5mm led on D1
  26. led = Pin(5, Pin.OUT)
  27.  
  28. # Prevents spamming of serial
  29. pressedMessagePrinted = False
  30. notPressedMessagePrinted = False
  31.  
  32. # wifi connection
  33. sta = network.WLAN(network.STA_IF)
  34. sta.active(True)
  35. sta.connect("xxxxx", "xxxxxx")
  36.  
  37. while True:
  38.  
  39. # Led indication for Wifi connection
  40.     if sta.isconnected():
  41.         led.value(1)
  42.     else:
  43.         led.value(0)
  44.  
  45.     if not button.value(): #button pressed
  46.         wait_pin_change(button)
  47.         if not pressedMessagePrinted:
  48.             print('Button pressed')
  49.             onboardLed.value(0)
  50.             pressedMessagePrinted = True
  51.             notPressedMessagePrinted = False
  52.             urequests.get("http://192.168.1.102/control?cmd=GPIO,5,1") # garage door relay on
  53.             #time.sleep_ms(100)
  54.             urequests.get("http://192.168.1.102/control?cmd=GPIO,5,0")  # garage door relay off
  55.            
  56.     else: #button not pressed
  57.         #wait_pin_change(button)
  58.         if not notPressedMessagePrinted:
  59.             print('Button not pressed')
  60.             onboardLed.value(1)
  61.             notPressedMessagePrinted = True
  62.             pressedMessagePrinted = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement