def init(self, bInitLTE=False): ''' Initialize local variables used within the script. Checks for network connectivity. If not able to connect after a timeout, resets the machine in order to try and conenct again. Initializes MQTTClient and checks if we are in a continuous GPS read state after wakeup (saved to self instance variables) ''' # Check if network is connected. If not, attempt to connect if bInitLTE: self.initLTE() else: #TODO remove # Check if network is connected. If not, attempt to connect counter = 0 while not self.wlan.isconnected(): # If we surpass some counter timeout and network is still not connected, reset and attempt to connect again if counter > 100000: machine.reset() machine.idle() counter += 1 # Initialize mqttClient self.mqttClient = self._getMqttClient(self.debug) # Check to see if we are in continued gps read (went to sleep and want to continue reading GPS data) if self._getNVS(ConfigGPS.NVS_SLEEP_CONTINUE_GPS_READ) is not None: self.continueGPSRead = True # Erase this key from NVS pycom.nvs_erase(ConfigGPS.NVS_SLEEP_CONTINUE_GPS_READ)
def forget_network(self, network_name): log.info('WiFi STA: Forgetting NVRAM data for network "{}"'.format( network_name)) auth_mode_nvs_key = self.auth_mode_nvs_key(network_name) try: import pycom pycom.nvs_erase(auth_mode_nvs_key) except: pass
def writeLongList(self, name, longVars, maxLen): i = 0 while i < len(longVars) and i < maxLen: pycom.nvs_set(name + str(i) + 'l', struct.unpack(">I", longVars[i][-4:])[0]) pycom.nvs_set(name + str(i) + 'm', struct.unpack(">H", longVars[i][0:2])[0]) i = i + 1 try: pycom.nvs_erase(name + str(i) + 'l') pycom.nvs_erase(name + str(i) + 'm') except: return else: return
def nvs_erase(key): """ Erases a key from non-volatile memory. Does not complain if key does not exist. Some pycom versions throw an error if the key you want to erase is not there. This function does not, making the action idempotent. """ try: return pycom.nvs_erase(key) except: return None
def send(self, dataframe: DataFrame): """ :param dataframe: """ import binascii import pycom self.ensure_lora_socket() # clean up payload from sensor data if pause command was received on last uplink try: _pause = pycom.nvs_get('pause') except: _pause = None if (_pause is None) or (_pause == 0): payload = dataframe.payload_out + binascii.unhexlify(b'000100') elif _pause == 1: payload = binascii.unhexlify(b'000101') log.info('[LoRa] Uplink payload (hex): %s', binascii.hexlify(payload).decode()) # Send payload try: log.info('[LoRa] Sending payload...') outcome = self.lora_manager.lora_send(payload) log.info('[LoRa] Sent %s bytes', outcome) except: log.exception('[LoRa] Transmission failed') return False # Receive downlink message and save integer value for # 1) deep sleep interval in minutes and # 2) pausing payload submission (1=true,0=false) # survives power cycle, reset and deep sleep rx, port = self.lora_manager.lora_receive() if port == 1: sleep = int.from_bytes(rx, "big") if sleep == 0: # use value from settings file log.info( '[LoRa] Received "reset deep sleep interval" command, erasing from NVRAM.' ) try: pycom.nvs_erase('deepsleep') except: pass else: # Use deepsleep interval received via LoRa. log.info( '[LoRa] Received "set deep sleep interval" command, will sleep for %s minutes.', sleep) pycom.nvs_set('deepsleep', sleep) elif port == 2: pause = int.from_bytes(rx, "big") log.info('[LoRa] Received "pause payload submission" command: %s', bool(pause)) pycom.nvs_set('pause', pause) else: log.info('[LoRa] No downlink message processed') return True
def test_pycom_nv_double_erase(self): pycom.nvs_set("test_key_asdf", 1234) pycom.nvs_erase("test_key_asdf") with self.assertRaises(KeyError): pycom.nvs_erase("test_key_asdf")
def test_pycom_nv_get_nonexistent_return_none(self): pycom.nvs_erase("test_key_asdf") # get non-existent key val = pycom.nvs_get("test_key_asdf") self.assertEqual(val, None)
def test_pycom_nv_get_nonexistent_raise_error(self): pycom.nvs_erase("test_key_asdf") # get non-existent key with self.assertRaises(mock_apis.PYCOM_EXCEPTION_ON_NONEXISTENT_KEY): pycom.nvs_get("test_key_asdf")
import pycom key = "nvram_test_key0" # max 15 characters long # print(len(key)) value = 42 pycom.nvs_set(key, value) x = 0 x = pycom.nvs_get(key) print(x) if not x == value: raise "hell" pycom.nvs_erase(key) print("OK")
def nvs_erase_idempotent(key): try: pycom.nvs_erase(key) except KeyError: pass
# Pycom specific modules & a few functions from 'os micropython module' import pycom, machine import os print("just after reboot: temp = %s, count = %s " % (pycom.nvs_get('temp'), pycom.nvs_get('count'))) pycom.nvs_set('temp', 25) pycom.nvs_set('count', 10) print("After initial setting: temp = %s, count = %s " % (pycom.nvs_get('temp'), pycom.nvs_get('count'))) pycom.nvs_erase('temp') # Erase the given key from the NVRAM memory area. print("After selective erase: temp = %s, count = %s " % (pycom.nvs_get('temp'), pycom.nvs_get('count'))) pycom.nvs_erase_all() # Erase the entire NVRAM memory area. print("After full erase: temp = %s, count = %s " % (pycom.nvs_get('temp'), pycom.nvs_get('count'))) pycom.nvs_set('temp', 25) pycom.nvs_set('count', 10) print("Before next reboot: temp = %s, count = %s " % (pycom.nvs_get('temp'), pycom.nvs_get('count'))) ''' pycom.wifi_on_boot([enable]) # Get or set the WiFi on boot flag. pycom.wdt_on_boot([enable]) # Enables the WDT at boot time with the timeout in ms set by the function wdt_on_boot_timeout pycom.wdt_on_boot_timeout([timeout]) # Sets or gets the WDT on boot timeout in milliseconds. The minimum value is 5000 ms. ''' print("system info : %s, cwd : %s" % (os.uname() , os.getcwd()))