pulkin / micropython

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

Example on how to automatically connect to the APN #38

Closed sebi5361 closed 4 years ago

sebi5361 commented 4 years ago

Could you provide an example on how to automatically connect to the APN when booting? I implemented a loop but often it just won't connect forever.

pulkin commented 4 years ago

You may try something like:

import cellular
import time
timeout = time.ticks_ms() + 1e4
while time.ticks_ms() < timeout:
    try:
        cellular.gprs("apn", "user", "pass")
        break
    except CellularError:
        if time.ticks_ms() < timeout:
            time.sleep(1)
        else:
            raise
pulkin commented 4 years ago

I added a callback on_status_event(f) for your convenience. The function you input will be triggered each time the network status changed. You may invent something like

def f(status):
    if status == 1:
        cellular.on_status_event(None)
        cellular.gprs("internet", "", "")

cellular.on_status_event(f)

and it will hopefully work.

pulkin commented 4 years ago

MWE https://github.com/pulkin/micropython/blob/master/ports/gprs_a9/examples/example_42_event_driven_gprs.py

pulkin commented 4 years ago

Feel free to reopen.