Получить строку ответа от веб-сервера PHP на ESP8266 - PullRequest
0 голосов
/ 23 октября 2019

У меня есть страница PHP на 000webhost.com (сервер) и ESP8266 (модуль), к которой подключен мой локальный интернет-провайдер. Теперь, как я могу получить ответ от живого веб-сервера на мой модуль ESP8266 NodeMCU простым способом. Даже я также пытался с помощью метода http.begin (), но я дал код состояния «-1».

Код Arduino

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>

const char* ssid = "Temporary";
const char* password = "qwerty54321";
const char* host = "files.000webhost.com";
const int httpPort = 80; 

void setup()
{
  //wifi connection...
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000); Serial.print(".");
  }
  Serial.println();Serial.println("Wifi Connected!"); 
  delay(1000); 
  Serial.print("IP:");Serial.println(WiFi.localIP());
  delay(1000);  

}

void loop()
{

  //Cloud connection...
  WiFiClient client;
  if (!client.connect(host, httpPort)) {
   Serial.println("server failed!");
   return;
  }
  else
  {
   Serial.println("Cloud connected!");
   delay(1000);
  }

  // Send HTTP request
  client.println(F("GET easycake2019.000webhostapp.com/automation/app.php HTTP/1.1"));
  client.println(F("Host: files.000webhost.com"));
  client.println(F("Connection: close"));
  if (client.println() == 0) {
    Serial.println(F("Failed to send request"));
    return;
  }
  else
  Serial.println("Request sent!");
  delay(1000);

  DynamicJsonDocument doc(256);
  DeserializationError error = deserializeJson(doc, client);
  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.c_str());
    return;
  }
  // Extract values
  const char* x = doc["val"];
  Serial.println(x);
  client.stop();
  Serial.print("disconnected!");
  delay(5000);

}


**app.php**

<?php
header('Access-Control-Allow-Origin:*');
header('Content-Type: application/json');
header('HTTP/1.1 200 OK');
$response = array();
$response['val'] = '1234';
echo json_encode($response);
?>

ArduinoВЫХОД

Пожалуйста, проверьте файл изображения:

Arduino output

...