2626
2727CircuitPython library for the Adafruit FONA cellular module
2828
29-
3029* Author(s): ladyada, Brent Rubell
3130
3231Implementation Notes
6261FONA_3G_A = const (0x4 )
6362FONA_3G_E = const (0x5 )
6463
65- # HTTP Actions
66- FONA_HTTP_GET = const (0x00 )
67- FONA_HTTP_POST = const (0x01 )
68- FONA_HTTP_HEAD = const (0x02 )
69-
7064FONA_MAX_SOCKETS = const (6 )
7165
72- # FONA SMS storage methods
73- FONA_SMS_STORAGE_SIM = b"\" SM\" "
74- FONA_SMS_STORAGE_INTERNAL = b"\" SM\" "
66+ FONA_SMS_STORAGE_SIM = b'"SM"'
67+ FONA_SMS_STORAGE_INTERNAL = b'"SM"'
7568
7669# pylint: enable=bad-whitespace
7770
@@ -175,7 +168,6 @@ def reset(self):
175168 """
176169 if self ._debug :
177170 print ("* Reset FONA" )
178- # Reset the module
179171 self ._rst .value = True
180172 time .sleep (0.01 )
181173 self ._rst .value = False
@@ -205,7 +197,6 @@ def iemi(self):
205197 """Returns FONA module's IEMI number."""
206198 if self ._debug :
207199 print ("FONA IEMI" )
208- self ._buf = b""
209200 self ._uart .reset_input_buffer ()
210201
211202 self .uart_write (b"AT+GSN\r \n " )
@@ -326,7 +317,6 @@ def set_gprs(self, apn=None, enable=True):
326317 if not self ._send_check_reply (b"AT+CIICR" , reply = REPLY_OK , timeout = 10000 ):
327318 return False
328319
329- # Query local IP
330320 if not self .local_ip :
331321 return False
332322 else :
@@ -345,26 +335,19 @@ def network_status(self):
345335 print ("Network status" )
346336 if not self ._send_parse_reply (b"AT+CREG?" , b"+CREG: " , idx = 1 ):
347337 return False
348- if self ._buf == 0 :
349- # Not Registered
338+ if self ._buf == 0 : # Not Registered
350339 return self ._buf
351- if self ._buf == 1 :
352- # Registered (home)
340+ if self ._buf == 1 : # Registered (home)
353341 return self ._buf
354- if self ._buf == 2 :
355- # Not Registered (searching)
342+ if self ._buf == 2 : # Not Registered (searching)
356343 return self ._buf
357- if self ._buf == 3 :
358- # Denied
344+ if self ._buf == 3 : # Denied
359345 return self ._buf
360- if self ._buf == 4 :
361- # Unknown
346+ if self ._buf == 4 : # Unknown
362347 return self ._buf
363- if self ._buf == 5 :
364- # Registered Roaming
348+ if self ._buf == 5 : # Registered Roaming
365349 return self ._buf
366- # "Unknown"
367- return self ._buf
350+ return False
368351
369352 @property
370353 def rssi (self ):
@@ -501,25 +484,25 @@ def send_sms(self, phone_number, message):
501484 :param str message: Message to send to the phone number.
502485
503486 """
504- if not hasattr (phone_number , ' to_bytes' ):
487+ if not hasattr (phone_number , " to_bytes" ):
505488 raise TypeError ("Phone number must be integer" )
506489
507490 # select SMS message format, text mode (4.2.2)
508491 if not self ._send_check_reply (b"AT+CMGF=1" , reply = REPLY_OK ):
509492 return False
510493
511- self .uart_write (b" AT+CMGS=\" +" + str (phone_number ).encode () + b" \" " + b"\r " )
494+ self .uart_write (b' AT+CMGS="+' + str (phone_number ).encode () + b'"' + b"\r " )
512495 self ._read_line ()
513496
514- # expect >
497+ # expect >
515498 if self ._buf [0 ] != 62 :
516499 # promoting mark ('>') not found
517500 return False
518501 self ._read_line ()
519502
520503 # write out message and ^z
521504 self .uart_write ((message + chr (26 )).encode ())
522-
505+
523506 if self ._fona_type == FONA_3G_A or self ._fona_type == FONA_3G_E :
524507 # eat 2x CRLF
525508 self ._read_line (200 )
@@ -529,7 +512,7 @@ def send_sms(self, phone_number, message):
529512
530513 if not "+CMGS" in self ._buf :
531514 return False
532-
515+
533516 # read OK
534517 if not self ._expect_reply (REPLY_OK ):
535518 return False
@@ -541,72 +524,64 @@ def num_sms(self, sim_storage=True):
541524
542525 :param bool sim_storage: SMS storage on the SIM, otherwise internal storage on FONA chip.
543526 """
544- # text mode
545527 if not self ._send_check_reply (b"AT+CMGF=1" , reply = REPLY_OK ):
546528 return False
547529
548- # ask how many SMS are stored
549- if sim_storage :
530+ if sim_storage : # ask how many SMS are stored
550531 if self ._send_parse_reply (b"AT+CPMS?" , FONA_SMS_STORAGE_SIM + b"," , idx = 1 ):
551532 return self ._buf
552533 else :
553- if self ._send_parse_reply (b"AT+CPMS?" , FONA_SMS_STORAGE_INTERNAL + b"," , idx = 1 ):
534+ if self ._send_parse_reply (
535+ b"AT+CPMS?" , FONA_SMS_STORAGE_INTERNAL + b"," , idx = 1
536+ ):
554537 return self ._buf
555-
556- self ._read_line () # eat OK
557- if self ._send_parse_reply (b"AT+CPMS?" , b" \" SM \" ," , idx = 1 ):
538+
539+ self ._read_line () # eat OK
540+ if self ._send_parse_reply (b"AT+CPMS?" , b'"SM",' , idx = 1 ):
558541 return self ._buf
559542
560- self ._read_line () # eat OK
561- if self ._send_parse_reply (b"AT+CPMS?" , b" \" SM_P\" ," , idx = 1 ):
543+ self ._read_line () # eat OK
544+ if self ._send_parse_reply (b"AT+CPMS?" , b'" SM_P",' , idx = 1 ):
562545 return self ._buf
563546 return False
564547
565548 def delete_all_sms (self ):
566549 """Deletes all SMS messages on the FONA SIM."""
567550 self ._read_line ()
568- # text mode
569551 if not self ._send_check_reply (b"AT+CMGF=1" , reply = REPLY_OK ):
570552 return False
571553
572- if not self ._send_check_reply (b"AT+CMGDA=\" DEL ALL\" " , reply = REPLY_OK , timeout = 25000 ):
554+ if not self ._send_check_reply (
555+ b'AT+CMGDA="DEL ALL"' , reply = REPLY_OK , timeout = 25000
556+ ):
573557 return False
574558 return True
575559
576560 def delete_sms (self , sms_slot ):
577561 """Deletes a SMS message in the provided SMS slot.
578- :param int sms_slot: SMS SIM or FONA memory slot number
562+ :param int sms_slot: SMS SIM or FONA memory slot number.
579563
580564 """
565+ self ._read_line ()
581566 if not self ._send_check_reply (b"AT+CMGF=1" , reply = REPLY_OK ):
582567 return False
583568
584- sendbuff = b"AT+CMGD=" ;
585- s2 = (sms_slot / 100 ) + 0
586-
587- sms_slot %= 100 ;
588- s3 = (sms_slot / 10 ) + 0
589-
590- sms_slot %= 10 ;
591- s4 = sms_slot + 0
592-
593- print (sendbuff , s2 , s3 , s4 )
594-
595- self ._send_check_reply (sendbuff , reply = REPLY_OK , timeout = 2000 )
569+ if not self ._send_check_reply (
570+ b"AT+CMGD=" + str (sms_slot ).encode (), reply = REPLY_OK
571+ ):
572+ return False
596573
574+ return True
597575
598576 def read_sms (self , sms_slot ):
599577 """Reads and parses SMS messages from FONA device. Returns the SMS
600578 sender's phone number and the message contents as a tuple.
601- :param int sms_slot: SMS SIM or FONA memory slot number
579+ :param int sms_slot: SMS SIM or FONA memory slot number.
602580
603581 """
604582 self ._read_line ()
605-
606- # text mode
607583 if not self ._send_check_reply (b"AT+CMGF=1" , reply = REPLY_OK ):
608584 return False
609-
610585 if not self ._send_check_reply (b"AT+CSDH=1" , reply = REPLY_OK ):
611586 return False
612587
@@ -625,16 +600,12 @@ def read_sms(self, sms_slot):
625600 return False
626601 sms_len = self ._buf
627602
628- # rsize shared buf
629603 self ._buf = bytearray (sms_len )
630- # read into buffer
631604 self ._uart .readinto (self ._buf )
632- # discard unread chars in buffer
633605 self ._uart .reset_input_buffer ()
634606
635607 return sender , bytes (self ._buf ).decode ()
636608
637-
638609 ### Socket API (TCP, UDP) ###
639610
640611 def get_socket (self ):
@@ -649,8 +620,7 @@ def get_socket(self):
649620 self ._read_line (100 ) # table header
650621
651622 allocated_socket = 0
652- for sock in range (0 , FONA_MAX_SOCKETS ):
653- # parse and check for INITIAL client state
623+ for sock in range (0 , FONA_MAX_SOCKETS ): # check if INITIAL state
654624 self ._read_line (100 )
655625 self ._parse_reply (b"C:" , idx = 5 )
656626 if self ._buf .strip ('"' ) == "INITIAL" or self ._buf .strip ('"' ) == "CLOSED" :
@@ -689,12 +659,9 @@ def socket_status(self, sock_num):
689659 sockets for the FONA module."
690660 if not self ._send_check_reply (b"AT+CIPSTATUS" , reply = REPLY_OK , timeout = 100 ):
691661 return False
692-
693- # eat the 'STATE: ' message
694662 self ._read_line ()
695663
696- # read "C: <n>" for each active connection
697- for state in range (0 , sock_num + 1 ):
664+ for state in range (0 , sock_num + 1 ): # read "C: <n>" for each active connection
698665 self ._read_line ()
699666 if state == sock_num :
700667 break
@@ -768,15 +735,12 @@ def socket_connect(self, sock_num, dest, port, conn_mode=TCP_MODE):
768735 self ._read_line ()
769736
770737 # Start connection
771- self .uart_write (b"AT+CIPSTART=" )
772- self .uart_write (str (sock_num ).encode ())
738+ self .uart_write (b"AT+CIPSTART=" + str (sock_num ).encode ())
773739 if conn_mode == 0 :
774740 self .uart_write (b',"TCP","' )
775741 else :
776742 self .uart_write (b',"UDP","' )
777- self .uart_write (dest .encode () + b'","' )
778- self .uart_write (str (port ).encode () + b'"' )
779- self .uart_write (b"\r \n " )
743+ self .uart_write (dest .encode () + b'","' + str (port ).encode () + b'"\r \n ' )
780744
781745 if not self ._expect_reply (REPLY_OK ):
782746 return False
@@ -851,8 +815,7 @@ def socket_write(self, sock_num, buffer):
851815
852816 self ._uart .reset_input_buffer ()
853817 self .uart_write (b"AT+CIPSEND=" + str (sock_num ).encode ())
854- self .uart_write (b"," + str (len (buffer )).encode ())
855- self .uart_write (b"\r \n " )
818+ self .uart_write (b"," + str (len (buffer )).encode () + b"\r \n " )
856819 self ._read_line ()
857820
858821 if self ._buf [0 ] != 62 :
@@ -907,16 +870,14 @@ def _get_reply(
907870 else :
908871 self .uart_write (prefix + suffix + b"\r \n " )
909872
910- line = self ._read_line (timeout )
911- return line
873+ return self ._read_line (timeout )
912874
913875 def _parse_reply (self , reply , divider = "," , idx = 0 ):
914876 """Attempts to find reply in UART buffer, reads up to divider.
915877 :param bytes reply: Expected response from FONA module.
916878 :param str divider: Divider character.
917879
918880 """
919- # attempt to find reply in buffer
920881 parsed_reply = self ._buf .find (reply )
921882 if parsed_reply == - 1 :
922883 return False
@@ -950,24 +911,18 @@ def _read_line(self, timeout=FONA_DEFAULT_TIMEOUT_MS, multiline=False):
950911
951912 while self ._uart .in_waiting :
952913 char = self ._uart .read (1 )
953- # print(char)
954914 if char == b"\r " :
955915 continue
956916 if char == b"\n " :
957- if reply_idx == 0 :
958- # ignore first '\n'
917+ if reply_idx == 0 : # ignore first '\n'
959918 continue
960- if not multiline :
961- # second '\n' is EOL
919+ if not multiline : # second '\n' is EOL
962920 timeout = 0
963921 break
964- # print(char, self._buf)
965922 self ._buf += char
966923 reply_idx += 1
967924
968925 if timeout == 0 :
969- # if self._debug:
970- # print("* Timed out!")
971926 break
972927 timeout -= 1
973928 time .sleep (0.001 )
@@ -997,7 +952,6 @@ def _send_check_reply(
997952 if not self ._get_reply (send , timeout = timeout ):
998953 return False
999954
1000- # validate response
1001955 if not self ._buf == reply :
1002956 return False
1003957
@@ -1017,7 +971,6 @@ def _send_check_reply_quoted(
1017971
1018972 self ._get_reply_quoted (prefix , suffix , timeout )
1019973
1020- # validate response
1021974 if reply not in self ._buf :
1022975 return False
1023976 return True
@@ -1032,11 +985,9 @@ def _get_reply_quoted(self, prefix, suffix, timeout):
1032985 """
1033986 self ._uart .reset_input_buffer ()
1034987
1035- self .uart_write (prefix + b'"' )
1036- self .uart_write (suffix + b'"\r \n ' )
988+ self .uart_write (prefix + b'"' + suffix + b'"\r \n ' )
1037989
1038- line = self ._read_line (timeout )
1039- return line
990+ return self ._read_line (timeout )
1040991
1041992 def _expect_reply (self , reply , timeout = 10000 ):
1042993 """Reads line from FONA module and compares to reply from FONA module.
0 commit comments