c++firebasehttpsgsmat-command

SIM7600E + ESP32: Unable to Send HTTPS POST to Firebase, HTTP_PEER_CLOSED


Got a project using ESP32 with SIM7600E 4g LTE module, and im trying to get it to send data to firebase realtime database. i can get it to successfully initialise and connect to cellular network. ive tested it sending sms and pining other servers. Whenever I try to post something to the database ,i get something like +HTTP_PEER_CLOSED. i thought this was because firebase was rejecting http requests, so i tried sending it as a HTTPS request, and failed at this too. Honestly been stuck on this for a week and could use some help really badly

#include <Arduino.h>
#include <HardwareSerial.h>

const long Baudrate = 115200;
const char RX_Pin = 16;
const char TX_Pin = 17;


HardwareSerial sim(1);

void command(String command, unsigned long timeout = 1000) {
    sim.println(command);
    unsigned long startTime = millis();
    while (millis() - startTime < timeout) {
        if (sim.available()) {
            String response = sim.readString();
            Serial.println(response);
            break;
        }
    }
}

void SetHTTPS() {
    command("");
}

void upload() {
    command("AT+CSSLCFG=\"sslversion\",0,3");
    command("AT+CSSLCFG=\"ignorelocaltime\",0,1");
    command("AT+CSSLCFG=\"seclevel\",0,0");

    command("AT+HTTPINIT", 5000);
    command("AT+HTTPPARA=\"URL\",\"https://DB-URL-STUFF.firebasedatabase.app/test.json?auth=AUTH-KEY\"");
    command("AT+HTTPPARA=\"CONTENT\",\"application/json\"");

    sim.println("AT+HTTPDATA=35,10000");
    delay(3000);

    if (sim.available()) {
        String response =  sim.readString();
        if (response.indexOf("DOWNLOAD") != -1) {
            Serial.println("Sending JSON...");
            sim.println("{\"message\":\"test from SIM7600\"}");
            delay(1000);
            sim.write(26);
            delay(2000);
        }
    }

    command("AT+HTTPACTION?");
    command("AT+HTTPACTION=1", 5000);
    command("AT+HTTPREAD");
    command("AT+HTTPTERM");
}

void setup() {
    Serial.begin(115200);
    sim.begin(Baudrate, SERIAL_8N1, RX_Pin, TX_Pin);
    command("AT"); // test stuff
    command("ATI"); // module status stuff
    command("AT+CSQ"); // signal
    command("AT+CGDCONT=1,\"IP\",\"everywhere\"");
    command("AT+CGATT=1");
    command("AT+CGACT=1,1");
    command("AT+NETOPEN");
    delay(100);
    upload();
}

void loop() {
    while (sim.available()) {
        Serial.write(sim.read());
    }
}

Output

AT

OK

ATI

Manufacturer: SIMCOM INCORPORATED

Model: SIMCOM_SIM7600E-L1C

Revision: SIM7600M11_A_V2.0.1

IMEI: 862499070415105

+GCAP: +CGSM

OK

AT+CSQ

+CSQ: 15,99

OK

AT+CGDCONT=1,"IP","everywhere"

OK

AT+CGATT=1

OK

AT+CGACT=1,1

OK

AT+NETOPEN

+IP ERROR: Network is already opened

ERROR

AT+CSSLCFG="sslversion",0,3

OK

AT+CSSLCFG="ignorelocaltime",0,1

OK

AT+CSSLCFG="seclevel",0,2

ERROR

AT+HTTPINIT

OK

AT+HTTPPARA="URL","https://DB-URL-STUFF.firebasedatabase.app/test.json?auth=AUTH-KEY"

OK

AT+HTTPPARA="CONTENT","application/json"

OK

Sending JSON...

{"message":"test from SIM7600"}

A

OK

AT+HTTPACTION=1

OK

+HTTPACTION: 1,400,77

AT+HTTPREAD

ERROR

AT+HTTPTERM

OK

Solution

  • so the problem was a certificate issue, so I went and downloaded the GTS Root R1 certificate, i also switched to using Libraries instead of entering in raw AT commands. (also had to put the auth key in the db path ) . The modem setup function needs some work because it says that it failed to restart even when the modem successfully restarts, and there is the occasional error about buffer size.

    waiting for network...
    connecting to APN: everywhere
    moden connected to networkLocal IP: 10.97.255.119
    Signal Quality: 
    18Uploading to firebase...
    payload: {"message":"test from TinyGSM","timestamp":30583}
    Status code: 200
    response {"name":"-OYghGheoAEDMeCMcCdr"}
    Uploading to firebase...
    payload: {"message":"test from TinyGSM","timestamp":64048}
    [ 64267][E][ssl__client.cpp:45] _handle_error(): [send_ssl_data():919]: (-27136) SSL - A buffer is too small to receive or write a message
    [ 64280][E][SSLClient.cpp:384] write(): Error sending data to SSL connection. Stopping SSLClient...
    Status code: -3
    response
    Uploading to firebase...
    payload: {"message":"test from TinyGSM","timestamp":154300}
    Status code: 200
    response {"name":"-OYghjyyzDfXChRhxgEf"}
    
    #define TINY_GSM_MODEM_SIM7600
    #include <Arduino.h>
    #include <SSLClient.h>
    #include <ArduinoHttpClient.h>
    #include <TinyGsmClient.h>
    #include "certs.h"
    
    const long Baudrate = 115200;
    const char RX_Pin = 16;
    const char TX_Pin = 17;
    const char firebase[] = "DB-URL.firebasedatabase.app";
    String path = "/test.json?auth=AUTH-KEY-HERE";
    const char apn[] = "everywhere";
    
    HardwareSerial sim(1);
    TinyGsm modem(sim);
    TinyGsmClient baseClient(modem, 0);
    SSLClient secureLayer(&baseClient);
    HttpClient client = HttpClient(secureLayer, firebase, 443);
    
    
    void modemSetup() {
      Serial.println("initalising modem...");
    
      if (!modem.restart()) {
        Serial.println("Failed to restart modem");
        return;
      }
    
      String name = modem.getModemName();
      Serial.println("Modem: " + name);
    
      String imei = modem.getIMEI();
      Serial.println("IMEI: " + imei);
    
      Serial.println("waiting for network...");
      if (!modem.waitForNetwork()) {
        Serial.println("network timeout");
        return;
      }
    
      Serial.println("connecting to APN: " + String(apn));
      if (!modem.gprsConnect(apn, "","")) {
        Serial.println("GPRS connection failed");
        return;
      }
    
      Serial.print("moden connected to network");
    
      IPAddress local = modem.localIP();
      Serial.print("Local IP: ");
      Serial.println(local);
    }
    
    void upload() {
      Serial.println("Uploading to firebase...");
    
      String payload = "{\"message\":\"test from TinyGSM\",\"timestamp\":";
      payload += millis();
      payload += "}";
    
      Serial.println("payload: " + payload);
    
      client.beginRequest();
      client.post(path);
      client.sendHeader("Content-Type", "application/json");
      client.sendHeader("Content-Length", payload.length());
      client.beginBody();
      client.print(payload);
      client.endRequest();
    
      int statusCode = client.responseStatusCode();
      String response = client.responseBody();
    
      Serial.print("Status code: ");
      Serial.println(statusCode);
      Serial.print("response ");
      Serial.println(response);
    
    }
    
    
    void setup() {
        Serial.begin(115200);
        sim.begin(Baudrate, SERIAL_8N1, RX_Pin, TX_Pin);
        secureLayer.setCACert(root_ca);
        delay(3000);
        modemSetup();
    }
    
    void loop() {
      upload();
      delay(3000);
    
      if (Serial.available()) {
        sim.write(Serial.read());
      }
      if (sim.available()) {
        Serial.write(sim.read());
      }
    }