Exemplo n.º 1
0
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
Exemplo n.º 2
0
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