【问题标题】:Arduino internet data disappear after 10 secondsArduino 互联网数据在 10 秒后消失
【发布时间】: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); 
}

谢谢!

【问题讨论】:

    标签: http arduino get


    【解决方案1】:

    我没有意识到的问题是,所有新数据都显示不出来,所以每次都需要在开始时设置光标 tft.setCursor();

    【讨论】:

      猜你喜欢
      • 2015-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 1970-01-01
      • 2019-06-10
      • 2017-03-28
      相关资源
      最近更新 更多