Hi guys
I'm trying call a HTTP POST method using SIM800L and an ARDUINO MEGA
I created an endpoint in firebase
and I'm trying to send a post request to this endpoint sending a JSON
I tested it on insomnia and the endpoint is working normally
However, when I try to execute the post method using the Arduino code below, I am unsuccessful.
#define TINY_GSM_MODEM_SIM800 // Defina o modelo do modem aqui
#include <TinyGPS.h>
#include <ArduinoJson.h>
#include <TinyGsmClient.h>
String responseGSM;
String imei;
int signal = 0;
TinyGPS gps1;
String apn = "timbrasil.br"; //APN
String apn_u = ""; //APN-Username
String apn_p = ""; //APN-Password
String url = "https://myEndpoint";
StaticJsonDocument<256> jsonDocument;
// Inicializa os objetos
TinyGsm modem(Serial2);
void setup() {
Serial.begin(9600); // Monitor Serial
Serial1.begin(9600); // GPS - RX:19 TX:18 - (Arduino mega2560)
Serial2.begin(9600); // GSM - RX:17 TX:16 - (Arduino mega2560)
delay(10000);
Serial.println("Aguardando sinal dos satélites...");
// Inicializa o modem
modem.restart();
delay(10000);
gsm_config_gprs();
jsonDocument["imei"] = modem.getIMEI();
}
void loop() {
bool recebido = false;
static unsigned long delayPrint;
// Módulo GPS
while (Serial1.available()) {
char cIn = Serial1.read();
recebido = (gps1.encode(cIn) || recebido);
}
if (recebido && ((millis() - delayPrint) > 1000)) {
delayPrint = millis();
float latitude, longitude;
unsigned long idadeInfo;
gps1.f_get_position(&latitude, &longitude, &idadeInfo);
if (latitude != TinyGPS::GPS_INVALID_F_ANGLE) {
jsonDocument["latitude"] = latitude;
}
if (longitude != TinyGPS::GPS_INVALID_F_ANGLE) {
jsonDocument["longitude"] = longitude;
}
int ano;
byte mes, dia, hora, minuto, segundo, centesimo;
gps1.crack_datetime(&ano, &mes, &dia, &hora, &minuto, &segundo, ¢esimo, &idadeInfo);
float altitudeGPS = gps1.f_altitude();
if ((altitudeGPS != TinyGPS::GPS_INVALID_ALTITUDE) && (altitudeGPS != 1000000)) {
//jsonDocument["altitude_cm"] = altitudeGPS;
}
float velocidade = gps1.f_speed_kmph();
//jsonDocument["velocidade_kmph"] = velocidade;
unsigned long sentido = gps1.course();
//jsonDocument["sentido_grau"] = float(sentido) / 100;
unsigned short satelites = gps1.satellites();
unsigned long precisao = gps1.hdop();
if (satelites != TinyGPS::GPS_INVALID_SATELLITES) {
//jsonDocument["satelites"] = satelites;
}
if (precisao != TinyGPS::GPS_INVALID_HDOP) {
//jsonDocument["precisao_cent"] = precisao;
}
String jsonString;
serializeJson(jsonDocument, jsonString);
Serial.println(modem.getSignalQuality());
Serial.println(jsonString);
// Envia a requisição HTTP POST
delay(2000);
signal = modem.getSignalQuality();
Serial.println("qualidade do sinal:");
Serial.println(signal);
if(signal >= 10 && signal <= 31){
gsm_http_post(jsonString);
}
}
}
void gsm_http_post( String postdata) {
Serial.println(" --- Start GPRS & HTTP --- ");
sendCommand(Serial2, "AT+SAPBR=1,1");
sendCommand(Serial2, "AT+SAPBR=2,1");
sendCommand(Serial2, "AT+HTTPINIT");
sendCommand(Serial2, "AT+HTTPPARA=CID,1");
sendCommand(Serial2, "AT+HTTPPARA=URL," + url);
sendCommand(Serial2, "AT+HTTPPARA=CONTENT,application/x-www-form-urlencoded");
sendCommand(Serial2, "AT+HTTPDATA=192,5000");
sendCommand(Serial2, postdata);
sendCommand(Serial2, "AT+HTTPACTION=1");
sendCommand(Serial2, "AT+HTTPREAD");
sendCommand(Serial2, "AT+HTTPTERM");
sendCommand(Serial2, "AT+SAPBR=0,1");
}
void gsm_config_gprs() {
Serial.println(" --- CONFIG GPRS --- ");
sendCommand(Serial2, "AT+SAPBR=3,1,Contype,GPRS");
sendCommand(Serial2, "AT+SAPBR=3,1,APN," + apn);
if (apn_u != "") {
sendCommand(Serial2, "AT+SAPBR=3,1,USER," + apn_u);
}
if (apn_p != "") {
sendCommand(Serial2, "AT+SAPBR=3,1,PWD," + apn_p);
}
}
void sendCommand(HardwareSerial &serial, String command) {
serial.println(command);
long wtimer = millis();
while (wtimer + 3000 > millis()) {
while (serial.available()) {
Serial.write(serial.read());
}
}
Serial.println();
}
String readResponse(HardwareSerial &serial) {
String response = "";
while (serial.available()) {
char c = serial.read();
response += c;
}
return response;
}
I tried to base myself by seeing this documentation
When executing the commands, I get a DNS ERROR
+HTTPACTION: 1,603,0
600 Not HTTP PDU
601 Network Error
602 No memory
603 DNS Error
604 Stack Busy
console with at command responses
Aguardando sinal dos satélites...
--- CONFIG GPRS ---
Call Ready
SMS Ready
*PSUTTZ: 2024,8,27,17,53,28,"-12",
OK
OK
19
{"imei":"867372050747224","latitude":-3.754134,"longitude":-38.51607}
qualidade do sinal:
19
--- Start GPRS & HTTP ---
OK
+SAPBR: 1,1,"100.115.159.171"
OK
OK
OK
OK
OK
DOWNLOAD
OK
OK
+HTTPACTION: 1,603,0
OK
OK
OK
in short
When I test the endpoint through insomnia passing the following json it works
{"imei":"867372050747224","latitude":-3.754134,"longitude":-38.51607}
the data are
stored correctly in firestore
however, in the code passing the same json it does not work
the code runs without any apparent errors but does not add any records to firebase
Would anyone have any tips to help me?