def OTA_update(_ip): ota = WiFiOTA("Guests@Tyndall", "", _ip, 8000) print("Performing OTA") pycom.rgbled(0xDB7093) try: ota.connect() ota.update() except: print("Cannot connect to server!") pass pycom.rgbled(0x000000)
def do_wifi_fota(ssid, password, server_ip, server_port): ota = WiFiOTA(ssid, password, server_ip, server_port) fota_wifi_release_sem.acquire( 0 ) # disconnect wifi but keep the task going to avoid triggering the watchdog is the upgrade is over 5 min # Perform OTA time.sleep(3) try: print("Connecting to " + str(ssid)) ota.connect() print("Updating...") ota.update() except Exception as e: print("Error while int FOTA: " + str(e))
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 receive(self,rx=None): if len(rx) == 0: #No hay mensaje de recepción print('No incoming message') pass else: if rx[0] == 73: #Orden de Cambio de intervalo (ASCII hex I=0x49 dec 73) print("Recibido cambio de intervalo %d" %(int.from_bytes(rx[1:],'big'))) self.sleep_time = int.from_bytes(rx[1:],'big') #Decodifica el valor del nuevo intervalo pycom.nvs_set('sleep_time',self.sleep_time) #Lo guarda en NVRAM elif rx[0] == 82: #Orden de Cambio Data Rate (ASCII hex R=0x52 dec 87) print("Cambiando Data Rate %d" %(int.from_bytes(rx[1:],'big'))) self.dr = int.from_bytes(rx[1:],'big') #Decodifica el valor del nuevo data Rate pycom.nvs_set('data_rate',self.data_rate) #Lo guarda en NVRAM elif rx[0] == 79: #4Fh ota = WiFiOTA('MBP_JuanMa','MBP_JuanMa',"54.154.225.138",8000) #Amazon Web Services Server print("Updating via OTA...") print('- ' * 20) ota.connect() ota.update() else: pass
from time import sleep import pycom import binascii from config import WIFI_SSID, WIFI_PW, SERVER_IP # Turn on GREEN LED pycom.heartbeat(False) pycom.rgbled(0xff) # Setup OTA ota = WiFiOTA(WIFI_SSID, WIFI_PW, SERVER_IP, # Update server address 8000) # Update server port # Turn off WiFi to save power # w = WLAN() # w.deinit() print("Firmware v1.0.1") print("Checking for OTA...") ota.connect() print("Connected to server.") print("Attempting to update...") ota.update() sleep(5)
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 __process_recv_message(self, message): print_debug(5, "This is PybytesProtocol.__process_recv_message()") network_type, message_type, body = self.__pybytes_library.unpack_message(message) print_debug(2, 'Recv message of type{}'.format(message_type)) print_debug(6, "network_type={}, message_type={}\nbody={}".format(network_type, message_type, body)) if self.__user_message_callback is not None: if (message_type == constants.__TYPE_PING): self.send_ping_message() elif message_type == constants.__TYPE_PONG and self.__conf.get('connection_watchdog', True): print_debug(1,'message type pong received, feeding watchdog...') self.__pybytes_connection.__wifi_lte_watchdog.feed() elif (message_type == constants.__TYPE_INFO): self.send_info_message() elif (message_type == constants.__TYPE_NETWORK_INFO): self.send_network_info_message() elif (message_type == constants.__TYPE_SCAN_INFO): self.__send_message(self.__pybytes_library.pack_scan_info_message(self.__pybytes_connection.lora)) elif (message_type == constants.__TYPE_BATTERY_INFO): self.send_battery_info() elif (message_type == constants.__TYPE_OTA): ota = WiFiOTA(self.__conf['wifi']['ssid'], self.__conf['wifi']['password'], self.__conf['ota_server']['domain'], self.__conf['ota_server']['port']) if (self.__pybytes_connection.__connection_status == constants.__CONNECTION_STATUS_DISCONNECTED): print('Connecting to WiFi') ota.connect() print("Performing OTA") result = ota.update() self.send_ota_response(result) time.sleep(1.5) if (result == 2): # Reboot the device to run the new decode machine.reset() elif (message_type == constants.__TYPE_FCOTA): print_debug(2, 'receiving FCOTA request') if (self.__pybytes_connection.__connection_status == constants.__CONNECTION_STATUS_DISCONNECTED): print('Not connected, Re-Connecting ...') ota.connect() command = body[0] if (command == constants.__FCOTA_COMMAND_HIERARCHY_ACQUISITION): self.send_fcota_ping('acquiring hierarchy...') hierarchy = self.__FCOTA.get_flash_hierarchy() self.send_fcota_hierarchy(hierarchy) elif (command == constants.__FCOTA_COMMAND_FILE_ACQUISITION): path = body[1:len(body)].decode() if (path[len(path)-2:len(path)] == '.py'): self.send_fcota_ping('acquiring file...') content = self.__FCOTA.get_file_content(path) size = self.__FCOTA.get_file_size(path) self.send_fcota_file(content, path, size) elif (command == constants.__FCOTA_COMMAND_FILE_UPDATE): bodyString = body[1:len(body)].decode() splittedBody = bodyString.split(',') if (len(splittedBody) >= 2): path = splittedBody[0] print(path[len(path)-7:len(path)]) if (path[len(path)-7:len(path)] != '.pymakr'): self.send_fcota_ping('updating file...') newContent = bodyString[len(path)+1:len(body)] if (self.__FCOTA.update_file_content(path, newContent) == True): size = self.__FCOTA.get_file_size(path) self.send_fcota_file(newContent, path, size) if (path[len(path)-7:len(path)] != '.pymakr'): time.sleep(2) self.send_fcota_ping('board restarting...') time.sleep(2) reset() else: self.send_fcota_ping('pymakr archive updated!') else: self.send_fcota_ping('file update failed!') else: self.send_fcota_ping("file update failed!") elif (command == constants.__FCOTA_PING): self.send_fcota_ping('') elif (command == constants.__FCOTA_COMMAND_FILE_DELETE): self.send_fcota_ping('deleting file...') path = body[1:len(body)].decode() success = self.__FCOTA.delete_file(path) if (success == True): self.send_fcota_ping('file deleted!') self.send_fcota_hierarchy(self.__FCOTA.get_flash_hierarchy()) else: self.send_fcota_ping('deletion failed!') else: print("Unknown FCOTA command received") elif (message_type == constants.__TYPE_PYBYTES): command = body[0] pin_number = body[1] value = 0 if (len(body) > 3): value = body[2] << 8 | body[3] if (command == constants.__COMMAND_PIN_MODE): pass elif (command == constants.__COMMAND_DIGITAL_READ): pin_mode = None try: pin_mode = self.__pin_modes[pin_number] except Exception as ex: pin_mode = Pin.PULL_UP self.send_pybytes_digital_value(False, pin_number, pin_mode) elif (command == constants.__COMMAND_DIGITAL_WRITE): if (not pin_number in self.__pins): self.__configure_digital_pin(pin_number, Pin.OUT, None) pin = self.__pins[pin_number] pin(value) elif (command == constants.__COMMAND_ANALOG_READ): self.send_pybytes_analog_value(False, pin_number) elif (command == constants.__COMMAND_ANALOG_WRITE): if (not pin_number in self.__pins): self.__configure_pwm_pin(pin_number) pin = self.__pins[pin_number] pin.duty_cycle(value * 100) elif (command == constants.__COMMAND_CUSTOM_METHOD): if (pin_number == constants.__TERMINAL_PIN and self.__terminal_enabled): self.__terminal.message_sent_from_pybytes_start() terminal_command = body[2: len(body)] terminal_command = terminal_command.decode("utf-8") try: out = eval(terminal_command) if out is not None: print(repr(out)) else: print('\n') except: try: exec(terminal_command) print('\n') except Exception as e: print('Exception:\n ' + repr(e)) self.__terminal.message_sent_from_pybytes_end() return if (self.__custom_methods[pin_number] is not None): parameters = {} for i in range(2, len(body), 3): value = body[i: i + 2] parameters[i / 3] = (value[0] << 8) | value[1] method_return = self.__custom_methods[pin_number](parameters) if (method_return is not None and len(method_return) > 0): self.send_pybytes_custom_method_values(pin_number, method_return) else: print("WARNING: Trying to write to an unregistered Virtual Pin") else: try: self.__user_message_callback(message) except Exception as ex: print(ex)