def sendAT(self, atcom, atres, trys = 1, timeout = 1, csd = 1): mdm_timeout = int(self.config.get('TIMEOUT_MDM')) s = MDM2.receive(mdm_timeout) result = -2 while(1): if(csd): self.tryCSD() if(self.config.get('DEBUG_AT') == '1'): self.debug.send('AT OUT: ' + atcom) s = '' MDM2.send(atcom, mdm_timeout) timer = MOD.secCounter() + timeout while(1): s = s + MDM2.receive(mdm_timeout) if(s.find(atres) != -1): result = 0 break if(s.find('ERROR') != -1): result = -1 break if(MOD.secCounter() > timer): break if(self.config.get('DEBUG_AT') == '1'): self.debug.send('AT IN: ' + s[2:]) trys = trys - 1 if((trys <= 0) or (result == 0)): break MOD.sleep(15) return (result, s)
def sendData(PIN,PO,HO,A,GU,GP,data,ll): b = SER.send('\r\nIn sendData...sending\r\n' + data + '\r\n') b = SER.send('\r\nHO=\r\n') b = SER.send(HO) b = SER.send('\r\npO=\r\n') b = SER.send(PO) b = SER.send('\r\n') a = openGPRS(PIN,A,GU,GP) a = openSD(HO) res = MDM2.send('POST /' + PO + ' HTTP/1.1 Connection: close\r\n', 0) res = MDM2.send('HOST: ' + HO + '\r\n', 0) res = MDM2.send('User-Agent: Terminal Connect\r\n', 0) res = MDM2.send('Content-Type: application/x-www-form-urlencoded\r\n', 0) res = MDM2.send('Content-Length: '+ str(ll) +'\r\n\r\n', 0) res = MDM2.send(data, 0) res = MDM2.send('\r\n\r\n', 0) cnt = 20 res = MDM2.receive(20) a = SER.send('\r\nResponse from server ------------------------\r\n') while ( (res.find('EOT') == -1) and (cnt > 0) ): a = SER.send(res) res = MDM2.receive(20) cnt = cnt - 1 a = SER.send(res) res = closeCon() a = SER.send('\r\nend -----------------------------------------\r\n') return res
def sendAT(self, atcom, atres, trys=1, timeout=1, csd=1): mdm_timeout = int(self.config.get('TIMEOUT_MDM')) s = MDM2.receive(mdm_timeout) result = -2 while (1): if (csd): self.tryCSD() if (self.config.get('DEBUG_AT') == '1'): self.debug.send('AT OUT: ' + atcom) s = '' MDM2.send(atcom, mdm_timeout) timer = MOD.secCounter() + timeout while (1): s = s + MDM2.receive(mdm_timeout) if (s.find(atres) != -1): result = 0 break if (s.find('ERROR') != -1): result = -1 break if (MOD.secCounter() > timer): break if (self.config.get('DEBUG_AT') == '1'): self.debug.send('AT IN: ' + s[2:]) trys = trys - 1 if ((trys <= 0) or (result == 0)): break MOD.sleep(15) return (result, s)
def checkCon(pin): a = SER.send('DEBUG: Now in checkCon...\r\n') #print '\r\ncheckCon PIN:',pin res = MDM2.send('AT+CREG?\r',0) res = MDM2.receive(30) b = SER.send('DEBUG: following is result from AT+CREG?\r\n') c = SER.send(res); if ( (res.find('0,1') > -1) or (res.find('0,5') > -1 ) ): return 1 ret = '' ret = setPin(pin) if ( ret != 1 ): GPIO.setIOdir(19,1,1) return -1 timer = MOD.secCounter() + 120 while ( (res.find('0,1') == -1) and (res.find('0,5') == -1 ) ): res = MDM2.send('AT+CREG?\r',0) res = MDM2.receive(20) b = SER.send('DEBUG: following is result from AT+CREG?\r\n') c = SER.send(res); if ( MOD.secCounter() > timer ): return -1 MOD.sleep(50) MOD.sleep(20) return 1
def checkRing(): data = MDM2.receive(5) pos = data.find('+CLIP') if(pos != -1): data = data[pos + 6:].strip() tokens = data.split(',') if(len(tokens) > 0): number = tokens[0].replace('"', '') return number return ''
def closeCon(): res = MDM2.send('+++\r', 10) timer = MOD.secCounter() + 30 # Antwort +++ auswerten while (MOD.secCounter() < timer): res = MDM2.receive(10) if ((res.find('OK') >= 0) ): timer = timer - 100 MOD.sleep(10) res = MDM2.send('AT#SH=1\r', 0) timer = MOD.secCounter() + 30 # answere AT#SD while (MOD.secCounter() < timer): res = MDM2.receive(10) if ((res.find('OK') >= 0) ): timer = timer - 100 MOD.sleep(10) # print '\r\ncloseCon =',res return res
def setPin(pin): rr = '' tt = MOD.secCounter() + 30 while ( rr.find('READY') == -1): rr = MDM2.send('AT+CPIN?\r',0) # CPIN Status rr = MDM2.receive(5) if ( rr.find('SIM PIN') != -1 ): bb = MDM2.send('AT+CPIN=' + pin + '\r',0) # set PIN MOD.sleep(10) # wait 1sec if ( MOD.secCounter() > tt ): return -1 MOD.sleep(10) return 1
def openSD(H): res = MDM2.send('AT#SD=1,0,80,' + H +',0\r', 0) timer = MOD.secCounter() + 30 while (MOD.secCounter() < timer): res = MDM2.receive(10) a = SER.send('\r\nDEBUG: Back in openGPRS just did AT#SD=1,0,80....\r\n') a = SER.send(H) a = SER.send(res) if ((res.find('CONNECT') >= 0) ): timer = timer - 100 b = SER.send('\r\nsuccessfull connect, returning from openSD\r\n') return 1 MOD.sleep(10) return -1
def openGPRS(P,A,GU,GP): #PIN = CONFIG.config['PIN_SIM'],H = CONFIG.config['HOST'],A = CONFIG.config['APN'] a = SER.send('\r\nDEBUG: Now in openGPRS.\r\n') ret = checkCon(P) a = SER.send('\r\nDEBUG: Back in openGPRS after doing checkCon.\r\n') if ( ret != 1 ): return -1 a = SER.send('\r\nDEBUG: Back in openGPRS about to do AT#SGACT?.\r\n') res = MDM2.send('AT#SGACT?\r', 2) timer = MOD.secCounter() + 20 while (MOD.secCounter() < timer): res = MDM2.receive(10) a = SER.send('DEBUG: Back in openGPRS just did AT#SGACT?.\r\n') a = SER.send(res) if ((res.find(',0') >= 0) ): timer = timer - 100 if ((res.find(',1') >= 0) ): timer = timer - 100 #print 'GPRS stil active\r' return 1 MOD.sleep(10) res = MDM2.send('AT+CGDCONT=1,"IP","' + A + '","0.0.0.0",1,1\r', 0) timer = MOD.secCounter() + 30 # check answer CGDCONT while (MOD.secCounter() < timer): res = MDM2.receive(10) a = SER.send('\r\nDEBUG: Back in openGPRS just did AT#CGDCONT?.\r\n') a = SER.send(res) if ((res.find('OK') >= 0) ): timer = timer - 100 MOD.sleep(10) if (GU != ''): print 'set GPRS username\r' res = MDM2.send('AT#USERID="'+ GU + '"\r', 2) res = MDM2.receive(20) res = res.find ('OK') if (res == -1): a = SER.send('\r\nerror setting GPRS username\r\n') if (GP != ''): print 'set GPRS password\r' res = MDM2.send('AT#PASSW="'+ GP + '"\r', 2) res = MDM2.receive(20) res = res.find ('OK') if (res == -1): a = SER.send('\r\nerror setting GPRS password\r\n') res = MDM2.send('AT#SGACT=1,1\r', 1) timer = MOD.secCounter() + 50 while (MOD.secCounter() < timer): res = MDM2.receive(10) a = SER.send('\r\nDEBUG: Back in openGPRS just did AT#SGACT=1.\r\n') a = SER.send(res) if (res.find('#SGACT:') >= 0): timer = timer - 100 res = cleanCRLF(res) res = res.replace('#SGACT:','') res = res.replace('OK','') a = SER.send('\r\nConnected with IP:' + str(res)) return 1 MOD.sleep(10) return -1