def init(PDPindex,APN): try: #Define GPRS Settings, MUST change APN String in script for your Carrier Specific setting res = ATC.sendAtCmd('AT+CGDCONT=' + str(PDPindex) + ',"IP","' + str(APN) + '","0.0.0.0",0,0' ,ATC.properties.CMD_TERMINATOR,0,20) #How long does system wait before sending undersized packet measured in 100ms settings res = ATC.sendAtCmd('AT#DSTO=10' ,ATC.properties.CMD_TERMINATOR,0,20) #Define Min/required Quality of Service res = ATC.sendAtCmd('AT+CGQMIN=1,0,0,0,0,0' ,ATC.properties.CMD_TERMINATOR,0,20) res = ATC.sendAtCmd('AT+CGQREQ=1,0,0,3,0,0' ,ATC.properties.CMD_TERMINATOR,0,20) #escape guard time, after set time escape sequence is accepted, set in 20ms settings res = ATC.sendAtCmd('ATS12=40' ,ATC.properties.CMD_TERMINATOR,0,20) #disable the escape sequence from transmitting during a data session res = ATC.sendAtCmd('AT#SKIPESC=1' ,ATC.properties.CMD_TERMINATOR,0,20) #Set connect timeOuts and packet sizes for PDP#1 and Socket#1 res = ATC.sendAtCmd('AT#SCFG=1,1,1500,600,100,10' ,ATC.properties.CMD_TERMINATOR,0,20) #Sets the behavior of #SSEND and #SREVC, Socket#1 res = ATC.sendAtCmd('AT#SCFGEXT=1,2,0,30,0,0' ,ATC.properties.CMD_TERMINATOR,0,20) except: printException("initGPRS(" + PDPindex + "," + APN + ")") return
def init(LNA_Arg): # This function initializes the GPS # Arguments: # LNA_Arg: External LNA? TRUE or FALSE # # Returns: # 0: Pass # -1: Exception # -2: AT command ERROR tmpReturn = -1 try: res = ATC.sendAtCmd( "AT$GPSD?", ATC.properties.CMD_TERMINATOR, 0, 2 ) # query if the modem is in "controlled" mode, required for this tracking unit. if res != "$GPSD: 2": res = ATC.sendAtCmd("AT$GPSD=2", ATC.properties.CMD_TERMINATOR, 0, 2) # If it's not set, set it. # Check if the GPS command failed out if res == "ERROR": return -2 if LNA_Arg == "TRUE": # Query if we have an active GPS antenna with powered LNA res = ATC.sendAtCmd( "AT$GPSAT=1", ATC.properties.CMD_TERMINATOR, 0, 2 ) # We do, so turn OFF the internal LNA to avoid oversaturation. else: res = ATC.sendAtCmd( "AT$GPSAT=0", ATC.properties.CMD_TERMINATOR, 0, 2 ) # We do not, it's passive, so turn ON the internal LNA # Check if the GPS command failed out if res == "ERROR": return -2 # Turn the GPS ON GPS.powerOnOff(1) # No errors tmpReturn = 0 except: printException("GPS_Init") JANUS_SER.sendUART("GPS Init exception. \r\n") return tmpReturn
def configSMS(): try: #Enable TEXT format for SMS Message res = ATC.sendAtCmd('AT+CMGF=1' ,ATC.properties.CMD_TERMINATOR,3,2) #no indications, we will poll manually res = ATC.sendAtCmd('AT+CNMI=0,0,0,0,0' ,ATC.properties.CMD_TERMINATOR,3,2) #Storage location res = ATC.sendAtCmd('AT+CPMS="SM"' ,ATC.properties.CMD_TERMINATOR,3,2) #Received SMS extra information display res = ATC.sendAtCmd('AT+CSDH=0' ,ATC.properties.CMD_TERMINATOR,3,2) except: printException("configSMS()") return
def isGprsAttached(timeOut): # This function waits until the GPRS attaches exitLoop = -1 try: #Start timeout counter timerA = timers.timer(0) timerA.start(timeOut) #Wait until registered to GSM Network while (exitLoop == -1): MOD.watchdogReset() res = ATC.sendAtCmd('AT+CGREG?',ATC.properties.CMD_TERMINATOR,0,5) if (res[res.rfind(',')+1:len(res)] == '5' or res[res.rfind(',')+1:len(res)] == '1'): exitLoop = 0 break if timerA.isexpired(): break #Exit while except: printException("isGprsAttached()") return exitLoop
def Cellular_LED(inSLED): # This function sets the cellular LED # Arguments: # inStatus : GPS LED status. Pass in either 'ON' or 'OFF' # OFF - LED always OFF # ON - LED function ON # # Returns: # 0: Pass # -1: Exception tmpReturn = -1 try: #Set Stat LED to default value, 0 for OFF, 2 for ON if (inSLED == 'ON'): res = GPIO.setSLED(2, 10, 90) else: res = GPIO.setSLED(0, 10, 90) res = ATC.sendAtCmd('AT#SLEDSAV',ATC.properties.CMD_TERMINATOR,0,20) if (res == -1): #Errored out, 1 if no error -1 if error return tmpReturn tmpReturn = 0 except: printException("Cellular_LED") JANUS_SER.sendUART("GPS LED exception. \r\n") return tmpReturn
def closeSocket(sockNum): try: #Close Socket res = ATC.sendAtCmd('AT#SH=' + str(sockNum),ATC.properties.CMD_TERMINATOR,0,20) except: printException("closeSocket(" + sockNum + ")") return res
def wait4SIMReady(): #SIM status control - to avoid the 'sim busy' error # The following sequence loops forever until SIM card is ready for use try: res = ATC.sendAtCmd("AT#SIMDET?",ATC.properties.CMD_TERMINATOR,0,2) #query Sim detection style if (res.find("#SIMDET: 2")< 0): res = ATC.sendAtCmd('AT#SIMDET=2',ATC.properties.CMD_TERMINATOR,0,2) #Ensure detection is automatic via SIMIN and not forced res = ATC.sendAtCmd('AT&P0',ATC.properties.CMD_TERMINATOR,0,2) #Save Profile res = ATC.sendAtCmd('AT&W0',ATC.properties.CMD_TERMINATOR,0,2) #Save Settings print 'SIM Verification Cycle' SIM_status = ATC.sendAtCmd('AT+CPBS?' ,ATC.properties.CMD_TERMINATOR,0,5) #We aren't using AT+CPIN? because there are too many possible answers #This lets us know that the SIM is at least inserted if SIM_status.find("+CPBS")<0: print 'SIM busy! Please wait!\n' while SIM_status.find("+CPBS:")< 0 : SIM_status = ATC.sendAtCmd('AT+CPBS?' ,ATC.properties.CMD_TERMINATOR,0,5) MOD.sleep(2) print 'SIM Ready' except: printException("wait4SIMReady()") return
def openSocket(addr,port,sockNum,userID,userPassword,protocol,connMode): #Function Open a socket and responds with CONNECT/NO CARRIER/ERROR try: #Close Socket res = ATC.sendAtCmd('AT#SS',ATC.properties.CMD_TERMINATOR,0,20) if (res!="#SS: 1,0"): res = ATC.sendAtCmd('AT#SH=1',ATC.properties.CMD_TERMINATOR,0,20) if (res=='ERROR'): return #Activate PDP if needed res = ATC.sendAtCmd('AT#SGACT?',ATC.properties.CMD_TERMINATOR,0,20) if (res!="#SGACT: 1,1"): delaySec(1) res = ATC.sendAtCmd('AT#SGACT=1,1,"' + str(userID) + '","' + str(userPassword) + '"' ,ATC.properties.CMD_TERMINATOR,0,180) if (res=='ERROR'): return #Open Socket to Server in Data Mode if (str(protocol)=='TCPIP'): res = ATC.sendAtCmd('AT#SD=1,0,' + str(port) + ',"' + str(addr) + '",0,0,' + str(connMode),'CONNECT\r\n',0,180) else: res = ATC.sendAtCmd('AT#SD=1,1,' + str(port) + ',"' + str(addr) + '",0,5559,' + str(connMode),'CONNECT\r\n',0,180) except: printException("openSocket(" + str(addr) + "," + str(port) + "," + str(sockNum) + "," + str(userID) + "," + str(userPassword) + "," + str(protocol) + str(connMode) + ")") return res
def wait4SIMReady(): #SIM status control - to avoid the 'sim busy' error # The following sequence loops forever until SIM card is ready for use try: print 'SIM Verification Cycle' SIM_status = ATC.sendAtCmd('AT+CPBS?' ,ATC.properties.CMD_TERMINATOR,0,5) if SIM_status.find("+CPBS")<0: print 'SIM busy! Please wait!\n' while SIM_status.find("+CPBS:")< 0 : SIM_status = ATC.sendAtCmd('AT+CPBS?' ,ATC.properties.CMD_TERMINATOR,0,5) MOD.sleep(2) print 'SIM Ready' except: printException("wait4SIMReady()") return
def getNetworkTime(timeOut): # This function forces the Network to update RTC with GSM Network Time tmpReturn = -1 try: res = ATC.sendAtCmd("AT#NITZ=1",ATC.properties.CMD_TERMINATOR,0,2) #set NITZ command to update Network Time res = ATC.sendAtCmd("AT+COPS=2",ATC.properties.CMD_TERMINATOR,0,2) #set COPS command to force GSM Registration to disconnect #Start timeout counter timerA = timers.timer(0) timerA.start(timeOut) #Wait until GSM module is not registered to Network while (1): MOD.watchdogReset() res = ATC.sendAtCmd('AT+CREG?',ATC.properties.CMD_TERMINATOR,0,5) if (res == "+CREG: 0,0"): break if timerA.isexpired(): return tmpReturn res = ATC.sendAtCmd("AT+COPS=0",ATC.properties.CMD_TERMINATOR,0,2) #set COPS command to force GSM Registration res = isGsmRegistered(timeOut) if (res == 0): tmpReturn = 0 res = ATC.sendAtCmd("AT+CCLK?",ATC.properties.CMD_TERMINATOR,0,2) #Query RTC Clock except: printException("getNetworkTime") return tmpReturn
def sendSMS(theSmsMsg,theDestination,theTerminator,retry,timeOut): #This function sends an SMS Message # Input Parameter Definitions # theSmsMsg: The text SMS Message # theTerminator: string or character at the end of AT Command # retry: How many times the command will attempt to retry if not successfully send # timeOut: number of [1/10 seconds] command could take to respond #Note that the 145 being sent in with the destination address is the "type" of destination address, with 145 including a "+" for international while (retry != -1): print 'AT+CMGS="' + str(theDestination) + '",145' res = MDM.send('AT+CMGS="' + str(theDestination) + '",145', 0) res = MDM.sendbyte(0x0d, 0) res = ATC.mdmResponse('\r\n>', timeOut) print res res = MDM.send(theSmsMsg, 0) res = MDM.sendbyte(0x1a, 0) #Wait for AT command response res = ATC.mdmResponse(theTerminator, timeOut) #Did AT command respond without error? pos1 = res.rfind(theTerminator,0,len(res)) if (pos1 != -1): retry = -1 res = ATC.parseResponse(res) else: retry = retry - 1 print res #If the function fails to find the proper response to the SMS send ('OK<cr><lf>') then we receive a timeout string: 'timeOut' return res
def openSocket(addr,port,sockNum,userID,userPassword,protocol,connMode): #Function Open a socket and responds with CONNECT/NO CARRIER/ERROR # Arguments: # addr: IP Address # port: Port of the address # sockNum: Socket number # userID: User ID # userPassword: User Password # protocol: TCPIP or UDP # connMode: 0 for Command Mode, 1 for Online Mode # # Returns: # 0: Pass # -1: Exception # -2: AT command ERROR tmpReturn = -1 try: #Close Socket res = ATC.sendAtCmd('AT#SS',ATC.properties.CMD_TERMINATOR,0,20) if (res!="#SS: 1,0"): res = ATC.sendAtCmd('AT#SH=1',ATC.properties.CMD_TERMINATOR,0,20) if (res=='ERROR'): return (-2) #Activate PDP if needed res = ATC.sendAtCmd('AT#SGACT?',ATC.properties.CMD_TERMINATOR,0,20) if (res!="#SGACT: 1,1"): delaySec(1) res = ATC.sendAtCmd('AT#SGACT=1,1,"' + str(userID) + '","' + str(userPassword) + '"' ,ATC.properties.CMD_TERMINATOR,0,180) if (res=='ERROR'): return (-2) #Open Socket to Server in Command mode if (str(protocol)=='TCPIP'): res = ATC.sendAtCmd('AT#SD=' + str(sockNum) + ',0,' + str(port) + ',"' + str(addr) + '",0,0,' + str(connMode),ATC.properties.CMD_TERMINATOR,0,180) #You get an OK, not a CONNECT else: res = ATC.sendAtCmd('AT#SD=' + str(sockNum) + ',1,' + str(port) + ',"' + str(addr) + '",0,5559,' + str(connMode),ATC.properties.CMD_TERMINATOR,0,180) #You get an OK, not a CONNECT if (res!='OK'): return (-2) #Pass tmpReturn = 0 except: printException("openSocket(" + str(addr) + "," + str(port) + "," + str(sockNum) + "," + str(userID) + "," + str(userPassword) + "," + str(protocol) + str(connMode) + ")") return tmpReturn
def openSocket(addr, port, sockNum, userID, userPassword, protocol, connMode): # Function Open a socket and responds with CONNECT/NO CARRIER/ERROR try: # Close Socket res = ATC.sendAtCmd("AT#SS", ATC.properties.CMD_TERMINATOR, 0, 20) if res != "#SS: 1,0": res = ATC.sendAtCmd("AT#SH=1", ATC.properties.CMD_TERMINATOR, 0, 20) if res == "ERROR": return # Activate PDP if needed res = ATC.sendAtCmd("AT#SGACT?", ATC.properties.CMD_TERMINATOR, 0, 20) if res != "#SGACT: 1,1": delaySec(1) res = ATC.sendAtCmd( 'AT#SGACT=1,1,"' + str(userID) + '","' + str(userPassword) + '"', ATC.properties.CMD_TERMINATOR, 0, 180 ) if res == "ERROR": return # Open Socket to Server if str(protocol) == "TCPIP": res = ATC.sendAtCmd( "AT#SD=1,0," + str(port) + ',"' + str(addr) + '",0,0,' + str(connMode), "OK\r\n", 0, 180 ) else: res = ATC.sendAtCmd( "AT#SD=1,1," + str(port) + ',"' + str(addr) + '",0,5559,' + str(connMode), "OK\r\n", 0, 180 ) except: printException("openSocket()") return res
def main(): try: timerA = timers.timer(0) timerA.start(1) # Set Global Watchdog timeout in Seconds MOD.watchdogEnable(300) #Increase CPU speed at cost of increased current consumption ATC.sendAtCmd('AT#CPUMODE=1',ATC.properties.CMD_TERMINATOR,0,5) #Turn off GSM TX/RX ATC.sendAtCmd('AT+CFUN=4',ATC.properties.CMD_TERMINATOR,0,5) #Initialize MS20 Receiver res = MS20.initGPS('9600','8N1') if not(res == 0): if (res == -1): DEBUG_CF.sendMsg("MS20 Exception occurred\r\n",myApp.RUN_MODE) elif (res == -3): DEBUG_CF.sendMsg("NMEA Command response Checksum fail\r\n",myApp.RUN_MODE) elif (res == -4): DEBUG_CF.sendMsg("No NMEA Command response\r\n",myApp.RUN_MODE) DEBUG_CF.sendMsg("Is NAVSYNC serial port connected to TRACE port via NULL MODEM cable?\r\n",myApp.RUN_MODE) DEBUG_CF.sendMsg("For CF_EVAL_PCB001 V3.1 evaluation boards or newer => SW1, MODE1 = ON\r\n",myApp.RUN_MODE) DEBUG_CF.sendMsg("See GSM865CF Plug-in Terminal GPS Demonstration User Guide for more info\r\n",myApp.RUN_MODE) elif (res == -5): DEBUG_CF.sendMsg("Incorrect NMEA command response\r\n",myApp.RUN_MODE) elif (res > 0): DEBUG_CF.sendMsg("MS20 Error Number: " + str(res) + "\r\n",myApp.RUN_MODE) else: DEBUG_CF.sendMsg("Unknown error\r\n",myApp.RUN_MODE) MOD.sleep(40) return DEBUG_CF.sendMsg("MS20 Initialization Complete\r\n",myApp.RUN_MODE) DEBUG_CF.sendMsg("GPS Application Version: " + MS20.GPSdata.APP_VER + "\r\n",myApp.RUN_MODE) # Wait for GPS module to obtain position data DEBUG_CF.sendMsg("Waiting for valid position",myApp.RUN_MODE) #Poll NMEA GPGLL Sentence if (MS20.pollNMEA('2',5) == 0): exitLoop = MS20.GPSdata.GPGLL.split(',')[6] while(exitLoop != 'A'): MOD.watchdogReset() #Poll NMEA GPGLL Sentence if (MS20.pollNMEA('2',5) == 0): exitLoop = MS20.GPSdata.GPGLL.split(',')[6] DEBUG_CF.sendMsg(".",myApp.RUN_MODE) DEBUG_CF.sendMsg("\r\nPosition acquired: " +str(timerA.count()) + " Seconds (Referenced to script start time)\r\n",myApp.RUN_MODE) #Increase CPU speed during TX/RX only ATC.sendAtCmd('AT#CPUMODE=2',ATC.properties.CMD_TERMINATOR,0,5) #Activate Low Power mode, Turn off GSM TX/RX ATC.sendAtCmd('AT+CFUN=5',ATC.properties.CMD_TERMINATOR,0,5) #Set Network specific settings res = NETWORK.initGsmNetwork(myApp.NETWORK,myApp.BAND) if (res == -1): return DEBUG_CF.sendMsg("Network Initialization Complete\r\n",myApp.RUN_MODE) #Wait for GSM Registration DEBUG_CF.sendMsg("Waiting for GSM Registration",myApp.RUN_MODE) #Check GSM registration exitLoop = NETWORK.isGsmRegistered(1) while(exitLoop != 0): MOD.watchdogReset() #Check GSM registration exitLoop = NETWORK.isGsmRegistered(1) DEBUG_CF.sendMsg(".",myApp.RUN_MODE) DEBUG_CF.sendMsg("\r\nTerminal is registered to a GSM network\r\n",myApp.RUN_MODE) #Init GPRS GPRS.init('1',myApp.APN) DEBUG_CF.sendMsg("GPRS Initialization Complete\r\n",myApp.RUN_MODE) #Wait for GPRS attach DEBUG_CF.sendMsg("Waiting for GPRS Attach",myApp.RUN_MODE) #Check GPRS Attach exitLoop = NETWORK.isGprsAttached(1) while(exitLoop != 0): MOD.watchdogReset() #Check GPRS Attach exitLoop = NETWORK.isGprsAttached(1) DEBUG_CF.sendMsg(".",myApp.RUN_MODE) DEBUG_CF.sendMsg("\r\nTerminal is attached to GPRS service\r\n",myApp.RUN_MODE) #Record IMEI number myApp.IMEI = ATC.sendAtCmd('AT+CGSN',ATC.properties.CMD_TERMINATOR,0,5) DEBUG_CF.sendMsg("IMEI #: " + myApp.IMEI + "\r\n",myApp.RUN_MODE) # Start timeout timer timerB = timers.timer(0) timerB.start(1) while(not(timerB.isexpired())): exitCode = -1 # Loop forever, without this loop the script would run once and exit script mode. On reboot or power-on the script would run once more while (1): exitCode = -1 while (exitCode==-1): MOD.watchdogReset() #If interval timer expires then send packet to server if (timerB.isexpired()): #Poll NMEA GPGLL Sentence res = MS20.pollNMEA('2',5) DEBUG_CF.sendMsg("Current GPGLL sentence: " + MS20.GPSdata.GPGLL,myApp.RUN_MODE) DEBUG_CF.sendMsg("Opening Connection to server: " + myApp.IP + ":" + myApp.PORT + "\r\n",myApp.RUN_MODE) #Connect to server #Pass in: IP Address, IP Port, sockNum, GPRSuserName, GPRSuserPassword,Command Mode res = GPRS.openSocket(myApp.IP,myApp.PORT,'1','','',myApp.PROTOCOL,'1') try: #If socket open upload data if (res == 'OK'): DEBUG_CF.sendMsg("Connection opened\r\n",myApp.RUN_MODE) #Build String to send to customer server STR1 = myApp.IMEI +',' + MS20.GPSdata.GPGLL DEBUG_CF.sendMsg("Sending Data: " + STR1,myApp.RUN_MODE) #Send STR1 to server res = GPRS.send_CM(STR1,1,10) DEBUG_CF.sendMsg("Data Sent\r\n",myApp.RUN_MODE) #Close Socket res = GPRS.closeSocket('1') DEBUG_CF.sendMsg("Connection Closed\r\n",myApp.RUN_MODE) exitCode = 0 else: DEBUG_CF.sendMsg("Connection failed to open\r\n",myApp.RUN_MODE) #What is the signal strength? res = ATC.sendAtCmd('AT+CSQ',ATC.properties.CMD_TERMINATOR,0,5) DEBUG_CF.sendMsg("Signal Strength (AT+CSQ): " + res + "\r\n",myApp.RUN_MODE) # Is Terminus still connected to GSM Network? res = NETWORK.isGsmRegistered(1) if (res == 0): DEBUG_CF.sendMsg("GSM865CF is registered on GSM network\r\n",myApp.RUN_MODE) else: DEBUG_CF.sendMsg("GSM865CF is NOT registered on GSM network\r\n",myApp.RUN_MODE) #Is a PDP context activated? res = ATC.sendAtCmd('AT#SGACT?',ATC.properties.CMD_TERMINATOR,0,20) DEBUG_CF.sendMsg("PDP Context status (AT#SGACT?): " + res + "\r\n",myApp.RUN_MODE) # Is Terminus still attached to GPRS service? res = NETWORK.isGprsAttached(1) if (res == 0): DEBUG_CF.sendMsg("GSM865CF is attached to GPRS service\r\n",myApp.RUN_MODE) else: DEBUG_CF.sendMsg("GSM865CF is NOT attached to GPRS service\r\n",myApp.RUN_MODE) except: DEBUG_CF.sendMsg("Script encountered an exception while uploading data to server\r\n",myApp.RUN_MODE) DEBUG_CF.sendMsg("Exception Type: " + str(sys.exc_type) + "\r\n",myApp.RUN_MODE) DEBUG_CF.sendMsg("MODULE -> GSM865CF_GPS\r\n",myApp.RUN_MODE) return else: DEBUG_CF.CLS(myApp.RUN_MODE) DEBUG_CF.sendMsg("Next update in: " + str(int(myApp.INTERVAL) - timerB.count()) + " Seconds\r\n",myApp.RUN_MODE) MOD.sleep(10) ## Re-Start timeout timer timerB = timers.timer(0) timerB.start(int(myApp.INTERVAL)) except: DEBUG_CF.sendMsg("Script encountered an exception\r\n",myApp.RUN_MODE) DEBUG_CF.sendMsg("Exception Type: " + str(sys.exc_type) + "\r\n",myApp.RUN_MODE) DEBUG_CF.sendMsg("MODULE -> GSM865CF_GPS\r\n",myApp.RUN_MODE) return
MOD.watchdogEnable(300) try: RUN_MODE = 0 test = float(RUN_MODE) #float not implemented in Telit module except: #Running in IDE RUN_MODE = 1 DEBUG.CLS(RUN_MODE) #Clear screen command for VT100 terminals DEBUG.sendMsg("GPSDemo Script has started\r\n",RUN_MODE) #Apply Network Specific settings see myApp.xxxx assignment above if (myApp.NETWORK == "ATT"): #Set module to work on US ATT Network res = ATC.sendAtCmd("AT#ENS?",ATC.properties.CMD_TERMINATOR,3,2) #query ENS setting if (res == "#ENS: 0"): res = ATC.sendAtCmd('AT#ENS=1',ATC.properties.CMD_TERMINATOR,3,2) #sets all ATT requirements MOD.sleep(15) #required to halt Python thread and allow NVM Flash to update res = ATC.sendAtCmd('AT#REBOOT',ATC.properties.CMD_TERMINATOR,3,2) #must reboot to take effect res = ATC.sendAtCmd("AT#SELINT?",ATC.properties.CMD_TERMINATOR,3,2) #query SELINT setting if (res != "#SELINT: 2"): res = ATC.sendAtCmd('AT#SELINT=2',ATC.properties.CMD_TERMINATOR,3,2)#use of most recent AT command set MOD.sleep(15) #required to halt Python thread and allow NVM Flash to update res = ATC.sendAtCmd('AT#REBOOT',ATC.properties.CMD_TERMINATOR,3,2) #must reboot to take effect else: #Set module to work on all other Networks res = ATC.sendAtCmd('AT#ENS?',ATC.properties.CMD_TERMINATOR,3,2) if (res == "#ENS: 1"): res = ATC.sendAtCmd('AT#ENS=0',ATC.properties.CMD_TERMINATOR,3,2) #disable ATT requirements
def initGsmNetwork(inSTR1, inSTR2): # This function sets all Network specific settings # Input Parameter Definitions # inSTR1: GSM Network (ATT or GSM) # inSTR2: AT#BND=inSTR2 (0,1,2,or 3) tmpReturn = -1 try: #res = ATC.sendAtCmd("AT+CFUN=1",ATC.properties.CMD_TERMINATOR,0,2) #Set Automatic Operator Selection res = ATC.getFirmwareRev() if (res == -1): return tmpReturn res = ATC.sendAtCmd("AT#SELINT?",ATC.properties.CMD_TERMINATOR,0,2) #query SELINT setting if (res != "#SELINT: 2"): res = ATC.sendAtCmd('AT#SELINT=2',ATC.properties.CMD_TERMINATOR,0,2) #use of most recent AT command set res = ATC.sendAtCmd('AT&P0',ATC.properties.CMD_TERMINATOR,0,2) #Save Profile res = ATC.sendAtCmd('AT&W0',ATC.properties.CMD_TERMINATOR,0,2) #Save Settings return tmpReturn if (ATC.properties.firmwareMajor == '07'): #Firmware specific settings if (ATC.properties.firmwareMinor == '02'): res = ATC.sendAtCmd('AT#AUTOBND?',ATC.properties.CMD_TERMINATOR,3,2) #query AUTOBND setting if (res != "#AUTOBND: 1"): res = ATC.sendAtCmd('AT#AUTOBND=1',ATC.properties.CMD_TERMINATOR,3,2) #enable Quad band system selection res = ATC.sendAtCmd('AT&P0',ATC.properties.CMD_TERMINATOR,0,2) #Save Profile res = ATC.sendAtCmd('AT&W0',ATC.properties.CMD_TERMINATOR,0,2) #Save Settings return tmpReturn elif (ATC.properties.firmwareMinor == '03'): res = ATC.sendAtCmd('AT#AUTOBND?',ATC.properties.CMD_TERMINATOR,3,2) #query AUTOBND setting if (res != "#AUTOBND: 2"): res = ATC.sendAtCmd('AT#AUTOBND=2',ATC.properties.CMD_TERMINATOR,3,2) #enable Quad band system selection res = ATC.sendAtCmd('AT&P0',ATC.properties.CMD_TERMINATOR,0,2) #Save Profile res = ATC.sendAtCmd('AT&W0',ATC.properties.CMD_TERMINATOR,0,2) #Save Settings return tmpReturn #Apply Network Specific settings see myApp.xxxx assignment above if (inSTR1 == "ATT"): #Set module to work on US ATT Network res = ATC.sendAtCmd("AT#ENS?",ATC.properties.CMD_TERMINATOR,0,2) #query ENS setting if (res != "#ENS: 1"): res = ATC.sendAtCmd('AT#ENS=1',ATC.properties.CMD_TERMINATOR,0,2) #sets all ATT requirements res = ATC.sendAtCmd('AT&P0',ATC.properties.CMD_TERMINATOR,0,2) #Save Profile res = ATC.sendAtCmd('AT&W0',ATC.properties.CMD_TERMINATOR,0,2) #Save Settings return tmpReturn #Firmware specific settings if (ATC.properties.firmwareMinor == '02'): res = ATC.sendAtCmd("AT#PLMNMODE?",ATC.properties.CMD_TERMINATOR,0,2) #query PLMN setting if (res != "#PLMNMODE: 1"): res = ATC.sendAtCmd('AT#PLMNMODE=1',ATC.properties.CMD_TERMINATOR,0,2) #enable EONS (enhanced operator naming scheme) res = ATC.sendAtCmd('AT&P0',ATC.properties.CMD_TERMINATOR,0,2) #Save Profile res = ATC.sendAtCmd('AT&W0',ATC.properties.CMD_TERMINATOR,0,2) #Save Settings return tmpReturn else: #Set module to work on all other Networks res = ATC.sendAtCmd('AT#ENS?',ATC.properties.CMD_TERMINATOR,0,2) if (res != "#ENS: 0"): res = ATC.sendAtCmd('AT#ENS=0',ATC.properties.CMD_TERMINATOR,0,2) #disable all ATT requirements res = ATC.sendAtCmd('AT&P0',ATC.properties.CMD_TERMINATOR,0,2) #Save Profile res = ATC.sendAtCmd('AT&W0',ATC.properties.CMD_TERMINATOR,0,2) #Save Settings return tmpReturn res = ATC.sendAtCmd("AT#BND?",ATC.properties.CMD_TERMINATOR,0,2) #query BND setting if (res != "#BND: " + inSTR2): res = ATC.sendAtCmd('AT#BND='+ inSTR2,ATC.properties.CMD_TERMINATOR,3,2) #set bands to 850/1900 res = ATC.sendAtCmd('AT&P0',ATC.properties.CMD_TERMINATOR,0,2) #Save Profile res = ATC.sendAtCmd('AT&W0',ATC.properties.CMD_TERMINATOR,0,2) #Save Settings return tmpReturn res = ATC.sendAtCmd("AT#PLMNMODE?",ATC.properties.CMD_TERMINATOR,0,2) #query PLMN setting if (res != "#PLMNMODE: 1"): res = ATC.sendAtCmd('AT#PLMNMODE=1',ATC.properties.CMD_TERMINATOR,0,2) #enable EONS (enhanced operator naming scheme) res = ATC.sendAtCmd('AT&P0',ATC.properties.CMD_TERMINATOR,0,2) #Save Profile res = ATC.sendAtCmd('AT&W0',ATC.properties.CMD_TERMINATOR,0,2) #Save Settings return tmpReturn res = wait4SIMReady() #wait for SIM to be ready for use res = ATC.sendAtCmd("AT#STIA?",ATC.properties.CMD_TERMINATOR,0,2) #query STIA settings if ((res.find('STIA: 0,1')==-1) and (res.find('STIA: 1,1')==-1)): res = ATC.sendAtCmd('AT#STIA=1,10',ATC.properties.CMD_TERMINATOR,0,2) #enable SAT - SIM Application Tool-Kit res = ATC.sendAtCmd('AT&P0',ATC.properties.CMD_TERMINATOR,0,2) #Save Profile res = ATC.sendAtCmd('AT&W0',ATC.properties.CMD_TERMINATOR,0,2) #Save Settings return tmpReturn elif (ATC.properties.firmwareMajor == '10'): if (inSTR1 == "ATT"): #Set module to work on US ATT Network res = ATC.sendAtCmd("AT#ENS?",ATC.properties.CMD_TERMINATOR,0,2) #query ENS setting if (res != "#ENS: 1"): res = ATC.sendAtCmd('AT#ENS=1',ATC.properties.CMD_TERMINATOR,0,2) #sets all ATT requirements res = ATC.sendAtCmd('AT&P0',ATC.properties.CMD_TERMINATOR,0,2) #Save Profile res = ATC.sendAtCmd('AT&W0',ATC.properties.CMD_TERMINATOR,0,2) #Save Settings return tmpReturn else: res = ATC.sendAtCmd('AT#AUTOBND?',ATC.properties.CMD_TERMINATOR,3,2) #query AUTOBND setting if (res != "#AUTOBND: 2"): res = ATC.sendAtCmd('AT#AUTOBND=2',ATC.properties.CMD_TERMINATOR,3,2) #enable Quad band system selection res = ATC.sendAtCmd('AT&P0',ATC.properties.CMD_TERMINATOR,0,2) #Save Profile res = ATC.sendAtCmd('AT&W0',ATC.properties.CMD_TERMINATOR,0,2) #Save Settings return tmpReturn res = ATC.sendAtCmd("AT#PLMNMODE?",ATC.properties.CMD_TERMINATOR,0,2) #query PLMN setting if (res != "#PLMNMODE: 1"): res = ATC.sendAtCmd('AT#PLMNMODE=1',ATC.properties.CMD_TERMINATOR,0,2) #enable EONS (enhanced operator naming scheme) res = ATC.sendAtCmd('AT&P0',ATC.properties.CMD_TERMINATOR,0,2) #Save Profile res = ATC.sendAtCmd('AT&W0',ATC.properties.CMD_TERMINATOR,0,2) #Save Settings return tmpReturn res = wait4SIMReady() #wait for SIM to be ready for use res = ATC.sendAtCmd("AT#STIA?",ATC.properties.CMD_TERMINATOR,0,2) #query STIA settings if ((res.find('STIA: 0,1')==-1) and (res.find('STIA: 1,1')==-1)): res = ATC.sendAtCmd('AT#STIA=1,10',ATC.properties.CMD_TERMINATOR,0,2) #enable SAT - SIM Application Tool-Kit res = ATC.sendAtCmd('AT&P0',ATC.properties.CMD_TERMINATOR,0,2) #Save Profile res = ATC.sendAtCmd('AT&W0',ATC.properties.CMD_TERMINATOR,0,2) #Save Settings return tmpReturn else: print 'Unknown Firmware' tmpReturn = 0 except: printException("initGsmNetwork()") return tmpReturn
def SMSCommand(inStr): # This function checks a newly received SMS for commands. # If it finds specific command, using the header "CMD: " it will parse the command out, adjust the configuration parameter, and then respond to the originating PN as verification. # If it finds an AT command, by simply finding AT it will take the command and carry it out, replying to the originating PN with the AT response. # Input Parameter Definitions # inStr: The received SMS # Returns: # 0: Pass, action carried out and SMS sent back # -1: Exception # -2: Pass, Unrecognized change command or AT command received though # -3: Unrecognized SMS # -4: Error sending an SMS to the originating P/N ## Currently supported command list for things that can be altered. Simple list for now. ## The user may also query the unit's information with a simple STATUS ## INTERVAL ## NOSWITCH ## IGNITION ## SLED ## ULED ## AUTOON tmpReturn = -1 try: #Check for either change command or an AT command. Splitting ATCMD check into specifics because STATUS can trigger a generic AT check (alternative is to switch query word) changeCMD = inStr.find('CMD: ',0,len(inStr)) ATCMD = inStr.find('AT',0,len(inStr)) StatusQuery = inStr.find('STATUS',0,len(inStr)) if (changeCMD != -1): #Change command found, take the commad, find what it's adjusting and then send an SMS back to the originator for verification #We know that it should be found at 0, and 5 characters after the command should start "CMD: x". ReceivedCmd = inStr[+5:len(inStr)] if (ReceivedCmd == 'NOSWITCH = TRUE'): JANUS_CONFIG.Config.NOSWITCH = 'TRUE' JANUS_SER.sendUART("Switch Report : " + JANUS_CONFIG.Config.NOSWITCH + "\r\n") res = sendSMS("Switch Report : " + JANUS_CONFIG.Config.NOSWITCH,SMSInfo.OriginatingPN,ATC.properties.CMD_TERMINATOR,3,180) tmpReturn = 0 elif (ReceivedCmd == 'NOSWITCH = FALSE'): JANUS_CONFIG.Config.NOSWITCH = 'FALSE' JANUS_SER.sendUART("Switch Report : " + JANUS_CONFIG.Config.NOSWITCH + "\r\n") res = sendSMS("Switch Report : " + JANUS_CONFIG.Config.NOSWITCH,SMSInfo.OriginatingPN,ATC.properties.CMD_TERMINATOR,3,180) tmpReturn = 0 elif (ReceivedCmd == 'IGNITION = TRUE'): JANUS_CONFIG.Config.IGNITION = 'TRUE' JANUS_SER.sendUART("Ignition Report : " + JANUS_CONFIG.Config.IGNITION + "\r\n") res = sendSMS("Ignition Report : " + JANUS_CONFIG.Config.IGNITION,SMSInfo.OriginatingPN,ATC.properties.CMD_TERMINATOR,3,180) tmpReturn = 0 elif (ReceivedCmd == 'IGNITION = FALSE'): JANUS_CONFIG.Config.IGNITION = 'FALSE' JANUS_SER.sendUART("Ignition Report : " + JANUS_CONFIG.Config.IGNITION + "\r\n") res = sendSMS("Ignition Report : " + JANUS_CONFIG.Config.IGNITION,SMSInfo.OriginatingPN,ATC.properties.CMD_TERMINATOR,3,180) tmpReturn = 0 elif (ReceivedCmd == 'SLED = ON'): JANUS_CONFIG.Config.SLED = 'ON' JANUS_SER.sendUART("Cellular LED : " + JANUS_CONFIG.Config.SLED + "\r\n") res = sendSMS("Cellular LED : " + JANUS_CONFIG.Config.SLED,SMSInfo.OriginatingPN,ATC.properties.CMD_TERMINATOR,3,180) tmpReturn = 0 elif (ReceivedCmd == 'SLED = OFF'): JANUS_CONFIG.Config.SLED = 'OFF' JANUS_SER.sendUART("Cellular LED : " + JANUS_CONFIG.Config.SLED + "\r\n") res = sendSMS("Cellular LED : " + JANUS_CONFIG.Config.SLED,SMSInfo.OriginatingPN,ATC.properties.CMD_TERMINATOR,3,180) tmpReturn = 0 elif (ReceivedCmd == 'ULED = ON'): JANUS_CONFIG.Config.ULED = 'ON' JANUS_SER.sendUART("User LED : " + JANUS_CONFIG.Config.ULED + "\r\n") res = sendSMS("User LED : " + JANUS_CONFIG.Config.ULED,SMSInfo.OriginatingPN,ATC.properties.CMD_TERMINATOR,3,180) tmpReturn = 0 elif (ReceivedCmd == 'ULED = OFF'): JANUS_CONFIG.Config.ULED = 'OFF' JANUS_SER.sendUART("User LED : " + JANUS_CONFIG.Config.ULED + "\r\n") res = sendSMS("User LED : " + JANUS_CONFIG.Config.ULED,SMSInfo.OriginatingPN,ATC.properties.CMD_TERMINATOR,3,180) tmpReturn = 0 elif (ReceivedCmd == 'AUTOON = ON'): JANUS_CONFIG.Config.AUTOON = 'ON' JANUS_SER.sendUART("Auto ON : " + JANUS_CONFIG.Config.AUTOON + "\r\n") res = sendSMS("Auto ON : " + JANUS_CONFIG.Config.AUTOON,SMSInfo.OriginatingPN,ATC.properties.CMD_TERMINATOR,3,180) tmpReturn = 0 elif (ReceivedCmd == 'AUTOON = OFF'): JANUS_CONFIG.Config.AUTOON = 'OFF' JANUS_SER.sendUART("Auto ON : " + JANUS_CONFIG.Config.AUTOON + "\r\n") res = sendSMS("Auto ON : " + JANUS_CONFIG.Config.AUTOON,SMSInfo.OriginatingPN,ATC.properties.CMD_TERMINATOR,3,180) tmpReturn = 0 elif (ReceivedCmd.find("INTERVAL") !=-1): #We have a GPS reporting interval change, make sure the value is between 1 and 86400 #INTERVAL = xxx NewInterval = ReceivedCmd[+11:len(ReceivedCmd)] #Store original interval OrigInterval = JANUS_CONFIG.Config.INTERVAL #Change configuration interval JANUS_CONFIG.Config.INTERVAL = NewInterval if (int(JANUS_CONFIG.Config.INTERVAL) < 1 or int(JANUS_CONFIG.Config.INTERVAL) > 86400): JANUS_SER.sendUART("Interval not in range (1 - 86400).\r\n") res = sendSMS("Interval out of range (1 - 86400) : " + JANUS_CONFIG.Config.INTERVAL,SMSInfo.OriginatingPN,ATC.properties.CMD_TERMINATOR,3,180) #Return the configuration to the original state JANUS_CONFIG.Config.INTERVAL = OrigInterval tmpReturn = -2 else: JANUS_SER.sendUART("Report Interval : " + JANUS_CONFIG.Config.INTERVAL + "\r\n") res = sendSMS("Report Interval : " + JANUS_CONFIG.Config.INTERVAL,SMSInfo.OriginatingPN,ATC.properties.CMD_TERMINATOR,3,180) tmpReturn = 0 elif (ReceivedCmd == 'IGNITIONFOLLOW = TRUE'): JANUS_CONFIG.Config.IGNITIONFOLLOW = 'TRUE' JANUS_SER.sendUART("Ignition Follow : " + JANUS_CONFIG.Config.IGNITIONFOLLOW + "\r\n") res = sendSMS("Ignition Follow : " + JANUS_CONFIG.Config.IGNITIONFOLLOW,SMSInfo.OriginatingPN,ATC.properties.CMD_TERMINATOR,3,180) tmpReturn = 0 elif (ReceivedCmd == 'IGNITIONFOLLOW = FALSE'): JANUS_CONFIG.Config.IGNITIONFOLLOW = 'FALSE' JANUS_SER.sendUART("Ignition Follow : " + JANUS_CONFIG.Config.IGNITIONFOLLOW + "\r\n") res = sendSMS("Ignition Follow : " + JANUS_CONFIG.Config.IGNITIONFOLLOW,SMSInfo.OriginatingPN,ATC.properties.CMD_TERMINATOR,3,180) tmpReturn = 0 else: JANUS_SER.sendUART("Unrecognized/Unsupported Command Received: " + ReceivedCmd + "\r\n") res = sendSMS("Unrecognized/Unsupported Command Received: " + ReceivedCmd,SMSInfo.OriginatingPN,ATC.properties.CMD_TERMINATOR,3,180) tmpReturn = -2 #Did we timeout or get an ERROR during the SMS sending? if (res.find("timeOut")!=-1 or res.find("ERROR") != -1): tmpReturn = -4 #If we sucessfully changed the configuration amd sent the message, update and save the configuration file. if (tmpReturn == 0): res = JANUS_CONFIG.UpdateConfig() if (res == -1): JANUS_SER.sendUART("Configuration Save Error.\r\n\r\n") res = sendSMS("Configuration Save Error.",SMSInfo.OriginatingPN,ATC.properties.CMD_TERMINATOR,3,180) return else: JANUS_SER.sendUART("Configuration file updated.\r\n\r\n") elif (StatusQuery != -1): #Status query for the module, pass back the current configuration values and location #This is the first elif to catch the STATUS check without accidentally triggering the general AT check JANUS_SER.sendUART("Status Query :\r\n") #The following are available through the included GPS module: #GPS.getActualPosition(), returns all fields like AT$GPSACP would #GPS.getLastGGA() #GPS.getLastGLL() #GPS.getLastGSA() #GPS.getLastGSV() #GPS.getLastRMC() #GPS.getLastVTG() #GPS.getPosition(), this gives LAT/LONG in numeric format #For the purposes of this demo, GLL will be used CurrentLocation = GPS.getLastRMC() QueryResponse = str("Unit: " + ATC.properties.IMEI+ "\r\n" + "Switch Reporting: " + JANUS_CONFIG.Config.NOSWITCH + "\r\n" + "Ignition Reporting: " + JANUS_CONFIG.Config.IGNITION + "\r\n" + "Status LED: " + JANUS_CONFIG.Config.SLED + "\r\n" + "User LED: " + JANUS_CONFIG.Config.ULED + "\r\n" + "Auto ON: " + JANUS_CONFIG.Config.AUTOON + "\r\n" + "Report Interval: " + JANUS_CONFIG.Config.INTERVAL + "\r\n" + "Ignition Follow: " + JANUS_CONFIG.Config.IGNITIONFOLLOW + "\r\n" + "Current Location: " + CurrentLocation + "\r\n") JANUS_SER.sendUART(QueryResponse) res = sendSMS(QueryResponse,SMSInfo.OriginatingPN,ATC.properties.CMD_TERMINATOR,3,180) tmpReturn = 0 #Did we timeout or get an ERROR during the SMS sending? if (res.find("timeOut")!=-1 or res.find("ERROR") != -1): tmpReturn = -4 elif (ATCMD != -1): #AT command found, execute the command and pass back the response to the main program. #Using this instead of the sendatcommand method because it doesn't parse possibly useful information in an open ended response. #res = MDM.send(inStr, 0) #res = MDM.sendbyte(0x0d, 0) #Grab the response of the command in it's entirety #ATCMDResponse = ATC.mdmResponse(ATC.properties.CMD_TERMINATOR, 10) ATCMDResponse = ATC.sendAtCmd(inStr ,ATC.properties.CMD_TERMINATOR,3,2) #Pass it to the UART and also send an SMS with the response. JANUS_SER.sendUART(ATCMDResponse + "\r\n") res = sendSMS(ATCMDResponse,SMSInfo.OriginatingPN,ATC.properties.CMD_TERMINATOR,3,180) tmpReturn = 0 #Did we timeout or get an ERROR during the SMS sending? if (res.find("timeOut")!=-1 or res.find("ERROR") != -1): tmpReturn = -4 #Did we get an ERROR from the AT command? if (ATCMDResponse.find("ERROR") != -1): tmpReturn = -2 else: #Unrecognized SMS JANUS_SER.sendUART("Unrecognized/Unsupported SMS Received: " + inStr + "\r\n") tmpReturn = -3 res = sendSMS("Unrecognized/Unsupported SMS Received: " + inStr,SMSInfo.OriginatingPN,ATC.properties.CMD_TERMINATOR,3,180) tmpReturn = 0 #Did we timeout or get an ERROR during the SMS sending? Otherwise just return the -3 if (res.find("timeOut")!=-1 or res.find("ERROR") != -1): tmpReturn = -4 except: printException("SMSCommand()") return tmpReturn
def main(): try: # Set Global Watchdog timeout in Seconds MOD.watchdogEnable(300) #Initialize the serial interface @ 115200, we must do this or nothing comes out the serial port res = JANUS_SER.init("115200",'8N1') if (res == -1): return JANUS_SER.sendUART("Beginning the Terminus Tracker Demo Program. \r\n\r\n") #Initialize the configuration, returns 0 for defaults, 1 for normal. ConfigLoad = JANUS_CONFIG.init() #Transpose the configuration list to myApp for usage here #We transpose only the main Config class to handle updates if (ConfigLoad == 0): myApp = JANUS_CONFIG.Config JANUS_SER.sendUART("Defaults Loaded.\r\n") elif (ConfigLoad == 1): myApp = JANUS_CONFIG.Config JANUS_SER.sendUART("Configuration File Loaded.\r\n") else: myApp = JANUS_CONFIG.Config JANUS_SER.sendUART("Configuration ERROR. Defaults Loaded.\r\n") #Initialize the I/O, turn on the stat LED res = JANUS_IO.init(myApp.SLED) if (res == -1): return #Read if Auto-On is active or not, 1 is active, 0 is inactive. res = JANUS_IO.AutoONControl('READ', myApp.AUTOON) JANUS_SER.sendUART("\r\nAuto On: " + myApp.AUTOON + "\r\n") #If Auto-on is OFF and we want it ON, set it. if (res == 0 and myApp.AUTOON == 'ON'): res = JANUS_IO.AutoONControl('SET', myApp.AUTOON) if (res == -1): return #Errored out elif (res == -2): JANUS_SER.sendUART("Timed out while waiting for MCU response. \r\n") elif (res == 1): JANUS_SER.sendUART("Auto ON Enabled. \r\n") #If Auto-on is ON and we want it OFF, set it. if (res == 1 and myApp.AUTOON == 'OFF'): res = JANUS_IO.AutoONControl('SET', myApp.AUTOON) if (res == -1): return #Errored out elif (res == -2): JANUS_SER.sendUART("Timed out while waiting for MCU response. \r\n") elif (res == 0): JANUS_SER.sendUART("Auto ON Disabled. \r\n") #If Auto-on is OFF, and we have it set as OFF. Let's see what caused this wake up and report it. #Although we can read IGN/SW directly, we check the MCU instead because they #May not be active as this point even though the MCU caught it. WakeupCause = '' if (res == 0 and myApp.AUTOON == 'OFF'): res = JANUS_IO.SW_IGN_Status() if (res == -1): return #Errored out elif (res == 0): JANUS_SER.sendUART("Wake up cause: N/O Switch \r\n") WakeupCause = 'Switch' elif (res == 1): JANUS_SER.sendUART("Wake up cause: Ignition \r\n") WakeupCause = 'Ignition' elif (res == 2): JANUS_SER.sendUART("Wake up cause: Both the Ignition and N/O Switch \r\n") WakeupCause = 'Both' elif (res == -2): JANUS_SER.sendUART("Wake up cause: Unknown \r\n") WakeupCause = 'Unknown' JANUS_SER.sendUART("\r\nInitializing Module GPRS. \r\n") #Set Network specific settings, wait for SIM card to be ready res = NETWORK.initGsmNetwork(myApp.NETWORK,myApp.BAND) if (res == -1): return #Init GPRS GPRS.init('1',myApp.APN) JANUS_SER.sendUART("GPRS Initialized. \r\n") ################################################################ #### BEGIN Newly Added Config Stuff ################################################################ #Initalize GPS JANUS_SER.sendUART("\r\nInitializing Module GPS. \r\n") res = JANUS_GPS.init(myApp.LNA) if (res != 0): JANUS_SER.sendUART("Failed to Initialize GPS, ERROR: " + res + "\r\n\r\n") return JANUS_SER.sendUART("GPS Initialized. \r\n\r\n") #Setup SMS if (myApp.SMS_ENABLED == 'TRUE'): JANUS_SER.sendUART("SMS Enabled, Initializing. \r\n") JANUS_SMS.configSMS() JANUS_SER.sendUART("SMS Initialized. \r\n\r\n") ################################################################ #### END Newly Added Config Stuff ################################################################ # Loop forever, without this loop the script would run once and exit script mode. On reboot or power-on the script would run once more while (1): MOD.watchdogReset() JANUS_SER.sendUART("Registering Module. \r\n") #Wait until module is registered to GSM Network res = NETWORK.isGsmRegistered(180) #Wait 180 seconds for module to obtain GSM registration if (res == -1): return #Wait until module is attached to GPRS res = NETWORK.isGprsAttached(180) #Wait 180 seconds for module to obtain GPRS Attachment if (res == -1): return JANUS_SER.sendUART("Module Registered Successfully. \r\n\r\n") ############################################################################################################################# ## Opening Socket Connection to Server ## We are opening in Command Mode to have the best control ############################################################################################################################# #Update Unit information res = ATC.getUnitInfo() # Start timeout timer timerB = timers.timer(0) timerB.start(int(myApp.INTERVAL)) SwitchPos = 'Open' #Default State for Switch IgnPos = 'Inactive' #Default State for Switch FirstTimeThrough = 'TRUE' #Initialize flag for the first time running the connection/loop SENDSTRING = [] #Initialize the string list being sent to the server StringCount = 0 # Loop forever, without this loop the script would run once and exit script mode. On reboot or power-on the script would run once more while (1): while (1): MOD.watchdogReset() #Update NMEA Data #The following are available through the included GPS module: #GPS.getActualPosition(), returns all fields like AT$GPSACP would #GPS.getLastGGA() #GPS.getLastGLL() #GPS.getLastGSA() #GPS.getLastGSV() #GPS.getLastRMC() #GPS.getLastVTG() #GPS.getPosition(), this gives LAT/LONG in numeric format #For the purposes of this demo, RMC will be used #The returned value gives a \r\n at the end so we must strip it for proper usage. GPSPositionTemp = '' GPSPositionTemp = GPS.getLastRMC() GPSPositionTemp = GPSPositionTemp.rstrip() #Update switch and ignition information #We simply clear after the report is sent, so by default it's sent as Open/Inactive until it's read as the opposite. #We're polling the MCU signals and then signaling back "we got the information" so we only read once, giving us one or both events to report # 0: Pass, Switch is triggered # 1: Pass, Ignition is triggered # 2: Pass, BOTH are triggered # -1: Exception # -2: Pass, Neither is triggered EventCheck = JANUS_IO.SW_IGN_Status() if (EventCheck == 0): #Switch has been triggered SwitchPos = 'Closed' #JANUS_SER.sendUART("SwitchPos : " + SwitchPos + "\r\n") elif (EventCheck == 1): #Ignition has been triggered IgnPos = 'Active' #JANUS_SER.sendUART("IgnPos : " + IgnPos + "\r\n") elif (EventCheck == 2): #Both have been triggered SwitchPos = 'Closed' IgnPos = 'Active' #JANUS_SER.sendUART("IgnPos : " + IgnPos + "\r\n") #JANUS_SER.sendUART("SwitchPos : " + SwitchPos + "\r\n") if (myApp.SMS_ENABLED == 'TRUE'): res = JANUS_SMS.CheckNewSMS() #If a new SMS is found and is valid, pass it to the command logic if (res != '0'): #We have received a new SMS, let's find what it wants us to do. #JANUS_SER.sendUART("SMS Data : " + str(res) + "\r\n") res = JANUS_SMS.SMSCommand(str(res)) # 0: Pass, action carried out and SMS sent back # -1: Exception # -2: Pass, Unrecognized change command or AT command received though # -3: Unrecognized SMS # -4: Error sending an SMS to the originating P/N if (res == -1): return #If the STAT LED was updated via SMS, let's adjust it res = JANUS_IO.Cellular_LED(myApp.SLED) #AUTO ON Run time Change. #Read current Auto-On status, 1 is active, 0 is inactive. res = JANUS_IO.AutoONControl('READ', myApp.AUTOON) #The below will be ignored if there is no change #If Auto-on is OFF and we want it ON, set it. if (res == 0 and myApp.AUTOON == 'ON'): res = JANUS_IO.AutoONControl('SET', myApp.AUTOON) if (res == -1): return #Errored out elif (res == -2): JANUS_SER.sendUART("Timed out while waiting for MCU response. \r\n") elif (res == 1): JANUS_SER.sendUART("Auto ON Enabled. \r\n") #If Auto-on is ON and we want it OFF, set it. if (res == 1 and myApp.AUTOON == 'OFF'): res = JANUS_IO.AutoONControl('SET', myApp.AUTOON) if (res == -1): return #Errored out elif (res == -2): JANUS_SER.sendUART("Timed out while waiting for MCU response. \r\n") elif (res == 0): JANUS_SER.sendUART("Auto ON Disabled. \r\n") elif (res == '-1'): #Exception return #If interval timer expires then send packet to server if (timerB.isexpired() or FirstTimeThrough == 'TRUE'): #The first time we drop into this loop we want to send data immediately, after that it's timer based sends. FirstTimeThrough = 'FALSE' #Disable the flag ####################### ##BEGIN BUILDING STRING #If we were woken up by one of the inputs, add it to the string to be sent #This section can be used/altered to perhaps only display the wake up event for a certain amount of sends. if (WakeupCause != ''): WakeupString = WakeupCause #JANUS_SER.sendUART("Wake up Event : " + WakeupCause + "\r\n") #res = GPRS.send_CM("Wake up Event : " + WakeupCause + "\r\n",1,10) #WakeupCause = '' #Clear the cause so it only gets reported the first time else: WakeupString = '' #Build String to send to customer server, adjusts depending on what we want reported. Can only be one of these. #CW Google Earth format #STA = NMEA + ',' + IMEI + ',' + String1 + ',' + String2 + ',' + String3 #Strings 1/2/3 should remain in all sentences sent, but become null ('') when not being sent. #The Strings follow a standard display format for the demo, so only send the actual information based on this format: #String 1 will display in the Demo as "Switch" #String 2 will display in the Demo as "Ignition" #String 3 will display in the Demo as "Wake up Event" if (myApp.NOSWITCH == 'TRUE' and myApp.IGNITION == 'TRUE'): STA = GPSPositionTemp + ',' + ATC.properties.IMEI + ',' + SwitchPos + ',' + IgnPos + ',' + WakeupString elif (myApp.NOSWITCH == 'TRUE' and myApp.IGNITION == 'FALSE'): STA = GPSPositionTemp + ',' + ATC.properties.IMEI + ',' + SwitchPos + ',' + "" + ',' + WakeupString elif (myApp.NOSWITCH == 'FALSE' and myApp.IGNITION == 'TRUE'): STA = GPSPositionTemp + ',' + ATC.properties.IMEI + ',' + "" + ',' + IgnPos + ',' + WakeupString elif (myApp.NOSWITCH == 'FALSE' and myApp.IGNITION == 'FALSE'): STA = GPSPositionTemp + ',' + ATC.properties.IMEI + ',' + "" + ',' + "" + ',' + WakeupString #Concatenate string, this allows store and forward during disconnects. #STA is refreshed every time through, SENDSTRING is only cleared after a successful send #Let's say 100B per string #Max string length is 16kB, giving us (safely) 100 data points to store. #This can be improved upon by utilizing a list, since each element in a list is capable of 16kB #with a possible 4000 elements (keeping in mind overall memory limits) if (StringCount < 100): SENDSTRING.append(STA) StringCount = StringCount+1 else: JANUS_SER.sendUART("Store and forward limit reached (100). \r\n\r\n") ####################### ##END BUILDING STRING ############################################################### ##### Socket Open Check #If socket closed, open it DCD = MDM.getDCD() #Check for a valid SS too res = ATC.sendAtCmd('AT#SS',ATC.properties.CMD_TERMINATOR,0,20) #If there is not a valid DCD OR the socket status shows that there is no socket open. #We check for both because DCD seems to get stuck on 1 under certain conditions and we cannot set it, yet SS works fine. if (DCD == 0 or res == "#SS: 1,0"): JANUS_SER.sendUART("Opening socket to server: " + myApp.IP + ":" + myApp.PORT + "\r\n") #Connect to customer's server res = GPRS.openSocket(myApp.IP,myApp.PORT,1,myApp.USERNAME,myApp.PASSWORD,myApp.PROTOCOL,1) if (res != 0): JANUS_SER.sendUART("Connection failed to open. \r\n") #Turn OFF the LED permanently until we have a valid connection res = JANUS_IO.GPS_LED('OFF', myApp.ULED) elif (res == 0): JANUS_SER.sendUART("Socket opened successfully.\r\n") #Turn ON the LED to show we have a valid connection res = JANUS_IO.GPS_LED('ON', myApp.ULED) JANUS_SER.sendUART("Polling GPS receiver for current location every " + myApp.INTERVAL + " second(s)\r\n") #Do not return, end of this loop will handle the service checking ############################################################### ##### Socket Open Check try: #If socket open upload data DCD = MDM.getDCD() #Check for a valid SS too res = ATC.sendAtCmd('AT#SS',ATC.properties.CMD_TERMINATOR,0,20) #If there is a valid DCD AND the socket status shows that there is a valid socket connection too. #We check for both because DCD seems to get stuck on 1 under certain conditions and we cannot set it, yet SS works fine. if (DCD == 1 and res != "#SS: 1,0"): #Update the GPS LED. This will turn the LED on/off during run time if it has been updated via SMS. #Defaulted to ON res = JANUS_IO.GPS_LED('ON', myApp.ULED) #####Placeholder for built string OLD spot #Strip the last entry of the pipe #Pop the last item out, remove the last byte (the pipe) then append it back to the list. #This covers single sends and block sends ## LastItem = SENDSTRING.pop(StringCount-1) ## ItemLength = len(LastItem) ## LastItem = LastItem[0:ItemLength-1] ## SENDSTRING.append(LastItem) JANUS_SER.sendUART("Sending data: \r\n") res = 0 x = 0 while (res == 0 and x < StringCount): #Send Data JANUS_SER.sendUART(str(SENDSTRING[x]) + "\r\n") res = GPRS.send_CM(SENDSTRING[x],1,10) x = x+1 if (res == 1): JANUS_SER.sendUART("\r\n\r\nData Empty, Error receiving info from GPS module\r\n") return if (res == -2): JANUS_SER.sendUART("\r\n\r\nTimed out while sending data, checking socket connection\r\n") #Do not return, drop to service checks #Add the pipe to the end again since this got disconnected mid-send procedure and will become a store and forward send. ## LastItem = SENDSTRING.pop(StringCount-1) ## LastItem = LastItem + '|' ## SENDSTRING.append(LastItem) else: JANUS_SER.sendUART("\r\n\r\nData Sent Successfully.\r\n\r\n") #Blink OFF the LED to indicate data was sent res = JANUS_IO.GPS_LED('OFF', myApp.ULED) res = JANUS_IO.GPS_LED('ON', myApp.ULED) if (IgnPos == 'Inactive' and myApp.AUTOON == 'OFF' and myApp.IGNITIONFOLLOW == 'TRUE'): #Special Case, auto-on is not active and we want to only be active while the ignition is active. #Inigition has dropped, we've sent the last data packet, now shut the unit down res = ATC.sendAtCmd('AT#SHDN',ATC.properties.CMD_TERMINATOR,0,20) #Put the Switch and Ignition I/O back to default, clear the list and counter SwitchPos = 'Open' IgnPos = 'Inactive' SENDSTRING = [] StringCount = 0 #Exit data mode #DEBUG.sendMsg('Exiting data mode\r\n') #res = ATC.exitSocketDataMode() #Close Socket #Pass in: sockNum #res = ATC.closeSocket('1') #DEBUG.sendMsg('Connection closed\r\n') break else: JANUS_SER.sendUART("\r\nConnection not available, checking status.\r\n") #Wait until module is registered to GSM Network #res = NETWORK.isGsmRegistered(180) #Wait 180 seconds for module to obtain GSM registration #if (res == -1): # ATC.reboot() #Wait until module is attached to GPRS #res = NETWORK.isGprsAttached(180) #Wait 180 seconds for module to obtain GPRS Attachment #if (res == -1): # ATC.reboot() #What is the signal strength? res = ATC.sendAtCmd('AT+CSQ',ATC.properties.CMD_TERMINATOR,0,5) JANUS_SER.sendUART("Signal Strength (AT+CSQ): " + res + "\r\n") #Still registered? res = ATC.sendAtCmd('AT+CREG?',ATC.properties.CMD_TERMINATOR,0,5) JANUS_SER.sendUART("Registration Check (AT+CREG?): " + res + "\r\n") #GPRS Available? res = ATC.sendAtCmd('AT+CGREG?',ATC.properties.CMD_TERMINATOR,0,5) JANUS_SER.sendUART("GPRS Availability (AT+CGREG?): " + res + "\r\n") #Is a PDP context activated? res = ATC.sendAtCmd('AT#SGACT?',ATC.properties.CMD_TERMINATOR,0,20) JANUS_SER.sendUART("PDP Context status (AT#SGACT?): " + res + "\r\n\r\n") break except: JANUS_SER.sendUART("Script encountered an exception while uploading data to server\r\n") JANUS_SER.sendUART("Exception Type: " + str(sys.exc_type) + "\r\n") JANUS_SER.sendUART("MODULE -> LobosTrack\r\n") break ## Re-Start timeout timer timerB = timers.timer(0) timerB.start(int(myApp.INTERVAL)) #DEBUG.CLS() #Clear screen command for VT100 terminals except: print "Script encountered an exception" print "Exception Type: " + str(sys.exc_type) print "MODULE -> TerminusS2E" return
#DEBUG.CLS() #Clear screen command for VT100 terminals except: print "Script encountered an exception" print "Exception Type: " + str(sys.exc_type) print "MODULE -> TerminusS2E" return ##-------------------------------------------------------------------------------------------------------------------- ## Main Function ##-------------------------------------------------------------------------------------------------------------------- try: main() print "Main Script Exit" #Reboot or Script will not restart until a power cycle occurs ATC.reboot() except: print "Main Script encountered an exception" print "Exception Type: " + str(sys.exc_type) print "MODULE -> LobosTrack" #Reboot or Script will not restart until a power cycle occurs ATC.reboot()
def main(): try: # Set Global Watchdog timeout in Seconds MOD.watchdogEnable(300) res = JANUS_SER.init("115200",'8N1') if (res == -1): return #Set Network specific settings res = NETWORK.initGsmNetwork(myApp.NETWORK,myApp.BAND) if (res == -1): return #Init GPRS GPRS.init('1',myApp.APN) #Inform Application that a Data Connection is not available JANUS_SER.set_DCD(0) # Loop forever, without this loop the script would run once and exit script mode. On reboot or power-on the script would run once more while (1): MOD.watchdogReset() #Wait until module is registered to GSM Network res = NETWORK.isGsmRegistered(180) #Wait 180 seconds for module to obtain GSM registration if (res == -1): return #Wait until module is attached to GPRS res = NETWORK.isGprsAttached(180) #Wait 180 seconds for module to obtain GPRS Attachment if (res == -1): return ############################################################################################################################# ## Opening Socket Connection to Server ############################################################################################################################# res = GPRS.openSocket(myApp.IP,myApp.PORT,1,myApp.USERNAME,myApp.PASSWORD,myApp.PROTOCOL,0) #Inform Application that a Data Connection is not available DCD = MDM.getDCD() if (DCD == 1): JANUS_SER.set_DCD(1) #Forward CONNECT Message to Serial Port JANUS_SER.sendUART('\r\nCONNECT\r\n') ##Loop while Socket connected while(1): MOD.watchdogReset() #Forward serial port data to socket connection DCD = MDM.getDCD() if (DCD == 1): #Get data from serial port res = JANUS_SER.readUART() if (len(res)!=0): #Forward data to open socket res = MDM.send(res,1) #Forward socket data to serial port DCD = MDM.getDCD() if (DCD == 1): #Get data from open socket connection res = MDM.receive(1) if (len(res)!=0): #Forward socket data to serial port JANUS_SER.sendUART(res) #When socket is closed exit loop via this path #Will guarantee that '\r\nNO CARRIER\r\n' is sent every time DCD = MDM.getDCD() if (DCD == 0): #Inform Application that a Data Connection is not available JANUS_SER.set_DCD(0) ATC.delaySec(1) #Get any remaining data from closed socket connection res = MDM.receive(1) if (len(res)!=0): #Forward socket data to serial port JANUS_SER.sendUART(res) break except: print "Script encountered an exception" print "Exception Type: " + str(sys.exc_type) print "MODULE -> TerminusS2E" return
def CheckNewSMS(): # This function checks for a new SMS. If one is found it parses the information. # Input Parameter Definitions # None # Returns: # String: Returns the received SMS # 0: Pass, no SMS received # -1: Exception tmpReturn = '-1' try: #Now try to list all newly received SMS. #Using this method because the sendatcommand method ends up parsing out info we need due to the multiple \r\n response. res = MDM.send('AT+CMGL="REC UNREAD"', 0) res = MDM.sendbyte(0x0d, 0) #Grab the response of the command res = ATC.mdmResponse('OK', 10) #Try to list all currently stored SMS's. This is just a check to see if we have old/already read messages. res2 = MDM.send('AT+CMGL="ALL"', 0) res2 = MDM.sendbyte(0x0d, 0) #Grab the response of the command res2 = ATC.mdmResponse('OK', 10) #Check for messages, search from 0 to the length of the AT Command response pos0 = res.find('+CMGL: 1',0,len(res)) GeneralCheck = res2.find('+CMGL:',0,len(res2)) if (pos0 != -1): #New message found, let's parse the useful information #first, let's split the received information so we can echo a response to the originating phone number #Below is the normal response we are parsing #+CMGL: <index>,<stat>,<oa/da>,<alpha>,<scts><CR><LF><data><CR><LF> #+CMGL: 1,"REC UNREAD","+xxxxxxxxxxx","test","12/06/06,15:59:50-20" #Data #Separate by \r\n to separate the SMS data parts1 = res.split('\r\n') #Now split by comma to get individual data from the main chunk of information parts2 = parts1[1].split(",") SMSInfo.index = parts2[0] SMSInfo.stat = parts2[1] SMSInfo.OriginatingPN = parts2[2] SMSInfo.alpha = parts2[3] SMSInfo.date = parts2[4] SMSInfo.time = parts2[5] SMSInfo.SMS = parts1[2] #Delete ALL SMS to ensure a clear buffer for the next read res = ATC.sendAtCmd('AT+CMGD=1,4' ,ATC.properties.CMD_TERMINATOR,3,2) return SMSInfo.SMS tmpReturn = '0' #Enter this AT command to ensure the buffer is empty and ready to receive new SMS messages if (GeneralCheck != -1): res = ATC.sendAtCmd('AT+CMGD=1,4' ,ATC.properties.CMD_TERMINATOR,3,2) except: printException("CheckNewSMS") return tmpReturn