41 lines
1.5 KiB
Python
41 lines
1.5 KiB
Python
#!/usr/bin/env python3
|
|
import time
|
|
import argparse
|
|
from m2m.nbiot import factory
|
|
from m2m.utils import encode_psm_timer
|
|
|
|
def industrial_sensor_cycle(port):
|
|
# Setup for ultra-low power
|
|
module = factory('BG95', port)
|
|
|
|
with module:
|
|
# 1. Wake up and check GNSS
|
|
module.enable_gnss(True)
|
|
location = module.get_gnss_location()
|
|
module.enable_gnss(False) # Turn off immediately to save mA
|
|
|
|
# 2. Attach and send payload
|
|
module.radio_on()
|
|
module.activate_pdp_context(1)
|
|
|
|
payload = f'{{"loc": "{location}", "batt": 3.8, "alert": false}}'
|
|
module.send_data_hex(0, payload.encode('ascii'))
|
|
|
|
# 3. Calculate optimized sleep
|
|
# T3324 (Active Time): 10 seconds (time to wait for incoming commands)
|
|
# T3412 (Periodic TAU): 12 hours (heartbeat interval)
|
|
t3324 = encode_psm_timer(10, is_t3324=True)
|
|
t3412 = encode_psm_timer(12 * 3600, is_t3324=False)
|
|
|
|
print(f"Entering deep sleep (PSM). T3324={t3324}, T3412={t3412}")
|
|
module.set_psm_settings(mode=1, tau=t3412, active_time=t3324)
|
|
|
|
# 4. Graceful Power Down
|
|
module.radio_off()
|
|
print("Device is now in hibernate. Power consumption < 5uA.")
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(description="Run power optimized sensor demo.")
|
|
parser.add_argument("port", nargs="?", default="/dev/ttyUSB0", help="Serial port (e.g. /dev/ttyUSB0 or MOCK)")
|
|
args = parser.parse_args()
|
|
industrial_sensor_cycle(args.port)
|