def setup_new_config(logger): """ Start a WiFi AP that provides a configuration page. The device automatically reboots and applies modifications upon successful configuration. :param logger: status logger :type logger: LoggerFactory """ # Only one of this thread is allowed to run at a time if not wifi_lock.locked(): with wifi_lock: logger.info("New configuration setup started") # Config uses LED colours to indicate the state of the connection - lock is necessary to disable error pings led_lock.acquire(1) unique_id = ubinascii.hexlify(machine.unique_id()).decode() # set pycom up as access point wlan = WLAN(mode=WLAN.AP, ssid=config.get_config("device_name") + unique_id) # Connect to PmSensor using password set by the user wlan.init( mode=WLAN.AP, ssid=config.get_config("device_name") + unique_id, auth=(WLAN.WPA2, config.get_config("password")), channel=1, antenna=WLAN.INT_ANT, ) # Load HTML via entering 192,168.4.10 to browser wlan.ifconfig( id=1, config=("192.168.4.10", "255.255.255.0", "192.168.4.1", "192.168.4.1"), ) logger.info("Access point turned on as {}".format( config.get_config("device_name") + unique_id)) logger.info( "Configuration website can be accessed at 192.168.4.10") address = socket.getaddrinfo( "0.0.0.0", 80)[0][-1] # Accept stations from all addresses sct = socket.socket() # Create socket for communication sct.settimeout(int( float(config.get_config("config_timeout")) * 60)) # session times out after x seconds gc.collect( ) # frees up unused memory if there was a previous connection sct.bind(address) # Bind address to socket sct.listen(1) # Allow one station to connect to socket pycom.rgbled(0x000055) # Blue LED - waiting for connection get_new_config(sct, logger) wlan.deinit() # turn off wifi gc.collect() logger.info("rebooting...") machine.reset()
def software_update(logger): with wifi_lock: try: logger.info("Over the Air update started") led_lock.acquire(1) # disable all other indicator LEDs pycom.rgbled(0x555500) # Yellow LED # Get credentials from configuration ssid = config.get_config("SSID") password = config.get_config("wifi_password") server_ip = config.get_config("server") port = int(config.get_config("port")) print(ssid, password, server_ip, port) # Perform OTA update ota = WiFiOTA(ssid, password, server_ip, port) # Turn off WiFi to save power w = WLAN() w.deinit() ota.connect() ota.update() time.sleep(5) except Exception as e: logger.exception("Failed to update the device") pycom.rgbled(0x550000) time.sleep(3) finally: # Turn off update mode config.save_configuration({"update": False}) pycom.rgbled(0x000000) led_lock.release() # Reboot the device to apply patches machine.reset()
def software_update(logger): """ Connects to the wlan and fetches updates from a server. After having applied the patches successfully, it reboots the device. :param logger: status logger :type logger: LoggerFactory object """ with wifi_lock: try: logger.info("Over the Air update started") led_lock.acquire(1) # disable all other indicator LEDs pycom.rgbled(0x555500) # Yellow LED # Get credentials from configuration ssid = config.get_config("SSID") password = config.get_config("wifi_password") server_ip = config.get_config("server") port = int(config.get_config("port")) logger.info("SSID: " + str(ssid)) logger.info("server_ip: " + str(server_ip)) logger.info("port: " + str(port)) version = config.get_config("code_version") # Perform OTA update ota = WiFiOTA(logger, ssid, password, server_ip, port, version) # Turn off WiFi to save power w = WLAN() w.deinit() logger.info("connecting...") ota.connect() if ota.update(): new_version = str( ota.get_current_version()) # get updated version config.save_config({"code_version": str(new_version) }) # save new version to config on SD logger.info( "Successfully updated the device from version {} to version {}" .format(version, new_version)) except Exception as e: logger.exception("Failed to update the device") pycom.rgbled(0x550000) # turn LED to RED time.sleep(3) finally: # Turn off update mode config.save_config({"update": False}) # Turn off indicator LED pycom.rgbled(0x000000) led_lock.release() # Reboot the device to apply patches logger.info("rebooting...") machine.reset()
def new_config(logger, arg): """ Thread that turns on access point on the device to modify configurations. Name of access point: PmSensor Password: pmsensor Enter 192.168.4.10 on device browser to get configuration form. Indicator LEDs: Blue - Access point turned on, not connected, Green - Connected, configuration is open on browser, Red - An error has occured The device automatically reboots and applies modifications upon successful configuration. :param thread_name: Thread id :type thread_name: str :param logger: status logger :type logger: LoggerFactory object :param timeout: timeout (in seconds) for user configuration :type timeout: int """ # Only one of this thread is allowed to run at a time if not wifi_lock.locked(): with wifi_lock: logger.info("New configuration setup started") # Config uses LED colours to indicate the state of the connection - lock is necessary to disable error pings led_lock.acquire(1) # set pycom up as access point wlan = network.WLAN(mode=WLAN.AP, ssid=config.get_config("device_name")) # Connect to PmSensor using password set by the user wlan.init(mode=WLAN.AP, ssid=config.get_config("device_name"), auth=(WLAN.WPA2, config.get_config("password")), channel=1, antenna=WLAN.INT_ANT) # Load HTML via entering 192,168.4.10 to browser wlan.ifconfig(id=1, config=('192.168.4.10', '255.255.255.0', '192.168.4.1', '192.168.4.1')) logger.info('Access point turned on as {}'.format( config.get_config("device_name"))) logger.info( 'Configuration website can be accessed at 192.168.4.10') address = socket.getaddrinfo( '0.0.0.0', 80)[0][-1] # Accept stations from all addresses sct = socket.socket() # Create socket for communication sct.settimeout(config.get_config( "config_timeout")) # session times out after x seconds gc.collect( ) # frees up unused memory if there was a previous connection sct.bind(address) # Bind address to socket sct.listen(1) # Allow one station to connect to socket pycom.rgbled(0x000055) # Blue LED - waiting for connection get_new_config(sct, logger) wlan.deinit() # turn off wifi gc.collect() logger.info('rebooting...') machine.reset()
sensor_loggers=sensor_loggers) if gps_on: GPS_Events = EventScheduler(logger=status_logger, data_type="gps", lora=lora) status_logger.info("Initialisation finished") # Blink green three times to identify that the device has been initialised for val in range(3): blink_led((0x005500, 0.5, True)) time.sleep(0.5) # Heartbeat blink that triggers every 5 seconds heartbeat = Timer.Alarm(blink_led, s=5, arg=(0x005500, 0.1, True), periodic=True) # Try to update RTC module with accurate UTC datetime if GPS is enabled and has not yet synchronized if gps_on and update_time_later: # Start a new thread to update time from gps if available _thread.start_new_thread(GpsSIM28.get_time, (rtc, status_logger)) except Exception as e: status_logger.exception("Exception in main") led_lock.acquire() pycom.rgbled(0x550000) while True: time.sleep(5)