无法使用 GPRS GSM A6 和 Arduino post 数据到网络服务器

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

一段时间以来,我一直在尝试 post 向服务器发送数据。即使我得到了 OK 响应,数据也没有 post 发送到服务器。

这是我的代码:

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

void setup() {
  myGsm.begin(115200);
  Serial.begin(9600);
  delay(500);
  myGsm.println("AT+CIPSHUT\r"); //RESPONSE= OK
  delay(1000);
  myGsm.println("AT+CIPMUX=0\r"); //RESPONSE= OK
  delay(2000);
  myGsm.println("AT+CGATT=1\r"); //RESPONSE= OK
  delay(1000);
  myGsm.println("AT+CSTT=\"internet\",\"\",\"\"\r"); //RESPONSE= OK
  delay(5000);
  myGsm.println("AT+CIICR\r"); //RESPONSE= OK
  delay(5000);
  myGsm.println("AT+CIFSR\r"); //RESPONSE= Returns an IP
  delay(2000);
  myGsm.println("AT+CIPSTART=\"TCP\",\"159.203.180.107\", 80\r") //RESPONSE= CONNECTED OK
  delay(3000);
  myGsm.println("AT+CIPSEND\r"); //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: 25\r\n");
  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() {
}

因此,如您所见,我在发送数据后收到 200 OK 响应,但数据并未写入服务器中的文件中。文件中的现有内容被删除,但新数据未写入该文件。

这是我在服务器中的 PHP 文件:

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

那么,这可能是什么问题?服务器中的 PHP 文件有问题吗?请帮忙。我也在一个 Arduino 社区中 post 编辑了同样的问题,但它没有帮助。

PHP 不会自动解析 JSON.

您需要将其 post 设为 application/x-www-form-urlencoded,看起来像

Celsius=TEMPERATURE

或自己解析 POST 负载

$data = json_decode(file_get_contents('php://input'), true);

我认为这段代码很容易工作,因为我更改了你的部分代码并在函数中添加了一些命令我最终测试了代码并且它适用于 me.also 不要忘记更改服务器代码,如插入的答案post。 感谢大家。

我的服务器代码:

 $res=[];
 $data = json_decode(file_get_contents('php://input'), true);
$res['name']=$data['Celsius'];

Arduino 代码:

#include <SoftwareSerial.h>
SoftwareSerial myGsm(10,11);
void updateSerial()
{
  delay(2000);
  while (Serial.available()) {
    myGsm.write(Serial.read());//Data received by Serial will be outputted by mySerial
  }
  while (myGsm.available()) {
    Serial.write(myGsm.read());//Data received by myGsm will be outputted by Serial
}
  }
}
void setup() {
  myGsm.begin(9600);
  Serial.begin(9600);
  delay(500);
  myGsm.println("AT+CIPSHUT\r"); //RESPONSE= OK
  updateSerial();
  myGsm.println("AT+CIPMUX=0\r"); //RESPONSE= OK
  updateSerial();
  myGsm.println("AT+CGATT=1\r"); //RESPONSE= OK
 updateSerial();
  myGsm.println("AT+CSTT=\"internet\",\"\",\"\"\r"); //RESPONSE= OK
  updateSerial();
  myGsm.println("AT+CIICR\r"); //RESPONSE= OK
  updateSerial();
  myGsm.println("AT+CIFSR\r"); //RESPONSE= Returns an IP
 updateSerial();
  myGsm.println("AT+CIPSTART=\"TCP\",\"example.com\", 80\r"); //RESPONSE= CONNECTED OK
  updateSerial();
  myGsm.println("AT+CIPSEND\r"); //RESPONSE= >
  updateSerial();
  myGsm.println("POST example.com/index.php/ HTTP/1.1");
  updateSerial();
  myGsm.println("Host: example.com");
  updateSerial();
  myGsm.println("Content-Type: application/json");
 updateSerial();
  myGsm.println("Content-Length: 25\r\n");
  updateSerial();
  myGsm.println("{\"Celsius\":\"TEMPERATURE\"}");
  updateSerial();
  myGsm.write(0x1A); // Ctrl Z
  delay(3000);
  updateSerial();
  /*
    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
  updateSerial();
  myGsm.println("AT+CIPSHUT"); //RESPONSE= OK
 updateSerial();
}
void loop() {
}