@@ -1448,7 +1448,6 @@ def open_mqtt_connection(self, cid=0, host="", port=8883):
14481448
14491449 if result ["status" ] == Status .SUCCESS :
14501450 result = self .atcom .get_response (desired_response , timeout = 60 )
1451- return result
14521451 return result
14531452
14541453 def close_mqtt_connection (self , cid = 0 ):
@@ -1477,7 +1476,6 @@ def close_mqtt_connection(self, cid=0):
14771476 if result ["status" ] == Status .SUCCESS :
14781477 desired_response = f"+QMTCLOSE: { cid } ,0"
14791478 result = self .atcom .get_response (desired_response , timeout = 60 )
1480- return result
14811479 return result
14821480
14831481 def connect_mqtt_broker (self , cid = 0 , client_id_string = "picocell" , username = None , password = None ):
@@ -1528,7 +1526,6 @@ def connect_mqtt_broker(self, cid=0, client_id_string="picocell", username=None,
15281526 if result ["status" ] == Status .SUCCESS :
15291527 desired_response = f"+QMTCONN: { cid } ,0,0"
15301528 result = self .atcom .get_response (desired_response , timeout = 60 )
1531- return result
15321529 return result
15331530
15341531 def disconnect_mqtt_broker (self , cid = 0 ):
@@ -1604,7 +1601,6 @@ def subscribe_mqtt_topic(self, cid=0, message_id=1, topic="", qos=0):
16041601 if result ["status" ] == Status .SUCCESS :
16051602 desired_response = f"+QMTSUB: { cid } ,{ message_id } ,0"
16061603 result = self .atcom .get_response (desired_response , timeout = 60 )
1607- return result
16081604 return result
16091605
16101606 def unsubscribe_mqtt_topic (self , cid = 0 , message_id = 1 , topic = "" ):
@@ -1687,16 +1683,15 @@ def publish_mqtt_message(self, cid=0, message_id=1, qos=1, retain=0, topic="", p
16871683 2 --> Failed to send a packet
16881684 value : int
16891685 If <result> is 1, number of times a packet has been retransmitted.
1690- If <result> is 0 or 2, it will not be presente
1686+ If <result> is 0 or 2, it will not be presented
16911687 """
16921688 command = f'AT+QMTPUB={ cid } ,{ message_id } ,{ qos } ,{ retain } ,"{ topic } "'
16931689 result = self .atcom .send_at_comm (command ,">" )
16941690
16951691 if result ["status" ] == Status .SUCCESS :
1696- self .atcom .send_at_comm_once (payload ,"OK" ) # Send message
1697- return self .atcom .send_at_comm (self .CTRL_Z ,"OK" ) # Send end char --> CTRL+Z
1698- else :
1699- return result
1692+ self .atcom .send_at_comm_once (payload , line_end = False ) # Send message
1693+ result = self .atcom .send_at_comm (self .CTRL_Z ,"OK" ) # Send end char --> CTRL+Z
1694+ return result
17001695
17011696 def check_any_mqtt_messages (self , cid = 0 ):
17021697 """
@@ -1985,6 +1980,17 @@ def publish_message_to_aws(self, payload, host=None, port=None, topic=None):
19851980 """
19861981 Function for publishing a message to AWS IoT by using MQTT.
19871982
1983+ Parameters
1984+ ----------
1985+ payload : str
1986+ Payload of the message.
1987+ host : str
1988+ Host of the MQTT broker.
1989+ port : int
1990+ Port of the MQTT broker.
1991+ topic : str
1992+ Topic of the message.
1993+
19881994 Returns
19891995 -------
19901996 (status, modem_response) : tuple
@@ -2091,3 +2097,111 @@ def publish_message_to_aws(self, payload, host=None, port=None, topic=None):
20912097 elif result ["status" ] == Status .ERROR :
20922098 return result
20932099 time .sleep (result ["interval" ])
2100+
2101+ def publish_message_to_aws_https (self , payload , url = None ):
2102+ """
2103+ Function for publishing a message to AWS IoT by using HTTPS.
2104+
2105+ Parameters
2106+ ----------
2107+ payload : str
2108+ Payload of the message.
2109+ url : str
2110+ URL of the AWS device shadow
2111+
2112+ Returns
2113+ -------
2114+ (status, modem_response) : tuple
2115+ status : int
2116+ Status of the command.
2117+ modem_response : str
2118+ Response of the modem.
2119+ """
2120+
2121+ step_network_reg = Step (
2122+ function = self .register_network ,
2123+ name = "register_network" ,
2124+ success = "pdp_deactivate" ,
2125+ fail = "failure" ,
2126+ )
2127+
2128+ step_pdp_deactivate = Step (
2129+ function = self .deactivate_pdp_context ,
2130+ name = "pdp_deactivate" ,
2131+ success = "pdp_activate" ,
2132+ fail = "failure" ,
2133+ )
2134+
2135+ step_pdp_activate = Step (
2136+ function = self .activate_pdp_context ,
2137+ name = "pdp_activate" ,
2138+ success = "ssl_configuration" ,
2139+ fail = "failure" ,
2140+ cachable = True ,
2141+ )
2142+
2143+ step_ssl_configuration = Step (
2144+ function = self .configure_modem_ssl_for_x509_certification ,
2145+ name = "ssl_configuration" ,
2146+ success = "http_ssl_configuration" ,
2147+ fail = "failure" ,
2148+ cachable = True ,
2149+ )
2150+
2151+ step_http_ssl_configuration = Step (
2152+ function = self .set_modem_http_ssl_context_id ,
2153+ name = "http_ssl_configuration" ,
2154+ success = "set_server_url" ,
2155+ fail = "failure" ,
2156+ function_params = {"id" : 2 },
2157+ cachable = True ,
2158+ )
2159+
2160+ step_set_server_url = Step (
2161+ function = self .set_modem_http_server_url ,
2162+ name = "set_server_url" ,
2163+ success = "post_request" ,
2164+ fail = "failure" ,
2165+ function_params = {"url" : url },
2166+ cachable = True ,
2167+ )
2168+
2169+ step_post_request = Step (
2170+ function = self .http_post_request ,
2171+ name = "post_request" ,
2172+ success = "read_response" ,
2173+ fail = "failure" ,
2174+ function_params = {"data" : payload },
2175+ cachable = True ,
2176+ )
2177+
2178+ step_read_response = Step (
2179+ function = self .http_read_response ,
2180+ name = "read_response" ,
2181+ success = "success" ,
2182+ fail = "failure" ,
2183+ )
2184+
2185+ # Add cache if it is not already existed
2186+ function_name = "publish_message_to_aws_https"
2187+
2188+ sm = StateManager (first_step = step_network_reg ,
2189+ cache = self .cache , function_name = function_name )
2190+
2191+ sm .add_step (step_network_reg )
2192+ sm .add_step (step_pdp_deactivate )
2193+ sm .add_step (step_pdp_activate )
2194+ sm .add_step (step_ssl_configuration )
2195+ sm .add_step (step_http_ssl_configuration )
2196+ sm .add_step (step_set_server_url )
2197+ sm .add_step (step_post_request )
2198+ sm .add_step (step_read_response )
2199+
2200+ while True :
2201+ result = sm .run ()
2202+
2203+ if result ["status" ] == Status .SUCCESS :
2204+ return result
2205+ elif result ["status" ] == Status .ERROR :
2206+ return result
2207+ time .sleep (result ["interval" ])
0 commit comments