Ejemplo n.º 1
0
def main():
    logging.debug("=== MAIN START ===")

    # Increase stack size per thread this increases micropython recursion depth
    _thread.stack_size(8192 * 2)

    # Read and parse configuration from config.json
    utils.init()

    controller = MainController()

    # Check if configuration via access point has to be started
    if not config.cfg.ap_config_done or wake_reason() == PIN_WAKE:
        logging.debug("AP_DONE: {}, wake_reason: {}".format(
            config.cfg.ap_config_done, wake_reason()))
        logging.debug("SSID: {}, Password: {}".format(config.cfg.ssid,
                                                      config.cfg.password))
        if config.cfg.ssid != 'ssid' and config.cfg.password != 'password':
            logging.debug("SSID and password aren't default. Try to connect")
            pass
        else:
            logging.debug("=== Entering configuration mode ===")

            event = MainControllerEvent(
                MainControllerEventType.CONFIGURE_ACCESS_POINT)
            controller.add_event(event)

    logging.debug("Main loop")
    # If the device is powered on, then actual time from NTP server must be downloaded

    if reset_cause() == HARD_RESET or reset_cause(
    ) == PWRON_RESET or reset_cause() == SOFT_RESET:
        event = MainControllerEvent(MainControllerEventType.TEST_CONNECTION)
        controller.add_event(event)

    # Print actual time
    event = MainControllerEvent(MainControllerEventType.PRINT_TIME)
    controller.add_event(event)

    # Read temperature and humidity from the sensor and return the data as JSON
    event = MainControllerEvent(MainControllerEventType.GET_SENSOR_DATA)
    controller.add_event(event)

    # Connect to WIFI and publish JSON with data to AWS via MQTT
    event = MainControllerEvent(MainControllerEventType.PUBLISH_DATA)
    controller.add_event(event)

    # Good night!
    event = MainControllerEvent(MainControllerEventType.GO_TO_SLEEP,
                                callback=None,
                                ms=config.cfg.data_publishing_period_in_ms)
    controller.add_event(event)

    controller.perform()