Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't send SMS #4

Open
git-oliver opened this issue Dec 23, 2020 · 10 comments
Open

Can't send SMS #4

git-oliver opened this issue Dec 23, 2020 · 10 comments

Comments

@git-oliver
Copy link

git-oliver commented Dec 23, 2020

Hello,

I try to send an sms with the python code below. I use the "SIM800L GPRS GSM Module" [1] with 5V. I have double checked the connection and have one GND only.
I tried it with python 2.7.16 and 3.7.3

I always get an ERROR on "send_sms"...
What am I'm doing wrong ?? I get the same error, even if I remove the "sim800l.setup()".

from sim800l import SIM800L

sim800l=SIM800L('/dev/serial0')
sms="Hello there"

sim800l.setup()

ret = sim800l.send_sms('valid_number',sms)
print("ret sms = "+ret)

[1] https://www.google.com/search?q=SIM800L+GPRS+GSM+Module&client=firefox-b-d&tbm=isch&source=iu&ictx=1&fir=eSmeUzNvPs3r2M%252Cak_jUtfyMKpvRM%252C_&vet=1&usg=AI4_-kSwRDCnyhMak3Mgerc_miL-RAXoTA&sa=X&ved=2ahUKEwipov-9v-TtAhXDKewKHfUhC0sQ9QF6BAgKEAE#imgrc=eSmeUzNvPs3r2M

@jakhax
Copy link
Owner

jakhax commented Dec 23, 2020

Hi @git-oliver

Can you share your logs including the full error messages you get.

Also confirm that you have followed the setup instructions in the doc, including removing the Pin as the script does not support that and disabling serial console.

Also from the link you shared seems like you are using a sim800l module that does not have a voltage regulator , i wouldnt advice using 5v, see here https://lastminuteengineers.com/sim800l-gsm-module-arduino-tutorial/#supplying-power-for-sim800l-module

@git-oliver
Copy link
Author

So first I changed the voltage to 4V. Thats recommended.
I don't understand completely you instruction doc. I have disabled the serial ports (0,1) (over raspi-config) and activate the serial0 device
Now I have the following inital state: (serial0 is linked to ttyS0)

pi@raspberrypi:/dev $ ls -la ttyA* ttyS* serial*
lrwxrwxrwx 1 root root          5 Dez 23 20:04 serial0 -> ttyS0
lrwxrwxrwx 1 root root          7 Dez 23 20:03 serial1 -> ttyAMA0
crw-rw---- 1 root dialout 204, 64 Dez 23 20:04 ttyAMA0
crw-rw---- 1 root dialout   4, 64 Dez 23 20:10 ttyS0
pi@raspberrypi:/dev $ 

Is this inital state correct ??

If I now run the python code

from sim800l import SIM800L

sim800l=SIM800L('/dev/serial0')
sms="Hello there"

sim800l.setup()      # No different if I remove this

ret = sim800l.send_sms('XXXXXXX',sms)#valid number is given
print("ret sms = "+ret)

with python 2.7.16 or 3.7.3 the output is

pi@raspberrypi:~/raspberry-pi-sim800l-gsm-module $ sudo python send.py 
ret sms = ERROR
pi@raspberrypi:~/raspberry-pi-sim800l-gsm-module $

The ERROR comes from file sim800l.py in line 77- 83:

    77	    def send_sms(self,destno,msgtext):
    78	        result = self.command('AT+CMGS="{}"\n'.format(destno),99,5000,msgtext+'\x1A')
    79	        if result and result=='>' and self.savbuf:
    80	            params = self.savbuf.split(':')
    81	            if params[0]=='+CUSD' or params[0] == '+CMGS':
    82	                return 'OK'
    83	        return 'ERROR'

Somehow the if-Statement (line 79) failed...

@jakhax
Copy link
Owner

jakhax commented Dec 24, 2020

This is my output after disabling serial console

pi@raspberrypi:/dev $ ls -la ttyA* ttyS* serial*
lrwxrwxrwx 1 root root          7 Dec 22 03:17 serial0 -> ttyAMA0
lrwxrwxrwx 1 root root          5 Dec 22 03:17 serial1 -> ttyS0
crw-rw---- 1 root dialout 204, 64 Dec 22 03:17 ttyAMA0
crw-rw---- 1 root dialout   4, 64 Dec 22 03:17 ttyS0

Make sure after disabling serial console this is the output

 The serial login shell is disabled 
The serial interface is enabled 
<OK>

It's also important to check following

  • Make sure you connect an antennae to your SIM Module, you may be in area where the GSM signal is weak or crowded.
  • Does your network provider support 2G? in some places 2G has already been phased out (sim800l is 2G only)

Can you try to run the script(python3 script.py) below to debug what is happening, remember to change the number

import serial
import time

NUMBER = "1234567890"

s = serial.Serial("/dev/serial0", 115200)

def get_at_response(sleep = 0.5):
    #give modem enough time to reply
    time.sleep(sleep)
    r = s.read_all()
    if not r:
        print("[AT] ERR <Response Timeout>")
        return None

    r = r.decode()
    print("[AT RX]: ", r)
    return r

def write_at(cmd):
    cmd = b"AT" + cmd.encode("UTF-8") + b"\r\n"
    print("[AT TX]: ", cmd.decode())
    s.write(cmd)


def send_sms(msg):
    print("> ", msg)
    msg = msg + "\x1A"
    s.write(msg.encode())


#echo off
write_at("E0")
get_at_response()

#set error codes to verbose
write_at("+CMEE=2")
get_at_response()

#get the status of SIM presense
write_at("+CPIN?")
get_at_response()

# caller line identification
write_at("+CLIP=1")
get_at_response()

# Set preferred message format to text mode
write_at("+CMGF=1")
get_at_response()

#send sms
write_at("+CMGS=\"%s\""%NUMBER)
if get_at_response(0.1) :
    send_sms("Test")
    # you may have to increase the sleep here to depending on the signal strength
    get_at_response(60)

You should get a response like

[AT TX]:  ATE0

[AT RX]:  
OK

[AT TX]:  AT+CMEE=2

[AT RX]:  
OK

[AT TX]:  AT+CPIN?

[AT RX]:  
+CPIN: READY

OK

[AT TX]:  AT+CLIP=1

[AT RX]:  
OK

[AT TX]:  AT+CMGF=1

[AT RX]:  
OK

[AT TX]:  AT+CMGS="1234567890"

[AT RX]:  
> 
Test
[AT RX]:  

+CMGS: 134

OK

@git-oliver
Copy link
Author

Antenna and 2G GSM support are warranted.

After disabling the serial console I get the output:

pi@raspberrypi:/dev $ ls -la ttyA* ttyS* serial*
lrwxrwxrwx 1 root root          5 Dez 25 13:32 serial0 -> ttyS0
lrwxrwxrwx 1 root root          7 Dez 25 13:32 serial1 -> ttyAMA0
crw-rw---- 1 root dialout 204, 64 Dez 25 13:33 ttyAMA0
crw-rw---- 1 root dialout   4, 64 Dez 25 13:38 ttyS0
pi@raspberrypi:/dev $

My serial ports are swapped. If I change the softlinks (serial0 & serial1) as you have the script didnt produce any output.
If I run your python code the output is

pi@raspberrypi:~/raspberry-pi-sim800l-gsm-module $ python3 script.py 
[AT TX]:  ATE0

[AT RX]:  ATE0
OK

[AT TX]:  AT+CMEE=2

[AT RX]:  
OK

[AT TX]:  AT+CPIN?

[AT RX]:  
+CME ERROR: SIM busy 

[AT TX]:  AT+CLIP=1

[AT RX]:  
OK

[AT TX]:  AT+CMGF=1

[AT RX]:  
+CME ERROR: operation not allowed

[AT TX]:  AT+CMGS="0123456789" 

[AT RX]:  
+CMS ERROR: invalid input value

>  Test
Traceback (most recent call last):
  File "script.py", line 57, in <module>
    get_at_response(60)
  File "script.py", line 16, in get_at_response
    r = r.decode()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf0 in position 84: invalid continuation byte
pi@raspberrypi:~/raspberry-pi-sim800l-gsm-module $

If I run your python script multiple times I get different outputs. Another output is

pi@raspberrypi:~/raspberry-pi-sim800l-gsm-module $ python3 script.py 
[AT TX]:  ATE0

[AT RX]:  ATE0
OK

[AT TX]:  AT+CMEE=2

[AT RX]:  
OK

[AT TX]:  AT+CPIN?

[AT RX]:  
+CPIN: READY

OK

Call Ready

[AT TX]:  AT+CLIP=1

[AT RX]:  
OK

SMS Ready

[AT TX]:  AT+CMGF=1

[AT RX]:  
OK

[AT TX]:  AT+CMGS="0123456789"

[AT RX]:  
> 
>  Test
Traceback (most recent call last):
  File "script.py", line 57, in <module>
    get_at_response(60)
  File "script.py", line 16, in get_at_response
    r = r.decode()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 39: invalid start byte
pi@raspberrypi:~/raspberry-pi-sim800l-gsm-module $ 

I believe my module is the problem. I will buy a better module soon...
Or do you have more ideas ?

@jakhax
Copy link
Owner

jakhax commented Dec 25, 2020

i dont know what could be the issue, you could try printing the raw bytes without decoding, comment out this line r = r.decode() i.e #r = r.decode() to see what it contains, i also noticed i had set the baud rate to 115200 instead of 9600 in the example but i guess you already changed that.

The reason you get different inputs with the script could be

  • the modem is still connecting to the network, notice the verbose error says +CME ERROR: SIM busy
  • it may also be that the modem is still attempting to send the sms (its a blocking operation) .
  • the modem wasn't able to read \x1A (CTRL+Z) to terminate input and start sending.

You can also try to debug your modem with a microcontroller, see https://github.com/vshymanskyy/TinyGSM heres an example with esp32 but the concept is the same https://randomnerdtutorials.com/esp32-sim800l-send-text-messages-sms/ or https://lastminuteengineers.com/sim800l-gsm-module-arduino-tutorial/

@garudaonekh
Copy link

Same issue here. I can readsms but send always return "Error'. I use sim800l module. And it looks very unstable in response, for example sometimes i can read sms sometimes not. I use 5v input.

@garudaonekh
Copy link

`AT TX: ATE0

AT RX:
+CPIN: READY

OK

AT RX:

Test
AT RX:
+CMS ERROR: CP retry exceed
`

@garudaonekh
Copy link

[AT RX]:

Test

It stucks here and timeout. However, sometimes the receiving end can get the sms.

@jakhax
Copy link
Owner

jakhax commented Dec 29, 2020

It stucks here and timeout. However, sometimes the receiving end can get the sms.

hi. I have also experienced the same issue, its usually because of bad 2G connection, SIM800l only connects to 2G which is being phased out in a lot of areas. Make sure your subscriber still supports 2G, connect a 2G antenna to the module, check voltage and current requirements, low power is also a common issue for this especially when an antenna is not connected. Also if your module does not have a voltage regulator use the correct voltage to avoid damaging it or undefined behavior (like this one https://lastminuteengineers.com/sim800l-gsm-module-arduino-tutorial/#supplying-power-for-sim800l-module )

@git-oliver
Copy link
Author

Thanks for helping me.
I still cant send any sms messages. In my opinion the sim800l module without more periphery is useless.
Furthermore there is less / no documentation available for the module. So no chance to solve internal errors like device respond busy mode
I will try soon your software with a different module

Tanks for all :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants