ESP32 tinygsm SIM70000G http post request to server not successful

Hi all, I'm new to using esp(this is my first project actually) and I'm struggling to find out answer for my question.
I'm using the esp board to send data to rest api of my own deployed on AWS. The data sent is JSON.
If I use the onboard Wifi everything works fine , but when i use gprs the server doesn't respond and here isPreformatted text my codePreformatted text for this:

// Your GPRS credentials (leave empty, if not needed)
const char apn[]      = "internet.apn"; // APN (example: internet.vodafone.pt) use https://wiki.apnchanger.org
const char gprsUser[] = ""; // GPRS User
const char gprsPass[] = ""; // GPRS Password

// SIM card PIN (leave empty, if not defined)
const char simPIN[]   = "0000"; 

// Server details
// The server variable can be just a domain name or it can have a subdomain. It depends on the service you are using
const char server[] = "20.43.**.**"; // domain name: example.com, maker.ifttt.com, etc
const char resource[] = "/api/v1/position_data";         // resource path, for example: /post-data.php
const int  port = 443;                             // server port number

// TTGO T-Call pins
#define MODEM_RST            5
#define MODEM_PWKEY          4
#define MODEM_POWER_ON       23
#define MODEM_TX             27
#define MODEM_RX             26
#define I2C_SDA              21
#define I2C_SCL              22

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

// Configure TinyGSM library
#define TINY_GSM_MODEM_SIM800      // Modem is SIM800
#define TINY_GSM_RX_BUFFER   1024  // Set RX buffer to 1Kb

// Define the serial console for debug prints, if needed
//#define DUMP_AT_COMMANDS

#include <Wire.h>
#include <TinyGsmClient.h>

#ifdef DUMP_AT_COMMANDS
  #include <StreamDebugger.h>
  StreamDebugger debugger(SerialAT, SerialMon);
  TinyGsm modem(debugger);
#else
  TinyGsm modem(SerialAT);
#endif

// I2C for SIM800 (to keep it running when powered from battery)
TwoWire I2CPower = TwoWire(0);

// TinyGSM Client for Internet connection
TinyGsmClient client(modem);

#define uS_TO_S_FACTOR 1000000UL   /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP  3600        /* Time ESP32 will go to sleep (in seconds) 3600 seconds = 1 hour */

#define IP5306_ADDR          0x75
#define IP5306_REG_SYS_CTL0  0x00

bool setPowerBoostKeepOn(int en){
  I2CPower.beginTransmission(IP5306_ADDR);
  I2CPower.write(IP5306_REG_SYS_CTL0);
  if (en) {
    I2CPower.write(0x37); // Set bit1: 1 enable 0 disable boost keep on
  } else {
    I2CPower.write(0x35); // 0x37 is default reg value
  }
  return I2CPower.endTransmission() == 0;
}

void setup() {
  // Set serial monitor debugging window baud rate to 115200
  SerialMon.begin(115200);


  // Keep power when running from battery
  bool isOk = setPowerBoostKeepOn(1);
  SerialMon.println(String("IP5306 KeepOn ") + (isOk ? "OK" : "FAIL"));

  // Set modem reset, enable, power pins
  pinMode(MODEM_PWKEY, OUTPUT);
  pinMode(MODEM_RST, OUTPUT);
  pinMode(MODEM_POWER_ON, OUTPUT);
  digitalWrite(MODEM_PWKEY, LOW);
  digitalWrite(MODEM_RST, HIGH);
  digitalWrite(MODEM_POWER_ON, HIGH);

  // Set GSM module baud rate and UART pins
  SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
  delay(3000);

  // Restart SIM800 module, it takes quite some time
  // To skip it, call init() instead of restart()
  SerialMon.println("Initializing modem...");
  modem.restart();
  // use modem.init() if you don't need the complete restart

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

  // Configure the wake up source as timer wake up  
  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
}

void loop() {
  SerialMon.print("Connecting to APN: ");
  SerialMon.print(apn);
  if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
    SerialMon.println(" fail");
  }
  else {
    SerialMon.println(" OK");
    
    SerialMon.print("Connecting to ");
    SerialMon.print(server);
    if (!client.connect(server, port)) {
      SerialMon.println(" fail");
    }
    else {
      SerialMon.println(" OK");
    
      // Making an HTTP POST request
      SerialMon.println("Performing HTTP POST request...");
      // Prepare your HTTP POST request data
      String httpRequestData = "{\"latitude\":\"5656\",\"longitude\":\"5656\",\"altitude\":\"5656\",\"speed\":\"5656\",\"deviceId\":\"111112222200000\"}";
    
      client.print(String("POST ") + resource + " HTTP/1.1\r\n");
      client.print(String("Host: ") + server + "\r\n");
      client.println("Connection: close");
      client.println("Content-Type: application/json");
      client.print("Content-Length: ");
      client.println(httpRequestData.length());
      client.println();
      client.println(httpRequestData);

      unsigned long timeout = millis();
      while (client.connected() && millis() - timeout < 10000L) {
        // Print available data (HTTP response from server)
        while (client.available()) {
          char c = client.read();
          SerialMon.print(c);
          timeout = millis();
        }
      }
      SerialMon.println();
    
      // Close client and disconnect
      client.stop();
      SerialMon.println(F("Server disconnected"));
      modem.gprsDisconnect();
      SerialMon.println(F("GPRS disconnected"));
    }
  }
  // Put ESP32 into deep sleep mode (with timer wake up)
  esp_deep_sleep_start();
}

I successfully connect to the mobile network. The difference here is that I had to use the IP address of my server in order to connect to it. I've I used the URL as the one in the Wifi code but it never connects to the server. So I connect to the server as well but when I try to post JSON to the api endpoint nothing happens and there is no error as well. So my best guess is that the problem is somewhere aroung here:

 // Making an HTTP POST request
      SerialMon.println("Performing HTTP POST request...");
      // Prepare your HTTP POST request data
      String httpRequestData = "{\"latitude\":\"5656\",\"longitude\":\"5656\",\"altitude\":\"5656\",\"speed\":\"5656\",\"deviceId\":\"111112222200000\"}";
    
      client.print(String("POST ") + resource + " HTTP/1.1\r\n");
      client.print(String("Host: ") + server + "\r\n");
      client.println("Connection: close");
      client.println("Content-Type: application/json");
      client.print("Content-Length: ");
      client.println(httpRequestData.length());
      client.println();
      client.println(httpRequestData);

This is Serial output

Initializing modem...
Connecting to APN: internet.a1.bg OK
Connecting to 20.43.**.** OK
Performing HTTP POST request...

Server disconnected
GPRS disconnected

Can anyone help me please .

how are you powering the modem? it can take 2amps during transmission
if you are powering it from the ESP32 you can have problems
try an external power supply

1 Like

@horace I am using lilygo T-Sim 7000G board it has inbuilt esp32, tsim 7000G gps and 18650 batery ,, so I dont think power supply will be the issue.

Hi kaustubhkesarwani,
I am working on the similar stuff, and want to upload my ESP32 lilygo data on the web server using GSM (A7670). Have you managed to resolve your problem? How you did server side api call?

Thanks in advance!
Ivica

can you communicate with the modem using AT commands

I used lilygo github test case for A7670E and I can download data via gsm modem. Idea is to use in addition loRA to collect data from nearby sensors and then upload on the web using modem SIM mobile data. Tried to add lora sparkfun RFM97 as receiver but struggling as Lilygo doesn't have pin 5 available....

Cheers
Ivica

what is the problem ? can you use a different pins or even a different LoRa module?

This is fully packed board, having SIM A7670E and a normal SD card , GPS and it is already using SPI and pins for MISO/MOSI... so many pins have already been taken and I am not an expert in SPI multiple devices. In theory, I know it is possible.
Based on their github example

pins in use are:
#define PIN_DTR 25
#define PIN_TX 26
#define PIN_RX 27
#define PWR_PIN 4
#define BAT_ADC 35
#define BAT_EN 12
#define PIN_RI 33
#define RESET 5

#define SD_MISO 2
#define SD_MOSI 15
#define SD_SCLK 14
#define SD_CS 13

So if I want to add LoRA then I have to use alternative pins, and not sure what is the best / possible. What would you use to connect LoRA module in my case? How to define alternative SPI for Lora? In the tutorial for RFM97 they use pins for DIO0, DIO1, and standard MOSI/MISO/CS/SCK/RST ... Do you have any suggestions for LoRA module to use?

Cheers,
Ivica

you can attach multiple devices to the SPI (MISO MOSI and SCLK) pins but each device requires a seperate CS chip select pin - the RFM97 also requires DIO0 and DIO1 connected (used to indicate transmission/receive complete)
you could consider using a LoRa device which has a serial interface, e.g. Grove LoRa E5,, Ebyte E220, etc

  1. do you require LoRaWAN or just LoRa point to point?
  2. check what frequency LoRa devices are allowed in your country

Edit: using a RA-02 lora module with the lora.h library the CS pin can be set using setpins() function call

  Serial.println("LoRa Receiver");
  // setPins(int ss = LORA_DEFAULT_SS_PIN, int reset = LORA_DEFAULT_RESET_PIN, int dio0 = LORA_DEFAULT_DIO0_PIN);
  LoRa.setPins(10, 9, 2);  // 10 for UNO and Nano, 53 for Mega
  if (!LoRa.begin(433E6)) {
    Serial.println("Starting LoRa failed!");
    while (1)      ;
  }
  Serial.println("Lora Receiver started");

on the nano the defualt CS pin is D10 (first parameter of setpins) but can be changed to D7, D6, etc

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.