42 lines
1.5 KiB
Python
42 lines
1.5 KiB
Python
#!/usr/bin/env python3
|
|
import logging
|
|
import argparse
|
|
from m2m.nbiot import factory
|
|
|
|
def recover_connection(port):
|
|
module = factory('BG96', port)
|
|
|
|
with module:
|
|
print("Attempting to connect...")
|
|
module.radio_on()
|
|
module.attach_network()
|
|
|
|
status = module.read_cereg_status()
|
|
if status not in [1, 5]: # Not Home or Roaming
|
|
print("Registration failed. Starting diagnostics...")
|
|
|
|
# 1. Check Extended Error
|
|
error = module.get_extended_error_report()
|
|
print(f"3GPP Error: {error}")
|
|
|
|
# 2. Check for Forbidden Networks
|
|
fplmns = module.read_forbidden_plmns()
|
|
print(f"Forbidden Networks: {fplmns}")
|
|
|
|
# 3. Optimization: Force NB-IoT priority and re-scan
|
|
print("Switching strategy: Prioritizing NB-IoT and clearing scan sequence...")
|
|
module.set_iot_op_mode("NB-IoT")
|
|
module.set_nw_scan_priority("NB-IoT", "eMTC", "GSM")
|
|
|
|
# 4. Deep Reboot to apply logic
|
|
module.reboot()
|
|
|
|
# Retry
|
|
module.attach_network()
|
|
print(f"New Status: {module.read_cereg_status()}")
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(description="Run network recovery demo.")
|
|
parser.add_argument("port", nargs="?", default="/dev/ttyUSB0", help="Serial port (e.g. /dev/ttyUSB0 or MOCK)")
|
|
args = parser.parse_args()
|
|
recover_connection(args.port)
|