2828import time
2929from enum import Enum
3030from typing import Any , Callable
31+ import textwrap
3132
3233import serial
3334
3738
3839
3940# TODO In the serial init add AT+CGMM and throw an exception if you don't get a response or the correct response.
40- # TODO Need to include hardrired powerup/reset control
41+ # TODO Need to include hardwired powerup/reset control
4142
4243class LteState (Enum ):
4344 """LTE STate"""
@@ -109,6 +110,7 @@ class Sim767x:
109110
110111 start_time = time .time ()
111112
113+ _tx_lines = []
112114 _tx_message = ""
113115 _rx_message = ""
114116
@@ -382,14 +384,14 @@ def _process_at_commands(self):
382384 result_arr = result_str .split (',' )
383385 if len (result_arr ) == 1 : # Unsolicited response
384386 status = int (result_arr [0 ])
385- if status == 1 or status == 5 :
387+ if status == 1 or status == 5 or status == 6 :
386388 if self ._lte_state != LteState .LTE_CONNECTED :
387389 self ._lte_state = LteState .LTE_CONNECTED
388390 self ._disconnect_timer_s = 0
389391 self ._start_pdp_context ()
390392 else : # Request response
391393 status = int (result_arr [1 ])
392- if status == 1 or status == 5 :
394+ if status == 1 or status == 5 or status == 6 :
393395 if self ._lte_state != LteState .LTE_CONNECTED :
394396 self ._lte_state = LteState .LTE_CONNECTED
395397 self ._disconnect_timer_s = 0
@@ -452,16 +454,18 @@ def _process_at_commands(self):
452454 if error == 0 :
453455 if self ._pub_topic in self ._messages_dict :
454456 del self ._messages_dict [self ._pub_topic ]
457+ if self .mqtt_is_finished ():
458+ self ._pub_topic = ""
459+ self ._tx_message = ""
455460 else :
456461 logger .debug ("MQTT Pub: %s" , error )
457462 self ._req_mqtt_reconnect ()
463+ self ._pub_topic = ""
464+ self ._tx_message = ""
458465
459466 if self .on_mqtt_publish_callback is not None :
460467 self .on_mqtt_publish_callback (self ._pub_topic , self ._message_send_id , error ) # Do before topic is cleared below
461468 self ._message_send_id = - 1 # Probably don't need this
462-
463- self ._pub_topic = ""
464- self ._tx_message = ""
465469 elif data .startswith ("+CMQTTRXTOPIC:" ):
466470 # No need to do anything with the received topic as there shoud only be one topic.
467471 pass
@@ -618,7 +622,7 @@ def _set_unsolicited_network_reg_messages(self):
618622
619623 def _set_carrier (self ):
620624 if self ._network :
621- carrier_str = f'AT+COPS=4,2,"{ self ._network } "' # 1 = manual (4 = manual/auto), 2 = short format. For One NZ SIM cards not roaming in NZ, Could take up to 60s
625+ carrier_str = f'AT+COPS=4,2,"{ self ._network } \r \n "' # 1 = manual (4 = manual/auto), 2 = short format. For One NZ SIM cards not roaming in NZ, Could take up to 60s
622626 self .write_serial_buffer (carrier_str .encode ())
623627 # self._serial_at.write(carrier_str.encode()) # ??? Maybe should be protected
624628
@@ -700,6 +704,13 @@ def _mqtt_enter_sub_topic(self):
700704# ----- MQTT Publish ------
701705 def _mqtt_request_publish (self , topic : str , message : str ):
702706 self ._pub_topic = topic
707+
708+ if len (message ) > 380 :
709+ self ._tx_lines = textwrap .wrap (message , width = 380 , replace_whitespace = False , drop_whitespace = False , expand_tabs = False )
710+ self ._tx_message = self ._tx_lines .pop (0 )
711+ else :
712+ self ._tx_message = message
713+
703714 self ._tx_message = message
704715 if self ._tx_message :
705716 if self ._lte_state == LteState .MODULE_SHUTTING_DOWN :
@@ -723,6 +734,15 @@ def _mqtt_publish(self):
723734 if self ._protected_at_cmd ("CMQTTPUB=0,2,60" , self ._print_ok , None ): # clientIndex = 0
724735 self ._mqtt_is_publishing = True # Block further publishing until publish succeeds or fails.
725736
737+ def mqtt_is_finished (self ):
738+ if len (self ._tx_lines ) > 0 :
739+ self .tx_message = self ._tx_lines .pop (0 )
740+ # time.sleep(0.1) # ??? This is not good!!!
741+ self ._protected_at_cmd (f"CMQTTTOPIC=0,{ str (len (self ._pub_topic ))} " , lambda : self ._mqtt_request_payload (), lambda : self ._mqtt_enter_pub_topic ()) # clientIndex = 0
742+ return False
743+ else :
744+ return True
745+
726746# --------- GNSS ----------
727747 def gnss_start (self , interval : int , one_shot : bool ):
728748 """Start GNSS
0 commit comments