def aggregate(): try: pycom.nvs_set('val', int(pycom.nvs_get('val')) + 1) except: pycom.nvs_set('val', 0) print('Deepsleep Remaining: ' + str(machine.remaining_sleep_time())) print('New Aggregate: ' + str(pycom.nvs_get('val'))) print('Current Message ID: ' + str(pycom.nvs_get('msgID')))
from machine import ADC adc = ADC() vpin = adc.channel(pin='P13') for i in range (0,999): volts+=vpin.voltage()/0.24444/1000 print("volts/i) return volts/i if (machine.wake_reason()[0])==1: #pin wakeup pycom.rgbled(0x7f0000) # green total_count = pycom.nvs_get('counter') +1 pycom.nvs_set('counter', total_count) print('remaining deepsleep time is {}'.format(machine.remaining_sleep_time())) print('Counted: {} people'.format(total_count)) while chrono.read() < 5: pass machine.pin_deepsleep_wakeup(pins = ['P10'], mode = machine.WAKEUP_ANY_HIGH, enable_pull = True) print("RST1: Wake & Sleep") pycom.rgbled(0) utime.sleep(0.5) if machine.remaining_sleep_time()-5000 < 1: sleeping = 1 else: sleeping = machine.remaining_sleep_time()-5000
""" TOCS example usage """ import machine from machine import Pin import time Wake_reason = { 0: 'None', machine.PWRON_WAKE: 'PWRON_WAKE', machine.PIN_WAKE: 'PIN_WAKE', machine.RTC_WAKE: 'RTC_WAKE', machine.ULP_WAKE: 'ULP_WAKE' } print("Wake reason is: %s and remaining sleep duration = %s" % (Wake_reason[machine.wake_reason()[0]], machine.remaining_sleep_time())) def pin_handler(arg): time.sleep(0.5) print("Setting %s to listen for wakeup" % (arg.id())) machine.pin_deepsleep_wakeup([arg.id()], machine.WAKEUP_ALL_LOW, True) print("Going into deepsleep: %s" % (arg.id())) machine.deepsleep(10000) p_in = Pin('P10', mode=Pin.IN, pull=Pin.PULL_UP) p_in.callback(Pin.IRQ_FALLING, pin_handler)
def run(hw, run_state): if run_state == STATE_UNKNOWN: _logger.warning("Unknown start state. Default to crash recovery") run_state = STATE_CRASH_RECOVERY if run_state == STATE_RECORD_FLASH: record_flash(hw) remaining = machine.remaining_sleep_time() if remaining: scheduled_state = next_state_on_boot() _logger.info( "Before-wake scheduled state: 0x%02x; Sleep remaining: %d ms", scheduled_state, remaining) return (remaining, scheduled_state) else: _logger.warning( "Could not determine previous schedule before wake, rescheduling..." ) return (0, STATE_SCHEDULE) # In case of unexpected reset (e.g. watch-dog), go to crash recovery next_state_on_boot(STATE_CRASH_RECOVERY) # Turn on peripherals hw.power_peripherals(True) # Trying to access the SD card too quickly often results in IO errors _logger.info("Giving hardware a moment after power on") time.sleep_ms(100) if run_state == STATE_CRASH_RECOVERY: crash_recovery_sequence(hw) return (0, STATE_SCHEDULE) if run_state == STATE_QUICK_HW_TEST: next_state_on_boot(STATE_LTE_TEST) import co2unit_self_test co2unit_self_test.wdt = wdt co2unit_self_test.quick_test_hw(hw) return (0, STATE_LTE_TEST) if run_state == STATE_LTE_TEST: next_state_on_boot(STATE_UPDATE) import co2unit_self_test co2unit_self_test.wdt = wdt co2unit_self_test.test_lte_ntp(hw) return (0, STATE_UPDATE) if run_state == STATE_SCHEDULE: _logger.info("Scheduling only....") return schedule_wake(hw) if run_state == STATE_UPDATE: next_state_on_boot(STATE_COMMUNICATE) set_persistent_settings() import co2unit_update co2unit_update.wdt = wdt updated = co2unit_update.update_sequence(hw) # After update, try communicating again so we know it worked return (0, STATE_COMMUNICATE) if run_state == STATE_MEASURE: flash_count = nv_flash_count() import co2unit_measure co2unit_measure.wdt = wdt co2unit_measure.measure_sequence(hw, flash_count=flash_count) _logger.info("Resetting flash count after recording it") nv_flash_count(0) if run_state == STATE_COMMUNICATE: import co2unit_comm co2unit_comm.wdt = wdt lte, got_updates = co2unit_comm.comm_sequence(hw) if got_updates: _logger.info("Updates downloaded") return (0, STATE_UPDATE) return schedule_wake(hw)
def go_back_to_sleep(): machine.pin_deepsleep_wakeup(pins=['P2'], mode=machine.WAKEUP_ANY_HIGH) machine.deepsleep(machine.remaining_sleep_time())