Get begun with the sim7000g and also the esp32 LTE/GPS/GPRS component. Throughout this tutorial, we’ll use the LILYGO T-SIM7000G ESP32 board that integrates the ESP32 chip, the SIM7000G module, microSD card slot, battery owner, and also charger on the very same board. Wi-Fi as well as Bluetooth, you can connect with this ESP32 board utilizing SMS. You can likewise attach it to the internet using your SIM card data plan and obtain GPS place.
Introducing the LILYGO T-SIM7000G ESP32
The LILYGO T-SIM7000G is an ESP32 advancement board with a SIM7000G chip. This adds LTE (4G), GPS, as well as GPRS to your board. This implies that with this board you can send out SMS, get area and time utilizing GPS, and also attach it to the net making use of a SIM card data strategy. This board does not sustain phone telephone calls.
Besides the SIM7000G module, the board also features some interesting features like a battery owner for a 18650 battery, a battery billing circuit where you can link solar panels to reenergize the battery, and a microSD card slot that can be useful for data logging projects or to save arrangement settings.
Here’s a recap of the LILYGO T-SIM7000G ESP32 board attributes:
- Supply voltage: 3.3V DC or 5V DC
- ESP32 chip (WROVER-B Module) (240MHz dual-core processor)
- Flash memory: 4MB
- PSRAM: 8MB
- SRAM: 520KB
- Built-in Wi-Fi
- Built-in Bluetooth
- USB to serial converter: CP2104 or CH9102 (drivers)
- Built-in SIM7000G module
- Built-in nano SIM card slot
- Built-in SIM antenna slot
- Built-in GPS antenna slot
- Built-in Li-ion/Li-Po battery charging circuit:
- DW01A battery protection IC
- CN3065 solar energy charging interface for 4.4-6.8V solar panel
- Built-in 1x 18650 battery holder
- Built-in solar panel connector 2p JST-PH
- Built-in Micro SD card slot
- Built-in on/off switch
To make use of the capacities of this board you require to have a nano SIM card with an information plan and also a to upload code to the board.
The bundle consists of an outside antenna for LTE, and an additional antenna for GPS.
There are 2 variations of this board (Version 20191227 as well as version 20200415). The image below programs the 2 versions. Aesthetically, they mostly differ on the placement of the nano SIM card owner.
The initial variation had some design issues, so it is advised to get the most recent variation. Aditionally, the most up to date version features some improvements considering customers’ comments. I obtained my board a very long time earlier, I’ve obtained the first version as well as that’s the one we’ll use throughout this tutorial. This is additionally compatible with the latest board.
Here’s a list of the renovations on the most up to date variation ():
- Added active GPS antenna power control, when the module GPIO 4 is not turned on, the antenna consumes only the static current of the LDO;
- Replaced TP4056 with CN3065 for solar charge input management;
- Added reverse battery protection;
- Added battery overcharge protection;
- Added battery over-discharge protection.
You can inspect the schematic layouts for each and every variation on the adhering to web links:
Where to acquire LILYGO T-SIM7000G ESP32?
Check the adhering to web link:
All shops in the previous web link need to market the current version, however verify the item page, simply in situation the vendor changes something.
You can make use of the coming before links or go straight to discover all the components for your tasks at the best price!
LILYGO T-SIM7000G ESP32 Pinout
The complying with photos reveal the pinout of the T-SIM7000G ESP32 board.
This is the pinout for version V1.0.
And this is the pinout for the enhanced board V1.1.
The complying with table reveals the links between the ESP32 as well as the SIM7000G chip:
SIM7000G | ESP32 |
TX | GPIO 26 |
RX | GPIO 27 |
POWER | GPIO 4 |
To connect with the microSD card, you require SPI interaction procedure. These are the GPIOs used:
MicroSD Card (TF card) | ESP32 |
MOSI | GPIO 15 |
SCLK | GPIO 14 |
CS | GPIO 13 |
MISO | GPIO 2 |
SIM Card
This board just supports nano SIM cards. You require a SIM card for LTE and GPRS. If you just desire to use GPS information, you do not need a SIM card.
To make use of LTE and also GPRS you need a SIM card with some data plan. This can be costly in some countries, so it could be cost too high depending upon exactly how much you can obtain an information prepare for in your nation.
Where we live (Portugal), we can get a SIM card with data strategy, calls, as well as SMS (sufficient for ESP32 projects) for approximately $12. We recommend utilizing a SIM card with a month-to-month or pre paid strategy, so that you recognize specifically just how much you’ll spend. There are additionally business focused on SIM cards for IoT tasks.
APN Details
To connect your SIM card to the web, you need to have your phone plan company’s APN details. You require the domain name, username, and password.
In my instance, I’m utilizing Vodafone Portugal. If you search for GPRS APN settings adhered to by your phone strategy company name, (in my instance its: “GPRS APN Vodafone Portugal”), you can generally find in an online forum or in their site all the details that you need. If you don’t utilize a popular service provider, pendtag
It might be a little bit complicated to discover the details. So, you might require to contact them straight.
AT Commands
AT commands are made use of to manage MODEMs, as holds true of the SIM7000G. In the case of the ESP32, you send out the AT commands using serial communication procedure. Then, the modem reacts back additionally through serial communication.
There are 4 kinds of AT commands: test; read; established; execution. You can discover the total list of AT commands for the SIM7000G on the following link:
- (mudar para um link nosso?)
Here are some of one of the most usual AT commands:
- check communication with the module: AT
- check if SIM card is ready: AT+CPIN?
- check the registration status of the device: AT+CGREG?
- send SMS to a number: AT+CMGS=PHONE_NUMBER(in international format)
Libraries
As we explained formerly, the ESP32 interacts with the SIM7000G chip by sending AT commands using serial communication. You don’t require a collection, you can simply develop a serial interaction with the module and also begin sending out AT commands.
However, it might be a lot more practical to utilize a collection. For instance, the collection knows which commands to send out, and also exactly how to take care of AT responses, and also covers that right into the conventional Arduino Client user interface– that’s the collection we’ll make use of in this tutorial.
Installing the TinyGSM Library
Open your Arduino IDE as well as most likely to Sketch > > Include Library > > Manage Libraries. The Library Manager must open up. Look for TinyGSM. Select the TinyGSM collection by Volodymyr Shymanskyy.
You also need to mount the StreamDebugger collection. Most likely to Sketch > > Include Library > > Manage Libraries, search for StreamDebugger, and also install it.
Preparing the LILYGO T-SIM7000G ESP32 Board
Before examining your board, you require to follow the next steps:
1) Insert a nano SIM card;
2) Connect the Full Band LTE antenna (SIM);
3) Connect the GPS antenna.
If you intend to test the microSD card features, you ought to only connect a microSD card, after publishing the code.
LILYGO T-SIM7000G ESP32 Network Test
The first sketch you ought to operate on your board is the Network Test. This will tell you the network option settings you must utilize– this depends upon the SIM card, modem( SIM7000G), as well as the mobile network driver it makes use of.
- Copy the following code to your Arduino IDE (the code was adapted ).
/*
Rui Santos
Complete project details at https://RandomNerdTutorials.com/lilygo-t-sim7000g-esp32-lte-gprs-gps/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
// Original code: https://github.com/Xinyuan-LilyGO/LilyGO-T-SIM7000G/blob/master/examples/Arduino_NetworkTest/Arduino_NetworkTest.ino
#define TINY_GSM_MODEM_SIM7000
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb
#define SerialAT Serial1
// Set serial for debug console (to the Serial Monitor, default speed 115200)
#define SerialMon Serial
// See all AT commands, if wanted
// #define DUMP_AT_COMMANDS
// set GSM PIN, if any
#define GSM_PIN ""
// Your GPRS credentials, if any
const char apn[] = ""; //SET TO YOUR APN
const char gprsUser[] = "";
const char gprsPass[] = "";
#include <TinyGsmClient.h>
#include <SPI.h>
#include <SD.h>
#include <Ticker.h>
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif
// LilyGO T-SIM7000G Pinout
#define UART_BAUD 115200
#define PIN_DTR 25
#define PIN_TX 27
#define PIN_RX 26
#define PWR_PIN 4
#define SD_MISO 2
#define SD_MOSI 15
#define SD_SCLK 14
#define SD_CS 13
#define LED_PIN 12
void modemPowerOn(){
pinMode(PWR_PIN, OUTPUT);
digitalWrite(PWR_PIN, LOW);
delay(1000);
digitalWrite(PWR_PIN, HIGH);
}
void modemPowerOff(){
pinMode(PWR_PIN, OUTPUT);
digitalWrite(PWR_PIN, LOW);
delay(1500);
digitalWrite(PWR_PIN, HIGH);
}
void modemRestart(){
modemPowerOff();
delay(1000);
modemPowerOn();
}
void setup(){
// Set console baud rate
SerialMon.begin(115200);
delay(10);
// Set LED OFF
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
modemPowerOn();
Serial.println("========SDCard Detect.======");
SPI.begin(SD_SCLK, SD_MISO, SD_MOSI);
if (!SD.begin(SD_CS)) {
Serial.println("SDCard MOUNT FAIL");
} else {
uint32_t cardSize = SD.cardSize() / (1024 * 1024);
String str = "SDCard Size: " + String(cardSize) + "MB";
Serial.println(str);
}
Serial.println("===========================");
SerialAT.begin(UART_BAUD, SERIAL_8N1, PIN_RX, PIN_TX);
Serial.println("/**********************************************************/");
Serial.println("To initialize the network test, please make sure your LTE ");
Serial.println("antenna has been connected to the SIM interface on the board.");
Serial.println("/**********************************************************//n/n");
delay(10000);
}
void loop(){
String res;
Serial.println("========INIT========");
if (!modem.init()) {
modemRestart();
delay(2000);
Serial.println("Failed to restart modem, attempting to continue without restarting");
return;
}
Serial.println("========SIMCOMATI======");
modem.sendAT("+SIMCOMATI");
modem.waitResponse(1000L, res);
res.replace(GSM_NL "OK" GSM_NL, "");
Serial.println(res);
res = "";
Serial.println("=======================");
Serial.println("=====Preferred mode selection=====");
modem.sendAT("+CNMP?");
if (modem.waitResponse(1000L, res) == 1) {
res.replace(GSM_NL "OK" GSM_NL, "");
Serial.println(res);
}
res = "";
Serial.println("=======================");
Serial.println("=====Preferred selection between CAT-M and NB-IoT=====");
modem.sendAT("+CMNB?");
if (modem.waitResponse(1000L, res) == 1) {
res.replace(GSM_NL "OK" GSM_NL, "");
Serial.println(res);
}
res = "";
Serial.println("=======================");
String name = modem.getModemName();
Serial.println("Modem Name: " + name);
String modemInfo = modem.getModemInfo();
Serial.println("Modem Info: " + modemInfo);
// Unlock your SIM card with a PIN if needed
if ( GSM_PIN && modem.getSimStatus() != 3 ) {
modem.simUnlock(GSM_PIN);
}
for (int i = 0; i <= 4; i++) {
uint8_t network[] = {
2, /*Automatic*/
13, /*GSM only*/
38, /*LTE only*/
51 /*GSM and LTE only*/
};
Serial.printf("Try %d method/n", network[i]);
modem.setNetworkMode(network[i]);
delay(3000);
bool isConnected = false;
int tryCount = 60;
while (tryCount--) {
int16_t signal = modem.getSignalQuality();
Serial.print("Signal: ");
Serial.print(signal);
Serial.print(" ");
Serial.print("isNetworkConnected: ");
isConnected = modem.isNetworkConnected();
Serial.println( isConnected ? "CONNECT" : "NO CONNECT");
if (isConnected) {
break;
}
delay(1000);
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
}
if (isConnected) {
break;
}
}
digitalWrite(LED_PIN, HIGH);
Serial.println();
Serial.println("Device is connected .");
Serial.println();
Serial.println("=====Inquiring UE system information=====");
modem.sendAT("+CPSI?");
if (modem.waitResponse(1000L, res) == 1) {
res.replace(GSM_NL "OK" GSM_NL, "");
Serial.println(res);
}
Serial.println("/**********************************************************/");
Serial.println("After the network test is complete, please enter the ");
Serial.println("AT command in the serial terminal.");
Serial.println("/**********************************************************//n/n");
while (1) {
while (SerialAT.available()) {
SerialMon.write(SerialAT.read());
}
while (SerialMon.available()) {
SerialAT.write(SerialMon.read());
}
}
}
- Insert your SIM card pin, if you have it. In my case, I disabled the pin.
#define GSM_PIN ""
- Insert your apn details on the following lines:
const char apn[] = ""; //SET TO YOUR APN
const char gprsUser[] = "";
const char gprsPass[] = "";
For instance, in my situation:
const char apn[] = "net2.vodafone.pt"; //SET TO YOUR APN
const char gprsUser[] = "vodafone";
const char gprsPass[] = "vodafone";
- Go to Tools > Board and select ESP32 Dev Module.
- Finally, upload the code to your board.
Now, you can put a microSD card, if you desire to check the microSD card functions.
Then, open the Serial Monitor at a baud price of 115200. Press the on-board RST button to reactivate the board.
Wait time up until the board links to the network (in my instance, it might use up to 2 minutes).
You should get something similar in the Serial Monitor.
You can see that it recognizes the microSD card as well as connects to the network effectively.
Check the recommended setting selection and the favored option in between CAT-M as well as NB-IoT. You’ll need those parameters later, and also they vary depending on your SIM card and also provider.
LILYGO T-SIM7000G ESP32: Connect to the Internet, Send SMS, and Get GPS Data
If whatever went as anticipated, currently you’re prepared to evaluate various other features like attaching to the internet, sending SMS, as well as obtaining GPS information.
Copy the following code to your Arduino IDE. This code was adjusted.
/*
Rui Santos
Complete project details at https://RandomNerdTutorials.com/lilygo-t-sim7000g-esp32-lte-gprs-gps/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
// Based on the following example: https://github.com/Xinyuan-LilyGO/LilyGO-T-SIM7000G/blob/master/examples/Arduino_TinyGSM/AllFunctions/AllFunctions.ino
#define TINY_GSM_MODEM_SIM7000
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb
#define SerialAT Serial1
// See all AT commands, if wanted
#define DUMP_AT_COMMANDS
// set GSM PIN, if any
#define GSM_PIN ""
// Your GPRS credentials, if any
const char apn[] = ""; //SET TO YOUR APN
const char gprsUser[] = "";
const char gprsPass[] = "";
// Set phone number, if you want to test SMS
// Set a recipient phone number to test sending SMS (it must be in international format including the "+" sign)
#define SMS_TARGET ""
#include <TinyGsmClient.h>
#include <SPI.h>
#include <SD.h>
#include <Ticker.h>
#ifdef DUMP_AT_COMMANDS // if enabled it requires the streamDebugger lib
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, Serial);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif
#define uS_TO_S_FACTOR 1000000ULL // Conversion factor for micro seconds to seconds
#define TIME_TO_SLEEP 60 // Time ESP32 will go to sleep (in seconds)
#define UART_BAUD 115200
#define PIN_DTR 25
#define PIN_TX 27
#define PIN_RX 26
#define PWR_PIN 4
#define SD_MISO 2
#define SD_MOSI 15
#define SD_SCLK 14
#define SD_CS 13
#define LED_PIN 12
int counter, lastIndex, numberOfPieces = 24;
String pieces[24], input;
void setup(){
// Set console baud rate
Serial.begin(115200);
delay(10);
// Set LED OFF
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
pinMode(PWR_PIN, OUTPUT);
digitalWrite(PWR_PIN, HIGH);
delay(300);
digitalWrite(PWR_PIN, LOW);
SPI.begin(SD_SCLK, SD_MISO, SD_MOSI, SD_CS);
if (!SD.begin(SD_CS)) {
Serial.println("SDCard MOUNT FAIL");
} else {
uint32_t cardSize = SD.cardSize() / (1024 * 1024);
String str = "SDCard Size: " + String(cardSize) + "MB";
Serial.println(str);
}
Serial.println("/nWait...");
delay(1000);
SerialAT.begin(UART_BAUD, SERIAL_8N1, PIN_RX, PIN_TX);
// Restart takes quite some time
// To skip it, call init() instead of restart()
Serial.println("Initializing modem...");
if (!modem.restart()) {
Serial.println("Failed to restart modem, attempting to continue without restarting");
}
}
void loop(){
// Restart takes quite some time
// To skip it, call init() instead of restart()
Serial.println("Initializing modem...");
if (!modem.init()) {
Serial.println("Failed to restart modem, attempting to continue without restarting");
}
String name = modem.getModemName();
delay(500);
Serial.println("Modem Name: " + name);
String modemInfo = modem.getModemInfo();
delay(500);
Serial.println("Modem Info: " + modemInfo);
// Unlock your SIM card with a PIN if needed
if ( GSM_PIN && modem.getSimStatus() != 3 ) {
modem.simUnlock(GSM_PIN);
}
modem.sendAT("+CFUN=0 ");
if (modem.waitResponse(10000L) != 1) {
DBG(" +CFUN=0 false ");
}
delay(200);
/*
2 Automatic
13 GSM only
38 LTE only
51 GSM and LTE only
* * * */
String res;
// CHANGE NETWORK MODE, IF NEEDED
res = modem.setNetworkMode(2);
if (res != "1") {
DBG("setNetworkMode false ");
return ;
}
delay(200);
/*
1 CAT-M
2 NB-Iot
3 CAT-M and NB-IoT
* * */
// CHANGE PREFERRED MODE, IF NEEDED
res = modem.setPreferredMode(1);
if (res != "1") {
DBG("setPreferredMode false ");
return ;
}
delay(200);
/*AT+CBANDCFG=<mode>,<band>[,<band>…]
* <mode> "CAT-M" "NB-IOT"
* <band> The value of <band> must is in the band list of getting from AT+CBANDCFG=?
* For example, my SIM card carrier "NB-iot" supports B8. I will configure +CBANDCFG= "Nb-iot ",8
*/
/* modem.sendAT("+CBANDCFG=/"NB-IOT/",8 ");*/
/* if (modem.waitResponse(10000L) != 1) {
DBG(" +CBANDCFG=/"NB-IOT/" ");
}*/
delay(200);
modem.sendAT("+CFUN=1 ");
if (modem.waitResponse(10000L) != 1) {
DBG(" +CFUN=1 false ");
}
delay(200);
SerialAT.println("AT+CGDCONT?");
delay(500);
if (SerialAT.available()) {
input = SerialAT.readString();
for (int i = 0; i < input.length(); i++) {
if (input.substring(i, i + 1) == "/n") {
pieces[counter] = input.substring(lastIndex, i);
lastIndex = i + 1;
counter++;
}
if (i == input.length() - 1) {
pieces[counter] = input.substring(lastIndex, i);
}
}
// Reset for reuse
input = "";
counter = 0;
lastIndex = 0;
for ( int y = 0; y < numberOfPieces; y++) {
for ( int x = 0; x < pieces[y].length(); x++) {
char c = pieces[y][x]; //gets one byte from buffer
if (c == ',') {
if (input.indexOf(": ") >= 0) {
String data = input.substring((input.indexOf(": ") + 1));
if ( data.toInt() > 0 && data.toInt() < 25) {
modem.sendAT("+CGDCONT=" + String(data.toInt()) + ",/"IP/",/"" + String(apn) + "/",/"0.0.0.0/",0,0,0,0");
}
input = "";
break;
}
// Reset for reuse
input = "";
} else {
input += c;
}
}
}
} else {
Serial.println("Failed to get PDP!");
}
Serial.println("/n/n/nWaiting for network...");
if (!modem.waitForNetwork()) {
delay(10000);
return;
}
if (modem.isNetworkConnected()) {
Serial.println("Network connected");
}
// --------TESTING GPRS--------
Serial.println("/n---Starting GPRS TEST---/n");
Serial.println("Connecting to: " + String(apn));
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
delay(10000);
return;
}
Serial.print("GPRS status: ");
if (modem.isGprsConnected()) {
Serial.println("connected");
} else {
Serial.println("not connected");
}
String ccid = modem.getSimCCID();
Serial.println("CCID: " + ccid);
String imei = modem.getIMEI();
Serial.println("IMEI: " + imei);
String cop = modem.getOperator();
Serial.println("Operator: " + cop);
IPAddress local = modem.localIP();
Serial.println("Local IP: " + String(local));
int csq = modem.getSignalQuality();
Serial.println("Signal quality: " + String(csq));
SerialAT.println("AT+CPSI?"); //Get connection type and band
delay(500);
if (SerialAT.available()) {
String r = SerialAT.readString();
Serial.println(r);
}
Serial.println("/n---End of GPRS TEST---/n");
modem.gprsDisconnect();
if (!modem.isGprsConnected()) {
Serial.println("GPRS disconnected");
} else {
Serial.println("GPRS disconnect: Failed.");
}
// --------TESTING GPS--------
Serial.println("/n---Starting GPS TEST---/n");
// Set SIM7000G GPIO4 HIGH ,turn on GPS power
// CMD:AT+SGPIO=0,4,1,1
// Only in version 20200415 is there a function to control GPS power
modem.sendAT("+SGPIO=0,4,1,1");
if (modem.waitResponse(10000L) != 1) {
DBG(" SGPIO=0,4,1,1 false ");
}
modem.enableGPS();
float lat, lon;
while (1) {
if (modem.getGPS(&lat, &lon)) {
Serial.printf("lat:%f lon:%f/n", lat, lon);
break;
} else {
Serial.print("getGPS ");
Serial.println(millis());
}
delay(2000);
}
modem.disableGPS();
// Set SIM7000G GPIO4 LOW ,turn off GPS power
// CMD:AT+SGPIO=0,4,1,0
// Only in version 20200415 is there a function to control GPS power
modem.sendAT("+SGPIO=0,4,1,0");
if (modem.waitResponse(10000L) != 1) {
DBG(" SGPIO=0,4,1,0 false ");
}
Serial.println("/n---End of GPRS TEST---/n");
// --------TESTING SENDING SMS--------
res = modem.sendSMS(SMS_TARGET, String("Hello from ") + imei);
DBG("SMS:", res ? "OK" : "fail");
// --------TESTING POWER DONW--------
// Try to power-off (modem may decide to restart automatically)
// To turn off modem completely, please use Reset/Enable pins
modem.sendAT("+CPOWD=1");
if (modem.waitResponse(10000L) != 1) {
DBG("+CPOWD=1");
}
// The following command does the same as the previous lines
modem.poweroff();
Serial.println("Poweroff.");
// GO TO SLEEP
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
delay(200);
esp_deep_sleep_start();
// Do nothing forevermore
while (true) {
modem.maintain();
}
}
Insert your SIM card pin on the following line:
#define GSM_PIN ""
Fill your APN details:
const char apn[] = "net2.vodafone.pt"; //SET TO YOUR APN
const char gprsUser[] = "vodafone";
const char gprsPass[] = "vodafone";
Set a recipient contact number to test sending out SMS (it should be in global format consisting of the “+” indication):
// Set phone number, if you want to test SMS
#define SMS_TARGET "+XXXXXXXXXXXXXXXX"
Set the network mode with the value you received from the previous example.
/*
2 Automatic
13 GSM only
38 LTE only
51 GSM and LTE only
* * * */
String res;
// CHANGE NETWORK MODE, IF NEEDED
res = modem.setNetworkMode(2);
if (res != "1") {
DBG("setNetworkMode false ");
return ;
}
delay(200);
Change the preferred mode with the value you obtained from the previous example.
/*
1 CAT-M
2 NB-Iot
3 CAT-M and NB-IoT
* * */
// CHANGE PREFERRED MODE, IF NEEDED
res = modem.setPreferredMode(1);
if (res != "1") {
DBG("setPreferredMode false ");
return ;
}
delay(200);
After that, you can publish the code to your board. Do not fail to remember to select ESP32 Dev Module in Tools > > Board. You also need to remove the microSD card every single time you wish to submit a brand-new sketch.
After submitting, you can insert the microSD card. Open Up the Serial Monitor at a baud rate of 115200, and also press the on-board RST switch to reactivate it.
The board might take a while to obtain GPS information for the very first time. Your board requires to be put outside to be able to get a satellite signal. I positioned my own alongside the home window and it was able to accurately get the GPS position.
You need to obtain something similar.
Wait...
Initializing modem...
ATE0
AT+CFUN=0
Failed to restart modem, attempting to continue without restarting
Initializing modem...
AT
AT
AT
OK
ATE0
ATE0
OK
AT+CMEE=0
OK
AT+CLTS=1
OK
AT+CBATCHK=1
OK
AT+CPIN?
+CPIN: READY
OK
AT+GMM
SIMCOM_SIM7000G
OK
Modem Name: SIMCOM SIM7000G
ATI
SIM7000G R1529
OK
Modem Info: SIM7000G R1529
AT+SGPIO=0,4,1,0
OK
AT+CPIN?
+CPIN: READY
OK
AT+CFUN=0
+CPIN: NOT READY
OK
AT+CNMP=2
OK
AT+CMNB=1
OK
AT+CFUN=1
OK
AT+CGDCONT=1,"IP","net2.vodafone.pt","0.0.0.0",0,0,0,0
AT+CGDCONT=13,"IP","net2.vodafone.pt","0.0.0.0",0,0,0,0
Waiting for network...
AT+CEREG?
OK
AT+CGREG?
+CGREG: 0,2
OK
AT+CEREG?
+CEREG: 0,0
OK
AT+CGREG?
+CGREG: 0,2
OK
AT+CEREG?
+CEREG: 0,0
OK
AT+CGREG?
+CGREG: 0,2
OK
AT+CEREG?
+CEREG: 0,0
OK
AT+CGREG?
+CGREG: 0,2
OK
AT+CEREG?
+CEREG: 0,0
OK
AT+CGREG?
+CGREG: 0,2
OK
AT+CEREG?
+CEREG: 0,0
OK
AT+CGREG?
+CGREG: 0,2
OK
AT+CEREG?
DST: 1
*PSUTTZ: 22/08/07,13:45:17","+04",1
+CEREG: 0,0
OK
AT+CGREG?
+CGREG: 0,1
OK
AT+CEREG?
+CEREG: 0,0
OK
AT+CGREG?
+CGREG: 0,1
OK
Network connected
---Starting GPRS TEST---
Connecting to: net2.vodafone.pt
AT+CIPSHUT
SHUT OK
AT+CGATT=0
OK
AT+SAPBR=3,1,"Contype","GPRS"
OK
AT+SAPBR=3,1,"APN","net2.vodafone.pt"
OK
AT+SAPBR=3,1,"USER","vodafone"
OK
AT+SAPBR=3,1,"PWD","vodafone"
OK
AT+CGDCONT=1,"IP","net2.vodafone.pt"
OK
AT+CGATT=1
OK
AT+CGACT=1,1
DST: 1
*PSUTTZ: 22/08/07,13:45:19","+04",1
OK
AT+SAPBR=1,1
OK
AT+SAPBR=2,1
+SAPBR: 1,1,"10.196.118.208"
OK
AT+CIPMUX=1
OK
AT+CIPQSEND=1
OK
AT+CIPRXGET=1
OK
AT+CSTT="net2.vodafone.pt","vodafone","vodafone"
OK
AT+CIICR
OK
AT+CIFSR;E0
10.196.118.208
OK
GPRS status: AT+CGATT?
+CGATT: 1
OK
AT+CIFSR;E0
10.196.118.208
OK
connected
AT+CCID
8935101211825295132f
OK
CCID: 8935101211825295132f
AT+GSN
869951031125929
OK
IMEI: 869951031125929
AT+COPS?
+COPS: 0,0,"vodafone P",3
OK
Operator: vodafone P
AT+CIFSR;E0
10.196.118.208
OK
Local IP: 3497444362
AT+CSQ
+CSQ: 22,0
OK
Signal quality: 22
+CPSI: GSM,Online,268-01,0x000e,63308,15 EGSM 900,-73,0,38-38
OK
---End of GPRS TEST---
AT+CIPSHUT
SHUT OK
AT+CGATT=0
+SAPBR 1: DEACT
OK
AT+CGATT?
+CGATT: 0
OK
GPRS disconnected
---Starting GPS TEST---
AT+SGPIO=0,4,1,1
OK
AT+CGNSPWR=1
OK
AT+CGNSINF
+CGNSINF: 0,,,,,,,,,,,,,,,,,,,,
OK
getGPS 26839
AT+CGNSINF
+CGNSINF: 1,0,,,,,,,0,,,,,,12,,,,50,,
OK
getGPS 28844
AT+CGNSINF
+CGNSINF: 1,0,,,,,,,0,,,,,,12,,,,51,,
OK
getGPS 30850
AT+CGNSINF
+CGNSINF: 1,0,,,,,,,0,,,,,,13,,,,51,,
OK
getGPS 32856
AT+CGNSINF
+CGNSINF: 1,0,,,,,,,0,,,,,,12,,,,51,,
OK
getGPS 34862
AT+CGNSINF
+CGNSINF: 1,0,,,,,,,0,,,,,,12,,,,51,,
OK
getGPS 36868
AT+CGNSINF
+CGNSINF: 1,1,20220807134533.000,41.12XXXX,-8.530XXXX,116.200,0.00,0.0,1,,4.2,,,,13,4,,,51,,
OK
lat:41.12XXXX lon:-8.530XXXX
AT+CGNSPWR=0
OK
AT+SGPIO=0,4,1,0
OK
---End of GPRS TEST---
AT+CMGF=1
OK
AT+CSCS="GSM"
OK
AT+CMGS="+351916XXXXXXXX"
>Hello from 86995103XXXXXXXXX
+CMGS: 228
OK
AT+CPOWD=1
NORMAL POWER DOWN
AT+CPOWD=1
Poweroff.
How the Code Works
Let’s take a peek at the pertinent components of code.
First, you need to define the module you’re making use of. The collection works with several components. To utilize the SIM7000G, consist of the complying with line:
#define TINY_GSM_MODEM_SIM7000
Insert the SIM card pin, APN details, as well as SMS recipient:
// set GSM PIN, if any
#define GSM_PIN ""
// Your GPRS credentials, if any
const char apn[] = "net2.vodafone.pt"; //SET TO YOUR APN
const char gprsUser[] = "vodafone";
const char gprsPass[] = "vodafone";
// Set phone numbers, if you want to test SMS
#define SMS_TARGET "+351916301581"
Include the TinyGSM and SPI collections. You also need to include the SD library if you wish to make use of the microSD card.
#include <TinyGsmClient.h>
#include <SPI.h>
#include <SD.h>
#include <Ticker.h>
Create a TinyGsmClient instance:
#ifdef DUMP_AT_COMMANDS // if enabled it requires the streamDebugger lib
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, Serial);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif
SIM7000G pinout
The adhering to lines established the module baud rate and pinout:
#define UART_BAUD 115200
#define PIN_DTR 25
#define PIN_TX 27
#define PIN_RX 26
#define PWR_PIN 4
#define SD_MISO 2
#define SD_MOSI 15
#define SD_SCLK 14
#define SD_CS 13
#define LED_PIN 12
Power the modem
In the configuration(), you always need to include the following guidelines to switch on the modem:
pinMode(PWR_PIN, OUTPUT);
digitalWrite(PWR_PIN, HIGH);
delay(300);
digitalWrite(PWR_PIN, LOW);
Initialize microSD Card
The following lines boot up the microSD card on the pins we specified earlier.
SPI.begin(SD_SCLK, SD_MISO, SD_MOSI, SD_CS);
if (!SD.begin(SD_CS)) {
Serial.println("SDCard MOUNT FAIL");
} else {
uint32_t cardSize = SD.cardSize() / (1024 * 1024);
String str = "SDCard Size: " + String(cardSize) + "MB";
Serial.println(str);
}
To discover more concerning utilizing the microSD card with the ESP32, you can review the following guide:.
Start Serial Communication
Start a serial interaction with the modem:
SerialAT.begin(UART_BAUD, SERIAL_8N1, PIN_RX, PIN_TX);
Restart and Initialize the Modem
Call the complying with lines to reboot the modem:
// Restart takes quite some time
// To skip it, call init() instead of restart()
Serial.println("Initializing modem...");
if (!modem.restart()) {
Serial.println("Failed to restart modem, attempting to continue without restarting");
}
Or the complying with lines to boot up:
// To skip it, call init() instead of restart()
Serial.println("Initializing modem...");
if (!modem.init()) {
Serial.println("Failed to restart modem, attempting to continue without restarting");
}
Difference in between reactivate() as well as init() according to paperwork:” restart() generally takes longer than init() however makes sure the module does not have lingering connections”.
Get Modem Name and Info
You can use the getModemName() and getModemInfo() to obtain information concerning the modem.
String name = modem.getModemName();
delay(500);
Serial.println("Modem Name: " + name);
String modemInfo = modem.getModemInfo();
delay(500);
Serial.println("Modem Info: " + modemInfo);
Connect GPRS
To connect GPRS utilizing the APN details:
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
delay(10000);
return;
}
To check if it is connected, you can make use of the isGprsConnected() technique:
if (modem.isGprsConnected()) {
Serial.println("connected");
} else {
Serial.println("not connected");
}
Start GPS and Get Location
As pointed out previously, there are two variations of the LILYGO SIM7000G ESP32 board. When the module GPIO 4 is not turned on the antenna eats just the fixed current of the LDO, the most recent comes with active GPS antenna power control–. This suggests we require to turn GPIO 4 on prior to obtaining GPS data to power the antenna. That’s what the next lines do:
// Set SIM7000G GPIO4 HIGH ,turn on GPS power
// CMD:AT+SGPIO=0,4,1,1
// Only in version 20200415 is there a function to control GPS power
modem.sendAT("+SGPIO=0,4,1,1");
if (modem.waitResponse(10000L) != 1) {
DBG(" SGPIO=0,4,1,1 false ");
}
If you have the earliest version, you don’t require those lines of code.
You can begin GPS utilizing the enableGPS() approach.
modem.enableGPS();
Get latitude and longitude utilizing the getGPS() method.
float lat, lon;
while (1) {
if (modem.getGPS(&lat, &lon)) {
Serial.printf("lat:%f lon:%f/n", lat, lon);
break;
} else {
Serial.print("getGPS ");
Serial.println(millis());
}
delay(2000);
}
When you’re done making use of GPS, you can transform it off using the disableGPS() technique:
modem.disableGPS();
And lastly, turn off the power to the antenna by turning GPIO 4 off:
// Set SIM7000G GPIO4 LOW ,turn off GPS power
// CMD:AT+SGPIO=0,4,1,0
// Only in version 20200415 is there a function to control GPS power
modem.sendAT("+SGPIO=0,4,1,0");
if (modem.waitResponse(10000L) != 1) {
DBG(" SGPIO=0,4,1,0 false ");
}
Sending SMS
To send an SMS, you can just use the sendSMS() approach and also pass as disagreements the recipient number as well as the message.
res = modem.sendSMS(SMS_TARGET, String("Hello from ") + imei);
DBG("SMS:", res ? "OK" : "fail");
Powering Down the Module
The LILYGO is supposed to deal with a 18650 battery and photovoltaic panel, so we need to cut power whenever it’s not needed. It’s useful to have a function to transform off the modem entirely. You can utilize the poweroff() technique or send the +CPOWD= 1 AT command.
modem.sendAT("+CPOWD=1");
if (modem.waitResponse(10000L) != 1) {
DBG("+CPOWD=1");
}
// The following command does the same as the previous lines
modem.poweroff();
Serial.println("Poweroff.");
And that’s it for the most relevant components of code.
Wrapping Up
In this tutorial, you learned exactly how to make use of the LILYGO T-SIM7000G ESP32 board. This tutorial can additionally be applied if you’re using a “routine” ESP32 connected to an exterior SIM7000G component. This module sustains LTE, GPRS, as well as GPS, which can be extremely valuable for IoT and Home Automation tasks.
You learned just how to attach GPRS, just how to send out SMS messages and also exactly how to get GPS information. The suggestion is to consist of the snippets of code you need in your very own tasks.
The LILYGO T-SIM7000G ESP32 board also comes with a microSD card port that can be helpful for datalogging jobs or to save setup settings. In addition, it comes with a battery holder as well as a battery charging circuit to utilize with photovoltaic panels. So, it’s ideal to use in remote locations. However, I have not explore the battery circuit yet.
For more examples, you can discover the or the authorities.
Do you have among these boards? What do you assume? Allow us recognize in the remarks listed below.
You may likewise such as the following tutorials (that with minor changes can be utilized with the SIM7000G board):
Learn more concerning the ESP32 with our resources:
Thanks for analysis.