Skip to content
Advertisement

Can’t post data to web server using GPRS GSM A6 and Arduino

I’ve been trying to post data to server for sometime now. Even though I am getting OK response, data is not posted to the server.

Here’s my code:

#include <SoftwareSerial.h>
SoftwareSerial myGsm(7,8);

void setup() {
  myGsm.begin(115200);
  Serial.begin(9600);
  delay(500);
  myGsm.println("AT+CIPSHUTr"); //RESPONSE= OK
  delay(1000);
  myGsm.println("AT+CIPMUX=0r"); //RESPONSE= OK
  delay(2000);
  myGsm.println("AT+CGATT=1r"); //RESPONSE= OK
  delay(1000);
  myGsm.println("AT+CSTT="internet","",""r"); //RESPONSE= OK
  delay(5000);
  myGsm.println("AT+CIICRr"); //RESPONSE= OK
  delay(5000);
  myGsm.println("AT+CIFSRr"); //RESPONSE= Returns an IP
  delay(2000);
  myGsm.println("AT+CIPSTART="TCP","159.203.180.107", 80r") //RESPONSE= CONNECTED OK
  delay(3000);
  myGsm.println("AT+CIPSENDr"); //RESPONSE= >
  delay(500);
  myGsm.println("POST http://159.203.180.107/Code/ HTTP/1.1");
  delay(500);
  myGsm.println("Host: 159.203.180.107");
  delay(500);
  myGsm.println("Content-Type: application/json");
  delay(500);
  myGsm.println("Content-Length: 25rn");
  delay(500);
  myGsm.println("{"Celsius":"TEMPERATURE"}");
  delay(500);
  myGsm.write(0x1A); // Ctrl Z
  delay(10000);
  /*
    After sending all these instructions, I get the following response,
    OK
    HTTP/1.1 200 OK
    Friday December, 22
    +TCPCLOSE=0
    OK
  */
  myGsm.println("AT+CIPCLOSE"); //RESPONSE= OK
  delay(1000);
  myGsm.println("AT+CIPSHUT"); //RESPONSE= OK
  delay(1000);
}

void loop() {
}

So, as you can see, I get a 200 OK response after sending the data, but the data is not written into the file in the server. The existing content in the file is erased, but new data is not written in that file.

Here’s my PHP file in the server:

<?php
    echo "<pre>";
    print_r($_REQUEST);
    file_put_contents("data.txt", $_REQUEST);
    die("<br>DONE!");
?>

So, what could be the problem here? Is there something wrong with the PHP file in the server? Please help. I’ve also posted this same question in one of the Arduino communities but it didn’t help.

Advertisement

Answer

PHP doesn’t automatically parse JSON.

You need to either post it as application/x-www-form-urlencoded, which looks like

Celsius=TEMPERATURE

or parse the POST payload yourself

$data = json_decode(file_get_contents('php://input'), true);
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement