Esempio n. 1
0
async def run_event_test(lp):
    print('Test Lock class')
    loop = asyncio.get_event_loop()
    lock = asyn.Lock()
    loop.create_task(run_lock(1, lock))
    loop.create_task(run_lock(2, lock))
    loop.create_task(run_lock(3, lock))
    print('Test Event class')
    event = asyn.Event(lp)
    loop.create_task(eventset(event))
    await eventwait(event)  # run_event_test runs fast until this point
    print('Event status {}'.format('Incorrect' if event.is_set() else 'OK'))
    print('Tasks complete')
    try:
        uos.mount(sd, "/sd")
        break
    except:
        utime.sleep_ms(100)

# create file exception.txt if it does not yet exist
files = uos.listdir("/sd")
if not 'exception.txt' in files:
    f = open('/sd/exception.txt', mode='wt', encoding='utf-8')
    f.close()

# wrap the application in a global exception catcher
try:
    loop = asyncio.get_event_loop(ioq_len=2)
    lock = asyn.Lock()
    event_new_pm_data = asyn.Event(PM_POLLING_DELAY_MS)
    event_mqtt_publish = asyn.Event()
    
    spec_sensors = SpecSensors()
    temp_hum = THSensor()
    ps = ParticulateSensor(lock, event_new_pm_data)
    display = Display()
    
    if modes[operating_mode].aq == 'periodic':
        interval_timer = IntervalTimer(event_mqtt_publish)
    
    if modes[operating_mode].logging == 1:
        sdcard_logger = SDCardLogger()
        
    mic = Microphone()
Esempio n. 3
0
STATUS_TOPIC = 'home/garage/door/status'    # mqtt topic to publish to for status
# esp8266 pins
DOOR_TOGGLE_PIN = 15            # Pick a pin with internal pull-down
TRIG_PIN = 16
ECHO_PIN = 12
# data for calculating door state
CHECK_DOOR_INTERVAL = .05       # Interval to check door status, in minutes
DOOR_OPEN_DISTANCE = .5         # Distance (m) from sensor to door when open
SPEED_OF_SOUND = 29.1           # Speed of sound (cm/us)

### Globals
# Generate unique name based on uid and start mqtt client
umqtt_uid = PROJECT + "_" + ''.join('%02X' % b for b in machine.unique_id())
umqtt_client = MQTTClient(umqtt_uid, MQTT_SERVER)
# Lock for accessing mqtt client
umqtt_lock = asyn.Lock()
mqtt_connected = False
# Coroutine loop
loop = asyncio.get_event_loop()


# On my garage door opener, door is toggled by shorting the two bell wires
# that come from it. They are fed to a relay, which is controlled by
# the DOOR_TOGGLE_PIN
async def toggle_door():
    p = machine.Pin(DOOR_TOGGLE_PIN, machine.Pin.OUT)
    p.value(1)
    await asyncio.sleep_ms(100)
    p.value(0)