其他分享
首页 > 其他分享> > ESP8266+OLED显示实时时间

ESP8266+OLED显示实时时间

作者:互联网

话不多说上代码

注:此处有引用其他博主代码:tongyue

在其基础并有部分更新
放在此处方便参考,并自己保存用

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <Arduino.h>
#include <U8g2lib.h>
#include "dingyi.h"
#include <Wire.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
const char* ssid     = "NETGEAR"; 
const char* password = "dahai121";  
WiFiClient client;
HTTPClient http;
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0,D2,D1,U8X8_PIN_NONE);
void setup() {
  u8g2.begin();
  u8g2.enableUTF8Print(); 
  delay(1000);
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  String GetUrl = "http://quan.suning.com/getSysTime.do";
  http.setTimeout(5000);
  http.begin(client,GetUrl);
}
void loop() {
  int httpCode = http.GET();
  u8g2.setFont(u8g2_font_unifont_t_chinese2);
  u8g2.setFontDirection(0);
  u8g2.clearBuffer();
  if (httpCode > 0) {
      Serial.printf("[HTTP] GET... code: %d\n", httpCode);
      if (httpCode == HTTP_CODE_OK) {
        String response = http.getString();
        //Serial.println(response);
        u8g2.setCursor(0, 15);
        u8g2.print((response.substring(13,23)));
        u8g2.setCursor(0, 30);
        u8g2.print((response.substring(24,32)));
        u8g2.sendBuffer();  
      }
  } else {
      Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
      
  }
  http.end();
  delay(900);
}

运行效果:

在这里插入图片描述

标签:u8g2,http,ESP8266,WiFi,实时,OLED,println,Serial,include
来源: https://blog.csdn.net/weixin_44301306/article/details/121292415