How to Use SIM7600 with ESP32 to Send API Requests

Protonest IoT
4 min readAug 13, 2024

--

Flyer image

When it comes to integrating cellular connectivity into your ESP32 projects, the SIM7600 module is a reliable choice. The SIM7600 supports 4G and can achieve data speeds of up to 150 Mbps, whereas the SIM800 and SIM900 only support 2G. Additionally, the SIM7600 has a built-in GPS, which the SIM800 and SIM900 lack. It also offers SSL/TLS support, a feature not available in the SIM800 and SIM900.

While the power consumption and price are a bit higher, the SIM7600 remains crucial for IoT applications.

Whether you’re working on a remote IoT device or a mobile data logger, the SIM7600 provides a versatile way to send data over the internet using cellular networks. In this guide, we’ll walk you through how to use the SIM7600 with an ESP32 to send API requests.

Components

Wiring it Up

You can follow the below circuit diagram in wiring the sim7600.

Circuit diagram for implementation, TXD2 (GPIO 17 on ESP32) to SIM7600 RXD, RXD2 (GPIO 16 on ESP32) to SIM7600 TXD, Power and GND connections as needed.

Now, I will explain the firmware step by step

Setting Up Serial Communication

In the code below, we set up the serial communication between the ESP32 and the SIM7600. We’ll use the second serial port of the ESP32, ‘HardwareSerial sim7600(1);’, and define the pins for RX and TX,

HardwareSerial sim7600(1); // Use the second serial port of ESP32

Sending AT Commands

Before sending API requests, you need to ensure that your SIM7600 module is configured and connected to the cellular network. This involves sending a few AT commands to the module. Here’s a snippet that does just that,

void setup() {
Serial.begin(115200);
sim7600.begin(115200, SERIAL_8N1, 16, 17); // Initialize SIM7600 communication

sendData("AT", 1000, true); // Check communication
sendData("AT+CRESET", 20000, true); // Reset the module
sendData("AT+CCID", 3000, true); // Check SIM card
sendData("AT+CREG?", 3000, true); // Check network registration
sendData("AT+CGATT=1", 1000, true); // Attach to GPRS
sendData("AT+CGACT=1,1", 1000, true); // Activate PDP context
}

In this setup code, we’re initializing the SIM7600 and checking that it’s properly connected and ready to send data.

Sending Data to an API

Now, let’s get to the fun part-sending data to an API. Imagine you’re collecting temperature data, and you want to send this to a remote server. The following function shows how you can do this using an HTTP POST request,

void sendDataToBackend(const char* url, const char* dataType, String value) {
String data = "{\"type\": \"" + String(dataType) + "\", \"value\": " + value + "}";

sendData("AT+HTTPINIT", 2000, true);
sendData((String("AT+HTTPPARA=\"URL\",\"") + url + "\"").c_str(), 2000, true);
sendData("AT+HTTPPARA=\"CONTENT\",\"application/json\"", 3000, true);
sendData((String("AT+HTTPDATA=") + data.length() + ",10000").c_str(), 2000, true);

sim7600.print(data);
delay(3000); // Wait for data to be sent

sendData("AT+HTTPACTION=1", 6000, true); // Send the POST request
sendData("AT+HTTPREAD=0,1000", 6000, true); // Read the server response
sendData("AT+HTTPTERM", 2000, true); // Terminate HTTP session
}

JSON body is in the form,

{
"type": "temperature",
"value": 36.5
}

Here’s what’s happening

  • Initialize HTTP Service: We start by sending ‘AT+HTTPINIT’ to begin an HTTP session.
  • Set the URL: ‘AT+HTTPPARA=” URL”,”<your_api_url>”’ specifies the endpoint where the data will be sent.
  • Set Content Type: ‘AT+HTTPPARA=” CONTENT”,” application/json”’ tells the server that we’re sending JSON data.
  • Send the Data: We prepare the data string in JSON format and send it using ‘sim7600.print(data);’.
  • Send the Request: The ‘AT+HTTPACTION=1’ command sends the HTTP POST request.
  • Read the Response: Finally, ‘AT+HTTPREAD=0,1000’ reads the server’s response, which is crucial for debugging.

Final code

#include <HardwareSerial.h>
#include <Wire.h>
#include <ArduinoJson.h>

HardwareSerial sim7600(1); // Use the second serial port of ESP32

// Constants for API requests
const char* apiUrlTemp = "http://example.com/api/temp/current"; // Replace with your API endpoint

// Initialization of SIM7600
void setup() {
Serial.begin(115200);
sim7600.begin(115200, SERIAL_8N1, 16, 17); // Initialize serial communication with SIM7600

// Initialize the SIM7600 module with basic AT commands
sendData("AT", 1000, true); // Check communication
sendData("AT+CRESET", 20000, true); // Reset the module
sendData("AT+CCID", 3000, true); // Check SIM card
sendData("AT+CREG?", 3000, true); // Check network registration
sendData("AT+CGATT=1", 1000, true); // Attach to GPRS
sendData("AT+CGACT=1,1", 1000, true); // Activate PDP context
}

void loop() {
// Example usage: Sending temperature data
float temperature = 36.5; // Replace with actual sensor reading
sendDataToBackend(apiUrlTemp, "temperature", String(temperature));
delay(10000);
}

// Function to send data to the backend via API
void sendDataToBackend(const char* url, const char* dataType, String value) {
String data = "{\"type\": \"" + String(dataType) + "\", \"value\": " + value + "}"; // Replace this according to your your Json body
sendData("AT+HTTPINIT", 2000, true);
sendData((String("AT+HTTPPARA=\"URL\",\"") + url + "\"").c_str(), 2000, true);
sendData("AT+HTTPPARA=\"CONTENT\",\"application/json\"", 3000, true);
sendData((String("AT+HTTPDATA=") + data.length() + ",10000").c_str(), 2000, true);

sim7600.print(data);
delay(3000); // Wait for data to be sent

sendData("AT+HTTPACTION=1", 6000, true); // Send the POST request
sendData("AT+HTTPREAD=0,1000", 6000, true); // Read the server response
sendData("AT+HTTPTERM", 2000, true); // Terminate HTTP session
}

// Function to send AT commands to the SIM7600 module
String sendData(String command, const int timeout, boolean debug) {
String response = "";
sim7600.println(command);

long int time = millis();
while ((time + timeout) > millis()) {
while (sim7600.available()) {
char c = sim7600.read();
response += c;
}
}
if (debug) {
Serial.print(response);
}
return response;
}

Results

You can see the response is received after successful API call.

Hope you enjoyed the guide.

Contact us for any consultations or projects related to SIM7600.

Email: udara@protonest.co

Protonest for more details.

Protonest specializes in transforming IoT ideas into reality. We offer prototyping services from concept to completion. Our commitment ensures that your visionary IoT concepts become tangible, innovative, and advanced prototypes.

Our Website: https://www.protonest.co/

Cheers!

--

--

Protonest IoT
Protonest IoT

Written by Protonest IoT

We make your IoT ideas a reality

No responses yet