hello,
I am trying to post text file in http server but i am getting 500 response all time. please guide me.
thank you
code:
#define SIM900 Serial1
#include <SPI.h>
#include <SD.h>
File myFile;
void setup() {
SIM900.begin(9600); /* Define baud rate for software serial communication /
Serial.begin(9600); / Define baud rate for serial communication */
Serial.print(“starting…”);
Serial.print(“Initializing SD card…”);
if (!SD.begin(4)) {
Serial.println(“initialization failed!”);
return;
}
Serial.println(“initialization done.”);
}
void loop() {
Serial.println(“HTTP post method :”);
Serial.print(“AT\r\n”);
SIM900.println(“AT”); /* Check Communication /
delay(5000);
ShowSerialData(); / Print response on the serial monitor /
delay(5000);
/ Configure bearer profile 1 /
Serial.print(“AT+SAPBR=3,1,“CONTYPE”,“GPRS”\r\n”);
SIM900.println(“AT+SAPBR=3,1,“CONTYPE”,“GPRS””); / Connection type GPRS /
delay(5000);
ShowSerialData();
delay(5000);
Serial.print(“AT+SAPBR=3,1,“APN”,“www”\r\n”);
SIM900.println(“AT+SAPBR=3,1,“APN”,“www””); / APN of the provider /
delay(5000);
ShowSerialData();
delay(5000);
Serial.print(“AT+SAPBR=1,1\r\n”);
SIM900.println(“AT+SAPBR=1,1”); / Open GPRS context /
delay(5000);
ShowSerialData();
delay(5000);
Serial.print(“AT+SAPBR=2,1\r\n”);
SIM900.println(“AT+SAPBR=2,1”); / Query the GPRS context /
delay(5000);
ShowSerialData();
delay(5000);
Serial.print(“AT+HTTPINIT\r\n”);
SIM900.println(“AT+HTTPINIT”); / Initialize HTTP service /
delay(5000);
ShowSerialData();
delay(5000);
Serial.print(“AT+HTTPPARA=“CID”,1\r\n”);
SIM900.println(“AT+HTTPPARA=“CID”,1”); / Set parameters for HTTP session */
delay(5000);
ShowSerialData();
delay(5000);
Serial.print("AT+HTTPPARA=“URL”,"http://ptsv2.com/t/eu87r-1529326906/post"\r\n");
SIM900.println(“AT+HTTPPARA=“URL”,“http://ptsv2.com/t/eu87r-1529326906/post””); /* Set parameters for HTTP session */
delay(5000);
ShowSerialData();
delay(5000);
Serial.println("AT+HTTPPARA=\"CONTENT\",\"multipart/form-data; boundary=--acebxz\"");
SIM900.println("AT+HTTPPARA=\"CONTENT\",\"multipart/form-data; boundary=--acebxz\"");
delay(5000);
delay(5000);
ShowSerialData();
delay(5000);
sendfile(); // sending the text file content in server
delay(5000);
ShowSerialData();
delay(5000);
Serial.print(“AT+HTTPACTION=1\r\n”);
SIM900.println(“AT+HTTPACTION=1”); /* Start POST session */
delay(5000);
ShowSerialData();
delay(5000);
delay(5000);
delay(5000);
delay(5000);
ShowSerialData();
delay(5000);
Serial.print(“AT+HTTPTERM\r\n”);
SIM900.println(“AT+HTTPTERM”); /* Terminate HTTP service /
delay(5000);
ShowSerialData();
delay(5000);
Serial.print(“AT+SAPBR=0,1\r\n”);
SIM900.println(“AT+SAPBR=0,1”); / Close GPRS context */
delay(5000);
ShowSerialData();
delay(5000);
}
void ShowSerialData()
{
Serial.print("\n");
while(SIM900.available()!=0) /* If data is available on serial port /
Serial.print(char (SIM900.read())); / Print character received on to the serial monitor */
}
//sending the file in multipart form data
void sendfile(void)
{
myFile = SD.open(“at.txt”);
if (myFile) {
Serial.println("\n open done at.txt");
//send file length
Serial.print(“AT+HTTPDATA=”+String(myFile.size()+105)+",50000");
SIM900.println(“AT+HTTPDATA=”+String(myFile.size()+105)+",50000");
delay(2000);
ShowSerialData();
delay(2000);
// read from the file until there’s nothing else in it:
Serial.print("–acebxz\n");
SIM900.println("–acebxz");
Serial.print(“Content-Disposition: form-data; name=“fi”; filename=“at.txt”\n”);
SIM900.print(“Content-Disposition: form-data; name=“fi”; filename=“at.txt”\n”);
Serial.print("Content-Type: text/plain\n");
SIM900.print("Content-Type: text/plain\n");
myFile.close();
myFile = SD.open(“at.txt”);
char op[500];
int j=0;
while (myFile.available())
{
byte ch=myFile.read();
op[j++]=ch;
}
op[j]=’\0’;
myFile.close();
Serial.write(op);
SIM900.write(op);
Serial.print("\n–acebxz\n");
SIM900.print("\n–acebxz\n");
Serial.print(“File Data sending done\n”);
//myFile.close();
} else {
// if the file didn’t open, print an error:
Serial.println(“file not open at.txt”);
}
}