def checkSIM(): print('-> Checking SIM status') if (send_at('AT+CPIN?', 'READY', 1) == 1): print(' -> SIM ready') return 1 else: if (send_at('AT+CPIN?', 'SIM PIN', 1) == 1): print(' -> SIM pin required!') return 0 else: print(' -> SIM error') return 2
def power_up(): if (send_at('AT', 'OK', 1) == 1): print('Already active') else: print('SIM7070X is starting...') GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(power_key, GPIO.OUT) time.sleep(0.1) GPIO.output(power_key, GPIO.HIGH) time.sleep(2) GPIO.output(power_key, GPIO.LOW) time.sleep(1) ser.flushInput() while (send_at('AT', 'OK', 1) == 0): time.sleep(1) send_at('AT', 'OK', 1) print('SIM7070X is ready!')
def power_down(): if (send_at('AT', 'OK', 1) != 1): print('Already powered off') else: print('Shutting SIM7070X down...') GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(power_key, GPIO.OUT) time.sleep(0.1) GPIO.output(power_key, GPIO.HIGH) time.sleep(2) GPIO.output(power_key, GPIO.LOW) time.sleep(1) ser.flushInput() while (send_at('AT', 'OK', 1) == 1): time.sleep(1) send_at('AT', 'OK', 1) print('SIM7070X is sleeping!')
def register(): print('Try to register network...') if (checkSIM()): roamingFix() # enable network registration send_at('AT+CREG=1', 'OK', 1) # wait for successful registration print('-> Registering...') while (send_at('AT+CREG?', '1,1', 1) != 1): time.sleep(1) print(' -> Success!') buff = send_at_get_result('AT+COPS?', 'OK', 1) print(' -> Registered to: ' + buff.split(',')[2]) print('-> Attach to GPRS/LTE now') send_at('AT+CGATT=1', 'OK', 1) while (send_at('AT+CGATT?', '1', 1) != 1): time.sleep(1) print('-> Set PDP Context') send_at('AT+CGDCONT=1,"IP","em"', 'OK', 1) print('-> Registration done. Ready to communicate!')
def roamingFix(): # set preferred mode to automatic (other options are lte or gsm only) send_at('AT+CNMP=2', 'OK', 1)
def initConnection(): print(' -> Init a new connection') send_at_get_result('AT+SMDISC', 'OK', 1) #disconnect from what might be a broken pipe send_at('AT+CGMR', 'OK', 0.1) send_at('AT+CMEE=0', 'OK', 0.1) send_at('AT+CSQ', 'OK', 0.1) send_at('AT+CPSI?', 'OK', 0.1) send_at('AT+CGREG?', '+CGREG: 0,1', 0.1) buff = send_at_get_result('AT+COPS?', 'OK', 1) print(' -> Registered to: ' + buff.split(',')[2]) if ('0,0' in send_at_get_result('AT+CNACT?', 'OK', 0.1)): send_at('AT+CNACT=0,1', 'OK', 0.5) if ('0' not in send_at_get_result('AT+CACID?', 'OK', 0.1)): send_at('AT+CACID=0', 'OK', 0.1) if ('0' in send_at_get_result('AT+SMSTATE?', 'OK', 0.1)): send_at('AT+SMCONF="CLIENTID","id"', 'OK', 0.1) send_at('AT+SMCONF="URL","' + MQTT_SERVER + '","' + MQTT_PORT + '"', 'OK', 0.1) send_at('AT+SMCONF="KEEPTIME",60', 'OK', 0.1) send_at('AT+SMCONF="QOS",1', 'OK', 0.1) send_at('AT+SMCONN', 'OK', 1)