首页 > 解决方案 > 我正在使用 esp8266 通过在服务器上运行 .php 脚本从服务器读取字节流,但我没有得到正确的数据

问题描述

这是向服务器发送/接收的 GET 请求和响应

连接到 MD1 ....... WiFi 连接 IP 地址:192.168.43.135 连接到 smbbitumen.com 请求 URL:http ://www.smbbitumen.com/smbbitumen.com/myftp/API/Users/read.php HTTP /1.1 200 OK 日期:2020 年 6 月 6 日星期六 03:00:17 GMT 服务器:nginx/1.17.9 内容类型:text/html X-Powered-By:PHP/5.4.45 变化:Accept-Encoding Accept-Ranges : none X-Server-Cache: true X-Proxy-Cache: MISS Transfer-Encoding: chunked

48 1111000019:30:0020:30:0019:40:0020:40:0019:45:0020:45:0019:50:0020:50:00 0

关闭连接


1111000019:30:0020:30:0019:40:0020:40:0019:45:0020:45:0019:50:0020:50:00 -> 这是我正在读入 esp 的数据流,但确实如此没有实时更新,它保持不变。任何想法,嗯,这正在发生......

还有一件事,当我在浏览器中运行上面的 php 链接时,每次它都会返回正确的数据。


我用来读取数据的 php 脚本

<?php
clearstatcache();
$myfile = fopen("TEST.txt", "r");
if($myFile === FALSE)
    echo "ERROR";
else
    echo fread($myfile,filesize("TEST.txt"));
fclose($myfile);
?>

ESP8266 代码,用于连接服务器并使用 GET 请求运行上述脚本

   #include <NTPClient.h>
   #include <WiFiUdp.h>
   #include <ESP8266WiFi.h>
   #include <WiFiClient.h>
   #include <ESP8266WebServer.h>
   #include <WiFiClientSecure.h> 
   #include <Ticker.h>
   #include <EEPROM.h>
   #include <ESP8266HTTPClient.h>
   #include<ESP8266WiFiMulti.h>


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


    void setup()
    {
        Serial.begin(115200);
        WiFi.begin(ssid,password);

        while(WiFi.status()!= WL_CONNECTED)
        {
            delay(1000);
            Serial.println("Connecting...");
        }
     }

    void loop()
    {
      if(WiFi.status() == WL_CONNECTED)
      {
  myhttp.begin("http://www.smbbitumen.com/smbbitumen.com/myftp/API/Users/read.php");
       int httpCode = myhttp.GET();

       if(httpCode > 0)
       {
          String payload = myhttp.getString();
          Serial.println(myhttp.getString());

       }
         httpCode = 0;
         myhttp.end();
      }
        delay(2000);
    } 

标签: phphtmlcarduinoesp8266

解决方案


该问题与缓存有关。更改 read.php 中的代码如下

<?php
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1.
clearstatcache();
$myfile = fopen("TEST.txt", "r");
if($myFile === FALSE)
    echo "ERROR";
else
    echo fread($myfile,filesize("TEST.txt"));
fclose($myfile);
clearstatcache();

?>

可以通过更改服务器上的缓存设置来完成类似的事情。


推荐阅读