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



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

连接到 MD1 ....... 无线网络已连接 IP地址: 192.168.43.135 连接到 smbbitumen.com 请求网址: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 内容类型:文本/html X驱动方:PHP/5.4.45 变化:接受编码 接受范围:无 X 服务器缓存:真 X 代理缓存:未命中 传输编码:分块

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);
} 

该问题与缓存有关。更改读取中的代码.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();
?>

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

最新更新