Skip to content

Commit 8b43df9

Browse files
Merge pull request #107 from sixfab/dev
release: 0.4.0
2 parents 5b9532a + 2e614e3 commit 8b43df9

File tree

4 files changed

+123
-7
lines changed

4 files changed

+123
-7
lines changed

examples/mqtt/publish.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
Example code for publishing data to an MQTT broker.
3+
4+
Example Configuration
5+
---------------------
6+
Create a config.json file in the root directory of the PicoLTE device.
7+
config.json file must include the following parameters for this example:
8+
9+
config.json
10+
{
11+
"mqtts":{
12+
"host":"[HOST_ADDRESS]",
13+
"port": [PORT_NUMBER],
14+
"pub_qos": [QoS],
15+
"client_id": "[CLIENT_ID]",
16+
"username":"[MQTT_USERNAME]",
17+
"password":"[MQTT_PASSWORD]"
18+
},
19+
}
20+
21+
- [HOST_ADDRESS] should be an IP address or a domain name (without "mqtt://").
22+
- [QoS] is the quality of service level for the message and can be 0, 1 or 2. Default value is 1.
23+
- "client_id", "username" and "password" are optional. If your MQTT broker does not require authentication, you can skip these parameters.
24+
"""
25+
26+
from pico_lte.utils.status import Status
27+
from pico_lte.core import PicoLTE
28+
from pico_lte.common import debug
29+
30+
picoLTE = PicoLTE()
31+
32+
picoLTE.network.register_network()
33+
picoLTE.network.get_pdp_ready()
34+
picoLTE.mqtt.open_connection()
35+
picoLTE.mqtt.connect_broker()
36+
37+
debug.info("Publishing a message.")
38+
39+
# PAYLOAD and TOPIC have to be in string format.
40+
PAYLOAD = "[PAYLOAD_MESSAGE]"
41+
TOPIC = "[TOPIC_NAME]"
42+
43+
# Publish the message to the topic.
44+
result = picoLTE.mqtt.publish_message(PAYLOAD, TOPIC)
45+
debug.info("Result:", result)
46+
47+
if result["status"] == Status.SUCCESS:
48+
debug.info("Publish succeeded.")

examples/mqtt/subscribe.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
Example code for subcribing to topic(s) of an MQTT broker.
3+
4+
Example Configuration
5+
---------------------
6+
Create a config.json file in the root directory of the PicoLTE device.
7+
config.json file must include the following parameters for this example:
8+
9+
config.json
10+
{
11+
"mqtts":{
12+
"host":"[HOST_ADDRESS]",
13+
"port": [PORT_NUMBER],
14+
"client_id": "[CLIENT_ID]",
15+
"username":"[MQTT_USERNAME]",
16+
"password":"[MQTT_PASSWORD]",
17+
"sub_topics": [
18+
["[YOUR_MQTT_TOPIC_1]", [QoS]],
19+
["[YOUR_MQTT_TOPIC_2]", [QoS]],
20+
...
21+
]
22+
},
23+
}
24+
25+
- [HOST_ADDRESS] could be an IP address or a domain name (without "mqtt://").
26+
- "client_id", "username" and "password" are optional. If your MQTT broker does not require authentication, you can skip these parameters.
27+
- [QoS] is the quality of service level for the message and can be 0, 1 or 2.
28+
"""
29+
30+
import time
31+
from pico_lte.utils.status import Status
32+
from pico_lte.core import PicoLTE
33+
from pico_lte.common import debug
34+
35+
picoLTE = PicoLTE()
36+
37+
picoLTE.network.register_network()
38+
picoLTE.network.get_pdp_ready()
39+
picoLTE.mqtt.open_connection()
40+
picoLTE.mqtt.connect_broker()
41+
42+
debug.info("Subscribing to topics...")
43+
result = picoLTE.mqtt.subscribe_topics()
44+
debug.info("Result:", result)
45+
46+
if result["status"] == Status.SUCCESS:
47+
# Check is there any data in subscribed topics in each 5 seconds for 5 times
48+
debug.info("Reading messages from subscribed topics...")
49+
for _ in range(0, 5):
50+
result = picoLTE.mqtt.read_messages()
51+
debug.info(result["messages"])
52+
time.sleep(5)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"urls": [],
33
"deps": [],
4-
"version": "0.3.0",
4+
"version": "0.4.0",
55
"license": "MIT"
66
}

pico_lte/modules/mqtt.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,9 @@ def open_connection(self, host=None, port=None, cid=0):
257257
]
258258

259259
if result["status"] == Status.SUCCESS:
260-
result = self.atcom.get_urc_response(desired_response, fault_responses, timeout=60)
260+
result = self.atcom.get_urc_response(
261+
desired_response, fault_responses, timeout=60
262+
)
261263
return result
262264
return {"status": Status.ERROR, "response": "Missing parameters : host"}
263265

@@ -301,15 +303,17 @@ def close_connection(self, cid=0):
301303
result = self.atcom.get_urc_response(desired_response, timeout=60)
302304
return result
303305

304-
def connect_broker(self, client_id_string="PicoLTE", username=None, password=None, cid=0):
306+
def connect_broker(
307+
self, client_id_string=None, username=None, password=None, cid=0
308+
):
305309
"""
306310
Function for connecting to MQTT broker. This function is used when a client requests a
307311
connection to the MQTT server. When a TCP/IP socket connection is established between
308312
a client and a server, a protocol level session must be created using a CONNECT flow.
309313
310314
Parameters
311315
----------
312-
client_id_string : str, default: "PicoLTE"
316+
client_id_string : str, default: None
313317
Client ID string. Maximum length: 23 bytes.
314318
username : str, default: None
315319
Username. Maximum length: 23 bytes.
@@ -327,6 +331,9 @@ def connect_broker(self, client_id_string="PicoLTE", username=None, password=Non
327331
username = get_parameter(["mqtts", "username"])
328332
password = get_parameter(["mqtts", "password"])
329333

334+
if client_id_string is None:
335+
client_id_string = get_parameter(["mqtts", "client_id"], "PicoLTE")
336+
330337
if username and password:
331338
command = f'AT+QMTCONN={cid},"{client_id_string}","{username}","{password}"'
332339
else:
@@ -337,7 +344,9 @@ def connect_broker(self, client_id_string="PicoLTE", username=None, password=Non
337344
if result["status"] == Status.SUCCESS:
338345
desired_response = f"+QMTCONN: {cid},0,0"
339346
fault_responses = [f"QMTSTAT: 0,{err_code}" for err_code in range(1, 8)]
340-
result = self.atcom.get_urc_response(desired_response, fault_responses, timeout=60)
347+
result = self.atcom.get_urc_response(
348+
desired_response, fault_responses, timeout=60
349+
)
341350
return result
342351

343352
def is_connected_to_broker(self, cid=0):
@@ -436,7 +445,9 @@ def unsubscribe_topic(self, topic, cid=0, message_id=1):
436445
command = f'AT+QMTUNS={cid},{message_id},"{topic}"'
437446
return self.atcom.send_at_comm(command)
438447

439-
def publish_message(self, payload, topic=None, qos=1, retain=0, message_id=1, cid=0):
448+
def publish_message(
449+
self, payload, topic=None, qos=None, retain=0, message_id=1, cid=0
450+
):
440451
"""
441452
Function for publishing MQTT message. This function is used when a client requests
442453
a message to be published. This method uses data mode of the modem to send the message.
@@ -473,13 +484,18 @@ def publish_message(self, payload, topic=None, qos=1, retain=0, message_id=1, ci
473484
if topic is None:
474485
topic = get_parameter(["mqtts", "pub_topic"])
475486

487+
if qos is None:
488+
qos = get_parameter(["mqtts", "pub_qos"], 1)
489+
476490
if payload and topic:
477491
command = f'AT+QMTPUB={cid},{message_id},{qos},{retain},"{topic}"'
478492
result = self.atcom.send_at_comm(command, ">", urc=True)
479493

480494
if result["status"] == Status.SUCCESS:
481495
self.atcom.send_at_comm_once(payload, line_end=False) # Send message
482-
result = self.atcom.send_at_comm(self.CTRL_Z) # Send end char --> CTRL+Z
496+
result = self.atcom.send_at_comm(
497+
self.CTRL_Z
498+
) # Send end char --> CTRL+Z
483499
return result
484500
return {"response": "Missing parameter", "status": Status.ERROR}
485501

0 commit comments

Comments
 (0)