0

AT Commands HTTP request to JSON source with status 200 but response that I get is incomplete (just some part of Response I received).

#include <SoftwareSerial.h>

#define RX_PIN 2
#define TX_PIN 3

SoftwareSerial gsmSerial(RX_PIN, TX_PIN);

void setup() {
  Serial.begin(9600);
  gsmSerial.begin(9600);
  configureAPN();
  Serial.println("Start Actual Communication:---->");

  // Additional setup code for your SIM800L module, if required
  // Ensure that the SIM800L module is configured and connected to the network
}

void loop() {
  // Make HTTP GET request to the specified API endpoint

  another_function("https://just_normal_server.com/user/heartbeat");

  // Adjust the delay based on your application requirements
  delay(2000);
}



void another_function(String url) {
  // Buffer to store the HTTP response data
  char buffer[512];
  int index = 0;

  // Helper function to read and append data to the buffer
  auto readAndAppendToBuffer = [&buffer, &index](int timeout) {
    long startTime = millis();
    while (millis() - startTime < timeout && gsmSerial.available()) {
      char c = gsmSerial.read();
      buffer[index++] = c;
      // Add additional conditions for handling specific characters or patterns if needed
    }
  };

  gsmSerial.println("AT+HTTPINIT");
  delay(1000);
  Serial.println("HTTP INITIALIZATION: " + get_data());

  gsmSerial.println("AT+HTTPPARA=\"CID\",1");
  delay(1000);
  Serial.println("CID PARA: " + get_data());

  gsmSerial.println("AT+HTTPPARA=\"URL\",\"" + url + "\"");
  delay(2000);
  Serial.println("URL Called: " + get_data());

  gsmSerial.println("\"CONTENT\",\"application/json\"");
  delay(2000);
  Serial.println("JSON PARA: " + get_data());

  gsmSerial.println("AT+HTTPPARA=\"REDIR\",1");
  delay(1000);
  Serial.println("REDIR PARA: " + get_data());

  gsmSerial.println("AT+HTTPSSL=1");
  delay(10000);
  Serial.println("SSL PARA: " + get_data());

  gsmSerial.println("AT+HTTPACTION=0");
  delay(10000);
  Serial.println("HTTP ACTION: " + get_data());

  gsmSerial.println("AT+HTTPREAD");
  delay(10000);
  Serial.println("HTTP Read DATA: " + get_data());
  /*
  readAndAppendToBuffer(30000);  // Adjust the timeout based on your server's response time

  // Null-terminate the buffer
  buffer[index] = '\0';

  // Print the entire response data
  Serial.println(buffer);
  */

  gsmSerial.println("AT+HTTPTERM");
  delay(1000);
  Serial.println("HTTP Termination: " + get_data());
}


String get_data() {
  String response = "";  // Initialize an empty string

  while (gsmSerial.available() > 0) {
    char character = gsmSerial.read();
    response += character;  // Append the character to the string
  }

  return response;
}


void configureAPN() {
  gsmSerial.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"\r");
  delay(1000);
  Serial.println("APN-1: " + get_data());
  gsmSerial.println("AT+SAPBR=3,1,\"APN\",\"airtelgprs.com\"\r");
  delay(1000);
  Serial.println("APN-2: " + get_data());
  gsmSerial.println("AT+SAPBR=1,1\r");
  delay(1000);
  Serial.println("APN-3: " + get_data());
}

This is my program, and I using Arduino UNO with SIM800L Module....

Got the response like:

HTTP ACTION: AT+HTTPACTION=0

OK

+HTTPACTION: 0,200,57

HTTP Read DATA: AT+HTTPREAD

+HTTPREAD: 57
{"status":"alive","timestamp":"202
HTTP Termination: AT+HTTPTERM

OK

I want the full response from that API. I test this API on Postman. On that response was correctly received, but in Arduino terminal, I am unable to get.

2
  • The two things I'm noticing here (based on a glance at a copy of the sim800 AT command documentation are that you aren't setting the "Content-Type" correctly (though that isn't your issue), and that you aren't doing anything with the size response from AT+HTTPREAD (or AT+HTTPACTION). If you know that you're expecting n bytes (57 here), it would make sense to continue reading until that is received (or a timeout occurs). Commented Feb 26, 2024 at 11:09
  • IMHO (I don't know the implementation you are using) you should output \r\n at the end of the commands instead of using println() command, as normally the router is expecting a \r and the println() command can send just a \n, making the modem not accept the command. Using \r\n will allow you to know what is being sent as line terminator (by default it is \r with AT commands) and the extra \n is always accepted by Hayes protocol. Commented Feb 28, 2024 at 7:38

0

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.