示例#1
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")
示例#2
0
文件: main.py 项目: hayesey/pycom
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()
         time.sleep(1)\
\
 # Once connect() succeeds, any call requiring Internet access will\
 # use the active LTE connection.\
 return lte\
\
# Clean disconnection of the LTE network is required for future\
# successful connections without a complete power cycle between.\
def endLTE():\
\
 print("Disonnecting LTE ... ", end='')\
 lte.disconnect()\
 print("OK")\
 time.sleep(1)\
 print("Detaching LTE ... ", end='')\
 lte.dettach()\
 print("OK")\
\
# Sets the internal real-time clock.\
# Needs LTE for Internet access.\
\
def pushtoDatabase(stickNumber, moistureValue, lightValue, temperatureValue):\
    stringStickNumber = str(stickNumber)\
    firebase.patch('https://growkit-020.firebaseio.com/Pins/4444/Plants/Stick'+stringStickNumber+'', \{'Water': moistureValue, 'Light': lightValue, 'Temperature': temperatureValue\})\
\
    '''if(stickNumber == 1):\
        firebase.patch('https://growkit-020.firebaseio.com/Pins/4444/Plants/Stick', \{'Water': moistureValue, 'Light': lightValue, 'Temperature': temperatureValue\})\
\
    if(stickNumber == 2):\
        firebase.put('https://growkit-020.firebaseio.com/Pins/4444/Plants/Stick2', \{'Water': moistureValue, 'Light': lightValue, 'Temperature': temperatureValue\})'''\
\
示例#4
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()
class LteComms:
    def __init__(self):
        self.message_storage = 'AT+CPMS="SM", "SM", "SM"'
        gc.collect()
        try:
            self.lte = LTE()
            time.sleep(4)
        except:
            print("initialize LTE object?")
        self.lte.reset()
        time.sleep(4)
        print("delay 4 secs")

    def at(self, cmd):
        print("modem command: {}".format(cmd))
        r = self.lte.send_at_cmd(cmd).split('\r\n')
        r = list(filter(None, r))
        print("response={}".format(r))
        return r

    def attach_LTE(self):
        gc.collect()
        time.sleep(10.0)

        if self.lte.isattached():
            try:
                print("LTE was already attached, disconnecting...")
                if self.lte.isconnected():
                    print("disconnect")
                    self.lte.disconnect()
            except:
                print("Exception during disconnect")

            try:
                if self.lte.isattached():
                    print("detach")
                    self.lte.dettach()
            except:
                print("Exception during dettach")

            try:
                print("resetting modem...")
                self.lte.reset()
            except:
                print("Exception during reset")

            print("delay 5 secs")
            time.sleep(5.0)

        # enable network registration and location information, unsolicited result code
        self.at('AT+CEREG=2')

        # print("full functionality level")
        self.at('AT+CFUN=1')
        time.sleep(1.0)

        # using Hologram SIM
        self.at('AT+CGDCONT=1,"IP","hologram"')

        print("attempt to attach cell modem to base station...")
        # lte.attach()  # do not use attach with custom init for Hologram SIM

        self.at("ATI")
        time.sleep(2.0)

        i = 0
        while self.lte.isattached() == False:
            # get EPS Network Registration Status:
            # +CEREG: <stat>[,[<tac>],[<ci>],[<AcT>]]
            # <tac> values:
            # 0 - not registered
            # 1 - registered, home network
            # 2 - not registered, but searching...
            # 3 - registration denied
            # 4 - unknown (out of E-UTRAN coverage)
            # 5 - registered, roaming
            r = self.at('AT+CEREG?')
            try:
                r0 = r[0]  # +CREG: 2,<tac>
                r0x = r0.split(',')  # ['+CREG: 2',<tac>]
                tac = int(r0x[1])  # 0..5
                print("tac={}".format(tac))
            except IndexError:
                tac = 0
                print("Index Error!!!")

            # get signal strength
            # +CSQ: <rssi>,<ber>
            # <rssi>: 0..31, 99-unknown
            r = self.at('AT+CSQ')

            # extended error report
            # r =self.at('AT+CEER')

            # if lte.isattached():
            #    print("Modem attached (isattached() function worked)!!!")
            #    break

            # if (tac==1) or (tac==5):
            #    print("Modem attached!!!")
            #    break

            i = i + 5
            print("not attached: {} secs".format(i))

        # while self.lte.isattached():
        #     # self.receive_and_forward_to_chat()
        #     continue
        # print("Modem not attached")
        print("set to check messages on sim")
        self.at(self.message_storage)

    def connect_lte_data(self):
        self.at('AT+CEREG?')
        print("Attempt to connect")
        if self.lte.isattached() == False:
            print("Not attached, try again, will fail")
        else:
            print("Attached and continue")

        self.lte.connect()
        i = 0
        while not self.lte.isconnected():
            i = i + 1
            print("not connected: {}".format(i))
            time.sleep(1.0)

        print("LTE connected for data!!!")  # also send this to chat
        # so just pymesh this to all nodes in leader_mesh_list
        while self.lte.isconnected():
            continue

    def scrape_webpage(self, url):
        s = socket.socket()
        s = ssl.wrap_socket(s)
        s.connect(socket.getaddrinfo(url, 443)[0][-1])
        s.send(b"GET / HTTP/1.0\r\n\r\n")
        print(s.recv(4096))
        s.close()

    def send_sms(self, number, msg):
        # this will somehow have to be connected to the chat with a JM msg1
        print("set mode to text")
        self.at('AT+CMGF=1')
        time.sleep(.5)
        # msg = ('AT+CMGS="%s"\r%s\0x1a' % (number, msg))
        # print(('ATTB+SQNSMSSEND="%s", "%s"' % (number, msg)))
        print('sendin an sms', end=' ')
        ans = self.lte.send_at_cmd(
            ('AT+SQNSMSSEND="%s", "%s"' % (number, msg))).split('\r\n')
        print(ans)
        # self.at(msg)
        time.sleep(4)
        print("sent!")

    def receive_and_forward_to_chat(self):
        # this will somehow have to be connected to the chat with a JM msg1
        print("set mode to text")
        self.at('AT+CMGF=1')
        msg_list = []
        msg_list = self.at('AT+CMGL="ALL"')
        number_of_messages = 0
        if len(msg_list) > 1:
            print("This'll print if there a msg")
            if len(msg_list) > 20:
                print("More then 10 messages, loop")
                i = 1
                while len(msg_list) > 20:
                    print("This is the inner loop running %s times" % i)
                    msg_list = self.at('AT+CMGL="ALL"')
                    number_of_messages += len(msg_list)
                    self.write_msg_to_file_and_delete(msg_list)
                    time.sleep(15)
                    i += 1
                print("This is to get the last group of messages")
                # you don't scan for messages while it sleep, almost Need
                # to run this in a thread in the background.
                time.sleep(10)
                msg_list = self.at('AT+CMGL="ALL"')
                number_of_messages += len(msg_list)
                self.write_msg_to_file_and_delete(msg_list)
            else:
                print("The list is less than 10, so straight to file")
                number_of_messages += len(msg_list)
                self.write_msg_to_file_and_delete(msg_list)
        else:
            print("This prints when no messages")
            self.at('AT+CMGD=1,4')
        # Cuz apparently you need to clean out the sim card, it only holds 10 msgs
        # at('AT+CMGD=1,4')
        time.sleep(5)
        actual_messages = (number_of_messages / 2) - 1
        print(actual_messages)

    def msg_parse(self, msg_list):
        parsed_msg_list = []
        msg_list_string = "".join(msg_list)
        split_msg_list = msg_list_string.split('+CMGL:')
        for i in range(len(split_msg_list)):
            temp_string = str(split_msg_list[i])
            if temp_string[-2:] == 'OK':
                parsed_msg_list.append(temp_string[:-2])
            else:
                parsed_msg_list.append(temp_string)
        return parsed_msg_list

    def disconnect_LTE(self):
        self.lte.disconnect()
        print("LTE Data disconnected")
        # send to chat

    def unattach_lte(self):
        self.lte.detach(reset=True)
        print("LTE modem deattached")

    def signal_strength(self):
        self.at('AT+CSQ')

    def check_read_sms(self):
        self.at('AT+CMGF=1')
        msg_list = self.at('AT+CMGL="ALL"')
        print(msg_list)

    def write_msg_to_file_and_delete(self, msg_list):
        parsed_msg_list = self.msg_parse(msg_list)
        print("Writing to SMS log")
        f = open('/sd/www/sms.txt', 'a+')
        for i in range(len(parsed_msg_list)):
            if parsed_msg_list[i] != '\r\n':
                f.write(str(parsed_msg_list[i]))
                f.write('\r\n')
        f.close()
        self.at('AT+CMGD=1,4')
示例#6
0
class StartIot():

    def __init__(self):
        self.lte = LTE()
        self.initModem()


    # METHOD FOR PRETTY PRINTING AT COMMANDS
    def send_at_cmd_pretty(self, cmd):
        response = self.lte.send_at_cmd(cmd)
        if response != None:
            lines=response.split('\r\n')
            print("Response is:< ")
            for line in lines:
                if len(line.strip()) != 0:
                    print(line)
            print(">")
        else:
            print("Response is None...")
        return response

    # SETUP AND START THE MODEM - ATTACH TO THE NETWORK
    def initModem(self):
        print ("Starting modem...")
        self.send_at_cmd_pretty('AT+CFUN=0')
        # Change this if you are using the NB1 network (uncomment the next 4 lines)
        #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=20 dl-earfcn=6352"')
        # End change this ....
        self.send_at_cmd_pretty('AT+CGDCONT=1,"IP","mda.ee"')
        self.send_at_cmd_pretty('AT+CFUN=1')
        self.send_at_cmd_pretty('AT+CSQ')

        print ("Waiting for attachement (To Radio Access Network)...")
        while not self.lte.isattached():
            time.sleep(0.25)
        else:
            print ("Attached (To Radio Access Network)...")

    # CONNECT TO THE NETWORK
    def connect(self):
        if not self.lte.isattached():
            raise Exception('NOT ATTACHED... call initModem() first')
        print ("Waiting for connection (To IP network)...")
        self.lte.connect()
        # Wait until we get connected to network
        while not self.lte.isconnected():
            machine.idle()
        print ("Connected (To IP network)!")

    # OPEN SOCKET AND SEND DATA
    def send(self, data):
        if not self.lte.isconnected():
            raise Exception('NOT CONNECTED')
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
        IP_address = socket.getaddrinfo('172.16.15.14', 1234)[0][-1]
        s.connect(IP_address)
        s.send(data)
        s.close()

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

    def dettach(self):
        if self.lte.isattached():
            self.lte.dettach()
        self.lte.send_at_cmd('AT+CFUN=0')
        print("Modem offline")
示例#7
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)
class NanoGateway:
    """
    Nano gateway class, set up by default for use with TTN, but can be configured
    for any other network supporting the Semtech Packet Forwarder.
    Only required configuration is wifi_ssid and wifi_password which are used for
    connecting to the Internet.
    """
    def __init__(self,
                 id,
                 frequency,
                 datarate,
                 server,
                 port,
                 ntp_server='pool.ntp.org',
                 ntp_period=3600):
        self.id = id
        self.server = server
        self.port = port

        self.frequency = frequency
        self.datarate = datarate

        # self.ssid = ssid
        # self.password = password

        self.ntp_server = ntp_server
        self.ntp_period = ntp_period

        self.server_ip = None

        self.rxnb = 0
        self.rxok = 0
        self.rxfw = 0
        self.dwnb = 0
        self.txnb = 0

        self.sf = self._dr_to_sf(self.datarate)
        self.bw = self._dr_to_bw(self.datarate)

        self.stat_alarm = None
        self.pull_alarm = None
        self.uplink_alarm = None

        self.lte = None
        self.sock = None
        self.udp_stop = False
        self.udp_lock = _thread.allocate_lock()

        self.lora = None
        self.lora_sock = None

        self.rtc = machine.RTC()

    def start(self):
        """
        Starts the LoRaWAN nano gateway.
        """

        pycom.heartbeat(False)

        self._log('Starting LoRaWAN nano gateway with id: {}', self.id)

        # # setup WiFi as a station and connect
        # self.wlan = WLAN(mode=WLAN.STA)
        # self._connect_to_wifi()

        # setup LTE CATM1 connection
        self.lte = LTE(carrier="verizon")
        self._connect_to_LTE()

        # get a time sync
        self._log('Syncing time with {} ...', self.ntp_server)
        self.rtc.ntp_sync(self.ntp_server, update_period=self.ntp_period)
        while not self.rtc.synced():
            utime.sleep_ms(50)
        self._log("RTC NTP sync complete")

        # get the server IP and create an UDP socket
        self.server_ip = usocket.getaddrinfo(self.server, self.port)[0][-1]
        self._log('Opening UDP socket to {} ({}) port {}...', self.server,
                  self.server_ip[0], self.server_ip[1])
        self.sock = usocket.socket(usocket.AF_INET, usocket.SOCK_DGRAM,
                                   usocket.IPPROTO_UDP)
        self.sock.setsockopt(usocket.SOL_SOCKET, usocket.SO_REUSEADDR, 1)
        self.sock.setblocking(False)

        # push the first time immediatelly
        self._push_data(self._make_stat_packet())

        # create the alarms
        self.stat_alarm = Timer.Alarm(
            handler=lambda t: self._push_data(self._make_stat_packet()),
            s=60,
            periodic=True)
        self.pull_alarm = Timer.Alarm(handler=lambda u: self._pull_data(),
                                      s=25,
                                      periodic=True)

        # start the UDP receive thread
        self.udp_stop = False
        _thread.start_new_thread(self._udp_thread, ())

        # initialize the LoRa radio in LORA mode
        self._log('Setting up the LoRa radio at {} Mhz using {}',
                  self._freq_to_float(self.frequency), self.datarate)
        self.lora = LoRa(mode=LoRa.LORA,
                         frequency=self.frequency,
                         bandwidth=self.bw,
                         sf=self.sf,
                         preamble=8,
                         coding_rate=LoRa.CODING_4_5,
                         tx_iq=True)

        # create a raw LoRa socket
        self.lora_sock = usocket.socket(usocket.AF_LORA, usocket.SOCK_RAW)
        self.lora_sock.setblocking(False)
        self.lora_tx_done = False

        self.lora.callback(trigger=(LoRa.RX_PACKET_EVENT
                                    | LoRa.TX_PACKET_EVENT),
                           handler=self._lora_cb)
        self._log('LoRaWAN nano gateway online')

    def stop(self):
        """
        Stops the LoRaWAN nano gateway.
        """

        self._log('Stopping...')

        # send the LoRa radio to sleep
        self.lora.callback(trigger=None, handler=None)
        self.lora.power_mode(LoRa.SLEEP)

        # stop the NTP sync
        self.rtc.ntp_sync(None)

        # cancel all the alarms
        self.stat_alarm.cancel()
        self.pull_alarm.cancel()

        # signal the UDP thread to stop
        self.udp_stop = True
        while self.udp_stop:
            utime.sleep_ms(50)

        # disable LTE
        self.lte.disconnect()
        self.lte.dettach()

    def _connect_to_wifi(self):
        self.wlan.connect(self.ssid, auth=(None, self.password))
        while not self.wlan.isconnected():
            utime.sleep_ms(50)
        self._log('WiFi connected to: {}', self.ssid)

    def _connect_to_LTE(self):
        print("reset modem")
        try:
            self.lte.reset()
        except:
            print("Exception during reset")

        print("delay 5 secs")
        utime.sleep(5.0)

        if self.lte.isattached():
            try:
                print("LTE was already attached, disconnecting...")
                if self.lte.isconnected():
                    print("disconnect")
                    self.lte.disconnect()
            except:
                print("Exception during disconnect")

            try:
                if self.lte.isattached():
                    print("detach")
                    self.lte.dettach()
            except:
                print("Exception during dettach")

            try:
                print("resetting modem...")
                self.lte.reset()
            except:
                print("Exception during reset")

            print("delay 5 secs")
            utime.sleep(5.0)

        # enable network registration and location information, unsolicited result code
        self.at('AT+CEREG=2')

        # print("full functionality level")
        self.at('AT+CFUN=1')
        utime.sleep(1.0)

        # using Hologram SIM
        self.at('AT+CGDCONT=1,"IP","hologram"')

        print("attempt to attach cell modem to base station...")
        # lte.attach()  # do not use attach with custom init for Hologram SIM

        self.at("ATI")
        utime.sleep(2.0)

        i = 0
        while self.lte.isattached() == False:
            # get EPS Network Registration Status:
            # +CEREG: <stat>[,[<tac>],[<ci>],[<AcT>]]
            # <tac> values:
            # 0 - not registered
            # 1 - registered, home network
            # 2 - not registered, but searching...
            # 3 - registration denied
            # 4 - unknown (out of E-UTRAN coverage)
            # 5 - registered, roaming
            r = self.at('AT+CEREG?')
            try:
                r0 = r[0]  # +CREG: 2,<tac>
                r0x = r0.split(',')  # ['+CREG: 2',<tac>]
                tac = int(r0x[1])  # 0..5
                print("tac={}".format(tac))
            except IndexError:
                tac = 0
                print("Index Error!!!")

            # get signal strength
            # +CSQ: <rssi>,<ber>
            # <rssi>: 0..31, 99-unknown
            r = self.at('AT+CSQ')

            # extended error report
            # r = at('AT+CEER')

            if self.lte.isattached():
                print("Modem attached (isattached() function worked)!!!")
                break

            if (tac == 1) or (tac == 5):
                print("Modem attached!!!")
                break

            i = i + 5
            print("not attached: {} secs".format(i))

            if (tac != 0):
                self.blink(BLUE, tac)
            else:
                self.blink(RED, 5)

            utime.sleep(2)

        self.at('AT+CEREG?')
        print("connect: start a data session and obtain an IP address")
        self.lte.connect(cid=3)
        i = 0
        while not self.lte.isconnected():
            i = i + 1
            print("not connected: {}".format(i))
            self.blink(YELLOW, 1)
            utime.sleep(1.0)

        print("connected!!!")
        pycom.rgbled(GREEN)

    def _dr_to_sf(self, dr):
        sf = dr[2:4]
        if sf[1] not in '0123456789':
            sf = sf[:1]
        return int(sf)

    def _dr_to_bw(self, dr):
        bw = dr[-5:]
        if bw == 'BW125':
            return LoRa.BW_125KHZ
        elif bw == 'BW250':
            return LoRa.BW_250KHZ
        else:
            return LoRa.BW_500KHZ

    def _sf_bw_to_dr(self, sf, bw):
        dr = 'SF' + str(sf)
        if bw == LoRa.BW_125KHZ:
            return dr + 'BW125'
        elif bw == LoRa.BW_250KHZ:
            return dr + 'BW250'
        else:
            return dr + 'BW500'

    def _lora_cb(self, lora):
        """
        LoRa radio events callback handler.
        """

        events = lora.events()
        if events & LoRa.RX_PACKET_EVENT:
            self.rxnb += 1
            self.rxok += 1
            rx_data = self.lora_sock.recv(256)
            stats = lora.stats()
            packet = self._make_node_packet(rx_data, self.rtc.now(),
                                            stats.rx_timestamp, stats.sfrx,
                                            self.bw, stats.rssi, stats.snr)
            self._push_data(packet)
            self._log('Received packet: {}', packet)
            self.rxfw += 1
        if events & LoRa.TX_PACKET_EVENT:
            self.txnb += 1
            lora.init(mode=LoRa.LORA,
                      frequency=self.frequency,
                      bandwidth=self.bw,
                      sf=self.sf,
                      preamble=8,
                      coding_rate=LoRa.CODING_4_5,
                      tx_iq=True)

    def _freq_to_float(self, frequency):
        """
        MicroPython has some inprecision when doing large float division.
        To counter this, this method first does integer division until we
        reach the decimal breaking point. This doesn't completely elimate
        the issue in all cases, but it does help for a number of commonly
        used frequencies.
        """

        divider = 6
        while divider > 0 and frequency % 10 == 0:
            frequency = frequency // 10
            divider -= 1
        if divider > 0:
            frequency = frequency / (10**divider)
        return frequency

    def _make_stat_packet(self):
        now = self.rtc.now()
        STAT_PK["stat"]["time"] = "%d-%02d-%02d %02d:%02d:%02d GMT" % (
            now[0], now[1], now[2], now[3], now[4], now[5])
        STAT_PK["stat"]["rxnb"] = self.rxnb
        STAT_PK["stat"]["rxok"] = self.rxok
        STAT_PK["stat"]["rxfw"] = self.rxfw
        STAT_PK["stat"]["dwnb"] = self.dwnb
        STAT_PK["stat"]["txnb"] = self.txnb
        return ujson.dumps(STAT_PK)

    def _make_node_packet(self, rx_data, rx_time, tmst, sf, bw, rssi, snr):
        RX_PK["rxpk"][0]["time"] = "%d-%02d-%02dT%02d:%02d:%02d.%dZ" % (
            rx_time[0], rx_time[1], rx_time[2], rx_time[3], rx_time[4],
            rx_time[5], rx_time[6])
        RX_PK["rxpk"][0]["tmst"] = tmst
        RX_PK["rxpk"][0]["freq"] = self._freq_to_float(self.frequency)
        RX_PK["rxpk"][0]["datr"] = self._sf_bw_to_dr(sf, bw)
        RX_PK["rxpk"][0]["rssi"] = rssi
        RX_PK["rxpk"][0]["lsnr"] = snr
        RX_PK["rxpk"][0]["data"] = ubinascii.b2a_base64(rx_data)[:-1]
        RX_PK["rxpk"][0]["size"] = len(rx_data)
        return ujson.dumps(RX_PK)

    def _push_data(self, data):
        token = uos.urandom(2)
        packet = bytes([PROTOCOL_VERSION]) + token + bytes(
            [PUSH_DATA]) + ubinascii.unhexlify(self.id) + data
        with self.udp_lock:
            try:
                self.sock.sendto(packet, self.server_ip)
            except Exception as ex:
                self._log('Failed to push uplink packet to server: {}', ex)

    def _pull_data(self):
        token = uos.urandom(2)
        packet = bytes([PROTOCOL_VERSION]) + token + bytes(
            [PULL_DATA]) + ubinascii.unhexlify(self.id)
        with self.udp_lock:
            try:
                self.sock.sendto(packet, self.server_ip)
            except Exception as ex:
                self._log('Failed to pull downlink packets from server: {}',
                          ex)

    def _ack_pull_rsp(self, token, error):
        TX_ACK_PK["txpk_ack"]["error"] = error
        resp = ujson.dumps(TX_ACK_PK)
        packet = bytes([PROTOCOL_VERSION]) + token + bytes(
            [PULL_ACK]) + ubinascii.unhexlify(self.id) + resp
        with self.udp_lock:
            try:
                self.sock.sendto(packet, self.server_ip)
            except Exception as ex:
                self._log('PULL RSP ACK exception: {}', ex)

    def _send_down_link(self, data, tmst, datarate, frequency):
        """
        Transmits a downlink message over LoRa.
        """

        self.lora.init(mode=LoRa.LORA,
                       frequency=frequency,
                       bandwidth=self._dr_to_bw(datarate),
                       sf=self._dr_to_sf(datarate),
                       preamble=8,
                       coding_rate=LoRa.CODING_4_5,
                       tx_iq=True)
        while utime.ticks_cpu() < tmst:
            pass
        self.lora_sock.send(data)
        self._log(
            'Sent downlink packet scheduled on {:.3f}, at {:.3f} Mhz using {}: {}',
            tmst / 1000000, self._freq_to_float(frequency), datarate, data)

    def _udp_thread(self):
        """
        UDP thread, reads data from the server and handles it.
        """

        while not self.udp_stop:
            try:
                data, src = self.sock.recvfrom(1024)
                _token = data[1:3]
                _type = data[3]
                if _type == PUSH_ACK:
                    self._log("Push ack")
                elif _type == PULL_ACK:
                    self._log("Pull ack")
                elif _type == PULL_RESP:
                    self.dwnb += 1
                    ack_error = TX_ERR_NONE
                    tx_pk = ujson.loads(data[4:])
                    tmst = tx_pk["txpk"]["tmst"]
                    t_us = tmst - utime.ticks_cpu() - 15000
                    if t_us < 0:
                        t_us += 0xFFFFFFFF
                    if t_us < 20000000:
                        self.uplink_alarm = Timer.Alarm(
                            handler=lambda x: self._send_down_link(
                                ubinascii.a2b_base64(tx_pk["txpk"]["data"]),
                                tx_pk["txpk"]["tmst"] - 50, tx_pk["txpk"][
                                    "datr"],
                                int(tx_pk["txpk"]["freq"] * 1000) * 1000),
                            us=t_us)
                    else:
                        ack_error = TX_ERR_TOO_LATE
                        self._log('Downlink timestamp error!, t_us: {}', t_us)
                    self._ack_pull_rsp(_token, ack_error)
                    self._log("Pull rsp")
            except usocket.timeout:
                pass
            except OSError as ex:
                if ex.errno != errno.EAGAIN:
                    self._log('UDP recv OSError Exception: {}', ex)
            except Exception as ex:
                self._log('UDP recv Exception: {}', ex)

            # wait before trying to receive again
            utime.sleep_ms(UDP_THREAD_CYCLE_MS)

        # we are to close the socket
        self.sock.close()
        self.udp_stop = False
        self._log('UDP thread stopped')

    def _log(self, message, *args):
        """
        Outputs a log message to stdout.
        """

        print('[{:>10.3f}] {}'.format(utime.ticks_ms() / 1000,
                                      str(message).format(*args)))

    def at(self, cmd):
        print("modem command: {}".format(cmd))
        r = self.lte.send_at_cmd(cmd).split('\r\n')
        r = list(filter(None, r))
        print("response={}".format(r))
        return r

    def blink(self, rgb, n):
        for i in range(n):
            pycom.rgbled(rgb)
            utime.sleep(0.25)
            pycom.rgbled(BLACK)
            utime.sleep(0.1)
示例#10
0
class Tracker:
    def __init__(self, pytrack=None, debug=False):
        if pytrack is not None:
            self.pytrack = pytrack
        else:
            self.pytrack = Pytrack()

        self.debug = debug
        # Instantiate and hold state for sensors (accelerometer, lte, gps, etc)
        self.accel = LIS2HH12()
        self.lte = LTE()
        self.gps = None
        # Holds the mqtt client to send messages to
        self.mqttClient = None
        # If after wakeup, we are in continuous GPS logging state
        self.continueGPSRead = False
        # Flag for handling wakeup and logging logic differently if owner is nearby
        self.checkOwnerNearby = True
        self.wlan = WLAN()  #TODO remove

    def init(self, bInitLTE=False):
        '''
        Initialize local variables used within the script. Checks for network connectivity. If not able to connect after
        a timeout, resets the machine in order to try and conenct again. 
        Initializes MQTTClient and checks if we are in a continuous GPS read state after wakeup (saved to self instance variables)
        '''
        # Check if network is connected. If not, attempt to connect
        if bInitLTE:
            self.initLTE()
        else:
            #TODO remove
            # Check if network is connected. If not, attempt to connect
            counter = 0
            while not self.wlan.isconnected():
                # If we surpass some counter timeout and network is still not connected, reset and attempt to connect again
                if counter > 100000:
                    machine.reset()
                machine.idle()
                counter += 1

        # Initialize mqttClient
        self.mqttClient = self._getMqttClient(self.debug)

        # Check to see if we are in continued gps read (went to sleep and want to continue reading GPS data)
        if self._getNVS(ConfigGPS.NVS_SLEEP_CONTINUE_GPS_READ) is not None:
            self.continueGPSRead = True
            # Erase this key from NVS
            pycom.nvs_erase(ConfigGPS.NVS_SLEEP_CONTINUE_GPS_READ)

    @staticmethod
    def _getRTC():
        '''
        Syncs the real time clock with latest time and returns RTC instance
        '''
        rtc = machine.RTC()
        if not rtc.synced():
            # Sync real time clock
            rtc.ntp_sync("pool.ntp.org")
        return rtc

    @staticmethod
    def _getNVS(key):
        '''
        Looks up at the non volatile storage for a specified key. If it exists, returns the value stored
        for that key. Otherwise, returns None
        '''
        try:
            if pycom.nvs_get(key) is not None:
                return pycom.nvs_get(key)
        except Exception:
            # Do nothing, key doesnt exist
            pass
        return None

    @staticmethod
    def _decodeBytes(data):
        '''
        Attempts to decode a byte array to string format. If not a byte type,
        just returns the original data
        '''
        try:
            return data.decode()
        except (UnicodeDecodeError, AttributeError):
            pass
        return data

    def _getMqttClient(self, debug):
        # Initialize mqttClient
        state = False
        count = 0
        mqttClient = None

        while not state and count < 5:
            try:
                count += 1
                mqttClient = MQTTClient(ConfigMqtt.CLIENT_ID,
                                        ConfigMqtt.SERVER,
                                        port=ConfigMqtt.PORT,
                                        user=ConfigMqtt.USER,
                                        password=ConfigMqtt.PASSWORD)
                # Set the callback method that will be invoked on subscription to topics
                mqttClient.set_callback(self.mqttCallback)
                mqttClient.connect()
                state = True
            except Exception as e:
                if debug:
                    print("Exception on initialize mqtt client: {}".format(e))
                time.sleep(0.5)

        #Subscribe to the disable tracking topic
        mqttClient.subscribe(topic=ConfigMqtt.TOPIC_TRACKING_STATE)
        time.sleep(0.5)
        if self.debug:
            print("Checking MQTT messages")

        mqttClient.check_msg()

        if self.debug:
            print("Messages checked. Returning MQTT client")

        return mqttClient

    def initLTE(self):
        #TODO remove
        return
        '''
        If already used, the lte device will have an active connection.
        If not, need to set up a new connection.
        '''
        lte = self.lte
        debug = self.debug

        if lte.isconnected():
            return

        # Modem does not connect successfully without first being reset.
        if debug:
            print("Resetting LTE modem ... ", end='')
        lte.send_at_cmd('AT^RESET')
        if debug:
            print("OK")
        time.sleep(5)
        lte.send_at_cmd('AT+CFUN=0')
        time.sleep(5)

        #send_at_cmd_pretty('AT!="fsm"')
        # While the configuration of the CGDCONT register survives resets,
        # the other configurations don't. So just set them all up every time.
        if debug:
            print("Configuring LTE ", end='')
        lte.send_at_cmd('AT!="clearscanconfig"')

        if debug:
            print(".", end='')
        lte.send_at_cmd('AT!="RRC::addScanBand band=26"')

        if debug:
            print(".", end='')
        lte.send_at_cmd('AT!="RRC::addScanBand band=18"')

        if debug:
            print(".", end='')
        lte.send_at_cmd('AT+CGDCONT=1,"IP","soracom.io"')

        if debug:
            print(".", end='')
        lte.send_at_cmd('AT+CGAUTH=1,1,"sora","sora"')
        print(".", end='')

        if debug:
            lte.send_at_cmd('AT+CFUN=1')
        print(" OK")

        # If correctly configured for carrier network, attach() should succeed.
        if not lte.isattached():
            if debug:
                print("Attaching to LTE network ", end='')
            lte.attach()

            while True:
                if lte.isattached():
                    #send_at_cmd_pretty('AT+COPS?')
                    time.sleep(5)
                    break

                if debug:
                    print('.', end='')
                    pycom.rgbled(0x0f0000)
                time.sleep(0.5)
                if debug:
                    pycom.rgbled(0x000000)
                time.sleep(1.5)

        # Once attached, connect() should succeed.
        if not lte.isconnected():
            if debug:
                print("Connecting on LTE network ", end='')
            lte.connect()
            while True:
                if lte.isconnected():
                    break
                if debug:
                    print('.', end='')
                time.sleep(1)

        # Once connect() succeeds, any call requiring Internet access will
        # use the active LTE connection.
        self.lte = lte

    def isContinueGPSRead(self):
        '''
        Returns true or false - whether or not we are in a continued gps read state after power cycle / deep sleep.
        If true, we should jump right to gps read. False if we should execute normal wakeup flow
        '''
        return self.continueGPSRead

    def getWakeReason(self):
        '''
        Returns the reason why the device was woken up.
        Return values are from the ConfigWakeup scope
        '''
        if self.continueGPSRead:
            return ConfigWakeup.WAKE_CONTINUE_GPS
        elif self.pytrack.get_wake_reason() == WAKE_REASON_ACCELEROMETER:
            return ConfigWakeup.WAKE_REASON_ACCELEROMATER
        else:
            return ConfigWakeup.WAKE_REASON_TIMEOUT

    def setCheckOwnerNearby(self, bOwnerNearby=True):
        '''
        If bOnwerNearby is set to true (defaults to true here and constructor instantiation), we handle
        logic for wakup and actively logging location and pushing mqtt messages differently.
        If checkowner flag is true and owner is detected nearby on device wakeup, we are much less active
        in wakeups and monitoring
        '''
        self.checkOwnerNearby = bOwnerNearby

    def isOwnerNearby(self):
        '''
        Logic here checks if a known BLE device is broadcasting nearby.
        If they are, return true. Else, return false
        '''
        # TODO remove
        return False
        bt = Bluetooth()
        bt.start_scan(ConfigBluetooth.SCAN_ALLOW_TIME)  # Scans for 10 seconds

        while bt.isscanning():
            adv = bt.get_adv()
            if adv and binascii.hexlify(adv.mac) == ConfigBluetooth.MAC_ADDR:
                try:
                    if self.debug:
                        print("Owner device found: {} Mac addr {}".format(
                            bt.resolve_adv_data(adv.data,
                                                Bluetooth.ADV_NAME_CMPL),
                            ConfigBluetooth.MAC_ADDR))
                    conn = bt.connect(adv.mac)
                    time.sleep(0.05)
                    conn.disconnect()
                    bt.stop_scan()
                except Exception:
                    bt.stop_scan()

                return True

            time.sleep(0.050)

        return False

    def handleOwnerNearby(self):
        '''
        If owner is determined to be nearby, puts device in deep sleep for specified amount
        of time without acceleration wakeup
        '''
        # Current time and last time we wokeup with owner nearby was less than 2 minutes apart.
        # Go to deep sleep for specified amount of time without accelerometer wakeup
        self.pytrack.setup_sleep(ConfigWakeup.SLEEP_TIME_OWNER_NEARBY)
        self.pytrack.go_to_sleep()

    def mqttCallback(self, topic, msg):
        '''
        Method to handle callbacks of any mqtt topics that we subscribe to.
        For now, only subscribes to bypass topic which is used to disable gps monitoring and accelerometer wakeup detection.
        topic - MQTT topic that we are subscribing to and processing the request for 
        msg - Message received from the topic
        '''
        if self.debug:
            print("In MQTT subscription callback")

        # Attempt to decode the topic and msg if in byte format
        topic = self._decodeBytes(topic)
        msg = self._decodeBytes(msg)

        # Handle mqtt topic for disabiling the monitoring
        if topic == ConfigMqtt.TOPIC_TRACKING_STATE:
            try:
                bSleep = False
                sleepTime = ConfigMqtt.SLEEP_TIME_MQTT_DISABLE

                # First check if this is a plaintext msg (not json format)
                if msg == ConfigMqtt.ENABLE_TRACKING_MSG:
                    # Tracking is enabled, continue
                    bSleep = False

                elif msg == ConfigMqtt.DISABLE_TRACKING_MSG:
                    # If tracking is disabled, go to sleep with configured sleep time
                    bSleep = True

                else:
                    # Structure of the message will be in json format:
                    # { msg: "XXX", "time": 123} where msg value is ON/OFF for disable (OFF) or enable(ON)
                    # and time value is time to disable tracking (will automatically be re-enabled after
                    # time surpasses if this value is present)
                    jMsg = ujson.loads(msg)
                    # Check if the msg is Y. If so, need to stop tracking
                    if jMsg['msg'] == ConfigMqtt.DISABLE_TRACKING_MSG:
                        # Requested to go to sleep, so set the flags
                        bSleep = True
                        # If theres a time with the disable flag, go to sleep for that amount of time
                        if jMsg['time'] and jMsg['time'] is not None:
                            sleepTime = jMsg['time']

                # If the flag was set, go to sleep
                if bSleep:
                    if self.debug:
                        print("Disable tracking from mqtt. Sleeping for {}".
                              format(sleepTime))
                    self.goToSleep(sleepTime=sleepTime)

            except ValueError:
                if self.debug:
                    print('Exception parsing disable tracking mqtt msg')

    def sendMQTTMessage(self, topic, msg, retain=False):
        '''
        Sends an MQTT message to the specified topic
        topic - Topic to send to
        msg - Message to send to topic
        mqttClient - MQTT Client used to send the message. If not defined, sends to default configured mqtt client in configuration
        '''
        debug = self.debug

        # If LTE is not setup, initialize it so we can send messages
        #self.initLTE()

        if self.mqttClient is None:
            try:
                # Instantiate a new MQTTClient
                self.mqttClient = self._getMqttClient(debug)
            except:
                self.mqttClient = None

        try:
            if self.mqttClient is not None:
                # Send the message topic
                self.mqttClient.publish(topic=topic, msg=msg, retain=retain)
        except:
            if debug:
                print(
                    "Exception occurred attempting to connect to MQTT server")

    def goToSleep(self, sleepTime=60, bWithInterrupt=False, bSleepGps=True):
        '''
        Puts the py to deepsleep, turning off lte in order to reduce battery consumption.
        By default, sleeps for 60 seconds and powers down gps.
        sleepTime - specifies the time (in seconds) to put the device in deep sleep before waking up
        bWithInterrupt - if True, will wakeup for both timer timeout as well as acceleration interrupt
        bSleepGps - If True, puts the gps in deepsleep state as well (will take longer to reinitialize and refix gps signal)
        '''
        # Enable wakeup source from INT pin
        self.pytrack.setup_int_pin_wake_up(False)

        # Enable activity and inactivity interrupts with acceleration threshold and min duration
        if bWithInterrupt:
            self.pytrack.setup_int_wake_up(True, True)
            self.accel.enable_activity_interrupt(
                ConfigAccelerometer.INTERRUPT_THRESHOLD,
                ConfigAccelerometer.INTERRUPT_DURATION)

        # If mqttClient is defined, disconnect
        if self.mqttClient is not None:
            try:
                self.mqttClient.disconnect()
            except:
                if self.debug:
                    print("Exception occurred disconnecting from mqtt client")

        # Disconnect lte
        if self.lte is not None and self.lte.isconnected():
            try:
                self.lte.disconnect()
                time.sleep(1)
                self.lte.dettach()
            except:
                if self.debug:
                    print("Exception disconnecting from lte")

        # Go to sleep for specified amount of time if no accelerometer wakeup
        if self.debug:
            print("Sleeping for {} seconds with accel interrupt? {}".format(
                sleepTime, bWithInterrupt))
            time.sleep(0.5)

        time.sleep(0.1)
        self.pytrack.setup_sleep(sleepTime)
        self.pytrack.go_to_sleep(gps=bSleepGps)

    def _getGpsFix(self):
        '''
        Attempts to lock on a signal to the gps.
        Returns true if signal is found, false otherwise
        '''
        # Attempt to get the gps lock for X number of attempts (defined in config)
        maxTries = max(ConfigGPS.LOCK_FAIL_ATTEMPTS, 1)
        signalFixTries = maxTries
        while signalFixTries > 0:
            signalFixTries -= 1
            if self.debug:
                print("On GPS fix try number {} of {}".format(
                    maxTries - signalFixTries, maxTries))
            self.gps.get_fix(debug=False)
            pycom.heartbeat(False)
            bIsFixed = False

            if self.gps.fixed():
                # Got the GPS fix, exit out of this while condition
                if self.debug:
                    pycom.rgbled(0x000f00)
                bIsFixed = True
            else:
                # If couldnt get a signal fix, try again
                if self.debug:
                    pycom.rgbled(0x0f0000)

        return bIsFixed

    def monitorLocation(self, bWithMotion=True):
        '''
        Sends GPS location to Mqtt topic. Continues sending data as long as motion is detected
        bWithMotion - If true, continues to monitor and send GPS location to topic as long as accelerometer activity is
            detected. If false, only publishes location once
        '''
        if self.debug:
            print("Monitoring Location")
        self.gps = L76GNSS(self.pytrack,
                           timeout=ConfigGPS.LOCK_TIMEOUT,
                           debug=False)
        self.gps.setAlwaysOn()

        if not self._getGpsFix():
            # Couldnt get a signal so send message to topic for gps not available and exit (go back to sleep)
            if self.debug:
                print("Couldnt get a GPS signal after {} attempts".format(
                    ConfigGPS.LOCK_FAIL_ATTEMPTS))
            self.sendMQTTMessage(ConfigMqtt.TOPIC_GPS_NOT_AVAILABLE, "-1")
            return

        # Otherwise we have a gps signal, so get the coordinates and send to topic
        coordinates = self.gps.coordinates()
        self.sendMQTTMessage(ConfigMqtt.TOPIC_GPS, coordinates)

        # Save current timestamp of log time
        self._getRTC()  # Syncs rtc
        pycom.nvs_set(ConfigGPS.NVS_LAST_LOCATION_LOG_TIME, utime.time())

        # If we want to monitor with motion (send multiple gps coordinates as long as there is motion), start monitoring
        if bWithMotion & self.accelInMotion():
            # Go to sleep for a specified amount of time (keeping GPS alive) to conserve some battery
            # TODO - check if this is better than setPeriodicMode
            if self.debug:
                print("Putting gps in low power and going to sleep")
            #Save state to nvs (non volatile storage)
            pycom.nvs_set(ConfigGPS.NVS_SLEEP_CONTINUE_GPS_READ, 1)
            self.goToSleep(sleepTime=ConfigGPS.SLEEP_BETWEEN_READS,
                           bWithInterrupt=False,
                           bSleepGps=False)

    def accelInMotion(self, numReads=10):
        '''
        Takes numReads measurements of accelerometer data to detect if there is motion.
        numReads - Number of measurements to take to detect motion
        Returns true if delta sensor data is above a threshold (meaning there is active motion), false otherwise
        '''
        accel = self.accel
        xyzList = list()
        xyzList.append(accel.acceleration())
        for index in range(0, numReads):
            # Print change from last reading
            time.sleep(0.5)
            xyzList.append(accel.acceleration())
            deltas = list(
                map(lambda b, a: abs(b - a), xyzList[-1], xyzList[-2])
            )  # Get last element (with -1) and subtract previous element (-2)
            # If max delta is greater than threshold, return true
            if max(deltas) >= ConfigAccelerometer.MOTION_CHECK_MIN_THRESHOLD:
                return True

        # Get the largest change in x, y, and z and see if that surpasses the threshold
        minVals = list()
        maxVals = list()
        for i in range(3):
            minVals.append(min(map(lambda a, i=i: a[i], xyzList)))
            maxVals.append(max(map(lambda a, i=i: a[i], xyzList)))

        # Get the maximum delta for x, y, z for all logged points
        deltas = list(map(lambda b, a: b - a, maxVals, minVals))
        if self.debug:
            print("Deltas accel motion {}".format(deltas))
        # If any x, y, or z axis in deltas array have a total delta change past the allowed threshold, return true. Otherwise return false
        return max(deltas) >= ConfigAccelerometer.MOTION_CHECK_MIN_THRESHOLD

    def logRegularCoordinates(self):
        '''
        On regular timed wakeups, we still log our location every so often. Check the last time the location was
        logged and if the time is greater than the surpassed time defined in config, log coordinates again
        '''
        lastLogTime = self._getNVS(ConfigGPS.NVS_LAST_LOCATION_LOG_TIME)
        self._getRTC()  # Updates RTC

        # Compare the last log time with current rtc to see if threshold has passed
        if (lastLogTime is None or
            (utime.time() - lastLogTime > ConfigGPS.LOCATION_LOG_INTERVAL)):
            # Need to update gps as time has elapsed since last log
            self.monitorLocation(bWithMotion=False)  # Only log once

    def accelWakeup(self):
        '''
        Logic to handle board wakeup interruption due to accelerometer.
        Sends mqtt messages for wakeup if client is defined.
        '''
        debug = self.debug
        if debug:
            print("Accelerometer wakeup")

        # Check if the owner is nearby (owner moving device). If so, handle differently
        if self.checkOwnerNearby and self.isOwnerNearby():
            # Owner is nearby. If last wake is with 2 minutes, we are most likely riding
            # so go to sleep for longer time without wakeup
            self.handleOwnerNearby()

        # Check the accelerometer is still active (preventing false negatives for alerting of theft)
        # If we dont ready any new accelerometer motion, exit this function and dont send any accelerometer wakeup or gps msgs
        if not self.accelInMotion():
            if debug:
                print("Not currently in motion, going back to sleep")
            return
        else:
            if debug:
                print("Motion detected after wakeup...")

        # Not a false wakeup, so send a message to the initial accelerometer wakeup topic
        self.sendMQTTMessage(ConfigMqtt.TOPIC_ACCEL_WAKEUP, "1")

        # Send GPS updates while there has been continued motion for some time
        self.monitorLocation(bWithMotion=True)