Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import time
from machine import Pin
from ir_tx import Player # from Peter Hinch's micropython_ir library
from uasyncio import asyncio

IR_TX_PIN = 17
OFF_INTERVAL_SECONDS = 3600 #runs every hour

PIR_PIN = 15

OFF_PULSES = [
6076, 7397, 555, 1634, 551, 1651, 557, 1633, 551, 1651,
557, 1606, 552, 1625, 531, 1625, 559, 1636, 548, 582,
Expand Down Expand Up @@ -38,11 +41,31 @@ def send_off(player: Player):
player.play(OFF_PULSES)
print("OFF frame sent.")

def main():
async def periodic_off(event):
ir_player = make_player(IR_TX_PIN)
while True:
send_off(ir_player)
time.sleep(OFF_INTERVAL_SECONDS)
try:
await asyncio.wait_for(event, OFF_INTERVAL_SECONDS)
event.clear()
# If we get here, we detected motion, the timer was reset.
except asyncio.TimeoutError:
send_off(ir_player)
# If we get here, we timed out. Turn the AC off

def monitor_pir_sensor(event):
trigger_pin = Pin(PIR_PIN, Pin.IN, Pin.PULL_UP)

def gpio_triggered(pin):
event.set()

# Level triggered IRQ - if the sensor continues to detect motion, it should continue to trigger
trigger_pin.irq(trigger=Pin.IRQ_HIGH_LEVEL, handler=gpio_triggered)

def main():
event = asyncio.Event()
monitor_pir_sensor(event)
asyncio.run(periodic_off(event))

if __name__ == "__main__":
main()