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

时间:2017-06-16 11:00:38

标签: php c++ json arduino esp8266

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

#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);
?>

2 个答案:

答案 0 :(得分:1)

处理响应时出现了问题。它在连接到我的服务器时起作用,但在连接到你的服务器时它不起作用。

这是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");
  }
}

答案 1 :(得分:0)

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