@@ -99,11 +99,8 @@ def __init__(self, uart, rst, debug=False):
9999 # pylint: disable=too-many-branches, too-many-statements
100100 def _init_fona (self ):
101101 """Initializes FONA module."""
102-
103102 self .reset ()
104103
105- if self ._debug :
106- print ("Attempting to open comm with ATs" )
107104 timeout = 7000
108105 while timeout > 0 :
109106 while self ._uart .in_waiting :
@@ -115,8 +112,6 @@ def _init_fona(self):
115112 timeout -= 500
116113
117114 if timeout <= 0 :
118- if self ._debug :
119- print (" * Timeout: No response to AT. Last ditch attempt." )
120115 self ._send_check_reply (CMD_AT , reply = REPLY_OK )
121116 time .sleep (0.1 )
122117 self ._send_check_reply (CMD_AT , reply = REPLY_OK )
@@ -139,9 +134,7 @@ def _init_fona(self):
139134 self ._buf = b""
140135 self ._uart .reset_input_buffer ()
141136
142- if self ._debug :
143- print ("\t ---> " , "ATI" )
144- self ._uart .write (b"ATI\r \n " )
137+ self .uart_write (b"ATI\r \n " )
145138 self ._read_line (multiline = True )
146139
147140 if self ._buf .find (b"SIM808 R14" ) != - 1 :
@@ -154,10 +147,8 @@ def _init_fona(self):
154147 self ._fona_type = FONA_3G_E
155148
156149 if self ._fona_type == FONA_800_L :
157- # determine if _H
158- if self ._debug :
159- print ("\t ---> AT+GMM" )
160- self ._uart .write (b"AT+GMM\r \n " )
150+ # determine if SIM800H
151+ self .uart_write (b"AT+GMM\r \n " )
161152 self ._read_line (multiline = True )
162153 if self ._debug :
163154 print ("\t <---" , self ._buf )
@@ -168,9 +159,7 @@ def _init_fona(self):
168159
169160 def factory_reset (self ):
170161 """Resets modem to factory configuration."""
171- if self ._debug :
172- print ("\t ---> ATZ" )
173- self ._uart .write (b"ATZ\r \n " )
162+ self .uart_write (b"ATZ\r \n " )
174163
175164 if not self ._expect_reply (REPLY_OK ):
176165 return False
@@ -182,7 +171,7 @@ def reset(self):
182171
183172 """
184173 if self ._debug :
185- print ("* Resetting modem. " )
174+ print ("* Reset FONA " )
186175 # Reset the module
187176 self ._rst .value = True
188177 time .sleep (0.01 )
@@ -211,23 +200,20 @@ def version(self):
211200 @property
212201 def iemi (self ):
213202 """Returns FONA module's IEMI number."""
203+ if self ._debug :
204+ print ("FONA IEMI" )
214205 self ._buf = b""
215206 self ._uart .reset_input_buffer ()
216207
217- if self ._debug :
218- print ("\t ---> " , "AT+GSN" )
219- self ._uart .write (b"AT+GSN\r \n " )
208+ self .uart_write (b"AT+GSN\r \n " )
220209 self ._read_line (multiline = True )
221210 iemi = self ._buf [0 :15 ]
222211 return iemi .decode ("utf-8" )
223212
224213 @property
225214 def local_ip (self ):
226215 """Returns the local IP Address, False if not set."""
227- if self ._debug :
228- print ("\t ---> AT+CIFSR" )
229-
230- self ._uart .write (b"AT+CIFSR\r \n " )
216+ self .uart_write (b"AT+CIFSR\r \n " )
231217 self ._read_line ()
232218 try :
233219 ip_addr = self .pretty_ip (self ._buf )
@@ -239,8 +225,8 @@ def local_ip(self):
239225 def iccid (self ):
240226 """Returns SIM Card's ICCID number as a string."""
241227 if self ._debug :
242- print ("\t ---> AT+CCID: " )
243- self ._uart . write (b"AT+CCID\r \n " )
228+ print ("ICCID " )
229+ self .uart_write (b"AT+CCID\r \n " )
244230 self ._read_line (timeout = 2000 ) # 6.2.23, 2sec max. response time
245231 iccid = self ._buf .decode ()
246232 self ._read_line () # eat 'OK'
@@ -250,8 +236,6 @@ def iccid(self):
250236 def gprs (self ):
251237 """Returns module's GPRS state."""
252238 self ._read_line ()
253- if self ._debug :
254- print ("* Check GPRS State" )
255239
256240 if not self ._send_parse_reply (b"AT+CGATT?" , b"+CGATT: " , ":" ):
257241 return False
@@ -266,9 +250,6 @@ def set_gprs(self, apn=None, enable=True):
266250
267251 """
268252 if enable :
269- if self ._debug :
270- print ("* Enabling GPRS..." )
271-
272253 apn_name , apn_user , apn_pass = apn
273254
274255 apn_name = apn_name .encode ()
@@ -310,14 +291,14 @@ def set_gprs(self, apn=None, enable=True):
310291 print ("setting APN..." )
311292 self ._uart .reset_input_buffer ()
312293
313- self ._uart . write (b'AT+CSTT="' + apn_name )
294+ self .uart_write (b'AT+CSTT="' + apn_name )
314295
315296 if apn_user is not None :
316- self ._uart . write (b'","' + apn_user )
297+ self .uart_write (b'","' + apn_user )
317298
318299 if apn_pass is not None :
319- self ._uart . write (b'","' + apn_pass )
320- self ._uart . write (b'"\r \n ' )
300+ self .uart_write (b'","' + apn_pass )
301+ self .uart_write (b'"\r \n ' )
321302
322303 if not self ._get_reply (REPLY_OK ):
323304 return False
@@ -347,10 +328,7 @@ def set_gprs(self, apn=None, enable=True):
347328 # Query local IP
348329 if not self .local_ip :
349330 return False
350-
351331 else :
352- if self ._debug :
353- print ("* Disabling GPRS..." )
354332 # reset PDP state
355333 if not self ._send_check_reply (
356334 b"AT+CIPSHUT" , reply = b"SHUT OK" , timeout = 20000
@@ -362,6 +340,8 @@ def set_gprs(self, apn=None, enable=True):
362340 @property
363341 def network_status (self ):
364342 """Returns cellular/network status"""
343+ if self ._debug :
344+ print ("Network status" )
365345 if not self ._send_parse_reply (b"AT+CREG?" , b"+CREG: " , idx = 1 ):
366346 return False
367347 if self ._buf == 0 :
@@ -388,6 +368,8 @@ def network_status(self):
388368 @property
389369 def rssi (self ):
390370 """Returns cellular network's Received Signal Strength Indicator (RSSI)."""
371+ if self ._debug :
372+ print ("RSSI" )
391373 if not self ._send_parse_reply (b"AT+CSQ" , b"+CSQ: " ):
392374 return False
393375
@@ -409,9 +391,9 @@ def rssi(self):
409391
410392 @property
411393 def gps (self ):
412- """Returns the GPS status ."""
394+ """Returns the GPS fix ."""
413395 if self ._debug :
414- print ("* GPS Status " )
396+ print ("GPS Fix " )
415397 if self ._fona_type == FONA_808_V2 :
416398 # 808 V2 uses GNS commands and doesn't have an explicit 2D/3D fix status.
417399 # Instead just look for a fix and if found assume it's a 3D fix.
@@ -483,14 +465,12 @@ def get_host_by_name(self, hostname):
483465 :param str hostname: Destination server.
484466
485467 """
486- self ._read_line ()
487468 if self ._debug :
488- print ("*** get_host_by_name: " , hostname )
469+ print ("*** Get host by name" )
470+ self ._read_line ()
489471 if isinstance (hostname , str ):
490472 hostname = bytes (hostname , "utf-8" )
491473
492- if self ._debug :
493- print ("\t ---> AT+CDNSGIP=" , hostname )
494474 if not self ._send_check_reply (
495475 b'AT+CDNSGIP="' + hostname + b'"\r \n ' , reply = REPLY_OK
496476 ):
@@ -521,9 +501,9 @@ def get_socket(self):
521501
522502 """
523503 if self ._debug :
524- print ("*** Allocating Socket " )
504+ print ("*** Get socket " )
525505
526- self ._uart . write (b"AT+CIPSTATUS\r \n " )
506+ self .uart_write (b"AT+CIPSTATUS\r \n " )
527507 self ._read_line (100 ) # OK
528508 self ._read_line (100 ) # table header
529509
@@ -538,6 +518,8 @@ def get_socket(self):
538518 # read out the rest of the responses
539519 for _ in range (allocated_socket , FONA_MAX_SOCKETS ):
540520 self ._read_line (100 )
521+ if self ._debug :
522+ print ("Allocated socket #%d" % allocated_socket )
541523 return allocated_socket
542524
543525 def remote_ip (self , sock_num ):
@@ -549,7 +531,7 @@ def remote_ip(self, sock_num):
549531 sock_num < FONA_MAX_SOCKETS
550532 ), "Provided socket exceeds the maximum number of \
551533 sockets for the FONA module."
552- self ._uart . write (b"AT+CIPSTATUS=" + str (sock_num ).encode () + b"\r \n " )
534+ self .uart_write (b"AT+CIPSTATUS=" + str (sock_num ).encode () + b"\r \n " )
553535 self ._read_line (100 )
554536
555537 self ._parse_reply (b"+CIPSTATUS:" , idx = 3 )
@@ -624,6 +606,13 @@ def socket_connect(self, sock_num, dest, port, conn_mode=TCP_MODE):
624606 :param int conn_mode: Connection mode (TCP/UDP)
625607
626608 """
609+ if self ._debug :
610+ print (
611+ "*** Socket connect, protocol={}, port={}, ip={}" .format (
612+ conn_mode , port , dest
613+ )
614+ )
615+
627616 self ._uart .reset_input_buffer ()
628617 assert (
629618 sock_num < FONA_MAX_SOCKETS
@@ -638,25 +627,19 @@ def socket_connect(self, sock_num, dest, port, conn_mode=TCP_MODE):
638627 )
639628
640629 # Query local IP Address
641- if self ._debug :
642- print ("\t ---> AT+CIFSR" )
643- self ._uart .write (b"AT+CIFSR\r \n " )
630+ self .uart_write (b"AT+CIFSR\r \n " )
644631 self ._read_line ()
645632
646633 # Start connection
647- self ._uart . write (b"AT+CIPSTART=" )
648- self ._uart . write (str (sock_num ).encode ())
634+ self .uart_write (b"AT+CIPSTART=" )
635+ self .uart_write (str (sock_num ).encode ())
649636 if conn_mode == 0 :
650- if self ._debug :
651- print ('\t --->AT+CIPSTART="TCP","{}",{}' .format (dest , port ))
652- self ._uart .write (b',"TCP","' )
637+ self .uart_write (b',"TCP","' )
653638 else :
654- if self ._debug :
655- print ('\t --->AT+CIPSTART="UDP","{}",{}' .format (dest , port ))
656- self ._uart .write (b',"UDP","' )
657- self ._uart .write (dest .encode () + b'","' )
658- self ._uart .write (str (port ).encode () + b'"' )
659- self ._uart .write (b"\r \n " )
639+ self .uart_write (b',"UDP","' )
640+ self .uart_write (dest .encode () + b'","' )
641+ self .uart_write (str (port ).encode () + b'"' )
642+ self .uart_write (b"\r \n " )
660643
661644 if not self ._expect_reply (REPLY_OK ):
662645 return False
@@ -672,14 +655,16 @@ def socket_close(self, sock_num, quick_close=1):
672655 :param int quick_close: Quickly or slowly close the socket. Enabled by default
673656
674657 """
658+ if self ._debug :
659+ print ("*** Closing socket #%d" % sock_num )
675660 assert (
676661 sock_num < FONA_MAX_SOCKETS
677662 ), "Provided socket exceeds the maximum number of \
678663 sockets for the FONA module."
679664 self ._read_line ()
680665
681- self ._uart . write (b"AT+CIPCLOSE=" + str (sock_num ).encode () + b"," )
682- self ._uart . write (str (quick_close ).encode () + b"\r \n " )
666+ self .uart_write (b"AT+CIPCLOSE=" + str (sock_num ).encode () + b"," )
667+ self .uart_write (str (quick_close ).encode () + b"\r \n " )
683668 self ._read_line ()
684669 if not self ._parse_reply (b"CLOSE OK" , idx = 0 ):
685670 return False
@@ -699,11 +684,11 @@ def socket_read(self, sock_num, length):
699684 self ._read_line ()
700685 if self ._debug :
701686 print ("* socket read" )
702- print ( " \t ---> AT+CIPRXGET=2,{},{}" . format ( sock_num , length ))
703- self ._uart . write (b"AT+CIPRXGET=2," )
704- self ._uart . write (str (sock_num ).encode ())
705- self ._uart . write (b"," )
706- self ._uart . write (str (length ).encode () + b"\r \n " )
687+
688+ self .uart_write (b"AT+CIPRXGET=2," )
689+ self .uart_write (str (sock_num ).encode ())
690+ self .uart_write (b"," )
691+ self .uart_write (str (length ).encode () + b"\r \n " )
707692
708693 self ._read_line ()
709694
@@ -727,13 +712,10 @@ def socket_write(self, sock_num, buffer):
727712 ), "Provided socket exceeds the maximum number of \
728713 sockets for the FONA module."
729714
730- if self ._debug :
731- print ("\t --->AT+CIPSEND={},{}" .format (sock_num , len (buffer )))
732-
733715 self ._uart .reset_input_buffer ()
734- self ._uart . write (b"AT+CIPSEND=" + str (sock_num ).encode ())
735- self ._uart . write (b"," + str (len (buffer )).encode ())
736- self ._uart . write (b"\r \n " )
716+ self .uart_write (b"AT+CIPSEND=" + str (sock_num ).encode ())
717+ self .uart_write (b"," + str (len (buffer )).encode ())
718+ self .uart_write (b"\r \n " )
737719 self ._read_line ()
738720
739721 if self ._debug :
@@ -743,7 +725,7 @@ def socket_write(self, sock_num, buffer):
743725 # promoting mark ('>') not found
744726 return False
745727
746- self ._uart . write (buffer + b"\r \n " )
728+ self .uart_write (buffer + b"\r \n " )
747729 self ._read_line (3000 )
748730
749731 if self ._debug :
@@ -756,6 +738,21 @@ def socket_write(self, sock_num, buffer):
756738
757739 ### UART Reply/Response Helpers ###
758740
741+ def uart_write (self , buffer ):
742+ """UART ``write`` with optional debug that prints
743+ the buffer before sending.
744+ :param bytes buffer: Buffer of bytes to send to the bus.
745+
746+ """
747+ #out_buffer_str = ", ".join([hex(i) for i in buffer])
748+ if self ._debug :
749+ print ("\t UARTWRITE ::" , buffer .decode ())
750+ self ._uart .write (buffer )
751+
752+ def uart_read (self ):
753+ # TODO!
754+ pass
755+
759756 def _send_parse_reply (self , send_data , reply_data , divider = "," , idx = 0 ):
760757 """Sends data to FONA module, parses reply data returned.
761758 :param bytes send_data: Data to send to the module.
@@ -780,13 +777,9 @@ def _get_reply(
780777 self ._uart .reset_input_buffer ()
781778
782779 if data is not None :
783- if self ._debug :
784- print ("\t ---> " , data )
785- self ._uart .write (data + "\r \n " )
780+ self .uart_write (data + b"\r \n " )
786781 else :
787- if self ._debug :
788- print ("\t ---> {}{}" .format (prefix , suffix ))
789- self ._uart .write (prefix + suffix + b"\r \n " )
782+ self .uart_write (prefix + suffix + b"\r \n " )
790783
791784 line = self ._read_line (timeout )
792785
@@ -912,18 +905,10 @@ def _get_reply_quoted(self, prefix, suffix, timeout):
912905 """
913906 self ._uart .reset_input_buffer ()
914907
915- if self ._debug :
916- print ("\t ---> " , end = "" )
917- print (prefix , end = "" )
918- print ('""' , end = "" )
919- print (suffix , end = "" )
920- print ('""' )
921-
922- self ._uart .write (prefix + b'"' )
923- self ._uart .write (suffix + b'"\r \n ' )
908+ self .uart_write (prefix + b'"' )
909+ self .uart_write (suffix + b'"\r \n ' )
924910
925911 line = self ._read_line (timeout )
926-
927912 if self ._debug :
928913 print ("\t <--- " , self ._buf )
929914
0 commit comments