ESP32: Make HTTPS Requests using SIM Card – LILYGO T-SIM7000G

In this project, you’ll learn how to make HTTPS requests using an ESP32 with the SIM7000G LTE/GPS/GPRS module. We’ll use the LILYGO T-SIM7000G ESP32 that combines the ESP32 chip, the SIM7000G module, a microSD card slot, battery holder, and charger on the same board. We’ll show you how to connect it to the internet using your SIM card data plan and make an HTTPS GET request.

ESP32 Make HTTPS Requests using SIM Card LILYGO T-SIM7000G

Compatibility

This board supports 2G, LTE CAT-M1, and NB-IoT protocols. You can go to the following links to check if any of these protocols are supported in your country:

Check network protocols supported in your country;

Check NB-IoT providers in your country.

Table of Contents

Throughout this tutorial, we’ll cover the following topics:

Introducing the LILYGO T-SIM7000G ESP32

The LILYGO T-SIM7000G is an ESP32 development board with a SIM7000G chip. This adds GPS, GPRS, LTE CAT-M1, and NB-IoT protocols to your board. This means that with this board you can send SMS, get location and time using GPS, and connect it to the internet using a SIM card data plan. You can find more information by reading our in-depth guide.

LILYGO T-SIM7000G ESP32

Where to buy LILYGO T-SIM7000G ESP32?

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

APN Details

To connect your SIM card to the internet, you need to have your phone plan provider’s APN details. You need the domain name, username, and password.

In my case, I’m using Vodafone Portugal. If you search for “GPRS APN settings” followed by your phone plan provider name, (in my case: “GPRS APN Vodafone Portugal”), you can usually find in a forum or their website all the information that you need.

It might be a bit tricky to find the details if you don’t use a well-known provider. So, you might need to contact them directly.

Libraries

The ESP32 communicates with the SIM7000G chip by sending AT commands via serial communication. You don’t need a library, you can simply establish a serial communication with the module and start sending AT commands.

However, it might be more practical to use a library. For example, the TinyGSM library knows which commands to send, and how to handle AT responses, and wraps that into the standard Arduino Client interface—that’s the library we’ll use in this tutorial.

Installing the TinyGSM Library

Open your Arduino IDE and go to Sketch Include Library > Manage Libraries. The Library Manager should open. Search for TinyGSM. Select the TinyGSM library by Volodymyr Shymanskyy.

Arduino IDE 2 Install TinyGSM Library using Libraries Manager

Installing the ArduinoHttpClient Library

You also need to install the ArduinoHttpClient library to make HTTPS requests. Go to Sketch Include Library > Manage Libraries, search for ArduinoHttpClient, and install it.

Arduino IDE 2 Install ArduinoHttpClient Library using Libraries Manager

Preparing the LILYGO T-SIM7000G ESP32 Board

Compatibility

This board supports 2G, LTE CAT-M1, and NB-IoT protocols. You can go to the following links to check if any of these protocols are supported in your country:

Check network protocols supported in your country;

Check NB-IoT providers in your country.

Before testing your board, you need to follow the next two steps:

1. Insert a nano SIM card;

Nano SIM card vodafone
SIM Card Connected to LILYGO T-SIM7000G ESP32

2. Connect the Full Band LTE antenna to the correct antenna port as illustrated in the following image:

Connect Band LTE Antenna LILYGO T-SIM7000G ESP32

Make HTTPS Get Requests – LILYGO T-SIM7000G ESP32

In this sample sketch, the ESP32 will initialize the modem, establish an internet connection using the SIM card data plan, and will make an HTTPS GET request to the following URL:

The URL returns the following ASCII art text:

example url random nerd tutorials logo
  1. Copy the following code to your Arduino IDE.
/*
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp32-https-requests-sim-card-sim7000g/
  
  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 library example: github.com/vshymanskyy/TinyGSM/blob/master/examples/HttpsClient/HttpsClient.ino
*/

// Select your modem
#define TINY_GSM_MODEM_SIM7000SSL
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb

// Set serial for debug console (to the Serial Monitor, default speed 115200)
#define SerialMon Serial
#define SerialAT Serial1

#include <TinyGsmClient.h>
#include <ArduinoHttpClient.h>

// Define the serial console for debug prints, if needed
#define TINY_GSM_DEBUG SerialMon
// #define LOGGING  // <- Logging is for the HTTP library

// Add a reception delay, if needed.
// This may be needed for a fast processor at a slow baud rate.
// #define TINY_GSM_YIELD() { delay(2); }

// set GSM PIN, if any
#define GSM_PIN ""

// flag to force SSL client authentication, if needed
// #define TINY_GSM_SSL_CLIENT_AUTHENTICATION

// Set your APN Details / GPRS credentials
const char apn[]      = "";
const char gprsUser[] = "";
const char gprsPass[] = "";

// Server details
const char server[]   = "gist.githubusercontent.com";
const char resource[] = "/RuiSantosdotme/7db8537cef1c84277c268c76a58d07ff/raw/d3fe4cd6eff1ed43e6dbd1883ab7eba8414e2406/gistfile1.txt";
const int  port       = 443;

TinyGsm        modem(SerialAT);

TinyGsmClientSecure client(modem);
HttpClient          http(client, server, port);

// 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 Serial Monitor baud rate
  SerialMon.begin(115200);
  delay(10);

  // Set LED OFF
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, HIGH);

  modemPowerOn();

  SerialMon.println("Wait...");

  // Set GSM module baud rate and Pins
  SerialAT.begin(UART_BAUD, SERIAL_8N1, PIN_RX, PIN_TX);
  delay(6000);

  // Restart takes quite some time
  // To skip it, call init() instead of restart()
  SerialMon.println("Initializing modem...");
  modem.restart();
  // modem.init();

  String modemInfo = modem.getModemInfo();
  SerialMon.print("Modem Info: ");
  SerialMon.println(modemInfo);

  // Unlock your SIM card with a PIN if needed
  if (GSM_PIN && modem.getSimStatus() != 3) {
    modem.simUnlock(GSM_PIN);
  }

  Serial.println("Make sure your LTE antenna has been connected to the SIM interface on the board.");
  delay(10000);
}

void loop() {
  modem.gprsConnect(apn, gprsUser, gprsPass);

  SerialMon.print("Waiting for network...");
  if (!modem.waitForNetwork()) {
    SerialMon.println(" fail");
    delay(10000);
    return;
  }
  SerialMon.println(" success");

  if (modem.isNetworkConnected()) {
    SerialMon.println("Network connected");
  }

  SerialMon.print(F("Performing HTTPS GET request... "));
  http.connectionKeepAlive();  // Currently, this is needed for HTTPS
  int err = http.get(resource);
  if (err != 0) {
    SerialMon.println(F("failed to connect"));
    delay(10000);
    return;
  }

  int status = http.responseStatusCode();
  SerialMon.print(F("Response status code: "));
  SerialMon.println(status);
  if (!status) {
    delay(10000);
    return;
  }

  SerialMon.println(F("Response Headers:"));
  while (http.headerAvailable()) {
    String headerName  = http.readHeaderName();
    String headerValue = http.readHeaderValue();
    SerialMon.println("    " + headerName + " : " + headerValue);
  }

  int length = http.contentLength();
  if (length >= 0) {
    SerialMon.print(F("Content length is: "));
    SerialMon.println(length);
  }
  if (http.isResponseChunked()) {
    SerialMon.println(F("The response is chunked"));
  }

  String body = http.responseBody();
  SerialMon.println(F("Response:"));
  SerialMon.println(body);

  SerialMon.print(F("Body length is: "));
  SerialMon.println(body.length());

  // Shutdown
  http.stop();
  SerialMon.println(F("Server disconnected"));

  modem.gprsDisconnect();
  SerialMon.println(F("GPRS disconnected"));

  // Do nothing forevermore
  while (true) {
    delay(1000);
  }
}

View raw code

  1. Insert your SIM card pin, if you have it. In my case, I disabled the pin.
#define GSM_PIN ""
  1. Insert your apn details on the following lines:
// Set your APN Details / GPRS credentials
const char apn[]  = "";
const char gprsUser[] = "";
const char gprsPass[] = "";

For example, in my case:

const char apn[]  = "net2.vodafone.pt";
const char gprsUser[] = "vodafone";
const char gprsPass[] = "vodafone";
  1. Go to Tools > Board and select ESP32 Dev Module.
Arduino IDE 2 Select ESP32 Dev Module Board and COM Port
  1. Finally, upload the code to your board.
Arduino IDE 2 Compile and Upload Sketch button

Then, open the Serial Monitor at a baud rate of 115200. Press the on-board RST button to restart the board.

Arduino IDE 2 Open Serial Monitor Button

Wait some time until the board connects to the network (in my case, it may take up to 2 minutes). You should get something similar in your Serial Monitor—see the picture below.

Demonstration ESP32 Make HTTPS Requests using SIM Card LILYGO T-SIM7000G

You can see that it identifies the SIM7000G module and connects to the network successfully. Finally, it establishes a connection to the example website, returns the “Random Nerd Tutorials” text and prints it in the response. If you see something similar in your Arduino IDE Serial Monitor, it means the code is running successfully.

How the Code Works

Let’s take a quick look at the parts of the code that are relevant to this example.

SIM Module

First, you need to define the module you’re using. The library is compatible with many different modules. To use the SIM7000G, include the following line:

#define TINY_GSM_MODEM_SIM7000SSL

Libraries

Include the TinyGSM and ArduinoHttpClient libraries.

#include <TinyGsmClient.h>
#include <ArduinoHttpClient.h>

APN Details

Insert the SIM card pin and APN details (in our case, it’s the following APN, user and pass):

// set GSM PIN, if any
#define GSM_PIN ""

// Set your APN Details / GPRS credentials
const char apn[]  = "net2.vodafone.pt";
const char gprsUser[] = "vodafone";
const char gprsPass[] = "vodafone";

Server Domain and Resource

Define the server (domain name), resource (URL path), and port where you want to make the HTTPS GET request:

// Server details
const char server[]   = "gist.githubusercontent.com";
const char resource[] = "/RuiSantosdotme/7db8537cef1c84277c268c76a58d07ff/raw/d3fe4cd6eff1ed43e6dbd1883ab7eba8414e2406/gistfile1.txt";
const int  port       = 443;

Initialize TinyGSMClient

Create a TinyGsmClient instance:

TinyGsm modem(SerialAT);

TinyGsmClientSecure client(modem);

HttpClient http(client, server, port);

SIM7000G pinout

The following lines set 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 setup(), you need to include the following instructions to turn on the modem:

pinMode(PWR_PIN, OUTPUT);
digitalWrite(PWR_PIN, HIGH);
delay(300);
digitalWrite(PWR_PIN, LOW);

Start Serial Communication

Start a serial communication with the modem:

SerialAT.begin(UART_BAUD, SERIAL_8N1, PIN_RX, PIN_TX);

Restart and Initialize the Modem

Call the following function to restart the modem:

SerialMon.println("Initializing modem...");
modem.restart();

You can also use the modem.init(). The difference between restart() and init() according to documentation: “restart() generally takes longer than init() but ensures the module doesn’t have lingering connections”.

Get Modem Info

You can use the getModemInfo() to get information about the modem.

String modemInfo = modem.getModemInfo();
SerialMon.print("Modem Info: ");
SerialMon.println(modemInfo);

If you’re using the same model, it will print in the Arduino IDE Serial monitor the following information (SIM7000G):

Demonstration print modem info ESP32 Make HTTPS Requests SIM Card LILYGO T-SIM7000G

Unlock and Connect GPRS

If your SIM card has a pin, the next command will use the pin and unlock the card:

if (GSM_PIN && modem.getSimStatus() != 3) {
  modem.simUnlock(GSM_PIN);
}

Next, connect the GPRS using the APN details:

modem.gprsConnect(apn, gprsUser, gprsPass);

SerialMon.print("Waiting for network...");
if (!modem.waitForNetwork()) {
  SerialMon.println(" fail");
  delay(10000);
  return;
}
SerialMon.println(" success");

To check if it is connected, you can use the isNetworkConnected() method:

if (modem.isNetworkConnected()) {
  SerialMon.println("Network connected");
}

Perform the HTTPS GET Request

Making an HTTPS GET request is fairly easy using the ArduinoHttpClient library, you just need to call http.get(resource).

SerialMon.print(F("Performing HTTPS GET request... "));
http.connectionKeepAlive();  // Currently, this is needed for HTTPS
int err = http.get(resource);
if (err != 0) {
  SerialMon.println(F("failed to connect"));
  delay(10000);
  return;
}

If the request is successful, it will print the Response status code: 200 in your Serial Monitor.

int status = http.responseStatusCode();
SerialMon.print(F("Response status code: "));
SerialMon.println(status);
if (!status) {
  delay(10000);
  return;
}
Demonstration Response code 200 ESP32 Make HTTPS Requests SIM Card LILYGO T-SIM7000G

The following snippet is for debugging purposes, it prints the response header and content length.

SerialMon.println(F("Response Headers:"));
while (http.headerAvailable()) {
  String headerName  = http.readHeaderName();
  String headerValue = http.readHeaderValue();
  SerialMon.println("    " + headerName + " : " + headerValue);
}

int length = http.contentLength();
if (length >= 0) {
  SerialMon.print(F("Content length is: "));
  SerialMon.println(length);
}
if (http.isResponseChunked()) {
  SerialMon.println(F("The response is chunked"));
}
Demonstration Response Headers length ESP32 Make HTTPS Requests SIM Card LILYGO T-SIM7000G

The response body is the actual interesting part of the response. The String body is what you can use to scrap some text or information from a website.

String body = http.responseBody();
SerialMon.println(F("Response:"));
SerialMon.println(body);

SerialMon.print(F("Body length is: "));
SerialMon.println(body.length());

In this case, it’s just a sample project, so we’ve returned some characters saying “Random Nerd Tutorials” in ASCII art. In future projects, we’ll scrap useful information from websites. You can easily modify this example to get data from other website by changing the URL and resource.

Demonstration Response ESP32 Make HTTPS Requests SIM Card LILYGO T-SIM7000G

Finally, stop the http connection and disconnect the GPRS modem from the Internet.

http.stop();
SerialMon.println(F("Server disconnected"));

modem.gprsDisconnect();
SerialMon.println(F("GPRS disconnected"));

That’s it, those are the most relevant code parts of this sketch.

Wrapping Up

In this guide, you’ve learned how to use the LILYGO T-SIM7000G ESP32 board to perform HTTPS GET requests. This tutorial can also be applied if you’re using a “regular” ESP32 connected to an external SIM7000G module, just make sure you adjust the pinout definition if needed.

With this sample sketch, you can connect GPRS to get some sample text from any website using an HTTPS GET request. In future guides, we’ll scrap useful information from websites or make API requests to get data or interact with web services.

You may also like the following SIM7000G tutorials:

Learn more about the ESP32 with our resources:

Thanks for reading.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

9 thoughts on “ESP32: Make HTTPS Requests using SIM Card – LILYGO T-SIM7000G”

  1. The web of LILYGO says that this model don’t support 4G and most countries, including Portugal, are shutting down their 3G networks.
    Maybe is time to switch to another model like LILYGO TTGO T-SIM7600.

    Reply
  2. You can also try this on this board
    bharatpi.net/product/bharat-pi-4g-lte-module-with-esp32-simcom-a7672-with-gps/

    They have a simple SMA connector comes with Antenna, Sim Slot and SD Card slot.

    They also have the sample code in their github page which works right out of the box, This board seems to come with GPS also.
    here is the sample code
    github.com/Bharat-Pi/iot-projects/tree/main/BharatPi_4G_based_Http_SMS_Samplecode

    Reply
  3. Hi,

    I have a problem. I replied the previous code on a SIM800 board based on ESP32. All is connected right, but when it try to carry on the “GET” command, it say “failed to connect”.
    Is all OK with the url and the code you show in the post?

    BR.

    Reply
  4. i am using SIMCOM A7670E 4G module i want to mak https post request using ESP8266 OR ESP32 for serial communication how ??

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.