Skip to content

Instantly share code, notes, and snippets.

@JayPalm
Created December 24, 2022 08:47
Show Gist options
  • Save JayPalm/deed52e6b8f7b394409679e1aebd0c41 to your computer and use it in GitHub Desktop.
Save JayPalm/deed52e6b8f7b394409679e1aebd0c41 to your computer and use it in GitHub Desktop.
PPP over GSM using MicroPython on a LilyGo-TSIM7000G
"""
# Establish PPP on ESP32 using SIM7000G.
[AT CMD reference for SIM7000](https://cdn.geekfactory.mx/sim7000g/SIM7000%20Series_AT%20Command%20Manual_V1.06.pdf)
I am using a LilyGo-TSIM7000G, which is a dev board that contains both an ESP32 and SIM7000G.
You may need to update the pinout differently for your setup and modem.
You will also need to update the apn variable to whatever is appropriate for your sim card and carrier.
"""
from time import sleep
import network
from machine import UART, Pin, reset
# Set APN. Specific to your cellular carrier.
apn = "mobilenet"
# Power PIN may be different for your config
GSM_PWR = Pin(4, Pin.OUT)
# Power up modem. Probably don't need this cycle, but I found it somewhere else (from LilyGo)
GSM_PWR.value(1)
sleep(0.5)
GSM_PWR.value(0)
sleep(0.5)
GSM_PWR.value(1)
sleep(10)
# Create UART object for SIM7000 modem
GSM = UART(1, baudrate=115200, rx=26, tx=27)
# Ping modem until it is on and ready
timeout = 20
while timeout:
GSM.write('AT\r\n')
r = GSM.read()
print(r)
if r:
break
sleep(5)
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")
# Make sure modem really is connected and available
GSM.write('AT\r\n')
GSM.read()
# Connect to network and establish PPP
GSM.write('AT+CGDCONT=1,"IP","{}"\r\n'.format(apn))
GSM.read()
GSM.write('AT+CGDATA="PPP",1\r\n')
GSM.read()
# May need to add an extra delay here if connection is slow
# Send these commands to check network operator
GSM.write("AT+COPS=3,0")
carrier_format = GSM.read()
GSM.write("AT+COPS?")
carreir = GSM.read()
# Hand modem object off to system PPP module
GPRS = network.PPP(GSM)
GPRS.active(True)
GPRS.connect(authmode=GPRS.AUTH_PAP, username="", password="")
# This will print out your IP, subnet, gateway, and dns
GPRS.ifconfig()
# Check that you can use a socket
addr = socket.getaddrinfo('micropython.org', 80)[0][-1]
print(addr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment