Created
December 18, 2021 16:22
-
-
Save halka/50c11b714fd9f78dd63220526c3d5b5f to your computer and use it in GitHub Desktop.
Location(GNSS) by CAT-M Module(U128)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import machine | |
import re | |
import time | |
from m5stack import * | |
from m5ui import * | |
from uiflow import * | |
lcd.setRotation(3) | |
setScreenColor(0x000000) | |
lcd.font(lcd.FONT_Default) | |
response = None | |
clbsResult = None | |
batteryColor = 0x000000 | |
def batteryStatus(): | |
if axp.getChargeState(): | |
batteryStatus = 'Charging' | |
batteryColor = 0x00ff00 | |
else: | |
batteryRemain = map_value(axp.getBatVoltage(), 3.7, 4.1, 0, 100) | |
if 75 <= batteryRemain <= 100: | |
batteryColor = 0x00ff00 | |
elif 50 <= batteryRemain <= 74: | |
batteryColor = 0xffd700 | |
elif 25 <= batteryRemain <= 49: | |
batteryColor = 0xffa103 | |
elif 0 <= batteryRemain <= 24: | |
batteryColor = 0xff0000 | |
batteryStatus = str(batteryRemain) + '%' | |
lcd.print(str('Battery: ') + batteryStatus, 0, 5, batteryColor) | |
def terminateApplication(): | |
lcd.clear() | |
lcd.print('Terminating...', 0, 5, 0xff0000) | |
uart1.write('AT+CNACT=0,0'+"\r\n") | |
wait(10) | |
lcd.print('Connection Deactivated.\nDevice will reboot.', 0, 25, 0x00ff00) | |
wait(3) | |
machine.reset() | |
btnB.wasPressed(terminateApplication) | |
# Connect to CAT-M Unit and Communication(SIM) | |
batteryStatus() | |
uart1 = machine.UART(1, tx=32, rx=33) | |
uart1.init(115200, bits=8, parity=None, stop=1) | |
uart1.write('AT+CNACT=0,1'+"\r\n") | |
lcd.print('Connection Activating.', 0, 25, 0x00ff00) | |
wait(10) | |
lcd.clear() | |
batteryStatus() | |
lcd.print('Positioning.', 0, 25, 0x00ff00) | |
# Get Location and datetime | |
while True: | |
uart1.write('AT+CLBS=4,0'+"\r\n") | |
if uart1.any(): | |
response = (uart1.readline()).decode() | |
if re.match(".*\+CLBS: 0.+", response): | |
lcd.clear() | |
batteryStatus() | |
M5Led.on() | |
wait_ms(100) | |
M5Led.off() | |
speaker.setVolume(1) | |
speaker.tone(432, 150) | |
clbsResult = response.split(',') | |
lcd.print('DateTime(UTC):', 0, 40, 0xffffff) | |
lcd.print(clbsResult[4] + ' ' + clbsResult[5], 0, 60, 0xffffff) | |
lcd.print('Latitude: ' + clbsResult[2], 0, 80, 0xffffff) | |
lcd.print('Longitude: ' + clbsResult[1], 0, 100, 0xffffff) | |
wait(60) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment