示例#1
0
    def power_off_lte_modem(self):
        """
        We don't use LTE yet.

        https://community.hiveeyes.org/t/lte-modem-des-pycom-fipy-komplett-stilllegen/2161
        https://forum.pycom.io/topic/4877/deepsleep-on-batteries/10
        """

        import pycom
        """
        if not pycom.lte_modem_en_on_boot():
            log.info('Skip turning off LTE modem')
            return
        """

        log.info('Turning off LTE modem')
        try:
            from network import LTE

            # Invoking this will cause `LTE.deinit()` to take around 6(!) seconds.
            #log.info('Enabling LTE modem on boot')
            #pycom.lte_modem_en_on_boot(True)

            log.info('Turning off LTE modem on boot')
            pycom.lte_modem_en_on_boot(False)

            log.info('Invoking LTE.deinit()')
            lte = LTE()
            lte.deinit()

        except:
            log.exception('Shutting down LTE modem failed')
示例#2
0
def lte_connect():
    lte = LTE()
    lte.init()
    #some carriers have special requirements, check print(lte.send_at_cmd("AT+SQNCTM=?")) to see if your carrier is listed.
    #when using verizon, use
    #lte.init(carrier=verizon)
    #when usint AT&T use,
    #lte.init(carrier=at&t)
    #some carriers do not require an APN
    #also, check the band settings with your carrier
    lte.attach(band=2, apn="vzwinternet")
    print("attaching..", end='')
    while not lte.isattached():
        time.delay(0.25)

        print('.', end='')
        print(lte.send_at_cmd('AT!="fsm"'))  # get the System FSM
    print("attached!")

    lte.connect()
    print("connecting [##", end='')
    while not lte.isconnected():
        time.sleep(0.25)
        print('#', end='')
        #print(lte.send_at_cmd('AT!="showphy"'))
        print(lte.send_at_cmd('AT!="fsm"'))
    print("] connected!")

    print(socket.getaddrinfo('pycom.io', 80))
    lte.deinit()
示例#3
0
def lte_shutdown(lte: LTE, detach=True):
    if lte.isconnected():
        print(">> disconnecting LTE")
        lte.disconnect()
    if detach and lte.isattached():
        print(">> detaching LTE")
        lte.detach()
    print(">> de-initializing LTE")
    lte.deinit(detach=False, reset=False)
示例#4
0
def sendData(dataList, deviceKey):
    # ******************** Hologram endpoint Definition
    HOST = "cloudsocket.hologram.io"
    PORT = 9999
    TOPIC = "SENSOR_DATA"

    blink(1, 0xffffff)  # blink white
    # Set up LTE connection
    lte = LTE()
    lte.init()
    print("Resetting LTE modem ... ", end="")
    lte.send_at_cmd('AT^RESET')
    print("Reset OK")
    time.sleep(1)
    # While the configuration of the CGDCONT register survives resets,
    # the other configurations don't. So just set them all up every time.
    print("Configuring LTE ", end='')
    # Changed this from origninal
    lte.send_at_cmd('AT+CGDCONT=1,"IP","hologram"')
    print(".", end='')
    # changed band from 28 to 4. I dont know what earfcn=9410 is;
    lte.send_at_cmd('AT!="RRC::addscanfreq band=4 dl-earfcn=9410"')
    print(".", end='')
    # lte.send_at_cmd

    # Do the attach (Enable radio functionality and attach to the LTE network authorized by the inserted SIM card)
    lte.attach()
    print("attaching..", end='')
    while not lte.isattached():
        blink(1, 0x0000ff)  # blue
        print('.', end='')
    # print(lte.send_at_cmd('AT!="fsm"'))         # get the System FSM
    print("attached!")

    # Do the connect (Start a data session and obtain and IP address)
    lte.connect()
    print("connecting [##", end='')
    while not lte.isconnected():
        time.sleep(1)
        print('#', end='')
    print("] connected!")
    blink(1, 0x00ff00)  # Green

    # **** Send data to hologram
    bodyData = buildData(dataList)
    lteSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    lteSocket.connect(socket.getaddrinfo(HOST, PORT)[0][-1])
    data = '{"k": "%s", "d": "{%s}", "t": "%s"}' % (deviceKey, bodyData, TOPIC)
    print("Send Data:", data)
    lteSocket.send(data)

    # Clean up and close connection
    lteSocket.close()
    lte.deinit()
    print("Disconnected")
    blink(1, 0xff0000)  # red
示例#5
0
    def power_off_lte_modem(self):
        """
        We don't use LTE yet.

        Important
        =========
        Once the LTE radio is initialized, it must be de-initialized
        before going to deepsleep in order to ensure minimum power consumption.
        This is required due to the LTE radio being powered independently and
        allowing use cases which require the system to be taken out from
        deepsleep by an event from the LTE network (data or SMS received for
        instance).

        Note
        ====
        When using the expansion board and the FiPy together, the RTS/CTS
        jumpers MUST be removed as those pins are being used by the LTE radio.
        Keeping those jumpers in place will lead to erratic operation and
        higher current consumption specially while in deepsleep.

        -- https://forum.pycom.io/topic/3090/fipy-current-consumption-analysis/17

        See also
        ========
        - https://community.hiveeyes.org/t/lte-modem-des-pycom-fipy-komplett-stilllegen/2161
        - https://forum.pycom.io/topic/4877/deepsleep-on-batteries/10
        """

        log.info('Turning off LTE modem')
        try:
            import pycom
            from network import LTE

            log.info('Turning off LTE modem on boot')
            pycom.lte_modem_en_on_boot(False)

            # Disables LTE modem completely. This presumably reduces the power
            # consumption to the minimum. Call this before entering deepsleep.
            log.info('Invoking LTE.deinit()')
            lte = LTE()
            lte.deinit(detach=False, reset=True)

        except Exception as ex:
            log.exc(ex, 'Shutting down LTE modem failed')
示例#6
0
def sleep():
    global client
    client.disconnect()  # Disconnects from Ubidots server.
    client = None
    print("Disconnected from Ubidots.")

    lte = LTE()
    lte.deinit(
    )  # Disables the LTE modem to lower the power consumption during deepsleep.

    global chrono
    time = 300 - chrono.read(
    )  # Calculates the time in seconds for how long the deepsleep will be active.
    print("Going to sleep for %s seconds." % (time))
    sleepTime = int(
        time *
        1000)  # Converts the time to milliseconds and the data type to int.
    machine.deepsleep(
        sleepTime
    )  # Turns deepsleep on. When time has passed the FipY will reboot and read the code from the top of the file.
示例#7
0
def connect_lte(timeout=30):
    lte = LTE()  # instantiate the LTE object
    lte.deinit()
    lte.init()

    lte.attach()  # attach the cellular modem to a base station
    cycles = 0
    while not lte.isattached():
        sleep(1)
        cycles += 1
        if cycles > timeout:
            raise Exception("Failed to attach cellular modem to base station")

    lte.connect()  # start a data session and obtain an IP address
    cycles = 0
    while not lte.isconnected():
        sleep(1)
        cycles += 1
        if cycles > timeout:
            raise Exception("Failed to obtain cellular data session")

    return lte
class PybytesConnection:
    def __init__(self, config, message_callback):
        if config is not None:
            self.__conf = config
            try:
                self.__host = pycom.nvs_get('pybytes_server')
            except:
                self.__host = config.get('server')
            self.__ssl = config.get('ssl', False)
            self.__ssl_params = config.get('ssl_params', {})
            self.__user_name = config.get('username')
            self.__device_id = config.get('device_id')
            self.__mqtt_download_topic = "d" + self.__device_id
            self.__mqtt_upload_topic = "u" + self.__device_id
            self.__pybytes_protocol = PybytesProtocol(
                config, message_callback, pybytes_connection=self
            )
        self.__connection = None
        self.__connection_status = constants.__CONNECTION_STATUS_DISCONNECTED
        self.__lora_socket = None
        self.lora = None
        self.lora_lock = _thread.allocate_lock()
        self.__sigfox_socket = None
        self.lte = None
        self.wlan = None
        self.__network_type = None
        self.__wifi_lte_watchdog = None

    def lte_ping_routine(self, delay):
        while True:
            self.send_ping_message()
            time.sleep(delay)

    def print_pretty_response(self, rsp):
        lines = rsp.split('\r\n')
        for line in lines:
            if line:
                if line not in ['OK']:
                    print(line)

    def __initialise_watchdog(self):
        if self.__conf.get('connection_watchdog', True):
            self.__wifi_lte_watchdog = WDT(
                timeout=constants.__WDT_TIMEOUT_MILLISECONDS
            )
            print('Initialized watchdog for WiFi and LTE connection with timeout {} ms'.format(constants.__WDT_TIMEOUT_MILLISECONDS)) # noqa
        else:
            print('Watchdog for WiFi and LTE was disabled, enable with "connection_watchdog": true in pybytes_config.json') # noqa

    # Establish a connection through WIFI before connecting to mqtt server
    def connect_wifi(self, reconnect=True, check_interval=0.5, timeout=120):
        self.__initialise_watchdog()

        if self.__connection_status != constants.__CONNECTION_STATUS_DISCONNECTED: # noqa
            print("Error connect_wifi: Connection already exists. Disconnect First") # noqa
            return False
        try:
            from network import WLAN
            antenna = self.__conf.get('wlan_antenna', WLAN.INT_ANT)
            known_nets = [((self.__conf['wifi']['ssid'], self.__conf['wifi']['password']))] # noqa
            if antenna == WLAN.EXT_ANT:
                print("WARNING! Using external WiFi antenna.")

            '''to connect it to an existing network,
            the WiFi class must be configured as a station'''

            self.wlan = WLAN(mode=WLAN.STA, antenna=antenna)

            attempt = 0

            print_debug(3, 'WLAN connected? {}'.format(self.wlan.isconnected()))

            while not self.wlan.isconnected() and attempt < 3:
                attempt += 1
                print_debug(3, "Wifi connection attempt: {}".format(attempt))
                print_debug(3, 'WLAN connected? {}'.format(self.wlan.isconnected()))
                available_nets = None
                while available_nets is None:
                    try:
                        available_nets = self.wlan.scan()
                        for x in available_nets:
                            print_debug(5, x)
                        time.sleep(1)
                    except:
                        pass

                nets = frozenset([e.ssid for e in available_nets])
                known_nets_names = frozenset([e[0]for e in known_nets])
                net_to_use = list(nets & known_nets_names)
                try:
                    net_to_use = net_to_use[0]
                    pwd = dict(known_nets)[net_to_use]
                    sec = [e.sec for e in available_nets if e.ssid == net_to_use][0] # noqa
                    print_debug(99, "Connecting with {} and {}".format(net_to_use, pwd))
                    if  sec == 0:
                        self.wlan.connect(net_to_use, timeout=10000)
                    else:
                        self.wlan.connect(net_to_use, (sec, pwd), timeout=10000)
                    start_time = time.time()
                    while not self.wlan.isconnected():
                        if time.time() - start_time > timeout:
                            raise TimeoutError('Timeout trying to connect via WiFi')
                        time.sleep(0.1)
                except Exception as e:
                    if str(e) == "list index out of range" and attempt == 3:
                        print("Please review Wifi SSID and password inside config")
                        self.wlan.deinit()
                        return False
                    elif attempt == 3:
                        print("Error connecting using WIFI: %s" % e)
                        return False

            self.__network_type = constants.__NETWORK_TYPE_WIFI
            print("WiFi connection established")
            try:
                self.__connection = MQTTClient(
                    self.__device_id,
                    self.__host,
                    self.__mqtt_download_topic,
                    self.__pybytes_protocol,
                    user=self.__user_name,
                    password=self.__device_id
                )
                self.__connection.connect()
                self.__connection_status = constants.__CONNECTION_STATUS_CONNECTED_MQTT_WIFI # noqa
                self.__pybytes_protocol.start_MQTT(
                    self,
                    constants.__NETWORK_TYPE_WIFI
                )
                return True
            except Exception as ex:
                if '{}'.format(ex) == '4':
                    print('MQTT ERROR! Bad credentials when connecting to server: "{}"'.format(self.__host)) # noqa
                else:
                    print("MQTT ERROR! {}".format(ex))
                return False
        except Exception as ex:
            print("Exception connect_wifi: {}".format(ex))
            return False

    # Establish a connection through LTE before connecting to mqtt server
    def connect_lte(self, activation_info=False, start_mqtt=True):
        if activation_info:
            lte_cfg = activation_info
        else:
            lte_cfg = self.__conf.get('lte')
            self.__initialise_watchdog()

        if lte_cfg is not None:
            if (os.uname()[0] not in ['FiPy', 'GPy']):
                print("You need a device with FiPy or GPy firmware to connect via LTE") # noqa
                return False
            try:
                from network import LTE
                time.sleep(3)
                if lte_cfg.get('carrier', 'standard') == 'standard':
                    carrier = None
                else:
                    carrier = lte_cfg.get('carrier')
                print_debug(1, 'LTE init(carrier={}, cid={})'.format(carrier, lte_cfg.get('cid', 1))) # noqa
                # instantiate the LTE object
                self.lte = LTE(carrier=carrier, cid=lte_cfg.get('cid', 1))
                try:
                    lte_type = lte_cfg.get('type') if len(lte_cfg.get('type')) > 0 else None
                except:
                    lte_type = None
                try:
                    lte_apn = lte_cfg.get('apn') if len(lte_cfg.get('type')) > 0 else None
                except:
                    lte_apn = None
                try:
                    lte_band = int(lte_cfg.get('band'))
                except:
                    lte_band = None
                print_debug(
                    1,
                    'LTE attach(band={}, apn={}, type={})'.format(
                        lte_band,
                        lte_apn,
                        lte_type
                    )
                )

                self.lte.attach(band=lte_band, apn=lte_apn, type=lte_type)  # noqa   # attach the cellular modem to a base station
                while not self.lte.isattached():
                    time.sleep(0.25)
                time.sleep(1)
                print_debug(1, 'LTE connect()')
                # start a data session and obtain an IP address
                self.lte.connect()
                print_debug(1, 'LTE is_connected()')
                while not self.lte.isconnected():
                    time.sleep(0.25)
                print("LTE connection established")
                self.__network_type = constants.__NETWORK_TYPE_LTE

                if start_mqtt:
                    try:
                        self.__connection = MQTTClient(
                            self.__device_id,
                            self.__host,
                            self.__mqtt_download_topic,
                            self.__pybytes_protocol,
                            user=self.__user_name,
                            password=self.__device_id
                        )
                        self.__connection.connect()
                        self.__connection_status = constants.__CONNECTION_STATUS_CONNECTED_MQTT_LTE # noqa
                        self.__pybytes_protocol.start_MQTT(
                            self,
                            constants.__NETWORK_TYPE_LTE
                        )
                        print("Connected to MQTT {}".format(self.__host))
                        return True
                    except Exception as ex:
                        if '{}'.format(ex) == '4':
                            print('MQTT ERROR! Bad credentials when connecting to server: "{}"'.format(self.__host)) # noqa
                        else:
                            print("MQTT ERROR! {}".format(ex))
                        return False
            except Exception as ex:
                print("Exception connect_lte: {}".format(ex))
                sys.print_exception(ex)
            return False
        else:
            print("Error... missing configuration!")
            return False

    # LORA
    def connect_lora_abp(self, lora_timeout, nanogateway):
        print_debug(1,'Attempting to connect via LoRa')
        if (self.__connection_status != constants.__CONNECTION_STATUS_DISCONNECTED): # noqa
            print("Error connect_lora_abp: Connection already exists. Disconnect First") # noqa
            return False
        try:
            from network import LoRa
        except Exception as ex:
            print("This device does not support LoRa connections: %s" % ex)
            return False

        lora_class = self.__conf.get('lora', {}).get('class', 0)
        if self.__conf.get('lora', {}).get('region') is not None:
            self.lora = LoRa(mode=LoRa.LORAWAN, region=self.__conf.get('lora').get('region'), device_class=lora_class)
        else:
            self.lora = LoRa(mode=LoRa.LORAWAN, device_class=lora_class)
        self.lora.nvram_restore()

        try:
            dev_addr = self.__conf['lora']['abp']['dev_addr']
            nwk_swkey = self.__conf['lora']['abp']['nwk_skey']
            app_swkey = self.__conf['lora']['abp']['app_skey']
        except Exception as ex:
            print("Invalid LoRaWAN ABP configuration!")
            print_debug(1, ex)
            return False
        timeout_ms = self.__conf.get('lora_timeout', lora_timeout) * 1000

        dev_addr = struct.unpack(">l", binascii.unhexlify(dev_addr.replace(' ', '')))[0] # noqa
        nwk_swkey = binascii.unhexlify(nwk_swkey.replace(' ', ''))
        app_swkey = binascii.unhexlify(app_swkey.replace(' ', ''))

        try:
            print("Trying to join LoRa.ABP for %d seconds..." % self.__conf.get('lora_timeout', lora_timeout))
            self.lora.join(
                activation=LoRa.ABP,
                auth=(dev_addr, nwk_swkey, app_swkey),
                timeout=timeout_ms
            )

            # if you want, uncomment this code, but timeout must be 0
            # while not self.lora.has_joined():
            #     print("Joining...")
            #     time.sleep(5)

            self.__open_lora_socket(nanogateway)
#            print_debug(5, 'Stack size: {}'.format(self.__thread_stack_size))
#            _thread.stack_size(self.__thread_stack_size)
#            _thread.start_new_thread(self.__check_lora_messages, ())
            return True
        except Exception as e:
            message = str(e)
            if message == 'timed out':
                print("LoRa connection timeout: %d seconds" % self.__conf.get('lora_timeout', lora_timeout))
            else:
                print_debug(3, 'Exception in LoRa connect: {}'.format(e))
            return False

    def connect_lora_otaa(self, lora_timeout, nanogateway):
        print_debug(1,'Attempting to connect via LoRa')
        if (self.__connection_status != constants.__CONNECTION_STATUS_DISCONNECTED): # noqa
            print("Error connect_lora_otaa: Connection already exists. Disconnect First") # noqa
            return False
        try:
            from network import LoRa
        except Exception as ex:
            print("This device does not support LoRa connections: %s" % ex)
            return False

        try:
            dev_eui = self.__conf['lora']['otaa']['app_device_eui']
            app_eui = self.__conf['lora']['otaa']['app_eui']
            app_key = self.__conf['lora']['otaa']['app_key']
        except Exception as ex:
            print("Invalid LoRaWAN OTAA configuration!")
            print_debug(1, ex)
            return False

        timeout_ms = self.__conf.get('lora_timeout', lora_timeout) * 1000

        lora_class = self.__conf.get('lora', {}).get('class', 0)
        if self.__conf.get('lora', {}).get('region') is not None:
            self.lora = LoRa(mode=LoRa.LORAWAN, region=self.__conf.get('lora', {}).get('region'), device_class=lora_class)
        else:
            self.lora = LoRa(mode=LoRa.LORAWAN, device_class=lora_class)
        self.lora.nvram_restore()

        dev_eui = binascii.unhexlify(dev_eui.replace(' ', ''))
        app_eui = binascii.unhexlify(app_eui.replace(' ', ''))
        app_key = binascii.unhexlify(app_key.replace(' ', ''))
        try:
            if not self.lora.has_joined():
                print("Trying to join LoRa.OTAA for %d seconds..." % self.__conf.get('lora_timeout', lora_timeout))
                self.lora.join(
                    activation=LoRa.OTAA,
                    auth=(dev_eui, app_eui, app_key),
                    timeout=timeout_ms
                )

            # if you want, uncomment this code, but timeout must be 0
            # while not self.lora.has_joined():
            #     print("Joining...")
            #     time.sleep(5)

            self.__open_lora_socket(nanogateway)
#            print_debug(5, 'Stack size: {}'.format(self.__thread_stack_size))
#            _thread.stack_size(self.__thread_stack_size)
#            _thread.start_new_thread(self.__check_lora_messages, ())
            return True
        except Exception as e:
            message = str(e)
            if message == 'timed out':
                print("LoRa connection timeout: %d seconds" % self.__conf.get('lora_timeout', lora_timeout))
            else:
                print_debug(3, 'Exception in LoRa connect: {}'.format(e))
            return False

    def __open_lora_socket(self, nanogateway):
        if (nanogateway):
            for i in range(3, 16):
                self.lora.remove_channel(i)

            self.lora.add_channel(0, frequency=868100000, dr_min=0, dr_max=5)
            self.lora.add_channel(1, frequency=868100000, dr_min=0, dr_max=5)
            self.lora.add_channel(2, frequency=868100000, dr_min=0, dr_max=5)

        print("Setting up LoRa socket...")
        self.__lora_socket = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
        self.__lora_socket.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)

        self.__connection_status = constants.__CONNECTION_STATUS_CONNECTED_LORA

        self.__pybytes_protocol.start_Lora(self)

        print("Connected using LoRa")

    # SIGFOX
    def connect_sigfox(self):
        if (self.__connection_status != constants.__CONNECTION_STATUS_DISCONNECTED): # noqa
            print("Error: Connection already exists. Disconnect First")
            pass
        try:
            from network import Sigfox
        except Exception:
            print("This device does not support Sigfox connections")
            return
        sigfox_config = self.__conf.get('sigfox', {})
        if sigfox_config is None or sigfox_config.get('RCZ') is None:
            print(constants.__SIGFOX_WARNING)
        try:
            sf_rcz = int(sigfox_config.get('RCZ', 1)) - 1
            if sf_rcz >= 0 and sf_rcz <= 3:
                Sigfox(mode=Sigfox.SIGFOX, rcz=sf_rcz)
                self.__sigfox_socket = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) # noqa
                self.__sigfox_socket.setblocking(True)
                self.__sigfox_socket.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, False) # noqa
                self.__network_type = constants.__NETWORK_TYPE_SIGFOX
                self.__connection_status = constants.__CONNECTION_STATUS_CONNECTED_SIGFOX # noqa
                self.__pybytes_protocol.start_Sigfox(self)
                print(
                    "Connected using Sigfox. Only upload stream is supported"
                )
                return True
            else:
                print('Invalid Sigfox RCZ specified in config!')
                return False
        except Exception as e:
            print('Exception in connect_sigfox: {}'.format(e))
            return False

    # COMMON
    def disconnect(self, keep_wifi=False, force=False):

        if self.__wifi_lte_watchdog is not None:
            self.__wifi_lte_watchdog = WDT(timeout=constants.__WDT_MAX_TIMEOUT_MILLISECONDS)
            print('Watchdog timeout has been increased to {} ms'.format(constants.__WDT_MAX_TIMEOUT_MILLISECONDS)) # noqa

        print_debug(
            1,
            'self.__connection_status={} | self.__network_type={}'.format(
                self.__connection_status, self.__network_type
            )
        )

        if (self.__connection_status == constants.__CONNECTION_STATUS_DISCONNECTED): # noqa
            print_debug(3, "Already disconnected")

        if (constants.__CONNECTION_STATUS_CONNECTED_MQTT_WIFI <= self.__connection_status <= constants.__CONNECTION_STATUS_CONNECTED_MQTT_LTE): # noqa
            print_debug(1, 'MQTT over WIFI||LTE... disconnecting MQTT')
            try:
                self.__connection.disconnect(force=force)
                self.__connection_status = constants.__CONNECTION_STATUS_DISCONNECTED # noqa
            except Exception as e:
                print("Error disconnecting: {}".format(e))

        if (self.__connection_status == constants.__CONNECTION_STATUS_CONNECTED_LORA): # noqa
            print_debug(1, 'Connected over LORA... closing socket and saving nvram') # noqa
            try:
                self.__lora_socket.close()
                self.lora.nvram_save()
            except Exception as e:
                print("Error disconnecting: {}".format(e))

        if (self.__connection_status == constants.__CONNECTION_STATUS_CONNECTED_SIGFOX): # noqa
            print_debug(1, 'Connected over SIGFOX... closing socket')
            try:
                self.__sigfox_socket.close()
            except Exception as e:
                print("Error disconnecting: {}".format(e))

        if (self.__network_type == constants.__NETWORK_TYPE_WIFI and not keep_wifi):
            print_debug(1, 'Connected over WIFI... disconnecting')
            try:
                self.wlan.deinit()
            except Exception as e:
                print("Error disconnecting: {}".format(e))

        if (self.__network_type == constants.__NETWORK_TYPE_LTE):
            print_debug(1, 'Connected over LTE... disconnecting')
            try:
                lte_cfg = self.__conf.get('lte')
                print_debug(1, 'lte.deinit(reset={})'.format(lte_cfg.get('reset', False))) # noqa
                self.lte.deinit(reset=lte_cfg.get('reset', False))
            except Exception as e:
                print("Error disconnecting: {}".format(e))

        self.__network_type = None
        self.__connection_status = constants.__CONNECTION_STATUS_DISCONNECTED

    def is_connected(self):
        return not (self.__connection_status == constants.__CONNECTION_STATUS_DISCONNECTED) # noqa

    # Added for convention with other connectivity classes
    def isconnected(self):
        return not (self.__connection_status == constants.__CONNECTION_STATUS_DISCONNECTED) # noqa
示例#9
0
time.sleep_ms(5000)

tft.fill(TFT.BLACK)
print("Sleep: ")
v = 0
fft_print = "Enter Sleep"
tft.text((0, v), fft_print, TFT.WHITE, sysfont, 1)
time.sleep_ms(1000)

#======================================
# Garbage Collector
#======================================

gc.collect()
gc.mem_free()

#sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1)
#sigfox.deinit()

#bt = Bluetooth()
#bt.deinit()

w = WLAN()
w.deinit()

lte = LTE()
lte.deinit()

time_ms = 60000
machine.deepsleep(time_ms)
示例#10
0
class StartIoT:
  def __init__(self, network=LTE_M):
    self._network = network
    self.lte = LTE()
    try:
      self.lte.deinit()
      self.lte.reset()
    except:
      pass
    sleep(5)

    self.lte.init()
    sleep(5)

    self._assure_modem_fw()

  def _assure_modem_fw(self):
    response = self.lte.send_at_cmd('ATI1')
    if response != None:
      lines = response.split('\r\n')
      fw_id = lines[1][0:3]
      is_nb = fw_id == 'UE6'
      if is_nb:
        print('Modem is using NB-IoT firmware (%s/%s).' % (lines[1], lines[2]))
      else:
        print('Modem in using LTE-M firmware (%s/%s).' % (lines[1], lines[2]))
      if not is_nb and self._network == NB_IOT:
        print('You cannot connect using NB-IoT with wrong modem firmware! Please re-flash the modem with the correct firmware.')
        raise WrongNetwork
      if is_nb and self._network == LTE_M:
        print('You cannot connect using LTE-M with wrong modem firmware! Please re-flash the modem with the correct firmware.')
        raise WrongNetwork
    else:
      print('Failed to determine modem firmware. Rebooting device...')
      reset() # Reboot the device


  def send_at_cmd_pretty(self, cmd):
    print('>', cmd)
    response = self.lte.send_at_cmd(cmd)
    if response != None:
      lines = response.split('\r\n')
      for line in lines:
        if len(line.strip()) != 0:
          print('>>', line)
    else:
      print('>> No response.')
    return response

  def connect(self):
    # NB-IoT
    if (self._network == NB_IOT):
      self.send_at_cmd_pretty('AT+CFUN=0')
      self.send_at_cmd_pretty('AT+CEMODE=0')
      self.send_at_cmd_pretty('AT+CEMODE?')
      self.send_at_cmd_pretty('AT!="clearscanconfig"')
      self.send_at_cmd_pretty('AT!="addscanfreq band=%s dl-earfcn=%s"' % (BAND, EARFCN))
      self.send_at_cmd_pretty('AT+CGDCONT=1,"IP","%s"' % APN)
      self.send_at_cmd_pretty('AT+COPS=1,2,"%s"' % COPS)
      self.send_at_cmd_pretty('AT+CFUN=1')

    # LTE-M (Cat M1)
    else:
      self.send_at_cmd_pretty('AT+CFUN=0')
      self.send_at_cmd_pretty('AT!="clearscanconfig"')
      self.send_at_cmd_pretty('AT!="addscanfreq band=%s dl-earfcn=%s"' % (BAND, EARFCN))
      self.send_at_cmd_pretty('AT+CGDCONT=1,"IP","%s"' % APN)
      self.send_at_cmd_pretty('AT+CFUN=1')
      self.send_at_cmd_pretty('AT+CSQ')

    # For a range scan:
    # AT!="addscanfreqrange band=20 dl-earfcn-min=3450 dl-earfcn-max=6352"

    print('Attaching...')
    seconds = 0
    while not self.lte.isattached() and seconds < attach_timeout:
      sleep(0.25)
      seconds += 0.25
    if self.lte.isattached():
      print('Attached!')
    else:
      print('Failed to attach to LTE (timeout)!')
      raise AttachTimeout
    self.lte.connect()

    print('Connecting...')
    seconds = 0
    while not self.lte.isconnected() and seconds < connect_timeout:
      sleep(0.25)
      seconds += 0.25
    if self.lte.isconnected():
      print('Connected!')
    else:
      print('Failed to connect to LTE (timeout)!')
      raise ConnectTimeout

  def disconnect(self):
    if self.lte.isconnected():
      self.lte.disconnect()

  def dettach(self):
    if self.lte.isattached():
      self.lte.dettach()
示例#11
0
    ###################
    #   GO TO SLEEP   #
    ###################
    set_led(LED_YELLOW)

    # prepare hardware for sleep (needed for low current draw and
    # freeing of resources for after the reset, as the modem stays on)
    print("++ preparing hardware for deepsleep")
    print("\tclose connection")
    connection.disconnect()

    print("\tdeinit SIM")
    sim.deinit()

    # not detaching causes smaller/no re-attach time on next reset but but
    # somewhat higher sleep current needs to be balanced based on your specific interval
    print("\tdeinit LTE")
    lte.deinit(detach=False)

    # go to deepsleep
    sleep_time = interval - int(time.time() - start_time)
    if sleep_time < 0:
        sleep_time = 0
    print(">> going into deepsleep for {} seconds".format(sleep_time))
    set_led(LED_OFF)
    machine.deepsleep(1000 * sleep_time)  # sleep, execution will resume from main.py entry point

except Exception as e:
    error_handler.log(e, COLOR_UNKNOWN_FAIL, reset=True)
示例#12
0
class StartIoT:
    def __init__(self, network=LTE_M):
        self._network = network
        self.lte = LTE()
        try:
            self.lte.deinit()
            self.lte.reset()
        except:
            pass
        sleep(5)

        self.lte.init()
        sleep(5)

        self._assure_modem_fw()

    def _assure_modem_fw(self):
        response = self.lte.send_at_cmd('ATI1')
        if response != None:
            lines = response.split('\r\n')
            fw_id = lines[1][0:3]
            is_nb = fw_id == 'UE6'
            if is_nb:
                print('Modem is using NB-IoT firmware (%s/%s).' %
                      (lines[1], lines[2]))
            else:
                print('Modem in using LTE-M firmware (%s/%s).' %
                      (lines[1], lines[2]))
            if not is_nb and self._network == NB_IOT:
                print(
                    'You cannot connect using NB-IoT with wrong modem firmware! Please re-flash the modem with the correct firmware.'
                )
                raise WrongNetwork
            if is_nb and self._network == LTE_M:
                print(
                    'You cannot connect using LTE-M with wrong modem firmware! Please re-flash the modem with the correct firmware.'
                )
                raise WrongNetwork
        else:
            print('Failed to determine modem firmware. Rebooting device...')
            reset()  # Reboot the device

    def _get_assigned_ip(self):
        ip_address = None
        try:
            self.lte.pppsuspend()
            response = self.send_at_cmd_pretty('AT+CGPADDR=1')
            self.lte.pppresume()
            lines = response.split('\r\n')
            sections = lines[1].split('"')
            ip_address = sections[1]
        except:
            print('Failed to retrieve assigned IP from LTE network.')

        return ip_address

    def send_at_cmd_pretty(self, cmd):
        print('>', cmd)
        response = self.lte.send_at_cmd(cmd)
        if response != None:
            lines = response.split('\r\n')
            for line in lines:
                if len(line.strip()) != 0:
                    print('>>', line)
        else:
            print('>> No response.')
        return response

    def connect(self):
        # NB-IoT
        if (self._network == NB_IOT):
            self.send_at_cmd_pretty('AT+CFUN=0')
            self.send_at_cmd_pretty('AT+CEMODE=0')
            self.send_at_cmd_pretty('AT+CEMODE?')
            self.send_at_cmd_pretty('AT!="clearscanconfig"')
            self.send_at_cmd_pretty('AT!="addscanfreq band=%s dl-earfcn=%s"' %
                                    (BAND, EARFCN))
            self.send_at_cmd_pretty('AT+CGDCONT=1,"IP","%s"' % APN)
            self.send_at_cmd_pretty('AT+COPS=1,2,"%s"' % COPS)
            self.send_at_cmd_pretty('AT+CFUN=1')

        # LTE-M (Cat M1)
        else:
            self.send_at_cmd_pretty('AT+CFUN=0')
            self.send_at_cmd_pretty('AT!="clearscanconfig"')
            self.send_at_cmd_pretty('AT!="addscanfreq band=%s dl-earfcn=%s"' %
                                    (BAND, EARFCN))
            self.send_at_cmd_pretty('AT+CGDCONT=1,"IP","%s"' % APN)
            self.send_at_cmd_pretty('AT+CFUN=1')
            self.send_at_cmd_pretty('AT+CSQ')

        # For a range scan:
        # AT!="addscanfreqrange band=20 dl-earfcn-min=3450 dl-earfcn-max=6352"

        print('Attaching...')
        seconds = 0
        while not self.lte.isattached() and seconds < attach_timeout:
            sleep(0.25)
            seconds += 0.25
        if self.lte.isattached():
            print('Attached!')
        else:
            print('Failed to attach to LTE (timeout)!')
            raise AttachTimeout
        self.lte.connect()

        print('Connecting...')
        seconds = 0
        while not self.lte.isconnected() and seconds < connect_timeout:
            sleep(0.25)
            seconds += 0.25
        if self.lte.isconnected():
            print('Connected!')
        else:
            print('Failed to connect to LTE (timeout)!')
            raise ConnectTimeout

        print('Retrieving assigned IP...')
        ip_address = self._get_assigned_ip()

        print("Device IP: {}".format(ip_address))
        print(ip_address)

        # Initialise the CoAP module
        Coap.init(ip_address)

        # Register the response handler for the requests that the module initiates as a CoAP Client
        Coap.register_response_handler(self.response_callback)

        # A CoAP server is needed if CoAP push is used (messages are pushed down from Managed IoT Cloud)
        # self.setup_coap_server()

    def setup_coap_server(self):
        # Add a resource with a default value and a plain text content format
        r = Coap.add_resource('',
                              media_type=Coap.MEDIATYPE_APP_JSON,
                              value='default_value')
        # Configure the possible operations on the resource
        r.callback(
            Coap.REQUEST_GET | Coap.REQUEST_POST | Coap.REQUEST_PUT
            | Coap.REQUEST_DELETE, True)

        # Get the UDP socket created for the CoAP module
        coap_server_socket = Coap.socket()
        # Create a new poll object
        p = uselect.poll()
        # Register the CoAP module's socket to the poll
        p.register(coap_server_socket,
                   uselect.POLLIN | uselect.POLLHUP | uselect.POLLERR)
        # Start a new thread which will handle the sockets of "p" poll
        _thread.start_new_thread(socket_thread, (p, coap_server_socket))

        print('CoAP server running!')

    # The callback that handles the responses generated from the requests sent to a CoAP Server
    def response_callback(self, code, id_param, type_param, token, payload):
        # The ID can be used to pair the requests with the responses
        print('ID: {}'.format(id_param))
        print('Code: {}'.format(code))
        print('Type: {}'.format(type_param))
        print('Token: {}'.format(token))
        print('Payload: {}'.format(payload))

    def disconnect(self):
        if self.lte.isconnected():
            self.lte.disconnect()

    def dettach(self):
        if self.lte.isattached():
            self.lte.dettach()

    def send(self, data):
        if not self.lte.isconnected():
            raise Exception('Not connected! Unable to send.')

        id = Coap.send_request(IOTGW_IP,
                               Coap.REQUEST_POST,
                               uri_port=IOTGW_PORT,
                               uri_path=IOTGW_ENDPOINT,
                               payload=data,
                               include_options=True)
        print('CoAP POST message ID: {}'.format(id))

    def pull(self, uri_path='/'):
        if not self.lte.isconnected():
            raise Exception('Not connected! Unable to pull.')

        id = Coap.send_request(IOTGW_IP,
                               Coap.REQUEST_GET,
                               uri_port=IOTGW_PORT,
                               uri_path=uri_path,
                               include_options=True)
        Coap.read()
        print('CoAP GET message ID: {}'.format(id))
def test_lte_ntp(hw, max_drift_secs=4):
    _logger.info("Starting LTE test...")
    pycom_util.reset_rgbled()

    global failures
    _logger.info("Testing LTE connectivity...")

    chrono = machine.Timer.Chrono()
    chrono.start()

    with CheckStep(FLAG_SD_CARD, suppress_exception=True):
        hw.mount_sd_card()

    ou_id = None
    cc = None
    cs = None

    with CheckStep(FLAG_COMM_CONFIG, suppress_exception=True):
        import os
        import co2unit_comm
        os.chdir(hw.SDCARD_MOUNT_POINT)
        ou_id, cc, cs = co2unit_comm.read_comm_config(hw)

    with CheckStep(FLAG_TIME_SOURCE, suppress_exception=True):
        hw.sync_to_most_reliable_rtc()

    lte = None
    signal_quality = None

    try:
        with CheckStep(FLAG_LTE_FW_API):
            from network import LTE

        with CheckStep(FLAG_LTE_INIT):
            # _logger.info("Give LTE a moment to boot")
            # LTE init seems to be successful more often if we give it time first
            # time.sleep_ms(1000)
            # wdt.feed()

            _logger.info("Init LTE...")
            chrono.reset()
            pycom.nvs_set("lte_on", True)
            lte = LTE()
            _logger.info("LTE init ok (%d ms)", chrono.read_ms())
    except:
        return failures

    try:
        with CheckStep(FLAG_LTE_ATTACH):
            _logger.info("LTE attaching... (up to 2 minutes)")
            chrono.reset()
            lte.attach()
            try:
                while True:
                    wdt.feed()
                    if lte.isattached(): break
                    if chrono.read_ms() > 150 * 1000:
                        raise TimeoutError("Timeout during LTE attach")
                    time.sleep_ms(50)
            finally:
                signal_quality = pycom_util.lte_signal_quality(lte)
                _logger.info("Signal quality: %s", signal_quality)
                import co2unit_errors
                co2unit_errors.info(
                    hw,
                    "Self-test. LTE attached: {}. Signal quality {}".format(
                        lte.isattached(), signal_quality))

            _logger.info("LTE attach ok (%d ms). Connecting...",
                         chrono.read_ms())

        if signal_quality["rssi_raw"] in range(0, 31):
            led_show_scalar(signal_quality["rssi_raw"], [0, 31])

        with CheckStep(FLAG_LTE_CONNECT):
            chrono.reset()
            lte.connect()
            while True:
                wdt.feed()
                if lte.isconnected(): break
                if chrono.read_ms() > 120 * 1000:
                    raise TimeoutError("Timeout during LTE connect")
                time.sleep_ms(50)
            _logger.info("LTE connect ok (%d ms)", chrono.read_ms())

        with CheckStep(FLAG_COMM_PING, suppress_exception=True):
            import co2unit_comm
            for sync_dest in cc.sync_dest:
                co2unit_comm.send_alive_ping(sync_dest, ou_id, cc, cs)
                wdt.feed()

        with CheckStep(FLAG_NTP_FETCH, suppress_exception=True):
            from machine import RTC
            import timeutil

            chrono.reset()
            irtc = RTC()
            ts = timeutil.fetch_ntp_time(cc.ntp_host if cc else None)
            idrift = ts - time.mktime(irtc.now())
            if abs(idrift) < max_drift_secs:
                _logger.info("Drift from NTP: %s s; within threshold (%d s)",
                             idrift, max_drift_secs)
            else:
                ntp_tuple = time.gmtime(ts)
                irtc = RTC()
                irtc.init(ntp_tuple)
                hw.ertc.save_time()
                _logger.info("RTC set from NTP %s; drift was %d s", ntp_tuple,
                             idrift)
            failures &= ~FLAG_TIME_SOURCE  # Clear FLAG_TIME_SOURCE if previously set
            _logger.info("Got time with NTP (%d ms). Shutting down...",
                         chrono.read_ms())
            wdt.feed()

        with CheckStep(FLAG_LTE_SHUTDOWN):
            if lte:
                try:
                    if lte.isconnected():
                        chrono.reset()
                        lte.disconnect()
                        _logger.info("LTE disconnected (%d ms)",
                                     chrono.read_ms())
                        wdt.feed()
                    if lte.isattached():
                        chrono.reset()
                        lte.dettach()
                        _logger.info("LTE detached (%d ms)", chrono.read_ms())
                        wdt.feed()
                finally:
                    chrono.reset()
                    lte.deinit()
                    pycom.nvs_set("lte_on", False)
                    _logger.info("LTE deinit-ed (%d ms)", chrono.read_ms())
                    wdt.feed()
    except:
        pass

    show_boot_flags()
    _logger.info("Failures after LTE test: 0x%04x", failures)
    display_errors_led()

    if signal_quality and signal_quality["rssi_raw"] in range(0, 32):
        led_show_scalar(signal_quality["rssi_raw"], [0, 31])

    pycom.rgbled(0x0)
示例#14
0
class SequansLTE:
    """
    Synopsis::

        sq = SequansLTE()
        sq.info()
        sq.firmware_info()

        sq.at('showphy')

    See also:
    - https://git.cicer.de/autonome-zelle/fipy-nbiot-rtd/blob/master/main.py

    """
    def __init__(self, network_manager, settings):
        self.network_manager = network_manager
        self.settings = settings

        from network import LTE
        self.lte = LTE()

        import machine
        self.chrono = machine.Timer.Chrono()
        self.chrono.start()

    def start(self):
        self.lte.init()
        self.attach()
        self.connect()

    def stop(self):
        self.lte.disconnect()
        time.sleep(0.25)

        self.lte.deinit()
        time.sleep(0.25)

    def attach(self):
        log.info('Attaching to LTE')
        self.lte.attach(band=self.settings.get('networking.lte.band'),
                        apn=self.settings.get('networking.lte.apn'))

        self.chrono.reset()
        while True:

            log.info('Signal strength: {}'.format(self.get_signal_strength()))

            if self.lte.isattached():
                break

            if self.chrono.read() > self.settings.get(
                    'networking.lte.attach_timeout'):
                raise Exception('Attaching to LTE timed out')

            time.sleep(0.25)

    def connect(self):
        log.info('Connecting to LTE')
        self.lte.connect()

        self.chrono.reset()
        while True:

            if self.lte.isconnected():
                break

            if self.chrono.read() > self.settings.get(
                    'networking.lte.connect_timeout'):
                raise Exception('Connecting to LTE timed out')

            time.sleep(0.25)

    def imei(self):
        """
        Return IMEI.
        """
        return self.at('AT+CGSN=1')

    def info(self):
        """
        Get infos from Modem.
        """

        log.info('Signal strength: {}'.format(self.get_signal_strength()))

        self.at('RRC:setDbgPerm full')
        self.at('RRC:showcaps')
        self.at('showver')

        # https://forum.pycom.io/topic/4022/unable-to-update-gpy-modem-firmware/8
        #self.at('AT')
        #self.at('ATI')
        #self.at('ATZ')

    def get_signal_strength(self):
        csq_at = self.lte.send_at_cmd("AT+CSQ")
        csq_line_regex = ure.compile("\n")
        csq_line = csq_line_regex.split(csq_at)
        csq_string_regex = ure.compile(" ")
        csq_string = csq_string_regex.split(csq_line[1])
        csq_comma = csq_string[1]
        csq_num_regex = ure.compile(",")
        csq_num = csq_num_regex.split(csq_comma)
        csq = csq_num[0]
        return csq

    def at(self, command):
        """

        :param command:

        """
        return self.raw('AT!="{}"'.format(command))

    def raw(self, command):
        """

        :param command:

        """
        log.info('Sending: {}'.format(command))
        answer = self.lte.send_at_cmd(command)
        log.info('Answer:  {}'.format(answer))
        return answer

    def firmware_info(self):
        """ """
        import sqnsupgrade
        sqnsupgrade.info(verbose=True, debug=True)

    def unbrick(self):
        """ """
        raise NotImplementedError(
            'https://forum.pycom.io/topic/4022/unable-to-update-gpy-modem-firmware/21'
        )