SIM800L + UART + network module?

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
headshaker
Posts: 8
Joined: Tue Feb 18, 2020 3:22 pm

SIM800L + UART + network module?

Post by headshaker » Sat Feb 22, 2020 1:02 pm

Hi!

I'm trying to get SIM800L work at PPPos (i.e. to use it just like WIFI connection to internet).

Here is a listing of my code:

Code: Select all

from machine import UART, Pin
import network

def send_command(command):
  
  command = command + '\r\n'
  uart.write(command)
  line = uart.read(100)
  if line:
    line = line.decode('utf-8')
    print(line.replace('\r\n','\n'))
  else:
    print('Hmmm...')


uart = UART(1, 9600, timeout=2000, rx=17, tx=16)


send_command('AT+CGATT?')
send_command('AT+CGATT=0')
send_command('AT+CGCLASS?')
send_command('AT+CGDCONT=1,"IP","internet"')
send_command('AT+CGATT?')
send_command('AT+CGACT=1,1')
send_command('AT+CGATT?')
send_command('AT+CGPADDR=1')

ppp = network.PPP(uart)

ppp.active(True)
print(ppp.ifconfig())
ppp.active(False)

After execution I have the following:

Code: Select all


AT+CGATT?

+CGATT: 1
OK

AT+CGATT=0

OK

AT+CGCLASS?

+CGCLASS: "B"
OK

AT+CGDCONT=1,"IP","internet"

OK

AT+CGATT?

+CGATT: 0
OK

AT+CGACT=1,1

OK

AT+CGPADDR=1

+CGPADDR: 1,"100.83.70.16"

OK

('0.0.0.0', '0.0.0.0', '255.255.255.255', '0.0.0.0')


So It receives IP-address but does not transfer it to network.PPP.

Any ideas where I messed up?

kinno
Posts: 32
Joined: Mon Oct 21, 2019 2:06 pm

Re: SIM800L + UART + network module?

Post by kinno » Sat Feb 22, 2020 3:51 pm

Try using either of these commands to have the simcom module allow the takeover of the TCP/IP stack:

ATD*99#

or

AT+CGDATA="PPP",1

You can google these commands as well to find out more information. So your code would look like this:

Code: Select all

from machine import UART, Pin
import network

def send_command(command):
  
  command = command + '\r\n'
  uart.write(command)
  line = uart.read(100)
  if line:
    line = line.decode('utf-8')
    print(line.replace('\r\n','\n'))
  else:
    print('Hmmm...')


uart = UART(1, 9600, timeout=2000, rx=17, tx=16)


send_command('AT+CGATT?')
send_command('AT+CGATT=0')
send_command('AT+CGCLASS?')
send_command('AT+CGDCONT=1,"IP","internet"')
send_command('AT+CGATT?')
send_command('AT+CGACT=1,1')
send_command('AT+CGATT?')
send_command('AT+CGPADDR=1')
send_command('AT+CGDATA="PPP",1')		# The new command. You can replace with the ATD*99# command as well.

ppp = network.PPP(uart)

ppp.active(True)
print(ppp.ifconfig())
ppp.active(False)

Please let me know how this goes. Good luck.

headshaker
Posts: 8
Joined: Tue Feb 18, 2020 3:22 pm

Re: SIM800L + UART + network module?

Post by headshaker » Sun Feb 23, 2020 5:44 pm

Unfortunately it did not help: Upon execution of

AT+CGDATA="PPP",1

I can see connection led blinking fast on SIM800L, but ppp.ifconfig() still shows zeroes, even after 10 seconds sleep.

kinno
Posts: 32
Joined: Mon Oct 21, 2019 2:06 pm

Re: SIM800L + UART + network module?

Post by kinno » Sun Feb 23, 2020 8:49 pm

Try putting sleeps here:

Code: Select all

from machine import UART, Pin
import network

def send_command(command):
  
  command = command + '\r\n'
  uart.write(command)
  line = uart.read(100)
  if line:
    line = line.decode('utf-8')
    print(line.replace('\r\n','\n'))
  else:
    print('Hmmm...')


uart = UART(1, 9600, timeout=2000, rx=17, tx=16)


send_command('AT+CGATT?')
send_command('AT+CGATT=0')
send_command('AT+CGCLASS?')
send_command('AT+CGDCONT=1,"IP","internet"')
send_command('AT+CGATT?')
send_command('AT+CGACT=1,1')
send_command('AT+CGATT?')
send_command('AT+CGPADDR=1')
send_command('AT+CGDATA="PPP",1')		# The new command. You can replace with the ATD*99# command as well.
time.sleep(10)

ppp = network.PPP(uart)
ppp.active(True)
time.sleep(10)
print(ppp.ifconfig())
# Remove this for now. ppp.active(False)
Maybe this works. I use the simcom 7000g and it works well but took some playing with especially on the sleeps side.

Again, let me know how you do.

headshaker
Posts: 8
Joined: Tue Feb 18, 2020 3:22 pm

Re: SIM800L + UART + network module?

Post by headshaker » Sun Feb 23, 2020 9:44 pm

No results: nor with 10 seconds sleep, nor with 20 seconds. It seems that it is a problem in network.PPP

kinno
Posts: 32
Joined: Mon Oct 21, 2019 2:06 pm

Re: SIM800L + UART + network module?

Post by kinno » Mon Feb 24, 2020 4:57 pm

I took some time to review my code and I believe you haven't called the connect funciton.

ppp.connect()

Code: Select all

from machine import UART, Pin
import network

def send_command(command):
  
  command = command + '\r\n'
  uart.write(command)
  line = uart.read(100)
  if line:
    line = line.decode('utf-8')
    print(line.replace('\r\n','\n'))
  else:
    print('Hmmm...')


uart = UART(1, 9600, timeout=2000, rx=17, tx=16)


send_command('AT+CGATT?')
send_command('AT+CGATT=0')
send_command('AT+CGCLASS?')
send_command('AT+CGDCONT=1,"IP","internet"')
send_command('AT+CGATT?')
send_command('AT+CGACT=1,1')
send_command('AT+CGATT?')
send_command('AT+CGPADDR=1')
send_command('AT+CGDATA="PPP",1')		# The new command. You can replace with the ATD*99# command as well.
time.sleep(10)

ppp = network.PPP(uart)
ppp.active(True)
time.sleep(10)
ppp.connect()		# The connect function.
time.sleep(10)
print(ppp.ifconfig())
# Remove this for now. ppp.active(False)
Again keep me in the loop.

headshaker
Posts: 8
Joined: Tue Feb 18, 2020 3:22 pm

Re: SIM800L + UART + network module?

Post by headshaker » Mon Mar 02, 2020 8:01 pm

ppp.connect() did not help. Still zeroes in ifconfig.

kinno
Posts: 32
Joined: Mon Oct 21, 2019 2:06 pm

Re: SIM800L + UART + network module?

Post by kinno » Tue Mar 03, 2020 4:34 pm

What do you get if you call this?

ppp.isconnected()

Hopefully we can figure it out still.

headshaker
Posts: 8
Joined: Tue Feb 18, 2020 3:22 pm

Re: SIM800L + UART + network module?

Post by headshaker » Thu Mar 05, 2020 10:27 am

So, latest code:

Code: Select all

uart = UART(1, 9600, timeout=3000, rx=17, tx=16)

uart.write('+++\r\n')
print(uart.read(100))
uart.write('AT\r\n')
print(uart.read(100))
uart.write('AT\r\n')
print(uart.read(100))
uart.write('ATZ\r\n')
print(uart.read(100))
time.sleep(2)
uart.write('AT+CGATT=0\r\n')
print(uart.read(100))
time.sleep(2)
uart.write('AT+CGDCONT=1,"IP","internet"\r\n')
print(uart.read(100))
time.sleep(2)
uart.write('AT+CGACT=1,1\r\n')
print(uart.read(100))
time.sleep(3)
uart.write('AT+CGPADDR=1\r\n')
print(uart.read(100))
time.sleep(2)
uart.write('AT+CGDATA="PPP",1\r\n')
print(uart.readline())

time.sleep(10)

ppp = network.PPP(uart)
ppp.active(True)
time.sleep(10)
ppp.connect()		# The connect function.
time.sleep(10)
print('ppp.isconnected: ', ppp.isconnected())
print('ppp.ifconfig:    ', ppp.ifconfig())
And repl:

Code: Select all

Ready to download this file,please wait!
.........
download ok
exec(open('./ppp_test.py').read(),globals())
b'+++\r'
b'AT\r\r\nOK\r\n'
b'AT\r\r\nOK\r\n'
b'ATZ\r\r\nOK\r\n'
b'AT+CGATT=0\r\r\nOK\r\n'
b'AT+CGDCONT=1,"IP","internet"\r\r\nOK\r\n'
b'AT+CGACT=1,1\r\r\nOK\r\n'
b'AT+CGPADDR=1\r\r\n+CGPADDR: 1,"172.18.211.19"\r\n\r\nOK\r\n'
b'AT+CGDATA="PPP",1\r\r\n'
ppp.isconnected:  False
ppp.ifconfig:     ('0.0.0.0', '0.0.0.0', '255.255.255.255', '0.0.0.0')
So, the problem is somewhere around PPP and network. I'm using generic esp32-idf3-20200221-v1.12-188-gd3b2c6e44.bin on DOIT Esp32 DevKit v1 and SIM800L 2.0 (evb), also tried SIM800L 1.0.

kinno
Posts: 32
Joined: Mon Oct 21, 2019 2:06 pm

Re: SIM800L + UART + network module?

Post by kinno » Thu Mar 05, 2020 8:47 pm

Here is the pertinent code I use for connecting.

Code: Select all

simcom = UART(1, 9600)  # Create with given baudrate
simcom.init(baudrate=9600, bits=8, parity=None, stop=1, rx=26, tx=27)  # init with given parameters check rx and tx.

AT('+CIPSHUT', success='SHUT OK')
AT('+CFUN=1,1', timeout=30, success="SMS Ready")

print("Running Modem Setup Scripts")
AT('', timeout=1)
AT('+IPR=9600')
AT('+GSN')
AT('+CPIN?')
AT('+CFUN=1')
AT('+CMNB=1')
AT('+CSTT="' + apn + '","",""')
AT('+CGDCONT=1,"IP","' + apn + '"')
AT('S7=10')

AT('+CGDATA="PPP",1', success='NO CARRIER', timeout=5)             # TODO First call often fails.... Why?
if AT('+CGDATA="PPP",1', timeout=0)[0] == 'Timeout':               	   # TODO Second Call Connects Immediately. Why?
        print('PPP Call Established.')
    else:
        print('PPP Call Failed.')
        
ppp = network.PPP(simcom.simcom)                	# Attaching Simcom to the PPP network mod.
ppp.active(True)                                			# Activate the ppp attachment.
ppp.connect()                                   			# Connect the ppp attachment.
print('Waiting For IP address.')                		# Wait before asking for IP.
time.sleep(5)                                   			# Wait for Connection.
print(ppp.ifconfig())                           			# Print the IP address to confirm connection is successful.

if ppp.isconnected() is True:
    print('Connected via PPP Data Layer.')
else:
    print('Connection Through PPP Failed.')
    close_restart()
        
This has been stripped out of a bigger program so it won't run and there may be some formatting errors but we can look through it to see if we can determine what is going on.

I will try to create a script for you that is more thought out but wanted to reply quickly in case you had time to sort through this on your own first. I hope there is something in there that may help.

Again, once I find some time I will see what more I can do. I would also try just the regular 1.12 micropython download not the nightly.

Post Reply