2017-06-16 17 views
2

ESP8266のコードがあり、私のサイトのデータを解析し、LEDのオン/オフを切り替えます。それが静的なJSONファイルだったとき、それは問題なく動作しました。しかし、データを動的に更新してJSON形式で表示するPHPにファイルを転送すると、スクリプトで読み取られません。何が問題なの?ESP8266は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); 
?> 
+0

ブラウザでそのPHPを開こうとしましたか? JSON文字列をシリアルに印刷してみましたか?そしてあなたのコードはどこで失敗するのですか? –

+0

@gre_gorはい、ブラウザにJSON文字列を表示します。この文字列を.jsonファイルにコピーすると、ESPがそれを読み取ります。失敗したparseObject()のエラーを表示します – StartProg

+0

ホスティングサービスがPHPファイルに何かを追加している可能性はありますか?一部のトラッキングコードまたは広告ですか?それを注意深くチェックしてください(ブラウザのウェブサイトのソースコード(ChromeのCtrl + U)をチェックしてください。表示されていないものが隠されている可能性があります)。また、あなたのlightStatus.txtの内容は何ですか?私のものは単に「オフ」です。 ESP8266とXAMPPサーバーでコードを実行しても問題なく動作しています。 – Defozo

答えて

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"); 
    } 
} 
+0

それは優れています。できます。ありがとうございました:) – StartProg

+0

スーパーはここで説明します。 – cagdas

0

JSONはそれが処理を必要としない、単に形式、構造体です。一方、PHPはサーバーサイド言語であり、それは別のアプリケーションによって解釈され、実際にはそのコードを解釈するアプリケーションであることを意味します。 esp8266にはそのアプリケーションがありません。そして、あなたのPHPファイルを実行することができなくなります。私は、どこかのサーバに格納されているPHPのAPIを作ることを検討し、あなたのespを呼び出すことをお勧めします。または、CPU、メモリ、処理能力によって制限されるかもしれませんが、espでコードを実装できるかどうかを確認してください。がんばろう!

+0

彼はESP8266でPHPファイルを実行しようとしていません。彼はPHPファイルで生成されたJSONを解析しようとしています。 –

+0

ああ、ごめんなさい申し訳ありません質問を理解してください – Kevin192291

関連する問題