Example #1
0
    def send_sms(self, msg, number):
        start_time = time.time()
        try:
            pdulist = PduDecoder.encodeSmsSubmitPdu(number, msg)
        except Exception as e:
            print(e)
            print("Error in pdu conversion. Skipping message sending")
            return -1

        parts = len(pdulist)
        count = 1
        for pdu in pdulist:
            a = ''
            now = time.time()
            temp_pdu = self.formatPDUtoSIM800(str(pdu))
            preamble = "AT+CMGS=" + str(pdu.tpduLength)
            self.gsm.write(str.encode(preamble + "\r"))
            now = time.time()
            while (a.find('>') < 0 and a.find("ERROR") < 0 and time.time() <
                   now + int(self.defaults['GSM_DEFAULT_SETTINGS']
                             ['SEND_INITIATE_REPLY_TIMEOUT'])):
                a = a + self.gsm.read(self.gsm.inWaiting()).decode('utf-8')
                time.sleep(
                    float(self.defaults['GSM_DEFAULT_SETTINGS']
                          ['WAIT_FOR_BYTES_DELAY']))
                print('.', end=" ")

            if (time.time() > now + int(self.defaults['GSM_DEFAULT_SETTINGS']
                                        ['SEND_INITIATE_REPLY_TIMEOUT'])
                    or a.find("ERROR") > -1):
                print('>> Error: GSM Unresponsive at finding >')
                return -1
            else:
                print('>', end=" ")

            a = ''
            now = time.time()
            self.gsm.write(str.encode(str(temp_pdu) + chr(26)))
            while (a.find('OK') < 0 and a.find("ERROR") < 0
                   and time.time() < now +
                   int(self.defaults['GSM_DEFAULT_SETTINGS']['REPLY_TIMEOUT'])
                   ):
                a += self.gsm.read(self.gsm.inWaiting()).decode('utf-8')
                time.sleep(
                    float(self.defaults['GSM_DEFAULT_SETTINGS']
                          ['WAIT_FOR_BYTES_DELAY']))
                print('-', end=" ")

            if time.time() - int(self.defaults['GSM_DEFAULT_SETTINGS']
                                 ['SENDING_REPLY_TIMEOUT']) > now:
                print('>> Error: timeout reached')
                return -1
            elif a.find('ERROR') > -1:
                print('>> Error: GSM reported ERROR in SMS reading')
                return -1
            else:
                print("Sending execution time:", (time.time() - start_time))
                print(">> Part %d/%d: Message sent!" % (count, parts))
                count += 1
        return 0
Example #2
0
    def send(self, message, number):
        if not self.pdu_mode:
            self.set_pdu_mode(enabled=True)
        pdu_messages = encodeSmsSubmitPdu(number,
                                          message,
                                          requestStatusReport=False)
        part = 1
        for pdu in pdu_messages:
            print('Part %d' % part)
            self.write('AT+CMGS={0}'.format(pdu.tpduLength))

            before = time.time()

            lines = self.ser.read(4)
            #lines = self.read_time(4)
            #lines = self.read_loop()
            #lines = self.ser.readline()
            #lines += self.ser.readline()
            # print('CMGS response: ', lines)

            # print('Ready for data, waited %d seconds for %d bytes' % (time.time()-before, len(lines)))
            if not lines.strip() == b'>':
                print('Invalid response: ', lines)
                lines += self.ser.readline()
                print(lines)
                return
            elif not lines:
                # if b'> ' not in lines:
                # lines = self.ser.read(4)
                print('Not ready, try again')
                print('CMGS response: ', lines)
                lines = self.read_time(4)
                if b'> ' in lines:
                    print(
                        'Ready for data try 2, waited %d seconds for %d bytes'
                        % (time.time() - before, len(lines)))
                    pass
                else:
                    # reset modem?
                    # AF+CFUN=1
                    # self.write('AT', verify=True)
                    raise ValueError(
                        'Data prompt not received in %d seconds: %s' %
                        (time.time() - before, lines))

            data_bytes = self.ser.write(str(pdu).encode())

            # time.sleep(10)

            # print('ctrl+z')
            self.ser.write(b'\x1a')  # CTRL+Z ASCII 26

            # lines = self.ser.read(19)
            # lines = self.read_time(19)
            before = time.time()
            lines = self.read_loop()
            print('Data sent, waited %d seconds for %d bytes' %
                  (time.time() - before, len(lines)))

            if not lines:
                # print('No data, sleep 1 second')
                print('No data, retry read')
                # time.sleep(1)
                #lines = self.ser.read(19)
                lines = self.read_time(19)
                if not lines:
                    raise ValueError('No data received in two attempts')

            if 'OK' not in lines:
                print(lines)
                raise ValueError('OK not found in response part %d' % part)
            else:
                matches = re.search(r'CMGS: ([0-9]+)', str(lines))
                if matches:
                    print('Message part %d sent with id %s' %
                          (part, matches.group(1)))
                else:
                    print('CMGS not found: ', lines)

            part += 1
Example #3
0
    def send_msg(self, msg, number, simulate=False):
        """
        Sends a command 'cmd' to GSM Module
        Returns the reply of the module
        Usage: str = gsm_cmd()
        """
        # under development
        # return
        # simulate sending success
        # False => sucess
        try:
            pdulist = PduDecoder.encodeSmsSubmitPdu(number, msg, 0, None)
            temp = pdulist[0]
        except:
            print("Error in pdu conversion. Skipping message sending")
            return -1

        temp_pdu = "0001000C813"+str(pdulist[0])[
            11:]
        pdulist[0] = temp_pdu

        parts = len(str(pdulist[0])[2:])
        count = 1
                
        for pdu in pdulist:
            a = ''
            now = time.time()
            preamble = "AT+CMGS=%d" % (pdu.length)

            self.gsm.write(preamble+"\r")
            now = time.time()
            while (a.find('>') < 0 and a.find("ERROR") < 0 and 
                time.time() < now + self.SEND_INITIATE_REPLY_TIMEOUT):
                a += self.gsm.read(self.gsm.inWaiting())
                time.sleep(self.WAIT_FOR_BYTES_DELAY)
                print ('.',)

            if (time.time() > now + self.SEND_INITIATE_REPLY_TIMEOUT or 
                a.find("ERROR") > -1):  
                print ('>> Error: GSM Unresponsive at finding >')
                print (a)
                return -1
            else:
                print ('>',)
            
            a = ''
            now = time.time()
            self.gsm.write(pdu.pdu+chr(26))
            while (a.find('OK') < 0 and a.find("ERROR") < 0 and 
                time.time() < now + self.REPLY_TIMEOUT):
                    a += self.gsm.read(self.gsm.inWaiting())
                    time.sleep(self.WAIT_FOR_BYTES_DELAY)
                    print (':',)

            if time.time() - self.SENDING_REPLY_TIMEOUT > now:
                print ('>> Error: timeout reached')
                return -1
            elif a.find('ERROR') > -1:
                print ('>> Error: GSM reported ERROR in SMS reading')
                return -1
            else:
                print (">> Part %d/%d: Message sent!" % (count,parts))
                count += 1
                    
        return 0
Example #4
0
    def send_sms(self, msg, number):
        try:
            pdulist = PduDecoder.encodeSmsSubmitPdu(number, msg, 0, None)
            temp = pdulist[0]
        except:
            print("Error in pdu conversion. Skipping message sending")
            return -1

        # SET CONSTANT
        temp_pdu = self.defaults['GSM_DEFAULT_SETTINGS']['PDU_HEADER'] + str(
            pdulist[0])[11:]
        pdulist[0] = temp_pdu

        parts = len(str(pdulist[0])[2:])
        count = 1

        for pdu in pdulist:
            a = ''
            now = time.time()
            preamble = "AT+CMGS=" + str(int(parts / 2))
            self.gsm.write(str.encode(preamble + "\r"))
            now = time.time()
            while (a.find('>') < 0 and a.find("ERROR") < 0 and time.time() <
                   now + int(self.defaults['GSM_DEFAULT_SETTINGS']
                             ['SEND_INITIATE_REPLY_TIMEOUT'])):
                a = a + self.gsm.read(self.gsm.inWaiting()).decode('utf-8')
                time.sleep(
                    float(self.defaults['GSM_DEFAULT_SETTINGS']
                          ['WAIT_FOR_BYTES_DELAY']))
                print('.', end=" ")

            if (time.time() > now + int(self.defaults['GSM_DEFAULT_SETTINGS']
                                        ['SEND_INITIATE_REPLY_TIMEOUT'])
                    or a.find("ERROR") > -1):
                print('>> Error: GSM Unresponsive at finding >')
                print(a)
                return -1
            else:
                print('>', end=" ")

            a = ''
            now = time.time()
            self.gsm.write(str.encode(pdu + chr(26)))
            while (a.find('OK') < 0 and a.find("ERROR") < 0
                   and time.time() < now +
                   int(self.defaults['GSM_DEFAULT_SETTINGS']['REPLY_TIMEOUT'])
                   ):
                a += self.gsm.read(self.gsm.inWaiting()).decode('utf-8')
                time.sleep(
                    float(self.defaults['GSM_DEFAULT_SETTINGS']
                          ['WAIT_FOR_BYTES_DELAY']))
                print(':', end=" ")

            if time.time() - int(self.defaults['GSM_DEFAULT_SETTINGS']
                                 ['SENDING_REPLY_TIMEOUT']) > now:
                print('>> Error: timeout reached')
                return -1
            elif a.find('ERROR') > -1:
                print('>> Error: GSM reported ERROR in SMS reading')
                return -1
            else:
                print(">> Part %d/%d: Message sent!" % (count, parts))
                count += 1
        return 0