← index #5439Issue #16887
Related · medium · value 1.623
QUERY · ISSUE

Can not ppp success on esp32

openby zhangxq87opened 2019-12-19updated 2024-08-14

Hi,
My esp32 is running this build(MicroPython v1.11-665-gfb0141559 on 2019-12-14; ESP32 module with ESP32)
I can ppp success on raspberry with GSM module but cannot success on ESP32 board.
Commands as below, anyone can help on it? thanks.

`

from machine import UART
import network
GSM = UART(1, baudrate=115200, rx=18, tx=19)
I (31450) uart: ALREADY NULL
GSM.write('AT\r\n')
4
GSM.read()
b'AT\r\n\r\nOK\r\n'
GSM.write('AT+CGDCONT=1,"IP","3GNET"\r\n')
27
GSM.read()
b'AT+CGDCONT=1,"IP","3GNET"\r\n\r\nOK\r\n'
GSM.write('AT+CGDATA="PPP",1\r\n')
19
GSM.read()
b'AT+CGDATA="PPP",1\r\n\r\nCONNECT\r\n'
GPRS=network.PPP(GSM)
GPRS.active(True)
True
GPRS.connect()
GPRS.isconnected()
False
GPRS.ifconfig()
('0.0.0.0', '0.0.0.0', '255.255.255.255', '0.0.0.0')`

CANDIDATE · ISSUE

MIMXRT/PPP: Board crashes sometimes accessing a LTE modem.

openby robert-hhopened 2025-03-09updated 2025-03-21
bugports

Port, board and/or hardware

MIMXRT1020_EVK or other MIMXRT board

MicroPython version

MicroPython v1.25.0-preview.371.g27699edce.dirty on 2025-03-09; i.MX RT1020 EVK with MIMXRT1021DAG5A
This is based on PR #16876, which enabled PPP on MIMXRT boards.

Reproduction

Run the below ugly test script thrice without longer delays. It behaves weird at the second call and crashes at the third call. If one waits > 10 seconds between the second and third call, the crash does not happen.

"""
# Establish PPP on ESP32 using SIM7608E.
"""

import time
from network import PPP
from machine import UART, Pin
import socket
import ssl

quiet = False
# Set APN. Specific to your  cellular carrier.
apn = "internet.telekom"
baud_rate = 115200

# Create UART object for SIM76xx modem. Start with 115200 baud.
uart = UART(1, baudrate=115200, rxbuf=1024, txbuf=1024, timeout=200, timeout_char=200)

def wait_response(port, response):
    port.flush()
    resp_msg = None
    while msg := port.readline():
        if not quiet:
            print(msg)
        if response and msg.startswith(response):
            resp_msg = msg
    return resp_msg

def get_http_page(url):
    s = socket.socket()
    try:
        i = socket.getaddrinfo(url, 80)[0][-1]
        print('Connecting to socket...')
        print(s.connect(i))
        print('Send...')
        print(s.send(b"GET / HTTP/1.0\r\n\r\n"))
        print("Get...")
        print(s.recv(4096))
    except:
        print("Connect & xchange failed")
    print('Close socket...')
    s.close()

# Ping modem until it is on and ready
timeout = 10
while timeout > 0:
    uart.write('AT\r\n')
    resp = wait_response(uart, b'AT')
    if resp:
        break
    print(".", end="")
    time.sleep(2)
    timeout -= 1
else:
    print("Unable to establish connection with modem. Try the above again, or potentially check that the pinout is correct")
    raise Exception("Modem unavailable")

uart.write('AT+SIMCOMATI\r\n')
wait_response(uart, None)

uart.write('AT+CEREG=0\r\n')
wait_response(uart, None)

# Wait for registration, which may take a while
timeout = 120
while timeout > 0:
    uart.write('AT+CEREG?\r\n')
    resp = wait_response(uart, b"+CEREG")
    # Wait for "+CEREG=0,1"
    if resp and resp.strip().split(b',')[1] == b"1":
        break
    time.sleep_ms(1000)
    timeout -= 1
else:
    print("Cannot register to the network")
    raise Exception("Network unavailable")

# disable unsolicited messaged
uart.write('AT+CGEREP=0\r\n')
wait_response(uart, None)

# switch the baud rate
if baud_rate != 115200:
    uart.write('AT+IPR={}\r\n'.format(baud_rate))
    wait_response(uart, None)
    uart.init(baudrate=baud_rate, rxbuf=1024, txbuf=1024)

# Connect to network and establish PPP
uart.write('AT+CGDCONT=1,"IP","{}"\r\n'.format(apn))
wait_response(uart, None)
# Enable PPP
uart.write('ATD*99#\r\n')
resp = wait_response(uart, b"CONNECT")

if resp:
    # Hand modem object off to system PPP module
    ppp = PPP(uart)
    # GPRS.active(True)
    resp = ppp.connect(security=PPP.SEC_PAP, user="t-mobile", key="tm")
    # This will print out your IP, subnet, gateway, and dns
    print(ppp.ifconfig())
    # Check that you can use a socket
    addr = socket.getaddrinfo('micropython.org', 80)[0][-1]
    print(addr)
    get_http_page('micropython.org')
    #disconnect
    ppp.disconnect()

# stop session
print("Stop PPP session")
uart.write('+++')
wait_response(uart, None)
uart.write('ATH\r\n')
resp = wait_response(uart, None)
# Switch the baud rate back if needed
if baud_rate != 115200:
    uart.write('AT+IPR=115200\r\n')
    wait_response(uart, None)

# Reset the modem to clear the internal states.
# That fixes the non-connect on the second call.
# uart.write('AT+CRESET\r\n')
# wait_response(uart, None)

print("Test finished")

Expected behavior

Runs fine the same ways all three times.

Observed behavior

Crash. The call stack is below. It is every time the same when it crashes.

Image

Edit: Uncommenting the reset modem code at the end of the script prevents the bug, since then the sequence is like starting from power-on reset. But even without reset and strange internal state of the modem the board should not crash.

Additional Information

Behaves the same strange way with a RP2 PICO W and PPP enabled.

Code of Conduct

Yes, I agree

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied