diff --git a/adafruit_fona/adafruit_fona.py b/adafruit_fona/adafruit_fona.py index e75acd2..3c6b498 100644 --- a/adafruit_fona/adafruit_fona.py +++ b/adafruit_fona/adafruit_fona.py @@ -71,7 +71,7 @@ # pylint: enable=bad-whitespace -# pylint: disable=too-many-instance-attributes +# pylint: disable=too-many-instance-attributes, too-many-public-methods class FONA: """CircuitPython FONA module interface. :param ~busio.uart UART: FONA UART connection. @@ -96,69 +96,11 @@ def __init__(self, uart, rst, debug=False): if not self._init_fona(): raise RuntimeError("Unable to find FONA. Please check connections.") - # GPRS - self._apn = None - self._apn_username = None - self._apn_password = None - - @property - # pylint: disable=too-many-return-statements - def version(self): - """Returns FONA Version, as a string.""" - if self._fona_type == FONA_800_L: - return "FONA 800L" - if self._fona_type == FONA_800_H: - return "FONA 800H" - if self._fona_type == FONA_808_V1: - return "FONA 808 (v1)" - if self._fona_type == FONA_808_V2: - return "FONA 808 (v2)" - if self._fona_type == FONA_3G_A: - return "FONA 3G (US)" - if self._fona_type == FONA_3G_E: - return "FONA 3G (EU)" - return -1 - - @property - def iemi(self): - """Returns FONA module's IEMI number.""" - self._buf = b"" - self._uart.reset_input_buffer() - - if self._debug: - print("\t---> ", "AT+GSN") - self._uart.write(b"AT+GSN\r\n") - self._read_line(multiline=True) - iemi = self._buf[0:15] - return iemi.decode("utf-8") - - def pretty_ip(self, ip): # pylint: disable=no-self-use, invalid-name - """Converts a bytearray IP address to a dotted-quad string for printing""" - return "%d.%d.%d.%d" % (ip[0], ip[1], ip[2], ip[3]) - - @property - def local_ip(self): - """Returns the local IP Address.""" - if self._debug: - print("\t---> AT+CIFSR") - - self._uart.write(b"AT+CIFSR\r\n") - self._read_line() - return self.pretty_ip(self._buf) - # pylint: disable=too-many-branches, too-many-statements def _init_fona(self): """Initializes FONA module.""" + self.reset() - # Reset the module - self._rst.value = True - time.sleep(0.01) - self._rst.value = False - time.sleep(0.1) - self._rst.value = True - - if self._debug: - print("Attempting to open comm with ATs") timeout = 7000 while timeout > 0: while self._uart.in_waiting: @@ -170,8 +112,6 @@ def _init_fona(self): timeout -= 500 if timeout <= 0: - if self._debug: - print(" * Timeout: No response to AT. Last ditch attempt.") self._send_check_reply(CMD_AT, reply=REPLY_OK) time.sleep(0.1) self._send_check_reply(CMD_AT, reply=REPLY_OK) @@ -194,9 +134,7 @@ def _init_fona(self): self._buf = b"" self._uart.reset_input_buffer() - if self._debug: - print("\t---> ", "ATI") - self._uart.write(b"ATI\r\n") + self.uart_write(b"ATI\r\n") self._read_line(multiline=True) if self._buf.find(b"SIM808 R14") != -1: @@ -209,76 +147,112 @@ def _init_fona(self): self._fona_type = FONA_3G_E if self._fona_type == FONA_800_L: - # determine if _H - if self._debug: - print("\t ---> AT+GMM") - self._uart.write(b"AT+GMM\r\n") + # determine if SIM800H + self.uart_write(b"AT+GMM\r\n") self._read_line(multiline=True) - if self._debug: - print("\t <---", self._buf) if self._buf.find(b"SIM800H") != -1: self._fona_type = FONA_800_H return True - @property - def gprs(self): - """Returns module's GPRS state.""" - if self._debug: - print("* Check GPRS State") - if not self._send_parse_reply(b"AT+CGATT?", b"+CGATT: ", ":"): + def factory_reset(self): + """Resets modem to factory configuration.""" + self.uart_write(b"ATZ\r\n") + + if not self._expect_reply(REPLY_OK): return False - return self._buf + return True - def configure_gprs(self, config): - """If config provided, sets GPRS configuration to provided tuple in format: - (apn_network, apn_username, apn_password) + def reset(self): + """Performs a hardware reset on the modem. + NOTE: This may take a few seconds to complete. """ if self._debug: - print("* Setting GPRS Config to: ", config) - apn, username, password = config - self._apn = apn.encode() - self._apn_username = username.encode() - self._apn_password = password.encode() - return self._apn, self._apn_username, self._apn_password - - @gprs.setter - def gprs(self, gprs_on=True): - """Sets GPRS configuration. - :param bool gprs_on: Turns on GPRS, enabled by default. + print("* Reset FONA") + # Reset the module + self._rst.value = True + time.sleep(0.01) + self._rst.value = False + time.sleep(0.1) + self._rst.value = True - """ - attempts = 5 - while not self._set_gprs(gprs_on): - if attempts == 0: - raise RuntimeError("Unable to establish PDP context.") - if self._debug: - print("* Unable to bringup network, retrying, ", attempts) - self._set_gprs(False) - attempts -= 1 - time.sleep(5) + @property + # pylint: disable=too-many-return-statements + def version(self): + """Returns FONA Version, as a string.""" + if self._fona_type == FONA_800_L: + return "FONA 800L" + if self._fona_type == FONA_800_H: + return "FONA 800H" + if self._fona_type == FONA_808_V1: + return "FONA 808 (v1)" + if self._fona_type == FONA_808_V2: + return "FONA 808 (v2)" + if self._fona_type == FONA_3G_A: + return "FONA 3G (US)" + if self._fona_type == FONA_3G_E: + return "FONA 3G (EU)" + return -1 + + @property + def iemi(self): + """Returns FONA module's IEMI number.""" + if self._debug: + print("FONA IEMI") + self._buf = b"" + self._uart.reset_input_buffer() + + self.uart_write(b"AT+GSN\r\n") + self._read_line(multiline=True) + iemi = self._buf[0:15] + return iemi.decode("utf-8") + + @property + def local_ip(self): + """Returns the local IP Address, False if not set.""" + self.uart_write(b"AT+CIFSR\r\n") + self._read_line() + try: + ip_addr = self.pretty_ip(self._buf) + except ValueError: + return False + return ip_addr + + @property + def iccid(self): + """Returns SIM Card's ICCID number as a string.""" + if self._debug: + print("ICCID") + self.uart_write(b"AT+CCID\r\n") + self._read_line(timeout=2000) # 6.2.23, 2sec max. response time + iccid = self._buf.decode() + self._read_line() # eat 'OK' + return iccid + + @property + def gprs(self): + """Returns module's GPRS state.""" + self._read_line() + + if not self._send_parse_reply(b"AT+CGATT?", b"+CGATT: ", ":"): + return False + if not self._buf: + return False return True # pylint: disable=too-many-return-statements - def _set_gprs(self, gprs_on=True): - """Enables or disables GPRS configuration. - :param bool gprs_on: Turns on GPRS, enabled by default. + def set_gprs(self, apn=None, enable=True): + """Configures and brings up GPRS. + :param bool enable: Enables or disables GPRS. """ - if gprs_on: - if self._debug: - print("* Enabling GPRS..") - - # ensure FONA is registered with cell network - attempts = 10 - while self.network_status != 1: - if attempts == 0: - return False - if self._debug: - print("* Not registered with network, retrying, ", attempts) - attempts -= 1 - time.sleep(5) + if enable: + apn_name, apn_user, apn_pass = apn + + apn_name = apn_name.encode() + apn_user = apn_user.encode() + apn_pass = apn_pass.encode() # enable multi connection mode (3,1) if not self._send_check_reply(b"AT+CIPMUX=1", reply=REPLY_OK): @@ -307,35 +281,33 @@ def _set_gprs(self, gprs_on=True): # Send command AT+SAPBR=3,1,"APN","" # where is the configured APN value. self._send_check_reply_quoted( - b'AT+SAPBR=3,1,"APN",', self._apn, REPLY_OK, 10000 + b'AT+SAPBR=3,1,"APN",', apn_name, REPLY_OK, 10000 ) # send AT+CSTT,"apn","user","pass" - if self._debug: - print("setting APN...") self._uart.reset_input_buffer() - self._uart.write(b'AT+CSTT="' + self._apn) + self.uart_write(b'AT+CSTT="' + apn_name) - if self._apn_username is not None: - self._uart.write(b'","' + self._apn_username) + if apn_user is not None: + self.uart_write(b'","' + apn_user) - if self._apn_password is not None: - self._uart.write(b'","' + self._apn_password) - self._uart.write(b'"\r\n') + if apn_pass is not None: + self.uart_write(b'","' + apn_pass) + self.uart_write(b'"\r\n') if not self._get_reply(REPLY_OK): return False # Set username if not self._send_check_reply_quoted( - b'AT+SAPBR=3,1,"USER",', self._apn_username, REPLY_OK, 10000 + b'AT+SAPBR=3,1,"USER",', apn_user, REPLY_OK, 10000 ): return False # Set password if not self._send_check_reply_quoted( - b'AT+SAPBR=3,1,"PWD",', self._apn_password, REPLY_OK, 100000 + b'AT+SAPBR=3,1,"PWD",', apn_pass, REPLY_OK, 100000 ): return False @@ -364,6 +336,8 @@ def _set_gprs(self, gprs_on=True): @property def network_status(self): """Returns cellular/network status""" + if self._debug: + print("Network status") if not self._send_parse_reply(b"AT+CREG?", b"+CREG: ", idx=1): return False if self._buf == 0: @@ -390,6 +364,8 @@ def network_status(self): @property def rssi(self): """Returns cellular network's Received Signal Strength Indicator (RSSI).""" + if self._debug: + print("RSSI") if not self._send_parse_reply(b"AT+CSQ", b"+CSQ: "): return False @@ -411,9 +387,9 @@ def rssi(self): @property def gps(self): - """Returns the GPS status.""" + """Returns the GPS fix.""" if self._debug: - print("GPS STATUS") + print("GPS Fix") if self._fona_type == FONA_808_V2: # 808 V2 uses GNS commands and doesn't have an explicit 2D/3D fix status. # Instead just look for a fix and if found assume it's a 3D fix. @@ -437,36 +413,11 @@ def gps(self): return status @gps.setter - def gps(self, enable_gps=False): - """Attempts to enable or disable the GPS module. - NOTE: This is only for FONA 3G or FONA808 modules. - :param bool enable_gps: Enables the GPS module, disabled by default. - - """ - # failed attempts before returning -1 - attempts = 10 - failure_count = 0 - # Set the GPS module - self._set_gps(enable_gps) - - # Wait for a GPS fix - while self.gps != 3: - if self._debug: - print("\t* GPS fix not found, retrying, ", failure_count) - failure_count += 1 - if failure_count >= attempts: - return False - time.sleep(1) - - return True - - def _set_gps(self, gps_on=False): + def gps(self, gps_on=False): """Sets GPS module power, parses returned buffer. :param bool gps_on: Enables the GPS module, disabled by default. """ - if self._debug: - print("* Setting GPS") if not ( self._fona_type == FONA_3G_A or self._fona_type == FONA_3G_E @@ -510,14 +461,12 @@ def get_host_by_name(self, hostname): :param str hostname: Destination server. """ - self._read_line() if self._debug: - print("*** get_host_by_name: ", hostname) + print("*** Get host by name") + self._read_line() if isinstance(hostname, str): hostname = bytes(hostname, "utf-8") - if self._debug: - print("\t---> AT+CDNSGIP=", hostname) if not self._send_check_reply( b'AT+CDNSGIP="' + hostname + b'"\r\n', reply=REPLY_OK ): @@ -528,10 +477,17 @@ def get_host_by_name(self, hostname): while not self._parse_reply(b"+CDNSGIP:", idx=2): self._read_line() - if self._debug: - print("\t<--- ", self._buf) return self._buf + def pretty_ip(self, ip): # pylint: disable=no-self-use, invalid-name + """Converts a bytearray IP address to a dotted-quad string for printing""" + return "%d.%d.%d.%d" % (ip[0], ip[1], ip[2], ip[3]) + + def unpretty_ip(self, ip): # pylint: disable=no-self-use, invalid-name + """Converts a dotted-quad string to a bytearray IP address""" + octets = [int(x) for x in ip.split(".")] + return bytes(octets) + ### Socket API (TCP, UDP) ### def get_socket(self): @@ -539,9 +495,9 @@ def get_socket(self): """ if self._debug: - print("*** Allocating Socket") + print("*** Get socket") - self._uart.write(b"AT+CIPSTATUS\r\n") + self.uart_write(b"AT+CIPSTATUS\r\n") self._read_line(100) # OK self._read_line(100) # table header @@ -556,6 +512,8 @@ def get_socket(self): # read out the rest of the responses for _ in range(allocated_socket, FONA_MAX_SOCKETS): self._read_line(100) + if self._debug: + print("Allocated socket #%d" % allocated_socket) return allocated_socket def remote_ip(self, sock_num): @@ -567,12 +525,10 @@ def remote_ip(self, sock_num): sock_num < FONA_MAX_SOCKETS ), "Provided socket exceeds the maximum number of \ sockets for the FONA module." - self._uart.write(b"AT+CIPSTATUS=" + str(sock_num).encode() + b"\r\n") + self.uart_write(b"AT+CIPSTATUS=" + str(sock_num).encode() + b"\r\n") self._read_line(100) self._parse_reply(b"+CIPSTATUS:", idx=3) - if self._debug: - print("\t<--- ", self._buf) return self._buf def socket_status(self, sock_num): @@ -589,8 +545,6 @@ def socket_status(self, sock_num): # eat the 'STATE: ' message self._read_line() - if self._debug: - print("\t<--- ", self._buf) # read "C: " for each active connection for state in range(0, sock_num + 1): @@ -642,6 +596,13 @@ def socket_connect(self, sock_num, dest, port, conn_mode=TCP_MODE): :param int conn_mode: Connection mode (TCP/UDP) """ + if self._debug: + print( + "*** Socket connect, protocol={}, port={}, ip={}".format( + conn_mode, port, dest + ) + ) + self._uart.reset_input_buffer() assert ( sock_num < FONA_MAX_SOCKETS @@ -656,25 +617,19 @@ def socket_connect(self, sock_num, dest, port, conn_mode=TCP_MODE): ) # Query local IP Address - if self._debug: - print("\t---> AT+CIFSR") - self._uart.write(b"AT+CIFSR\r\n") + self.uart_write(b"AT+CIFSR\r\n") self._read_line() # Start connection - self._uart.write(b"AT+CIPSTART=") - self._uart.write(str(sock_num).encode()) + self.uart_write(b"AT+CIPSTART=") + self.uart_write(str(sock_num).encode()) if conn_mode == 0: - if self._debug: - print('\t--->AT+CIPSTART="TCP","{}",{}'.format(dest, port)) - self._uart.write(b',"TCP","') + self.uart_write(b',"TCP","') else: - if self._debug: - print('\t--->AT+CIPSTART="UDP","{}",{}'.format(dest, port)) - self._uart.write(b',"UDP","') - self._uart.write(dest.encode() + b'","') - self._uart.write(str(port).encode() + b'"') - self._uart.write(b"\r\n") + self.uart_write(b',"UDP","') + self.uart_write(dest.encode() + b'","') + self.uart_write(str(port).encode() + b'"') + self.uart_write(b"\r\n") if not self._expect_reply(REPLY_OK): return False @@ -690,14 +645,16 @@ def socket_close(self, sock_num, quick_close=1): :param int quick_close: Quickly or slowly close the socket. Enabled by default """ + if self._debug: + print("*** Closing socket #%d" % sock_num) assert ( sock_num < FONA_MAX_SOCKETS ), "Provided socket exceeds the maximum number of \ sockets for the FONA module." self._read_line() - self._uart.write(b"AT+CIPCLOSE=" + str(sock_num).encode() + b",") - self._uart.write(str(quick_close).encode() + b"\r\n") + self.uart_write(b"AT+CIPCLOSE=" + str(sock_num).encode() + b",") + self.uart_write(str(quick_close).encode() + b"\r\n") self._read_line() if not self._parse_reply(b"CLOSE OK", idx=0): return False @@ -717,11 +674,11 @@ def socket_read(self, sock_num, length): self._read_line() if self._debug: print("* socket read") - print("\t ---> AT+CIPRXGET=2,{},{}".format(sock_num, length)) - self._uart.write(b"AT+CIPRXGET=2,") - self._uart.write(str(sock_num).encode()) - self._uart.write(b",") - self._uart.write(str(length).encode() + b"\r\n") + + self.uart_write(b"AT+CIPRXGET=2,") + self.uart_write(str(sock_num).encode()) + self.uart_write(b",") + self.uart_write(str(length).encode() + b"\r\n") self._read_line() @@ -745,28 +702,19 @@ def socket_write(self, sock_num, buffer): ), "Provided socket exceeds the maximum number of \ sockets for the FONA module." - if self._debug: - print("\t--->AT+CIPSEND={},{}".format(sock_num, len(buffer))) - self._uart.reset_input_buffer() - self._uart.write(b"AT+CIPSEND=" + str(sock_num).encode()) - self._uart.write(b"," + str(len(buffer)).encode()) - self._uart.write(b"\r\n") + self.uart_write(b"AT+CIPSEND=" + str(sock_num).encode()) + self.uart_write(b"," + str(len(buffer)).encode()) + self.uart_write(b"\r\n") self._read_line() - if self._debug: - print("\t<--- ", self._buf) - if self._buf[0] != 62: # promoting mark ('>') not found return False - self._uart.write(buffer + b"\r\n") + self.uart_write(buffer + b"\r\n") self._read_line(3000) - if self._debug: - print("\t<--- ", self._buf) - if "SEND OK" not in self._buf.decode(): return False @@ -774,6 +722,16 @@ def socket_write(self, sock_num, buffer): ### UART Reply/Response Helpers ### + def uart_write(self, buffer): + """UART ``write`` with optional debug that prints + the buffer before sending. + :param bytes buffer: Buffer of bytes to send to the bus. + + """ + if self._debug: + print("\tUARTWRITE ::", buffer.decode()) + self._uart.write(buffer) + def _send_parse_reply(self, send_data, reply_data, divider=",", idx=0): """Sends data to FONA module, parses reply data returned. :param bytes send_data: Data to send to the module. @@ -798,18 +756,11 @@ def _get_reply( self._uart.reset_input_buffer() if data is not None: - if self._debug: - print("\t---> ", data) - self._uart.write(data + "\r\n") + self.uart_write(data + b"\r\n") else: - if self._debug: - print("\t---> {}{}".format(prefix, suffix)) - self._uart.write(prefix + suffix + b"\r\n") + self.uart_write(prefix + suffix + b"\r\n") line = self._read_line(timeout) - - if self._debug: - print("\t<--- ", self._buf) return line def _parse_reply(self, reply, divider=",", idx=0): @@ -838,7 +789,8 @@ def _parse_reply(self, reply, divider=",", idx=0): return True def _read_line(self, timeout=FONA_DEFAULT_TIMEOUT_MS, multiline=False): - """Reads one or multiple lines into the buffer. + """Reads one or multiple lines into the buffer. Optionally prints the buffer + after reading. :param int timeout: Time to wait for UART serial to reply, in seconds. :param bool multiline: Read multiple lines. @@ -873,6 +825,9 @@ def _read_line(self, timeout=FONA_DEFAULT_TIMEOUT_MS, multiline=False): timeout -= 1 time.sleep(0.001) + if self._debug: + print("\tUARTREAD ::", self._buf.decode()) + return reply_idx def _send_check_reply( @@ -930,21 +885,10 @@ def _get_reply_quoted(self, prefix, suffix, timeout): """ self._uart.reset_input_buffer() - if self._debug: - print("\t---> ", end="") - print(prefix, end="") - print('""', end="") - print(suffix, end="") - print('""') - - self._uart.write(prefix + b'"') - self._uart.write(suffix + b'"\r\n') + self.uart_write(prefix + b'"') + self.uart_write(suffix + b'"\r\n') line = self._read_line(timeout) - - if self._debug: - print("\t<--- ", self._buf) - return line def _expect_reply(self, reply, timeout=10000): @@ -953,8 +897,6 @@ def _expect_reply(self, reply, timeout=10000): """ self._read_line(timeout) - if self._debug: - print("\t<--- ", self._buf) if reply not in self._buf: return False return True diff --git a/adafruit_fona/adafruit_fona_gsm.py b/adafruit_fona/adafruit_fona_gsm.py new file mode 100755 index 0000000..2c84cde --- /dev/null +++ b/adafruit_fona/adafruit_fona_gsm.py @@ -0,0 +1,96 @@ +# The MIT License (MIT) +# +# Copyright (c) 2020 Brent Rubell for Adafruit Industries +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +""" +`adafruit_fona_gsm` +================================================================================= + +Interface for 2G GSM cellular modems such as the Adafruit FONA808. + +* Author(s): Brent Rubell + +""" + + +class GSM: + """Interface for interacting with FONA 2G GSM modems. + """ + + def __init__(self, fona, apn): + """Initializes interface with 2G GSM modem. + :param adafruit_fona fona: The Adafruit FONA module we are using. + :param tuple apn: Tuple containing APN name, (optional) APN username, + and APN password. + + """ + self._iface = fona + self._apn = apn + self._gsm_connected = False + + # Enable GPS module + self._iface.gps = True + + def __enter__(self): + return self + + def __exit__(self, exception_type, exception_value, traceback): + self.disconnect() + + @property + def imei(self): + """Returns the GSM modem's IEMI number, as a string.""" + return self._iface.iemi + + @property + def iccid(self): + """Returns the SIM card's ICCID, as a string.""" + return self._iface.iccid + + @property + def is_attached(self): + """Returns if the modem is attached to the network + and the GPS has a fix.""" + if self._iface.gps == 3 and self._iface.network_status == 1: + return True + return False + + @property + def is_connected(self): + """Returns if attached to GSM + and an IP Addresss was obtained. + + """ + if not self._gsm_connected: + return False + return True + + def connect(self): + """Connect to GSM network.""" + if self._iface.set_gprs(self._apn, True): + self._gsm_connected = True + else: + # reset context for next connection attempt + self._iface.set_gprs(self._apn, False) + + def disconnect(self): + """Disconnect from GSM network.""" + self._iface.set_gprs(self._apn, False) + self._gsm_connected = False diff --git a/examples/fona_aio_post.py b/examples/fona_aio_post.py index 2119c37..1aa23cc 100755 --- a/examples/fona_aio_post.py +++ b/examples/fona_aio_post.py @@ -3,6 +3,7 @@ import busio import digitalio from adafruit_fona.adafruit_fona import FONA +from adafruit_fona.adafruit_fona_gsm import GSM import adafruit_fona.adafruit_fona_socket as cellular_socket import adafruit_requests as requests @@ -20,18 +21,19 @@ rst = digitalio.DigitalInOut(board.D4) # Initialize FONA module (this may take a few seconds) -print("Initializing FONA") fona = FONA(uart, rst) -# Enable GPS -fona.gps = True +# initialize gsm +gsm = GSM(fona, (secrets["apn"], secrets["apn_username"], secrets["apn_password"])) -# Bring up cellular connection -fona.configure_gprs((secrets["apn"], secrets["apn_username"], secrets["apn_password"])) +while not gsm.is_attached: + print("Attaching to network...") + time.sleep(0.5) -# Bring up GPRS -fona.gprs = True -print("FONA initialized") +while not gsm.is_connected: + print("Connecting to network...") + gsm.connect() + time.sleep(5) # Initialize a requests object with a socket and cellular interface requests.set_socket(cellular_socket, fona) diff --git a/examples/fona_cheerlights.py b/examples/fona_cheerlights.py index 879c612..8ed164d 100755 --- a/examples/fona_cheerlights.py +++ b/examples/fona_cheerlights.py @@ -3,12 +3,14 @@ import busio import digitalio from adafruit_fona.adafruit_fona import FONA +from adafruit_fona.adafruit_fona_gsm import GSM import adafruit_fona.adafruit_fona_socket as cellular_socket import adafruit_requests as requests import neopixel import adafruit_fancyled.adafruit_fancyled as fancy + # Get GPRS details and more from a secrets.py file try: from secrets import secrets @@ -23,18 +25,19 @@ rst = digitalio.DigitalInOut(board.D4) # Initialize FONA module (this may take a few seconds) -print("Initializing FONA") fona = FONA(uart, rst) -# Enable GPS -fona.gps = True +# initialize gsm +gsm = GSM(fona, (secrets["apn"], secrets["apn_username"], secrets["apn_password"])) -# Bring up cellular connection -fona.configure_gprs((secrets["apn"], secrets["apn_username"], secrets["apn_password"])) +while not gsm.is_attached: + print("Attaching to network...") + time.sleep(0.5) -# Bring up GPRS -fona.gprs = True -print("FONA initialized") +while not gsm.is_connected: + print("Connecting to network...") + gsm.connect() + time.sleep(5) # Initialize a requests object with a socket and cellular interface requests.set_socket(cellular_socket, fona) diff --git a/examples/fona_simpletest.py b/examples/fona_simpletest.py index 7998d0f..8eb5e1d 100644 --- a/examples/fona_simpletest.py +++ b/examples/fona_simpletest.py @@ -1,7 +1,9 @@ +import time import board import busio import digitalio from adafruit_fona.adafruit_fona import FONA +from adafruit_fona.adafruit_fona_gsm import GSM import adafruit_fona.adafruit_fona_socket as cellular_socket import adafruit_requests as requests @@ -26,21 +28,24 @@ # Initialize FONA module (this may take a few seconds) fona = FONA(uart, rst) -# Enable GPS -fona.gps = True +# initialize gsm +gsm = GSM(fona, (secrets["apn"], secrets["apn_username"], secrets["apn_password"])) -# Bring up cellular connection -fona.configure_gprs((secrets["apn"], secrets["apn_username"], secrets["apn_password"])) +while not gsm.is_attached: + print("Attaching to network...") + time.sleep(0.5) -# Bring up GPRS -fona.gprs = True - -# Initialize a requests object with a socket and cellular interface -requests.set_socket(cellular_socket, fona) +while not gsm.is_connected: + print("Connecting to network...") + gsm.connect() + time.sleep(5) print("My IP address is:", fona.local_ip) print("IP lookup adafruit.com: %s" % fona.get_host_by_name("adafruit.com")) +# Initialize a requests object with a socket and cellular interface +requests.set_socket(cellular_socket, fona) + # fona._debug = True print("Fetching text from", TEXT_URL) r = requests.get(TEXT_URL)