My sim800l failed to send data to the webserver

Hi everyone, sorry i need your help to solve my problem that am facing, i need to transfer data to the webserver using arduino mega 2560 and sim800l but i failed to manage it i don't know where might be the problem. Below is the code...

const char* server = "196.41.60.218";
const int port = 80;

// Sensor values
float volume = 1993.97;
float percentage = 67.58;
float water_height = 4.10;
float lat_val = -6.874645;
float lng_val = 39.465464;

// Device ID
String deviceID = "123456789";

void setup() {
  Serial.begin(9600);
  Serial3.begin(9600);  // SIM800L connected to Serial3

  delay(5000);  // Give SIM800L module time to start


    Serial3.print("AT\r\n");
    delay(2000);
    Serial3.print("AT+CMGF=1\r\n");
    delay(2000);
  
}

void loop() {


  // Create the JSON payload with the sensor data and device ID
  String payload = "{";
  payload += "\"deviceID\":\"" + deviceID + "\",";
  payload += "\"Volume\":" + String(volume) + ",";
  payload += "\"percentage\":" + String(percentage) + ",";
  payload += "\"height\":" + String(water_height) + ",";
  payload += "\"Latitude\":" + String(lat_val, 6) + ",";
  payload += "\"Longitude\":" + String(lng_val, 6);
  payload += "}";

  // Establish a TCP connection
  Serial3.println("AT+CIPSTART=\"TCP\",\"" + String(server) + "\"," + String(port));
  if (Serial3.find("OK")) {
    Serial.println("TCP connection established.");

    // Send the HTTP POST request
    String postRequest = "POST /twlms/data2.php HTTP/1.1\r\n";
    postRequest += "Host: " + String(server) + "\r\n";
    postRequest += "Content-Type: application/json\r\n";
    postRequest += "Content-Length: " + String(payload.length()) + "\r\n\r\n";
    postRequest += payload;

    Serial3.print("AT+CIPSEND=");
    Serial3.println(postRequest.length());
    delay(1000);

    Serial3.println(postRequest);
    delay(2000);  // Wait for the server response

    // Read and print the server response
    while (Serial3.available()) {
      char response = Serial3.read();
      Serial.print(response);
    }

    Serial3.println("AT+CIPCLOSE");
    Serial.println("TCP connection closed.");
  }

  delay(5000);  // Delay between each data transmission
}

Just using "blind" delays like this is very bad practice!

You need to read the actual response to each each command to know when it has completed & whether it was successful.

What you're doing is like driving blindfold down a busy street, and just relying on time delays for when to turn the wheel, brake, etc!

EDIT:

ok thanks for the reply so how can i solve it?

Take a look at this one:

it has sendAT() and waitResponse() ...

Please show what you see on the serial console of the Mega when you run this code.

1 Like

ok am getting this output

am not dealing with sms here, i need smi800l to send data to the webserver just see inside the loop function

ok am getting this output

I know. But that code shows you how to deal with AT command responses

that's why i came here to need some helps from the people including you, so if you know where might the problem in that code please correct it please..

Do you have access to the log file on the web server "196.41.60.218" ?
Have you got the source code of PHP file twlms/data2.php ?

1 Like

yes i have access to that server but i don't have the source code of PHP file twlms/data2.php right now

What can you look at on that server to determine if the data is getting transmitted to it ?
Is the Apache web server running on it . If not, what web server ?
There will be web server log files which should show the transmission attempts you are making from your sim800l/Mega and these are important for trouble shooting.

ok so you need the source code of PHP file twlms/data2.php?

As with the web server log files this is an important trouble shooting aid.

I'm taking a long break now.

so what do you need sir?

but this code is working fine with that server, the only problem is it has too much delays which is not fine when you want the arduino to perform multple tasts at the same time


#include <ArduinoJson.h>
StaticJsonBuffer<200> jsonBuffer;



// Sensor values
float volume = 1993.97;
float percentage = 67.58;
float water_height = 4.10;
float lat_val = -6.874645;
float lng_val = 39.465464;

char deviceID[12] = "MYTEST56";


void setup()
{
  Serial3.begin(9600);        // the GPRS baud rate
  Serial.begin(9600);
  Serial.println("Initializing..........");

  DynamicJsonBuffer jsonBuffer;

  delay(5000);
}

void loop()
{


  /********************GSM Communication Starts********************/

  if (Serial3.available())
    Serial.write(Serial3.read());

  Serial3.println("AT");
  delay(3000);

  Serial3.println("AT+SAPBR=3,1,\"Contype\",\"GPRS\"");
  delay(6000);
  ShowSerialData();

  Serial3.println("AT+SAPBR=3,1,\"APN\",\"internet\"");//APN
  delay(6000);
  ShowSerialData();

  Serial3.println("AT+SAPBR=1,1");
  delay(6000);
  ShowSerialData();

  Serial3.println("AT+SAPBR=2,1");
  delay(6000);
  ShowSerialData();


  Serial3.println("AT+HTTPINIT");
  delay(6000);
  ShowSerialData();

  Serial3.println("AT+HTTPPARA=\"CID\",1");
  delay(6000);
  ShowSerialData();

  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& object = jsonBuffer.createObject();

  object.set("deviceID", deviceID);
  object.set("Volume", volume);
  object.set("percentage", percentage);
  object.set("height", water_height);
  object.set("Latitude", lat_val, 6);
  object.set("Longitude", lng_val, 6);

  object.printTo(Serial);
  Serial.println(" ");
  String sendtoserver;
  object.prettyPrintTo(sendtoserver);
  delay(4000);

  Serial3.println("AT+HTTPPARA=\"URL\",\"http://196.41.60.218/twlms/data2.php\""); //Server address
  delay(4000);
  ShowSerialData();

  Serial3.println("AT+HTTPPARA=\"CONTENT\",\"application/json\"");
  delay(4000);
  ShowSerialData();


  Serial3.println("AT+HTTPDATA=" + String(sendtoserver.length()) + ",100000");
  Serial.println(sendtoserver);
  delay(6000);
  ShowSerialData();

  Serial3.println(sendtoserver);
  delay(6000);
  ShowSerialData;

  Serial3.println("AT+HTTPACTION=1");
  delay(6000);
  ShowSerialData();

  Serial3.println("AT+HTTPREAD");
  delay(6000);
  ShowSerialData();

  Serial3.println("AT+HTTPTERM");
  delay(10000);
  ShowSerialData;

  /********************GSM Communication Stops********************/

}


void ShowSerialData()
{
  while (Serial3.available() != 0)
    Serial.write(Serial3.read());
  delay(1000);

}

Good to hear that it is working fine but that is not what you said in your first post:

1 Like

Then use millis(), not delay().

That's another issue with using "blind" delays: you always have to delay the worst case time - even when the device replies more quickly!