0

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
4
  • I'm confident that Firebase requires TLS so you will get rejected using HTTP. The HTTPS rejection is probably a certificate issue. Can you open any HTTPS connections? Any Google HTTPS connections (https://www.google.com?)? Commented Aug 26 at 3:42
  • sent a GET request to https://www.google.com and the AT+HTTPACTION=0 returned +HTTPACTION: 0,200,138 which i think shows the HTTPS request succeeded so looks like it can open HTTPS connections Commented Aug 26 at 13:04
  • Agreed. I continue to suspect that perhaps your device does not have current/valid certs for the Firebase endpoints. Is there a way for you to validate this? Commented Aug 26 at 15:18
  • Try AT+CEER=2 to see if you get a more detailed error message. Commented Aug 26 at 15:24

2 Answers 2

1

Do yourself a favour and read ALL of chapter 5 in the V.250 specification to get a proper understanding of how AT commands operate. It is a really. really important document. Reading tip: ignore everything related to IA5 and just treat all text as ASCII. Also you should not change the value of any S-registers, so you can skip most of that as well. Only pay attention that a command line is terminated by a single character (from S3, defaulting to '\r'), and not whatever println is outputting.


The biggest issue here is nothing will ever work reliably or at all when you call delay instead of reading and parsing the responses given back from the modem. You MUST read and parse the responses given back. Just as you would not write a HTTP client that ignores the responses a HTTP server sends to it, you should not write a AT command program that ignores the responses the modems sends to it.

I am suspecting that delay(3000); is a misguided attempt to wait for a > prompt or similar before sending a payload, and that is this is also fundamentally flawed and has a high chance of aborting the command (which also will give final result code OK, so there is no way to observe). See this answer for more details on payload sending waiting.

Sign up to request clarification or add additional context in comments.

Comments

0

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());
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.