Arduino: ESP32 WIFI Get webpage content and display on OLED

It should be a very practical application scenario for ESP32 to access the specified Web page of the Web Server through the network and display the content on the display screen.

Resting at home today, I just have a Raspberry Pi and a small OLED to do this experiment. Connect the Raspberry Pi to the WIFI at home and sudo apt-get install apache2 to let the Raspberry Pi act as a web server. Created a.html under / var / www / html and wrote TEST to see what the result was.

#include <WiFi.h>
#include <HTTPClient.h>
HTTPClient http;

#include "SSD1306.h"
SSD1306 display(0x3c, 21, 22);

unsigned long startTime = millis();

void setup() {

  //Serial.begin(115200);

  display.init();
  display.setFont(ArialMT_Plain_16);
  display.drawString(0, 0, "Starting...");
  display.display();

  WiFi.mode(WIFI_STA); //设置为STA模式
  WiFi.disconnect();   //断开当前可能的连接
  delay(1000);

  const char *ssid = "WIFI名称"; //你的网络名称,区分大小写
  const char *password = "****马赛克****"; //你的网络密码

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) //等待网络连接成功
  {
    if (millis() - startTime >= 15000) // 15秒连接超时
    {
      //Serial.println("Timed Out...");
      display.clear();
      display.drawString(0, 0, "Timed Out...");
      display.display();
      
      break;
    }
    else
    {
      display.clear();
      display.drawString(0, 0, "Connecting....");
      display.display();      
    }

    delay(500);
  }

}

void loop() {

  if (WiFi.status() == WL_CONNECTED)
  {
    http.begin("http://192.168.1.9/a.html"); // 访问指定URL

    int httpCode = http.GET();
    if (httpCode == HTTP_CODE_OK) {
      String pageData = http.getString();

      //Serial.println(pageData); // 网页内容

      display.clear();
      display.drawString(0, 0, pageData);
      display.display();
    }
    else
    {
      display.clear();
      display.drawString(0, 0, "GET Error.");
      display.display();
    }

    http.end();
  }
  else
  {
    display.clear();
    display.drawString(0, 0, "WIFI Error.");
    display.display();

  }

  delay(5000);
}

Published 122 original articles · Like 61 · Visits 530,000+

Guess you like

Origin blog.csdn.net/ki1381/article/details/88625039