UART with GSM SIM800L

Questions and discussion about The WiPy 1.0 board and CC3200 boards.
Target audience: Users with a WiPy 1.0 or CC3200 board.
Gabi
Posts: 18
Joined: Sat Mar 05, 2016 3:29 pm

UART with GSM SIM800L

Post by Gabi » Sat Sep 10, 2016 3:48 pm

Hello guys (and girls),
I am currently trying to work with a GSM module: the SIM800L (http://wiki.seeedstudio.com/images/4/46 ... _V1.00.pdf)

I connected my UART 1 Pins to the RX and TX of the board but I could not get anything from the board: when I send an ATI message, I get nothing back.

My code is:

Code: Select all

from machine import UART, Pin

TERMINATION_CHAR = '\x1a'

TXD_PIN = 'GP16'
RXD_PIN = 'GP17'
RST_PIN = 'GP22'

RST = Pin(RST_PIN, mode=Pin.OUT)
RST.value(0)
uart = UART(1, baudrate=115200, pins=(TXD_PIN, RXD_PIN))
RST.value(1)
And the output of the commands I send are:

Code: Select all

>>> uart.write('ATI\r\n')
5
>>> uart.any()
0
>>> uart.readall()
>>> 
So, it seems I sent 5 bytes but get nothing in return while I should have something...

Could someone help me with that? Am I initializing the UART correctly? Might it be an issue with the SIM card (I am not sure it is activated yet, that's why I only sent the ATI message)

Thank you :)

User avatar
dhylands
Posts: 3415
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: UART with GSM SIM800L

Post by dhylands » Sat Sep 10, 2016 5:10 pm

Did you connect TX from the UART to RX on the SIM800? And RX from the UART to Tx on the SIM800? And also connect grounds beteween the two devices?

Gabi
Posts: 18
Joined: Sat Mar 05, 2016 3:29 pm

Re: UART with GSM SIM800L

Post by Gabi » Sat Sep 10, 2016 9:21 pm

dhylands wrote:Did you connect TX from the UART to RX on the SIM800? And RX from the UART to Tx on the SIM800? And also connect grounds beteween the two devices?
Hi :)
Yep, I have linked every pins from the WiPy to the GSM board: the GND and the 5v (from an USB cable that I have customize) (I have the LED flashing) and the TXD and RXD to the PIN 16 and 17 and the RESET to the Pin 22.

It might be related to an issue with the SIM card itself...I'll try to put the SIM card into a phone to be sure it is working.

If you have any other guess, let me know, I'll try them :)

User avatar
Roberthh
Posts: 2052
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: UART with GSM SIM800L

Post by Roberthh » Sun Sep 11, 2016 6:40 am

Hello @gabi, If you think that the connection is right (TX(Wipy) to RX(GSM), RX(Wipy) -> TX(GSM), GND to GND), the I would first verify that WiPY can send and receive. The simplest method would be to just disconnect GP16 and 17 from the modem and join GP16 and GP17. Then you should receive back what you just have sent.
Some more thoughts:
- Are you sure that 115200 is the right speed?
- From my modem times I recall, that some of them need just CR as first character for auto-baud detection.

Gabi
Posts: 18
Joined: Sat Mar 05, 2016 3:29 pm

Re: UART with GSM SIM800L

Post by Gabi » Sun Sep 11, 2016 12:30 pm

Roberthh wrote:Hello @gabi, If you think that the connection is right (TX(Wipy) to RX(GSM), RX(Wipy) -> TX(GSM), GND to GND), the I would first verify that WiPY can send and receive. The simplest method would be to just disconnect GP16 and 17 from the modem and join GP16 and GP17. Then you should receive back what you just have sent.
Some more thoughts:
- Are you sure that 115200 is the right speed?
- From my modem times I recall, that some of them need just CR as first character for auto-baud detection.
Hi Roberthh,
Thank you for the tips to test the UART.
When I did it, I have what you said:

Code: Select all

>>> uart.write('ATI\r\n')
5
>>> uart.any()
5
>>> uart.readall()
b'ATI\r\n'
>>> 
So the UARTs pins are OK and work correctly.

For the baudrate, on page 30 of the docs, it says:
Serial port supports the following baud rates:
1200, 2400, 4800, 9600, 19200, 38400, 57600 and 115200bps

Autobauding only supports the following baud rates:
1200, 2400, 4800, 9600, 19200, 38400 and 57600bps
The default setting is autobauding.

So maybe if I use 57600 or lower as speed it might do a difference? And as it is the default mode I do not think I have to send any first message (I'll try it anyway ^^)

Anyway, I'll try with 9600 :)

Thanks!

Gabi
Posts: 18
Joined: Sat Mar 05, 2016 3:29 pm

Re: UART with GSM SIM800L

Post by Gabi » Sun Sep 11, 2016 12:49 pm

Sorry for the double post :?

So, I tested what you suggested Roberthh and it did not work at first.

Then I try to switch the TXD and RXD pins...and it worked...now, I know what you think but I checked again and my pins were correctly linked to the RXD and TXD of the board, at least the pins wrote as TXD and RXD.

So, it seems that the pin of the board are switched...

Then I test with a baudrate of 9600 and be sending a CR message before the ATI command at it worked!

I tried again with 115200 and without the CR message at first, it do not work but with it, it is working correctly (I think I should add some delay before reading the UART after the command):

Code: Select all

>>> from machine import UART, Pin
>>> 
>>> TERMINATION_CHAR = '\x1a'
>>> 
>>> TXD_PIN = 'GP16'
>>> RXD_PIN = 'GP17'
>>> RST_PIN = 'GP22'
>>> #TXD = Pin(TXD_PIN, mode=Pin.OUT)
>>> #RXD = Pin(RXD_PIN)
>>> RST = Pin(RST_PIN, mode=Pin.OUT)
>>> RST.value(0)
>>> uart = UART(1, baudrate=115200, pins=(TXD_PIN, RXD_PIN))
>>> RST.value(1)
>>> uart.write('\r\n')
2
>>> uart.write('ATI\r\n')
5
>>> uart.any()
0
>>> uart.readall()
b'ATI\r\r\nSIM800 R14.18\r\n\r\nOK\r\n'
>>> 
Here

Code: Select all

uart.any()
return 0 that is why I think adding delay would be better and I think that with the delay I can maybe avoid sending the first CR message.

Anyway, I'll continue and I'll let you know my feedback :)

Thakn you!

HStro
Posts: 6
Joined: Wed May 17, 2017 7:04 pm

Re: UART with GSM SIM800L

Post by HStro » Wed May 17, 2017 7:22 pm

Hi Gabi,

I am using the SIM800l with a pyboard. I would be interested to hear what your experience was like with the SIM800l, as I am finding it to be very hit and miss when sending commands and trying to use it for internet. Sometimes it works, but sometimes it returns ERROR. I have tested all of the code by connecting the GSM to my computer and communicating that way. When I do this, it works every time...

Also, I hooked it up to a 5v terminal and as it requires I think between 3.7V - 4.7v, 5v causes it to power down due to over-voltage. Did this happen to you?

Thanks for the help! :D
(if necessary I'd be happy to open another thread!)

Harry

sarusso
Posts: 9
Joined: Fri Dec 09, 2016 9:49 am

Re: UART with GSM SIM800L

Post by sarusso » Thu Feb 13, 2020 3:02 pm

To anyone still interested in using the SIM800L from MicroPython, here is a pure-MicroPython driver ready to use. You can use it as-is or just see how to do things in the right way. Happy coding! :)

https://github.com/pythings/Drivers/blo ... SIM800L.py

terry24q
Posts: 3
Joined: Wed Mar 20, 2019 8:14 pm

Re: UART with GSM SIM800L

Post by terry24q » Fri Feb 14, 2020 2:01 pm

sarusso wrote:
Thu Feb 13, 2020 3:02 pm
To anyone still interested in using the SIM800L from MicroPython, here is a pure-MicroPython driver ready to use. You can use it as-is or just see how to do things in the right way. Happy coding! :)

https://github.com/pythings/Drivers/blo ... SIM800L.py
Hey sarusso, Sort of a general uPython question as you appear to be very accomplished and I'm not. I don't have one of these modules yet, just doing some research; Where does this driver go? Is it loaded on to the device? Or is it imported from somewhere else at runtime (displaying my ignorance). Just trying to learn.

sarusso
Posts: 9
Joined: Fri Dec 09, 2016 9:49 am

Re: UART with GSM SIM800L

Post by sarusso » Tue Feb 18, 2020 2:50 pm

Warning: this post refers to this specific code commit: https://github.com/pythings/Drivers/tre ... 90745504a9, since the driver is evolving if you just browse the repo at the latest version things won't match (but are similar).

When you flash MicroPython, you get a) a MicroPython interpreter and b) a filesystem (unless in some special cases where there is not enough flash memory, i.e. on the ESP01 with 512 Kb since 1 MB is required for the filesystem to work).

Let's take for granted that you have a+b. At this point you can connect with a serial console and use tools like ampy (https://github.com/scientifichackers/ampy) which make it easy to list, put and get files. MicroPython creates by default an empty-ish file named boot.py which gets executed at every boot.

Example serial console interaction:

Code: Select all

>>> 2+2
4
>>>
Example ampy output after a fresh MicroPython Install on esp32:

Code: Select all

$ ampy -p /dev/cu.SLAB_USBtoUART ls
/boot.py
You can then change the pins on which the SIM800L is connected at line 412 and the APN at line 428 of SIM800L.py (with respect to commit 865216c on the repo I linked) and put the driver on the board. Note that you need to manually change the pins and the the APN in the driver code only to make the "example_usage" function to work, when you use the driver in the "proper" way, which is to instantiate a Modem class from your scripts, the pins and the APN are parametrised (as you can see from the "example_usage" function at line 403 itself)

Code: Select all

$ ampy -p /dev/cu.SLAB_USBtoUART put SIM800L.py
$ ampy -p /dev/cu.SLAB_USBtoUART ls
/SIM800L.py
/boot.py
and now, two options:

1) Connect via a serial console to your board (I assume you are familiar with this, if not it is the first thing you need to get used to after flashing the firmware) and try/test the module/driver from there. Example:

Code: Select all

>>> import SIM800L
>>> SIM800L.example_usage()
Starting up...

Modem IP address: "191.219.151.34"

Now running demo http GET...
Response status code: 200
Response content: <html><head><title>Current IP Check</title></head><body>Current IP Address: 
191.219.151.34</body></html>

Now running demo https POST...
Response status code: 200
Response content: {"args":{},"data":{"myparameter":42},"files":{},"form":{},"headers":{"x-forwarded-proto":
"https","host":"postman-echo.com","content-length":"19","accept":"*/*","content-type":"application/json",
"user-agent":"SIMCOM_MODULE","x-forwarded-port":"443"},"json":{"myparameter":42},"url":"https://postman
-echo.com/post"}
>>>
2) Edit the boot.py, or create your own scripts to be imported from boot.py, and use this driver from there, so that when the board powers on your application logic gets automatically executed. The most simple thing you can do is to copy-paste the "example_usage" function code in the boot.py script (and add a "from SIM800L import Modem" at the top), then from there should be simple to evolve the code according to your needs

There is more than this of course, for example you can bake (or "freeze") python modules as this one in the MicroPython firmware at firmare built-time to have them both ready and to use way less memory (not flash, memory), but if you are a newcomer you really don't want to start with this as the build chain is quite complex to setup.

Warning: this post refers to this specific code commit: https://github.com/pythings/Drivers/tre ... 90745504a9, since the driver is evolving if you just browse the repo at the latest version things won't match (but are similar).
Last edited by sarusso on Wed Feb 19, 2020 5:09 pm, edited 1 time in total.

Post Reply