ESP8266 读取 JSON,但不读取 PHP 文件

ESP8266 reads JSON, but doesn't read the PHP file

有一个 ESP8266 的代码,它解析我网站上的数据并执行 LED 的开/关。当它是一个静态 JSON 文件时,一切都没有问题。但是当我将一个文件传输到动态更新数据并以 JSON 格式显示的 PHP 时,脚本无法读取它。可能是什么问题?

#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#define pin 5

const char* ssid     = "ssid";  
const char* password = "password";

const char* host     = "www.site.ru"; // domain  
String path          = "/lightAPI.php";  

void setup() {  
  pinMode(pin, OUTPUT); 
  pinMode(pin, HIGH);
  digitalWrite(5, HIGH);
  Serial.begin(9600);

  delay(10);
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);
  int wifi_ctr = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("WiFi connected");  
  Serial.println("IP address: " + WiFi.localIP());
}

void loop() {  
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }

  client.print(String("GET ") + path + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: keep-alive\r\n\r\n");


  delay(2000); // wait for server to respond

  // read response
  String section="header";
  while(client.available()){
    String line = client.readStringUntil('\r');
    // Serial.print(line);
    // we’ll parse the HTML body here
    if (section=="header") { // headers..
      Serial.print("");
      if (line=="\n") { // skips the empty space at the beginning 
        section="json";
      }
    }
    else if (section=="json") {  // print the good stuff
      section="ignore";
      String result = line.substring(1);

      // Parse JSON
      int size = result.length() + 1;
      char json[size];
      result.toCharArray(json, size);
      StaticJsonBuffer<200> jsonBuffer;
      JsonObject& json_parsed = jsonBuffer.parseObject(json);
      if (!json_parsed.success())
      {
        Serial.println("parseObject() failed");
        return;
      }

      // Make the decision to turn off or on the LED
      if (strcmp(json_parsed["light"], "OFF") == 0) {
        digitalWrite(5, HIGH); 
        Serial.println("LED OFF");
      }
      else {
        digitalWrite(5, LOW);
        Serial.println("LED ON");
      }
    }
  }
}

PHP 文件

<?php
header('Content-Type: application/json');
$status = file_get_contents('txt/lightStatus.txt');

$json = array('light' => $status, 'time' => date("G"));

echo json_encode($json);
?>

Json只是一种格式,一种结构,不需要任何处理。另一方面,PHP 是一种服务器端语言,这意味着它由另一个应用程序解释,实际上是该应用程序正在解释执行工作的代码。 esp8266 缺少该应用程序。并且将无法 运行 您的 PHP 文件。我建议考虑在 php 中制作一个 API 存储在某处服务器上并让你的 esp 调用它。或者看看你是否可以在 esp 上直接实现你的代码,尽管你可能会受到 CPU、内存和处理能力的限制。祝你好运!

处理响应时出现问题。它在连接到我的服务器时有效,但在连接到您的服务器时无效。

这是 ESP8266 连接到我的服务器时得到的:

HTTP/1.1 200 OK
Date: Sat, 17 Jun 2017 18:21:37 GMT
Server: Apache/2.4.17 (Win32) OpenSSL/1.0.2d PHP/5.6.19
X-Powered-By: PHP/5.6.19
Content-Length: 31
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/json

{"light":"OFF","time":"20"}

这是连接到您的时得到的结果:

HTTP/1.1 200 OK
Server: nginx admin
Date: Sat, 17 Jun 2017 18:25:53 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive

28
{"light":"OFF","online":"0","time":"21"}
0

不幸的是,我现在没有时间调查你的代码的问题,但同时这里有一个使用 HTTPClient 处理请求和响应的工作代码(无论如何我都会推荐使用它):

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#define pin 5

const char* ssid     = "ssid";  
const char* password = "password"; 

void setup() {  
  pinMode(pin, OUTPUT); 
  pinMode(pin, HIGH);
  digitalWrite(5, HIGH);
  Serial.begin(9600);

  delay(10);
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);
  int wifi_ctr = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("WiFi connected");  
  Serial.println("IP address: " + WiFi.localIP());
}

void loop() {  
  HTTPClient http;
  http.begin("http://bot.erm.today/lightAPI.php");
  int statusCode = http.GET();
  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& json_parsed = jsonBuffer.parseObject(http.getString());
  http.end();
  if (!json_parsed.success())
  {
    Serial.println("parseObject() failed");
    return;
  }

  // Make the decision to turn off or on the LED
  if (strcmp(json_parsed["light"], "OFF") == 0) {
    digitalWrite(5, HIGH); 
    Serial.println("LED OFF");
  }
  else {
    digitalWrite(5, LOW);
    Serial.println("LED ON");
  }
}