Hello everyone,
"Dear all,
I'm currently engaged in a project involving the ESP32 TTGO module paired with the SIM7600G-H. My objective is to make an HTTP POST request to the Google Firebase API using AT Commands for GSM.
However, I'm encountering an issue where I'm only receiving 830 bytes of the actual response, which should be around 1.3KB. Furthermore, I'm only able to print out 330 bytes from the received data before encountering the message "+HTTPREAD:0", which I understand marks the end of the response.
Thanks.
#include <Arduino.h>
#include <ArduinoJson.h>
// Define the UART RX pin
#define RX_PIN 26
#define TX_PIN 27
#define PWR_PIN 4
const char* apiUrl = "https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=AIzaSyAp7Q_Vz6llnLX6rN3go0H4_e1EbS-YOeY";
const char* email = "admin@12.com";
const char* password1 = "s5@spark";
const char* returnSecureToken = "true";
String response;
// Define the buffer size for incoming data
char buffer[4096] = {0};
void setup()
{
// Start serial communication
pinMode(PWR_PIN, OUTPUT);
digitalWrite(PWR_PIN, HIGH);
delay(300);
digitalWrite(PWR_PIN, LOW);
Serial.begin(115200);
Serial2.begin(115200, SERIAL_8N1, RX_PIN, TX_PIN);
Serial2.setRxBufferSize(4096);
Serial.setTxBufferSize(4096);
Serial2.setTxBufferSize(4096);
Serial.setRxBufferSize(4096);
delay(10000);
String idToken = makeAPICall();
}
void loop()
{
sendATCommand("AT+HTTPINIT"); // Initialize HTTP service
delay(2000);
// String authData = "{\"email\":\"" + String(email) + "\",\"password\":\"" + String(password1) + "\",\"returnSecureToken\":true}";
String authData = "{\"email\":\"" + String(email) + "\",\"password\":\"" + String(password1) + "\",\"returnSecureToken\":true}";
// Set headers with authentication data
sendATCommand("AT+HTTPPARA=\"URL\",\"" + String(apiUrl) + "\"");
delay(2000);
sendATCommand("AT+HTTPPARA=\"CONTENT\",\"application/json\"");
delay(2000);
// Make POST request
sendATCommand("AT+HTTPDATA=" + String(authData.length()) + ",10000"); // Send POST data length
delay(4000);
// sendATCommand("AT+HTTPDATA= 900,10000"); // Send POST data length
sendATCommand(authData); // Send POST data
receiveUartResponse();
delay(2000);
// sendATCommand("AT+HTTPPARA=\"CID\",1");
sendATCommand("AT+HTTPACTION=1"); // Execute HTTP POST request
receiveUartResponse();
delay(20000);
sendATCommand("AT+HTTPHEAD");
receiveUartResponse();
delay(5000);
sendATCommand("AT+HTTPREAD=0,2000");
// receiveUartResponse();
delay(8000);
String fullResponse = readHttpResponse();
Serial.println("Received response: ");
Serial.println(fullResponse);
receiveUartResponse();
sendATCommand("AT+HTTPREAD?");
delay(8000);
fullResponse = readHttpResponse();
Serial.println("Received response: ");
Serial.println(fullResponse);
sendATCommand("AT+HTTPTERM");
receiveUartResponse();
delay(8000);
delay(100);
}
String makeAPICall()
{
sendATCommand("AT+CGATT=1"); // Attach to GPRS service
delay(1000);
sendATCommand("AT+CGDCONT=1,\"IP\",\"airteliot.com\""); // Replace YOUR_APN with your APN
delay(1000);
sendATCommand("AT+CGACT=1,1"); // Activate PDP context
delay(1000);
return "OK";
}
String readHttpResponse() {
long startTime = millis();
const long timeout = 10000; // 10-second timeout
bool lengthParsed = false;
int expectedLength = 0;
int receivedLength = 0;
long lastReadTime = millis();
const long readInterval = 100;
String response = ""; // Accumulate the response here
// Await the +HTTPREAD: <len> response to get the expected length
while (!lengthParsed && (millis() - startTime) < timeout) {
// Serial.println("Inside While");
if (Serial2.available() > 0) {
Serial.println("Serial2 available");
String line1 = Serial2.readStringUntil('\n');
Serial.println("line read: " + line1);
if (line1.startsWith("+HTTPREAD: ")) {
Serial.println("line start with HTTPREAD");
// expectedLength = line.substring(line.indexOf(":") + 1).toInt();
expectedLength = line1.substring(line1.indexOf(",") + 1).toInt();
Serial.println("expectedLength " + String(expectedLength));
lengthParsed = true;
}
}
}
if (!lengthParsed) {
Serial.println("Failed to parse expected length or timeout waiting for +HTTPREAD.");
return "";
}
// Reset startTime for reading the response data
startTime = millis();
while ((receivedLength < expectedLength) && ((millis() - startTime) < timeout)) {
// Serial.println("inside 2nd while ");
if (Serial2.available() > 0) {
// Serial.println("Serial2 available");
char c = Serial2.read();
// Serial.println("Serial2 c: " + c);
response += c;
// Serial.println("Serial2 response: " + response);
receivedLength++;
// Serial.println("Serial2 receivedLength: " + String(receivedLength));
lastReadTime = millis();
} else if (millis() - lastReadTime > readInterval) {
delay(10);
}
}
Serial.println("Serial2 receivedLength: " + String(receivedLength));
Serial.println("Serial2 expectedLength: " + String(expectedLength));
Serial.println("Serial2 response: " + response);
if (receivedLength < expectedLength) {
Serial.println("Did not receive full response, possible timeout or data issue.");
// return ""; // Indicate a problem by returning an empty string
}
return response;
}
void sendATCommand(String command)
{
// Serial2.println(command.c_str());
Serial2.println(command);
Serial.println("Sent: " + command);
delay(1000);
}
void receiveUartResponse()
{
int i =0 ;
while (Serial2.available() > 0 )
{
buffer[i++] = Serial2.read();
}
Serial.println(buffer);
memset(buffer,'\0', sizeof(buffer));
}
Actual Response
{
"kind": "identitytoolkit#VerifyPasswordResponse",
"localId": "CBozW3y9KkORXO1Zc9mlc9R9kjH3",
"email": "admin@12.com",
"displayName": "",
"idToken": "eyJhbGciOiJSUzI1NiIsImtpZCI6IjgwNzhkMGViNzdhMjdlNGUxMGMzMTFmZTcxZDgwM2I5MmY3NjYwZGYiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL3NlY3VyZXRva2VuLmdvb2dsZS5jb20vZXNwLWZvdGEtY290YSIsImF1ZCI6ImVzcC1mb3RhLWNvdGEiLCJhdXRoX3RpbWUiOjE3MTIwNDQ5NDksInVzZXJfaWQiOiJDQm96VzN5OUtrT1JYTzFaYzltbGM5UjlrakgzIiwic3ViIjoiQ0JvelczeTlLa09SWE8xWmM5bWxjOVI5a2pIMyIsImlhdCI6MTcxMjA0NDk0OSwiZXhwIjoxNzEyMDQ4NTQ5LCJlbWFpbCI6ImFkbWluQHN5b29rLmNvbSIsImVtYWlsX3ZlcmlmaWVkIjpmYWxzZSwiZmlyZWJhc2UiOnsiaWRlbnRpdGllcyI6eyJlbWFpbCI6WyJhZG1pbkBzeW9vay5jb20iXX0sInNpZ25faW5fcHJvdmlkZXIiOiJwYXNzd29yZCJ9fQ.cVWv0ud05yXjUhwPt3KAjPW57tNo8AJ0P_C34dI02PDAaX33nGfYmE8SQKWWIBwGlLTrgUNGlZ_UVw5lQSUAcsAnoG7-EcM2vqt8V0zKSXAjfii4UvRYL_19dOf6xCvm4TIPlURZCDBBlhe3J1vKE90trv-dvq6IPCqmxrs5Cd-oer2RvoEoqV7pRGgWxjkkkiJm8uxoqnPnsJFS3OkOW5kQ9rtrgvRACTVZCIRBVP0fWMQWmnMXc-csDExaJrDginTfokoecVUYUn-ttUyiZ5Vy08l068ovmMzvlxpPtgXNtOZutu96TMwkOsgLCZkfCWdcaHNFs2F3PhjomMkaOw",
"registered": true,
"refreshToken": "AMf-vBzXL04sexpuKPkqZcY0iNbGlbyKE1GKzeKsabYPUmlXGzbmPiVoc_UKEVevWWpFQXcCiaaGQ3nfiT4T5jEos5riSHTdHqVQJ-MQ1nJO3Wwq0fEOaSZ-nUjdiNwDi_4fjnjcT9OlHPCx-BYSp8n1vSlu94_o0Tqb85OAm0hefW8em6nf8K35Os_GKGgxORF2wrPqfKXE",
"expiresIn": "3600"
}
Output
Sent: AT+HTTPINIT
Sent: AT+HTTPPARA="URL","https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=AIzaSyAp7Q_Vz6llnLX6rN3go0H4_e1EbS-YOeY"
Sent: AT+HTTPPARA="CONTENT","application/json"
Sent: AT+HTTPDATA=74,10000
Sent: {"email":"admin@12.com","password":"s5@spark","returnSecureToken":true}
AT+HTTPINIT
OK
AT+HTTPPARA="URL","https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=AIzaSyAp7Q_Vz6llnLX6rN3go0H4_e1EbS-YOeY"
OK
AT+HTTPPARA="CONTENT","application/json"
OK
AT+HTTPDATA=74,10000
DOWNLOAD
{"email":"admin@12.com","password":"s5@spark","returnSecureToken":true}
OK
Sent: AT+HTTPACTION=1
AT+HTTPACTION=1
OK
Sent: AT+HTTPHEAD
+HTTPACTION: 1,200,830
AT+HTTPHEAD
+HTTPHEAD: DATA,459
HTTP/1.0 200 OK
Expires: Mon, 01 Jan 1990 00:00:00 GMT
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Date: Tue, 02 Apr 2024 11:36:14 GMT
Content-Type: application/json; charset=UTf
Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
Accept-Ranges: none
Vary: Origin,Accept-Encoding
OK
OK
Sent: AT+HTTPREAD=0,2000
Serial2 available
line read: AT+HTTPREAD=0,2000
Serial2 available
line read: OK
Serial2 available
line read:
Serial2 available
line read: +HTTPREAD: DATA,830
line start with HTTPREAD
expectedLength 830
Serial2 receivedLength: 330
Serial2 expectedLength: 830
Serial2 response: {
"kind": "identitytoolkit#VerifyPasswordResponse",
"localId": "CBozW3y9KkORXO1Zc9mlc9R9kjH3",
"email": "admin@12.com",
"displayName": "",
"idToken": "eyJhbGciOiJSUzI1NiIsImtpZCI6ImJhNjI1OTZmNTJmNTJlZDQ0MDQ5Mzk2YmU3ZGYzNGQyYzY0ZjQ1M2UiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL3NlY3VyZXRva2VuLmdvb2dsZgm
+HTTPREAD: 0
Did not receive full response, possible timeout or data issue.
Received response:
{
"kind": "identitytoolkit#VerifyPasswordResponse",
"localId": "CBozW3y9KkORXO1Zc9mlc9R9kjH3",
"email": "admin@12.com",
"displayName": "",
"idToken": "eyJhbGciOiJSUzI1NiIsImtpZCI6ImJhNjI1OTZmNTJmNTJlZDQ0MDQ5Mzk2YmU3ZGYzNGQyYzY0ZjQ1M2UiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL3NlY3VyZXRva2VuLmdvb2dsZgm
+HTTPREAD: 0
Sent: AT+HTTPREAD?
Serial2 available
line read: AT+HTTPREAD?
Serial2 available
line read: +HTTPREAD: LEN,0
line start with HTTPREAD
expectedLength 0
Serial2 receivedLength: 0
Serial2 expectedLength: 0
Serial2 response:
Received response:
Sent: AT+HTTPTERM
OK
AT+HTTPTERM
OK