1. WITH LTE
This is a simple script that attaches to NZ Spark provider and reads the CSQ every few seconds.
Code: Select all
lte = LTE(debug = True)
lte.attach()
ctr=1
while not lte.isattached():
print(ctr,' Attaching to Spark LTE network...')
time.sleep(2.5)
ctr = ctr +1
print("Modem ATTACHED to Spark LTE Network...")
ctr = 1
lte.connect()
while not lte.isconnected():
print(ctr, 'Connecting to Spark LTE Network...')
time.sleep(2.5)
ctr = ctr + 1
print("CONNECTED !!!")
ctr = 0
lte.pppsuspend()
while True:
print(lte.send_at_cmd('AT+CSQ'), 'iteration',ctr)
sleep(1)
ctr = ctr+1
2. WITHOUT LTE
This script uses traditional AT commands to initialize uart1, define the cid and contextid, set the apn etc etc, attach and connect.
The main simply reads CSQ every 2 seconds. this script has been running now for 24 hrs without a hitch. The code is a bit clunky and I apologise for that but it has been hacked around a bit in testing.
Code: Select all
# test CSQ without lte
# Runs on Fipy
import time
import machine
import gc
from time import sleep
from machine import UART
from utime import sleep_ms, ticks_ms, ticks_diff
from machine import Pin
##########################################################################
# define Variables
##########################################################################
m=0
##################################################################
# ATTACH
# Attach to Network
#################################################################
def attach():
success = 0
for n in range(0,10):
resp = send_to_uart('AT+CFUN?'+'\r') # check Cfun
success = search(resp,'OK')
sleep(2)
if success >0:
break
try:
print('>> Attempting to attach')
success=0
for n in range(0,10):
resp = send_to_uart('AT+CGATT=1'+'\r')
success = search(resp,'OK')
if success >0:
print('>> ATTACHED')
return success
break
sleep(10)
if success<0:
print('>> failed to attach')
except Exception as e:
print('## fail attempting to attach',e)
##################################################################
# send_to_uart
# uart always responds to AT commands unless set to off
#
##################################################################
def send_to_uart(data):
try:
print('SND TO UART',data)
uart.write(data)
# print('>>data=',data)
sleep_ms(50)
r = read_uart()
# r= r.decode('utf-8').replace('\r','').replace('\n','').replace(' ','')
print ('>> uart said',r)
if r is None:
r = b'\r\n ERROR \r\n' #handling None is a PITA
return r # MUST be byte object
except Exception as e:
print('## -uart send failed with ',data,e)
#
# ####################################################################
# # Read Uart RESPONSE
# ##################################################################
def read_uart():
try:
uart_resp=uart.read()
return uart_resp
except Exception as e:
print('uart read failed',e)
##################################################################
#
# Init uart1 which connected to the modem
#################################################################
def init_uart():
print('INIT UART')
global uart
try:
print('>> initialize uart1')
uart = UART(1, baudrate=921600, pins=('P20', 'P18', 'P19', 'P17'),timeout_chars = 20000)
print('>> init chars=',uart.any())
return
except Exception as e:
print('## cannot init uart',e)
#################################################################
# SEARCH
# search for substr inside str
##############################################################
def search(str,substr):
#return 0 ??
# return 1 success
#return -1 unsuccessful
# -2 nothing
# -3 error
print('SEARCH')
try:
success=0
err1 = "ERROR"
err2 = "nothing"
str = str.decode('utf-8')
str = str.replace('\r','').replace('\n','').replace(' ','')
print('>> seeking ',substr ,'in',str)
if substr is err1:
success =-2
print('no match')
elif substr is err2:
success = -3
print('>> no match')
if str.find(substr) is not -1:
success = 1
print('>> match found!')
sleep(1)
else:
success = -1
print('>> no match')
sleep(1)
print()
return success
except Exception as e:
print('## "wait for", failed.',str,substr,e)
sleep(10)
#####################################################################
# Modem Setup
# Note --MUST use cfun1,1
######################################################################
def modem_setup():
try:
list1 = ['AT+CFUN?','AT+CFUN=1,1','AT+CGDCONT= 1,"IP","internet.telecom.co.nz",,,,0,0,0,0,0,0',
'AT+CPMS="SM","SM","SM"','AT+CREG=1','AT+CMGF=1']
success = 0
for command in list1:
for n in range(0,2):
resp = send_to_uart(command+'\r') #
success = search(resp,'OK')
if success >0 :
sleep(0.1)
break # jump to next 'command' from list Break exits the inner loop only
except Exception as e:
print('## new modem setup failed to run',e)
###################################################################
# RSSI
##################################################################
def rssi():
try:
print('>> RSSI')
#must be called when attached but not connected
csq=""
n=0
r=send_to_uart('AT+CSQ'+'\r') #text mode
success =0
for n in range(0,2):
success = search(r,'OK')
if success >0:
csq = r.decode('utf-8')
csq = csq.replace(" ","")
csq = csq.replace('\r','')
csq = csq.replace('\n','')
print ('>> csq=',csq)
rssi= (int((csq[5:7])) -2 )*2 -109
print ('>> rssi=',rssi,'dBm')
print()
print()
except Exception as e:
print('## calc RSSI function failed',e)
####################################################################
###################################################################
# Main loop
###################################################################
init_uart()
sleep(2)
modem_setup()
attach()
while True:
m=m+1
sleep(2)
print('>> main loop >>',m)
rssi()
So my questions are:
SEARCH
>> seeking OK in +CSQ:23,99OK
>> match found!
>> csq= +CSQ:23,99OK
>> rssi= -67 dBm
>> main loop >> 45
>> RSSI
SND TO UART AT+CSQ
>> uart said b'\r\n+CSQ: 22,99\r\n\r\nOK\r\n'
SEARCH
>> seeking OK in +CSQ:22,99OK
>> match found!
>> csq= +CSQ:22,99OK
>> rssi= -69 dBm
SEARCH
>> seeking OK in +CSQ:22,99OK
>> match found!
>> csq= +CSQ:22,99OK
>> rssi= -69 dBm
1. Why does the lte version fail? I'm guessing it is an issue with lte, ( or more likly the way i'm using it) and not the Cat M1 service because the rssi is pretty respectable and the service doesnt fail after 24 hrs with method 2.
2. If either code stops for some reason a soft reset doesn't cut the mustard and a re-attach takes a LONG LONG time. If I do a power off the reconnect is almost instant.