Exemple #1
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()
Exemple #2
0
def attachLte():
    global lte
    lte = LTE()
    lte.init()
    lte.attach(band=12, apn="soracom.io")
    while not lte.isattached():
        time.sleep(0.25)
        print('.',end='')
    print("attached!")
Exemple #3
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
Exemple #4
0
def get_LTE():
    global lte
    if lte == None:
        lte = LTE()
    if lte.isconnected():
        return True
    # lte.reset()
    # lte.send_at_cmd('AT+CGDCONT=1,"IP","nbiot.iot"')
    if (not lte.isattached()):
        lte.attach(band=20, apn="nbiot.iot")
    while not lte.isattached():
        debugprint('Attaching...')
        time.sleep(1)
    debugprint('LTE is attached')
    return True
def nb_iot_attach(lte: LTE, apn: str):
    sys.stdout.write(">> attaching to LTE network ({})".format(apn))
    # since we disable unsolicited CEREG messages in reset_modem(), as they interfere with AT communication with the SIM via CSIM commands,
    # we are required to use an attach method that does not require cereg messages, for pycom that is legacyattach=false
    lte.attach(band=8, apn=apn, legacyattach=False)
    i = 0
    while not lte.isattached() and i < 60:
        machine.idle()  # save power while waiting
        time.sleep(1.0)
        sys.stdout.write(".")
        i += 1
    print("")
    if not lte.isattached():
        raise Exception("unable to attach to LTE network")
    print("-- attached: " + str(i) + "s")
Exemple #6
0
class LteController:
    def __init__(self):
        self._lte = LTE()
        self._lte.attach()
        while not self._lte.isattached():
            time.sleep(1)
        self._lte.connect()
        while not self._lte.isconnected():
            time.sleep(1)
        print(self._lte.isattached())
        print(self._lte.isconnected())

    def canSend(self):
        """return true if interface can send data, false otherwise"""
        return self._lte.isconnected()

    def send(self, data):
        """attempts to send via interface, returns success status"""
        return False
def connect(timeout=30, lte=None):
    if lte is None:
        lte = LTE()  # instantiate the LTE object

    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
Exemple #8
0
def connect():
    global lte
    lte = LTE()  # instantiate the LTE object

    # Change this if you are using the M1 network (comment out the next 6 lines)
    lte.send_at_cmd('AT+CFUN=0')
    lte.send_at_cmd('AT+CEMODE=0')
    lte.send_at_cmd('AT+CEMODE?')
    lte.send_at_cmd('AT!="clearscanconfig"')
    lte.send_at_cmd('AT!="addscanfreq band=20 dl-earfcn=6352"')
    lte.send_at_cmd('AT+CFUN=1')
    # End change this ....

    lte.attach()  # attach the cellular modem to a base station
    while not lte.isattached():
        time.sleep(0.25)
    print("attatched")
    lte.connect()  # start a data session and obtain an IP address
    while not lte.isconnected():
        time.sleep(0.25)
    print("connected")
Exemple #9
0
class Connection:
    def __init__(self):
        self.lte = LTE()

    def nb_connect(self, band=20, apn="nb.inetd.gdsp"):
        counter1 = 0
        counter2 = 0

        if not self.lte.isattached():
            print("Attaching to LTE...")
            self.lte.attach(band=band, apn=apn)
            while not self.lte.isattached():
                counter1 += 1
                print(str(counter1) + ' seconds elapsed')
                if counter1 >= 50:
                    import machine
                    machine.reset()
                time.sleep(1)

        if not self.lte.isconnected():
            print("Obtaining IP address...")
            self.lte.connect()
            while not self.lte.isconnected():
                counter2 += 1
                print(str(counter2) + ' seconds elapsed')
                time.sleep(0.25)

        print("Network ready ...")

    def nb_disconnect(self):
        if self.lte.isconnected():
            self.lte.disconnect()
        while self.lte.isattached():
            try:
                self.lte.dettach()
            except OSError as e:
                print(e, type(e))
            else:
                print("Network is now disconnected")
 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='')\
 lte.send_at_cmd('AT+CGDCONT=1,"IP","vodafone.internet"')\
 print(".", end='')\
 lte.send_at_cmd('AT!="RRC::addscanfreq band=20 dl-earfcn=6300"')\
 print(".", end='')\
 lte.send_at_cmd('AT+CFUN=1')\
 print(" OK")\
\
 # If correctly configured for carrier network, attach() should succeed.\
 if not lte.isattached():\
     print("Attaching to LTE network ", end='')\
     lte.attach()\
     while True:\
         if lte.isattached():\
             print(" OK")\
             break\
             print('.', end='')\
         time.sleep(1)\
\
 # Once attached, connect() should succeed.\
 if not lte.isconnected():\
     print("Connecting on LTE network ", end='')\
     lte.connect()\
     while(True):\
         if lte.isconnected():\
             print(" OK")\
             break\
class SQNS:
    """
    Synopsis::
    
        sq = SQNS()
        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):
        from network import LTE
        self.lte = LTE()
        self.at('RRC:setDbgPerm full')

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

    def attach(self):
        self.lte.attach(band=8, apn="iot.1nce.net")
        while not self.lte.isattached():  # do we have a timeout?
            time.sleep(1)
            try:
                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]
                print("[LTE   ]   ... still attaching ... (CSQ: " + csq + ")")
            except:
                csq = "-999.0"
                print(
                    "[LTE   ]   ... no CSQ recevied, let us hope I am still attaching "
                    + csq)

    def at(self, command):
        """

        :param command: 

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

    def raw(self, command):
        """

        :param command: 

        """
        print('Sending command {}'.format(command))
        print(self.lte.send_at_cmd(command))

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

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

    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'
        )
from network import LTE
import time
import socket

start = time.ticks_ms()
lte = LTE()
print("\nModem is activated, time needed (s): ",
      time.ticks_diff(time.ticks_ms(), start) / 10000)

start = time.ticks_ms()
lte.attach(band=20, apn="company.iot.dk1.tdc")

while not lte.isattached():
    time.sleep(0.5)
    print(".", end="")
print("\nAttached to the Network!, time needed (s): ",
      time.ticks_diff(time.ticks_ms(), start) / 10000)

start = time.ticks_ms()
lte.connect()  # starting a data section and obtaining an ip address
while not lte.isconnected():
    time.sleep(0.5)
    print("-", end="")
print("Connected to the Network Operator!, time needed (s): ",
      time.ticks_diff(time.ticks_ms(), start) / 1000)

lte.disconnect()  # Dissconnect the module and send at commands
while lte.disconnect():
    #time.sleep(0.5)
    print("Disconnected!")
lte.send_at_cmd("AT+CSQ")  # Check the Signal Strength
Exemple #13
0
#some carriers do not require an APN
#also, check the band settings with your carrier

lte = LTE()
print("----AT+CGMI----")
#lte.send_at_cmd('AT+CFUN=0')
#lte.send_at_cmd('AT!="clearscanconfig"')
#lte.send_at_cmd('AT!="addscanfreq band=28 dl-earfcn=9410"')
#lte.send_at_cmd('AT+CGDCONT=1, "IP", "telstra.internet"')
#lte.send_at_cmd('AT+CEMODE=0')
#lte.send_at_cmd('AT+CEREG=2')
#lte.send_at_cmd('AT+CFUN=1')
lte.send_at_cmd('AT+CGMI')
###

lte.attach(band=4, apn="super")  #set band and APN
print("attaching..", end='')
while not lte.isattached():
    time.sleep(5)

    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"'))
Exemple #14
0
from network import LTE
import time
import socket

lte = LTE()
#Vodafone UK apn=nb.inetd.gdsp
print("Attempting to attach...", end="")
lte.attach(band=20, apn="ep.inetd.gdsp")
for j in range(10):
    print(".", end="")
    time.sleep(1)
    if lte.isattached():
        print("\nAttached in attempt #" + str(j + 1))
        break
lte.connect()
print("Attempting to connect..", end="")
for i in range(10):
    print(".", end="")
    if lte.isconnected():
        print("\nConnected in attempt #" + str(i + 1))
        break

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(20)
s.connect(("3.9.21.110", 6789))
result = s.send(b'helloworld')
print("Sent bytes: " + str(result))

lte.disconnect()
lte.dettach()
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
Exemple #16
0
import socket
import ssl
import time
from network import LTE

lte = LTE()  # instantiate the LTE object
lte.attach()  # attach the cellular modem to a base station
while not lte.isattached():
    time.sleep(0.25)
lte.connect()  # start a data session and obtain an IP address
while not lte.isconnected():
    time.sleep(0.25)

s = socket.socket()
s = ssl.wrap_socket(s)
s.connect(socket.getaddrinfo('www.google.com', 443)[0][-1])
s.send(b"GET / HTTP/1.0\r\n\r\n")
print(s.recv(4096))
s.close()

lte.disconnect()
lte.dettach()
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)
Exemple #18
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'
        )
Exemple #19
0
import socket
import ssl
import time
from network import LTE

print("init")
t = time.time()
lte = LTE()


print("attach", end="")
t = time.time()
#### amarisoft (the "anritsu" or the "blank" sim)
lte.attach(band=20)
# lte.attach(apn="Internet")
#### pycom vodafone
# lte.attach(apn="pycom.io", band=20)
#### vodafone
# lte.attach(band=20, apn="spe.inetd.vodafone.nbiot")
# lte.attach(cid=1, band=20, apn="spe.inetd.vodafone.nbiot", type=LTE.IP)

while not lte.isattached():
    print(".", end="")
    time.sleep(1)
print(time.time() - t, "seconds")


print("connect", end="")
t = time.time()
lte.connect()       # start a data session and obtain an IP address
while not lte.isconnected():
class DatacakeGateway:
    def machine_callback(self, arg):
        evt = machine.events()
        if (evt & machine.PYGATE_START_EVT):
            self.machine_state = config.GATEWAY_STATE_OK
            pycom.rgbled(config.RGB_GATEWAY_OK)
        elif (evt & machine.PYGATE_ERROR_EVT):
            self.machine_state = config.GATEWAY_STATE_ERROR
            pycom.rgbled(config.RGB_GATEWAY_ERROR)
        elif (evt & machine.PYGATE_STOP_EVT):
            self.machine_state = config.GATEWAY_STATE_STOP
            pycom.rgbled(config.RGB_GATEWAY_STOP)

    def __init__(self):

        print("Init: Initialization of Gateway class...")

        # Machine
        machine.callback(
            trigger=(machine.PYGATE_START_EVT | machine.PYGATE_STOP_EVT
                     | machine.PYGATE_ERROR_EVT),
            handler=self.machine_callback)
        self.machine_state = 0

        # LTE
        self.lte = LTE()
        self.lte_connection_state = 0

        # RTC
        self.rtc = RTC()

        # Gateway
        # Read the GW config file from Filesystem
        self.gateway_config_file = None

        # Timers
        self.rgb_breathe_timer = Timer.Chrono()

        # Startup
        # Should be called outside init
        # self.start_up()

    def lte_event_callback(self, arg):
        #self.blink_rgb_led(5, 0.25, config.RGB_LTE_ERROR)
        #self.lte.deinit()
        #machine.reset()
        print(
            "\n\n\n#############################################################"
        )
        print("CB LTE Callback Handler")
        ev = arg.events()  # NB: reading the events clears them
        t = time.ticks_ms()
        print("CB", t, time.time(), ev, time.gmtime())
        self.blink_rgb_led(3, 0.25, config.RGB_LTE_ERROR)
        if ev & LTE.EVENT_COVERAGE_LOSS:
            print("CB", t, "coverage loss")
        if ev & LTE.EVENT_BREAK:
            print("CB", t, "uart break signal")
        try:
            self.lte.pppsuspend()
            if not self.lte.isattached():
                print("not attached ... reattach")
                self.lte.detach()
                self.init_lte()
            else:
                print("attached ... resume")
                self.lte.pppresume()
        except Exception as ex:
            sys.print_exception(ex)
        print(
            "#############################################################\n\n\n"
        )

    def init_gateway(self):
        print("Init GW: Starting LoRaWAN Concentrator...")
        try:
            self.gateway_config_file = open(config.GW_CONFIG_FILE_PATH,
                                            'r').read()
        except Exception as e:
            print("Error opening Gateway Config: {}".format(e))
            # TODO: Handle Error
            return False
        else:
            machine.pygate_init(self.gateway_config_file)
            print("Init GW: LoRaWAN Concentrator UP!")
            return True

    def init_rtc(self):
        print("Init RTC: Syncing RTC...")
        try:
            self.rtc.ntp_sync(server="pool.ntp.org")
            while not self.rtc.synced():
                self.blink_rgb_led(1,
                                   0.25,
                                   config.RGB_RTC_IS_SYNCING,
                                   delay_end=False)
            self.blink_rgb_led(3, 0.1, config.RGB_RTC_IS_SYNCING)
        except Exception as e:
            print("Exception syncing RTC: {}".format(e))
            return False
        else:
            print("Init RTC: Synced!")
            return True

    def init_lte(self):

        self.lte_connection_state = 0
        self.lte.init()
        #self.lte.lte_callback(LTE.EVENT_COVERAGE_LOSS, self.lte_event_callback)
        self.lte.lte_callback(LTE.EVENT_BREAK, self.lte_event_callback)

        while True:

            # attach LTE
            if self.lte_connection_state == 0:
                print("Init LTE: Attaching LTE...")
                self.lte.attach(band=config.LTE_BAND, apn=config.LTE_APN)
                while not self.lte.isattached():
                    self.blink_rgb_led(1,
                                       0.25,
                                       config.RGB_LTE_IS_ATTACHING,
                                       delay_end=False)
                self.blink_rgb_led(3, 0.1, config.RGB_LTE_IS_ATTACHING)
                self.lte_connection_state += 1
                print("Init LTE: Attached!")

            # connect LTE
            if self.lte_connection_state == 1:
                print("Init LTE: Connecting LTE...")
                self.lte.connect()
                while not self.lte.isconnected():
                    self.blink_rgb_led(1,
                                       0.25,
                                       config.RGB_LTE_IS_CONNECTING,
                                       delay_end=False)
                self.blink_rgb_led(3, 0.1, config.RGB_LTE_IS_CONNECTING)
                self.lte_connection_state += 1
                print("Init LTE: Connected!")

            # done
            if self.lte_connection_state == 2:
                return True

    def blink_rgb_led(self,
                      times,
                      speed,
                      color_on,
                      color_off=config.RGB_OFF,
                      delay_end=True):
        for index in range(times):
            pycom.rgbled(config.RGB_OFF)
            time.sleep(speed)
            pycom.rgbled(color_on)
            time.sleep(speed)
        pycom.rgbled(config.RGB_OFF)
        if delay_end is True:
            time.sleep(0.1)

    def start_up(self):
        print("Start Up: Now starting up Gateway...")
        self.init_lte()
        self.init_rtc()
        self.init_gateway()
        #self.main_loop()

    def main_loop(self):

        # Start Timers
        self.rgb_breathe_timer.start()

        while True:

            if self.rgb_breathe_timer.read(
            ) > config.TIMER_RGB_BREATHE_INTERVAL:
                self.rgb_breathe_timer.reset()
class NetworkConnector:

    def __init__(self):
        self.logger = logging.get_logger(__name__)
        self.lte = LTE()

    def _attach(self):
        """
            Attaches to the 1nce network
        """
        self.lte.attach()
        while not self.lte.isattached():
            time.sleep(0.5)
            print(".", end="")
        self.logger.info("Sim attached to iot.1nce.net")

    def connect(self):
        """
            Connects to the 1nce network
        """
        self._attach()
        self.lte.connect()
        while not self.lte.isconnected():
            time.sleep(0.5)
        self.logger.info("Sim connected to iot.1nce.net")

    def disconnect(self):
        self.lte.disconnect()
        self.logger.info("Sim disconnected from iot.1nce.net")

    def _send_at_command(self, command):
        """
            Sends AT command over the modem

        :rtype: Response string
        """
        self.lte.pppsuspend()
        resp = self.lte.send_at_cmd(command)
        self.lte.pppresume()
        return resp

    def get_reception(self):
        """
            Gets the current reception to the 1nce network

        :return: Number Reception to the 1nce network
        """
        return self._send_at_command("AT+CSQ")

    def get_ip_address(self):
        """"
            Gets the Device it's Local IP address

        :return IP Address
        """
        resp = self._send_at_command("AT+CGPADDR=1")
        self.logger.info(resp)
        search = re.search(r"\"([1-2]?\d?\d\.[1-2]?\d?\d\.[1-2]?\d?\d\.[1-2]?\d?\d)\"", resp)
        if search:
            return search.group(1)
        return None
Exemple #22
0
print("attach")
#### amarisoft (the "anritsu" or the "blank" sim)
# lte.attach()
# lte.attach(band=20)
# lte.attach(band=20)
# lte.attach(apn="Internet")
#### pycom vodafone
# lte.attach(apn="pycom.io", band=20)
# lte.attach(apn="pycom.io")
#### vodafone
# lte.attach()
# lte.attach(cid=1, band=20, apn="spe.inetd.vodafone.nbiot", type=LTE.IP)
# lte.attach(band=20, apn="spe.inetd.vodafone.nbiot")
#### Jersey Telecom
lte.attach(band=8)

time.sleep(10)
while not lte.isattached():
    print(".", end="")
    time.sleep(10)
print("\nattached")

print("connect")
lte.connect()  # legacy=True)
while not lte.isconnected():
    print(".", end="")
    time.sleep(2)
print("\nconnected")

host = "pycom.io"