Closed sebi5361 closed 4 years ago
Yep, some examples needed. Here you go with GPS over MQTT:
# Get online
import cellular
cellular.gprs("internet", "", "")
# Import mqtt (download client if necessary)
try:
from umqtt import simple
except ImportError:
import upip
upip.install("micropython-umqtt.simple")
from umqtt import simple
# Turn GPS on
import gps
gps.on()
# Report location
name = "a9g-micropython-board"
server = "test.mosquitto.org"
topic = "a9g-micropython-board-topic"
print("To track messages run, for example\n mosquitto_sub -h {server} -t \"{topic}\" -v".format(server=server, topic=topic))
import json
import time
client = simple.MQTTClient(name, server)
client.connect()
while True:
data = json.dumps(gps.get_last_location())
print("Publish", data)
client.publish(topic, data)
time.sleep(5)
Please note that I am going to swap longitude and latitude output in the nearest future.
Btw, please share examples you'd like to see. I added some here: https://github.com/pulkin/micropython/tree/master/ports/gprs_a9/examples
Very nice. Thank you!
Your examples are neat! Could you add an example on how to connect to a MQTT server using credentials and ssl/tls? Can't wait to receive my board.
Try replacing
client = simple.MQTTClient(name, server)
with
client = simple.MQTTClient(name, server, user="user", password="pass", ssl=True)
Please note that ssl
is much slower (takes around 5s for a handshake). In addition, I did not test certificates (without supplying certificates your cell provider will be able to perform MITM). Maybe I need to merge upstream to make it more secure.
In addition, I did not test certificates (without supplying certificates your cell provider will be able to perform MITM).
Is the process as simple to use certificates? Something like:
...MQTTClient(name, server, user="user", password="pass", ssl=True, certificate="certificate")
Maybe I need to merge upstream to make it more secure.
I have noticed that you forked your project from Neutree/micropython rather than micropython/micropython so it might not be that trivial, but indeed merging upstream would be a must!
Please look here for available options:
https://github.com/micropython/micropython-lib/blob/master/umqtt.simple/umqtt/simple.py
Based on your link and learning more about the ussl library, it seems that whether certificates work or not depends on the port. I don't know on which port is based the A9G port, but according to this thread, it seems that, if it is based on the ESP8266 port, it should work with a recent build. That's a good news!
Thanks for your great work!
Do you have any Micropython example code to share to simplify my understanding of your different APIs?
I would like to transmit regularly some GPS (together with some AGPS) coordinates to a MQTT broker using SSL/TLS and the cellular connection.
Maybe you already wrote such piece of software?