Go Down

Topic: Facing trouble with SIM900A and Arduino Mega while HTTP POST  (Read 44 times) previous topic - next topic

muhammadbilalsakhawat

For last the last few days I have been working with SIM900A and Arduino mega. I am trying to send data on a sever which receive data only in application/json format. I am able to reach the server using commands for HTTP application but I am not able to post any data there. AT+HTTPDATA command is giving error. I am using following Code for this purpose. I also attached some photos showing sever is working and receiving data in json format. Serial Monitor output is there as well.


#include <SoftwareSerial.h>
#include <ArduinoJson.h>

SoftwareSerial SIM900(10, 9);

unsigned char buffer[64];
int count = 0;


void setup() {
  SIM900.begin(9600); 
  Serial.begin(9600);
}

void loop() {

  DynamicJsonBuffer jBuffer;
  JsonObject& root = jBuffer.createObject();
  root["message"] = "34";
  char JSONmessageBuffer[33];
  root.prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
  Serial.println(JSONmessageBuffer);
 
  Serial.println("HTTP post method :");
  SIM900.println("AT");
  delay(1000);
  ShowSerialData();
  delay(1000);
 
  SIM900.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
  delay(1000);
  ShowSerialData();
  delay(1000);
  SIM900.println("AT+SAPBR=3,1,\"APN\",\"ufone.pinternet\""); 
  delay(1000);
  ShowSerialData();
  delay(1000);
  SIM900.println("AT+SAPBR=1,1");
  delay(1000);
  ShowSerialData();
  delay(1000);

  SIM900.println("AT+SAPBR=2,1");
  delay(1000);
  ShowSerialData();
  delay(1000);
  SIM900.println("AT+HTTPINIT");
  delay(1000);
  ShowSerialData();
  delay(1000);
  SIM900.println("AT+HTTPPARA=\"CID\",1"); 
  delay(1000);
  ShowSerialData();
  delay(1000);
  SIM900.println("AT+HTTPPARA=\"URL\",\"http://103.31.82.44/api/addData\""); 
  delay(1000);
  ShowSerialData();
 delay(1000);
  SIM900.println("AT+HTTPPARA=\"CONTENT\",\"application/json\"");
  delay(1000);
  ShowSerialData();
 delay(1000);

  SIM900.println("AT+HTTPDATA=33,10000"); 
  delay(5000);
  ShowSerialData();
  delay(5000);

 SIM900.println(JSONmessageBuffer);
 delay(5000);
 ShowSerialData();
 delay(5000);
 
  SIM900.println("AT+HTTPACTION=1"); 
  delay(5000);
  ShowSerialData();
  delay(5000);

   
  SIM900.println("AT+HTTPTERM"); 
  delay(5000);
  ShowSerialData();
  delay(5000);
  SIM900.println("AT+SAPBR=0,1");
  delay(5000);
  ShowSerialData();
  delay(5000);
}
void ShowSerialData()
{
    while(SIM900.available())
  {
    buffer[count++]=SIM900.read();
    if(count == 64) break;
  }

  Serial.write(buffer, count);
  clearBufferArray();
  count = 0;
}
 void clearBufferArray()
 {
  for (int i=0; i<count; i++)
  {
    buffer=NULL;
  }
 }

Go Up