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 ## 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 ## SLED ## ULED try: rtnList = [-1,-1] #[return status,return data] # return status: # 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 #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 == 'SLED = ON'): mySER.sendUART("Cellular LED : ON \r\n\r\n") rtnList = IO_HE910.Cellular_LED('ON') rtnList = sendSMS("Cellular LED : ON",SMSInfo.OriginatingPN,ATC.properties.CMD_TERMINATOR,3,180) rtnList[0] = 0 elif (ReceivedCmd == 'SLED = OFF'): mySER.sendUART("Cellular LED : OFF \r\n\r\n") rtnList = IO_HE910.Cellular_LED('OFF') rtnList = sendSMS("Cellular LED : OFF",SMSInfo.OriginatingPN,ATC.properties.CMD_TERMINATOR,3,180) rtnList[0] = 0 elif (ReceivedCmd == 'ULED = ON'): mySER.sendUART("User LED : ON \r\n\r\n") rtnList = IO_HE910.USER_LED('ON') rtnList = sendSMS("User LED : ON",SMSInfo.OriginatingPN,ATC.properties.CMD_TERMINATOR,3,180) rtnList[0] = 0 elif (ReceivedCmd == 'ULED = OFF'): mySER.sendUART("User LED : OFF \r\n\r\n") rtnList = IO_HE910.USER_LED('OFF') rtnList = sendSMS("User LED : OFF",SMSInfo.OriginatingPN,ATC.properties.CMD_TERMINATOR,3,180) rtnList[0] = 0 else: mySER.sendUART("Unrecognized/Unsupported Command Received: " + ReceivedCmd + "\r\n\r\n") rtnList = sendSMS("Unrecognized Command Received: " + ReceivedCmd,SMSInfo.OriginatingPN,ATC.properties.CMD_TERMINATOR,3,180) rtnList[0] = -2 #Did we timeout or get an ERROR during the SMS sending? if (rtnList[1].find("timeOut")!=-1 or rtnList[1].find("ERROR") != -1): rtnList[0] = -4 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 mySER.sendUART("Status Query :\r\n") #Check the CELL LED Status to report rtnList = ATC.sendAtCmd('AT#GPIO=1,2' ,ATC.properties.CMD_TERMINATOR,5) #GPIO: 2,0 if (rtnList[1].find('#GPIO: 2') != -1): CELL_LED = 'ON' else: CELL_LED = 'OFF' #Check the GPS/USER LED Status to report rtnList = ATC.sendAtCmd('AT#GPIO=2,2' ,ATC.properties.CMD_TERMINATOR,5) #GPIO: 2,0 if (rtnList[1].find('#GPIO: 1,1') != -1): USER_LED = 'ON' else: USER_LED = 'OFF' #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 CurrentLocation = GPS.getLastRMC() QueryResponse = str("Unit: " + ATC.properties.IMEI+ "\r\n" + "Status LED: " + CELL_LED + "\r\n" + "USER LED: " + USER_LED + "\r\n" + "Current Location: " + CurrentLocation + "\r\n") mySER.sendUART(QueryResponse) rtnList = sendSMS(QueryResponse,SMSInfo.OriginatingPN,ATC.properties.CMD_TERMINATOR,3,180) rtnList[0] = 0 #Did we timeout or get an ERROR during the SMS sending? if (rtnList[1].find("timeOut")!=-1 or rtnList[1].find("ERROR") != -1): rtnList[0] = -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,5) #Did we get an ERROR from the AT command? if (ATCMDResponse[1].find("ERROR") != -1): rtnList[0] = -2 #Pass it to the UART and also send an SMS with the response. mySER.sendUART(ATCMDResponse[1] + "\r\n\r\n") rtnList = sendSMS(ATCMDResponse[1],SMSInfo.OriginatingPN,ATC.properties.CMD_TERMINATOR,3,180) rtnList[0] = 0 #Did we timeout or get an ERROR during the SMS sending? if (rtnList[1].find("timeOut")!=-1 or rtnList[1].find("ERROR") != -1): rtnList[0] = -4 else: #Unrecognized SMS mySER.sendUART("Unrecognized/Unsupported Command Received: " + inStr + "\r\n\r\n") rtnList = sendSMS("Unrecognized/Unsupported SMS Received: " + inStr,SMSInfo.OriginatingPN,ATC.properties.CMD_TERMINATOR,3,180) rtnList[0] = -3 #Did we timeout or get an ERROR during the SMS sending? Otherwise just return the -3 if (rtnList[1].find("timeOut")!=-1 or rtnList[1].find("ERROR") != -1): rtnList[0] = -4 except: print sys.exc_info() rtnList[0] = -1 return rtnList
def main(): try: # Set Global Watchdog timeout in Seconds MOD.watchdogEnable(300) ####################################### ## Initialization ####################################### rtnList = [-1,-1] #[return status,return data] # return status: # -1: Exception occurred # 0: No errors occurred, no return data # 1: No errors occurred, return data # 2: Error occurred #Initialize the serial interface @ 115200, we must do this or nothing comes out the serial port rtnList = mySER.init("115200",'8N1') if (rtnList[0] == -1): return mySER.sendUART("Beginning the serial bridge program. \r\n\r\n") # Set Global Watchdog timeout in Seconds #MOD.watchdogEnable(300) #Get configuration from demo.conf file, transpose into new local myApp class myApp = conf.conf('/sys','demo.conf') if myApp.CONF_STATUS != 0: mySER.sendUART("Demo configuration error: " + str(myApp.CONF_STATUS)) return rtnList mySER.sendUART("Configuration Loaded.\r\n") rtnList = myIO.init() if (rtnList[0] == -1) or (rtnList[0] == -2) or rtnList[1] == "ERROR": raise UserWarning mySER.sendUART("\r\nInitializing Network Setup. \r\n") #Set Network specific settings, wait for SIM card to be ready rtnList = NETWORK.initNetwork(myApp.ENS) if (rtnList[0] == -1) or (rtnList[0] == -2): raise UserWarning #Initialize SOCKET communications rtnList = SOCKET.init('1',myApp.APN) if (rtnList[0] == -1) or (rtnList[0] == -2): raise UserWarning mySER.sendUART("Network Setup Initialized. \r\n\r\n") #Update Unit information rtnList = ATC.getUnitInfo() if (rtnList[0] == -1): raise UserWarning # 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() ####################################### ## Registration ####################################### RegCheck = 0 #Initialize check DataCheck = 0 #Initialize Check #What is the signal strength? rtnList = ATC.sendAtCmd('AT+CSQ',ATC.properties.CMD_TERMINATOR,5) if (rtnList[0] == -1) or (rtnList[0] == -2): raise UserWarning mySER.sendUART("Signal Strength (AT+CSQ): " + rtnList[1] + "\r\n") #Wait until module is registered to GSM Network mySER.sendUART("Checking Modem Registration. \r\n") rtnList = NETWORK.isRegistered(180) #Wait 180 seconds for module to obtain GSM registration if (rtnList[0] == -1) or (rtnList[0] == -2): raise UserWarning if (rtnList[0] == 0): #Registered successfully RegCheck = 1 mySER.sendUART("Modem Registered. \r\n\r\n") #Wait until module is ready for data mySER.sendUART("Checking Data Availability. \r\n") rtnList = NETWORK.isDataAttached(myApp.CGMM, 180) #Wait 180 seconds for module to obtain GSM registration if (rtnList[0] == -1) or (rtnList[0] == -2): raise UserWarning if (rtnList[0] == 0): #Registered successfully DataCheck = 1 mySER.sendUART("Modem ready for data. \r\n\r\n") #check for exit prompt, let's use command EXITSCR rtnList = mySER.readUART() if (rtnList[1].find('EXITSCR') != -1): print '\r\nExit Command Found\r\n' return ####################################### ## Socket Connection ####################################### # Loop while we're registered/ready while (RegCheck == 1 and DataCheck == 1): #check for exit prompt, let's use command EXITSCR rtnList = mySER.readUART() if (rtnList[1].find('EXITSCR') != -1): print '\r\nExit Command Found\r\n' return rtnList = NETWORK.isRegistered(10) #Fast reg check if (rtnList[0] == -1) or (rtnList[0] == -2): #unavailable, drill down discretely and show the status RegCheck = 0 rtnList = ATC.sendAtCmd('AT+CREG?',ATC.properties.CMD_TERMINATOR,5) mySER.sendUART("Registration Unavailable, Status: " + rtnList[1] + "\r\n") break #exit loop rtnList = NETWORK.isDataAttached(myApp.CGMM, 10) #Fast data check if (rtnList[0] == -1) or (rtnList[0] == -2): #unavailable, drill down discretely and show the status DataCheck = 0 rtnList = ATC.sendAtCmd('AT+CGREG?',ATC.properties.CMD_TERMINATOR,5) mySER.sendUART("Data Unavailable, Status: " + rtnList[1] + "\r\n") break #exit loop #Open socket in online mode mySER.sendUART("Opening socket to server: " + myApp.IP + ":" + myApp.PORT + "\r\n") rtnList = SOCKET.openSocket(myApp.IP,myApp.PORT,1,myApp.USERNAME,myApp.PASSWORD,myApp.PROTOCOL,0) if (rtnList[0] == -1) or (rtnList[0] == -2): raise UserWarning #Check for open connection, this catches both online and command mode operations if (rtnList[1] == "OK" or rtnList[1] == "CONNECT"): mySER.sendUART("Socket opened successfully.\r\n") else: mySER.sendUART("Connection failed to open, trying again. \r\n") #Check for open socket DCD = MDM.getDCD() if (DCD == 1): #Set DCD on USIF0 SER.setDCD(1) #Forward CONNECT Message to Serial Port mySER.sendUART('\r\nConnected to Server\r\n') myIO.USER_LED('ON') ####################################### ## Data exchange loop ####################################### ##Loop while Socket connected. This loop operates on the notion that we are in online mode, so we are not using send_CM while(1): #Pet the watchdog MOD.watchdogReset() #Check for DCD active on MDM DCD = MDM.getDCD() if (DCD == 1): #Outbound #Get data from serial port, use function return rtnList = mySER.readUART() if (len(rtnList[1])!=0): #Forward data to open socket if it's not empty and not the exit script command #check for exit prompt, let's use command EXITSCR if (rtnList[1].find('EXITSCR') != -1): #exit found print '\r\nExit Command Found\r\n' print 'Exiting data mode\r\n' rtnList = SOCKET.exitDataMode() print 'Closing the socket\r\n' rtnList = SOCKET.closeSocket(1) return print rtnList[1] #Debug res = MDM.send(rtnList[1],1) #Inbound #Get data from open socket connection, straight MDM read res = MDM.read() if (len(res)!=0): #Forward socket data to serial port print res #Debug mySER.sendUART(res) #When socket is closed exit loop via this path #Will guarantee that '\r\nNO CARRIER\r\n' is sent every time if (DCD == 0): #Clear DCD on USIF0 SER.setDCD(0) myIO.USER_LED('OFF') #Get any remaining data from closed socket connection, straight MDM read res = MDM.read() if (len(res)!=0): #Forward socket data to serial port mySER.sendUART(res) break #exit loop to registration checks except UserWarning: print 'Controlled Script exit' except: print sys.exc_info() rtnList[0] = -1 return
def main(): try: rtnList = [-1, -1] # [return status,return data] # return status: # -1: Exception occurred # 0: No errors occurred, no return data # 1: No errors occurred, return data # 2: Error occurred # Initialize the serial interface @ 115200, we must do this or nothing comes out the serial port rtnList = mySER.init("115200", "8N1") if rtnList[0] == -1: return mySER.sendUART("Beginning the T2 SMS Query Program. \r\n\r\n") # Set Global Watchdog timeout in Seconds # MOD.watchdogEnable(300) # Get configuration from demoT2.conf file, transpose into new local myApp class myApp = conf.conf("/sys", "demoT2.conf") if myApp.CONF_STATUS != 0: mySER.sendUART("DemoT2 configuration error: " + str(myApp.CONF_STATUS)) return rtnList mySER.sendUART("Configuration Loaded.\r\n") rtnList = myIO.init() if (rtnList[0] == -1) or (rtnList[0] == -2) or rtnList[1] == "ERROR": raise UserWarning mySER.sendUART("\r\nInitializing Network Setup. \r\n") # Set Network specific settings, wait for SIM card to be ready rtnList = NETWORK.initNetwork(myApp.ENS) if (rtnList[0] == -1) or (rtnList[0] == -2) or rtnList[1] == "ERROR": raise UserWarning # Initialize SOCKET communications rtnList = SOCKET.init("1", myApp.APN) if (rtnList[0] == -1) or (rtnList[0] == -2) or rtnList[1] == "ERROR": raise UserWarning mySER.sendUART("Network Setup Initialized. \r\n\r\n") mySER.sendUART("Initializing GPS \r\n") # Turn the GPS ON rtnList[1] = GPS.getPowerOnOff() while rtnList[1] != 1: GPS.powerOnOff(1) rtnList[1] = GPS.getPowerOnOff() mySER.sendUART("GPS Status: " + str(rtnList[1]) + "\r\n") mySER.sendUART("GPS Initialized. \r\n\r\n") mySER.sendUART("Initializing SMS. \r\n") # Setup SMS rtnList = mySMS.configSMS() if rtnList[0] == 0: mySER.sendUART("SMS Initialized. \r\n\r\n") else: return # Update Unit information rtnList = ATC.getUnitInfo() if (rtnList[0] == -1) or rtnList[1] == "ERROR": raise UserWarning # 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() RegCheck = 0 # Initialize check mySER.sendUART("Checking Modem Registration. \r\n") # Wait until module is registered to GSM Network rtnList = NETWORK.isRegistered(180) # Wait 180 seconds for module to obtain GSM registration if (rtnList[0] == -1) or (rtnList[0] == -2) or rtnList[1] == "ERROR": raise UserWarning if rtnList[0] == 0: RegCheck = 1 mySER.sendUART("Modem Registered. Waiting for SMS. \r\n\r\n") # 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 RegCheck == 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. # sleep for 1 second to let the GPS catch up otherwise we eventually get a response that .rstrip errors on time.sleep(1) GPSPositionTemp = "" GPSPositionTemp = GPS.getLastRMC() GPSPositionTemp = GPSPositionTemp.rstrip() rtnList = mySMS.CheckNewSMS() # If a new SMS is found and is valid, pass it to the command logic if rtnList[0] == 1: # We have received a new SMS, let's find what it wants us to do. mySER.sendUART("SMS Received.\r\n") mySER.sendUART("SMS Data : " + str(rtnList[1]) + "\r\n") rtnList = mySMS.SMSCommand(rtnList[1]) # 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 rtnList[0] == -1: return rtnList = NETWORK.isRegistered(10) # Check Registration on the fly if (rtnList[0] == -1) or (rtnList[0] == -2): RegCheck = 0 mySER.sendUART("\r\nRegistration not available, checking status.\r\n") # What is the signal strength? rtnList = ATC.sendAtCmd("AT+CSQ", ATC.properties.CMD_TERMINATOR, 0, 5) mySER.sendUART("Signal Strength (AT+CSQ): " + rtnList[1] + "\r\n") # Still registered? rtnList = ATC.sendAtCmd("AT+CREG?", ATC.properties.CMD_TERMINATOR, 0, 5) mySER.sendUART("Registration Check (AT+CREG?): " + rtnList[1] + "\r\n") break except UserWarning: print "Controlled Script exit" except: print sys.exc_info() rtnList[0] = -1 return