pulkin / micropython

MicroPython implementation on Ai-Thinker GPRS module A9 (RDA8955)
https://micropython.org
MIT License
102 stars 30 forks source link

MPY Example Code #26

Closed sebi5361 closed 4 years ago

sebi5361 commented 4 years ago

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?

pulkin commented 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.

pulkin commented 4 years ago

Btw, please share examples you'd like to see. I added some here: https://github.com/pulkin/micropython/tree/master/ports/gprs_a9/examples

sebi5361 commented 4 years ago

Very nice. Thank you!

sebi5361 commented 4 years ago

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.

pulkin commented 4 years ago

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.

sebi5361 commented 4 years ago

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!

pulkin commented 4 years ago

Please look here for available options:

https://github.com/micropython/micropython-lib/blob/master/umqtt.simple/umqtt/simple.py

sebi5361 commented 4 years ago

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!