def main(): global relay_pin client = MQTTClient(CLIENT_ID, SERVER) client.set_callback(new_msg) try: client.connect() except OSError: print("MQTT Broker seems down") print("Resetting after 20 seconds") time.sleep(20) machine.reset() client.subscribe(COMMAND_TOPIC) # Publish as available once connected client.publish(AVAILABILITY_TOPIC, "online", retain=True) switch_pin = machine.Pin(5, machine.Pin.IN, machine.Pin.PULL_UP) reed_switch = Switch(switch_pin) # Initialize state of garage door after booting up if switch_pin.value(): client.publish(STATE_TOPIC, "open", retain=True) else: client.publish(STATE_TOPIC, "closed", retain=True) relay_pin = machine.Pin(4, machine.Pin.OUT, 0) try: while True: reed_switch_new_value = False # Disable interrupts for a short time to read shared variable irq_state = machine.disable_irq() if reed_switch.new_value_available: reed_switch_value = reed_switch.value reed_switch_new_value = True reed_switch.new_value_available = False machine.enable_irq(irq_state) # If the reed switch had a new value, publish the new state if reed_switch_new_value: if reed_switch_value: client.publish(STATE_TOPIC, "open") else: client.publish(STATE_TOPIC, "closed") # Process any MQTT messages if client.check_msg(): client.wait_msg() time.sleep_ms(500) finally: client.publish(AVAILABILITY_TOPIC, "offline", retain=False) client.disconnect() machine.reset()
def main(): my_switch = Switch(buzzer_button) while True: my_switch_new_value = False # Disable interrupts for a short time to read shared variable irq_state = machine.disable_irq() if my_switch.new_value_available: my_switch_value = my_switch.value my_switch_new_value = True my_switch.new_value_available = False machine.enable_irq(irq_state) # If my switch had a new value, print the new state if my_switch_new_value: if not my_switch_value: request_photo()
def main(): switch_pin = machine.Pin(5, machine.Pin.IN, machine.Pin.PULL_UP) my_switch = Switch(switch_pin) while True: my_switch_new_value = False # Disable interrupts for a short time to read shared variable irq_state = machine.disable_irq() if my_switch.new_value_available: my_switch_value = my_switch.value my_switch_new_value = True my_switch.new_value_available = False machine.enable_irq(irq_state) # If my switch had a new value, print the new state if my_switch_new_value: if my_switch_value: print("Switch Opened") else: print("Switch Closed") time.sleep(1)
rgb_end_content = [135, 120, 0] rgb_sparkle_content = [255, 255, 255] rgb_blue = [0, 0, 255] rgb_red = [255, 0, 0] while True: my_switch_green_new_value = False my_switch_red_new_value = False # Disable interrupts for a short time to read shared variable irq_state = machine.disable_irq() if my_switch_green.new_value_available: my_switch_green_value = my_switch_green.value my_switch_green_new_value = True my_switch_green.new_value_available = False if my_switch_red.new_value_available: my_switch_red_value = my_switch_red.value my_switch_red_new_value = True my_switch_red.new_value_available = False machine.enable_irq(irq_state) # If my switch had a new value, print the new state if my_switch_green_new_value: if my_switch_green_value: print("Green Switch Opened") else: print("Green Switch Closed") if my_switch_red_new_value: