【发布时间】:2022-01-16 01:11:50
【问题描述】:
我是 Arduino 新手,今天我尝试使用我的 TTGO 板连接到 WiFi 并从 URL 获取数据。它正确连接到 WiFi,从 URL 获取数据,但 10 秒后所有数据都消失了。
我知道这是因为 tft.fillScreen(TFT_GREY); 命令而发生的,但我不明白,为什么函数不能继续,在该命令之后是获取该数据的其他命令以及命令打印出来。
我的计划是每 10 秒从 URL 刷新一次数据。
我的代码:
#include <TFT_eSPI.h> // Graphics and font library for ST7735 driver chip
#include <SPI.h>
#include <WiFi.h>
#include <HTTPClient.h>
TFT_eSPI tft = TFT_eSPI(); // Invoke library, pins defined in User_Setup.h
#define TFT_GREY 0x5AEB // New colour
const char* ssid = "MyNetwork";
const char* password = "password";
int number = 10;
void setup(void) {
tft.init();
tft.setRotation(1);
delay(4000);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
tft.println("Connecting to WiFi..");
}
tft.println("Connected to the WiFi network");
}
void loop() {
refreshData();
}
void refreshData ()
{
// Fill screen with grey so we can see the effect of printing with and without
// a background colour defined
tft.fillScreen(TFT_GREY);
if ((WiFi.status() == WL_CONNECTED)) { //Check the current connection status
HTTPClient http;
http.begin("http://example.com"); //Specify the URL
int httpCode = http.GET(); //Make the request
if (httpCode > 0) { //Check for the returning code
String payload = http.getString();
//tft.println(httpCode);
tft.print(payload);
}
else {
tft.println("Error on HTTP request");
}
http.end(); //Free the resources
}
number ++;
delay(10000);
}
谢谢!
【问题讨论】: