RM新时代网站-首页

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

ESP8266網(wǎng)絡(luò)天氣時鐘OLED顯示

jf_07365693 ? 來源:jf_07365693 ? 作者:jf_07365693 ? 2024-06-28 04:46 ? 次閱讀

Arduino代碼

注意修改 WiFi 名稱和密碼
需要申請【心知天氣】API私鑰
心知天氣URL:https://www.seniverse.com

/*
參考:
https://blog.csdn.net/weixin_44668788/article/details/120643078
http://www.taichi-maker.com/homepage/iot-development/iot-platform/seniverse/esp8266-application/
OLED connect with ESP8266 NodeMCU
SCL -- D1
SDA -- D2
*/
//引入必要的頭文件
#include < ArduinoJson.h >
#include < ESP8266WiFi.h >
#include < SPI.h >
#include < U8g2lib.h >
#include < WiFiUdp.h >
#include < TimeLib.h >
#include < DNSServer.h >
#include < ESP8266WebServer.h >
WiFiUDP Udp;
unsigned int localPort = 8888; // 用于偵聽UDP數(shù)據(jù)包的本地端口

//網(wǎng)絡(luò)校時的相關(guān)配置
static const char ntpServerName[] = "ntp1.aliyun.com"; //NTP服務(wù)器,使用阿里云
int timeZone = 8; //時區(qū)設(shè)置,采用東8區(qū)

//保存斷網(wǎng)前的最新數(shù)據(jù)
int results_0_now_temperature_int_old;
String results_0_now_text_str_old;
int results_0_daily_1_high_int_old;
int results_0_daily_1_low_int_old;
String results_0_daily_1_text_day_str_old;

//函數(shù)聲明
time_t getNtpTime();
void sendNTPpacket(IPAddress &address);
void oledClockDisplay();
void sendCommand(int command, int value);
void initdisplay();
void connectWiFi();
void parseInfo_now(WiFiClient client,int i);
void parseInfo_fut(WiFiClient client,int i);
//
boolean isNTPConnected = false;

const unsigned char xing[] U8X8_PROGMEM = {
    0x00, 0x00, 0xF8, 0x0F, 0x08, 0x08, 0xF8, 0x0F, 0x08, 0x08, 0xF8, 0x0F, 0x80, 0x00, 0x88, 0x00,
    0xF8, 0x1F, 0x84, 0x00, 0x82, 0x00, 0xF8, 0x0F, 0x80, 0x00, 0x80, 0x00, 0xFE, 0x3F, 0x00, 0x00}; /*星*/
const unsigned char liu[] U8X8_PROGMEM = {
    0x40, 0x00, 0x80, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00,
    0x20, 0x02, 0x20, 0x04, 0x10, 0x08, 0x10, 0x10, 0x08, 0x10, 0x04, 0x20, 0x02, 0x20, 0x00, 0x00}; /*六*/

typedef struct
{                  //存儲配置結(jié)構(gòu)體
    int tz;        //時間戳
} config_type;
config_type config;

WiFiClient clientNULL;
DNSServer dnsServer;
ESP8266WebServer server(80);


//----------WIFI連接配置----------
const char* ssid     = "xxxxxx";       // WiFi名,需要更改
const char* password = "xxxxxx";          // WiFi密碼,需要更改
                                            // 請將您需要連接的WiFi密碼填入引號中
//----------天氣API配置----------
const char* host = "api.seniverse.com";   // 將要連接的服務(wù)器地址  
const int httpPort = 80;              // 將要連接的服務(wù)器端口      

// 心知天氣HTTP請求所需信息
String reqUserKey = "xxxxxxx";   // 私鑰,需要申請
String reqLocation = "Shanghai";            // 城市
String reqUnit = "c";                      // 攝氏/華氏

//----------設(shè)置屏幕----------
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE);
int sta = 0;
//----------初始化OLED----------
void initdisplay()
{
    u8g2.begin();
    u8g2.enableUTF8Print();
}
//----------用于獲取實時天氣的函數(shù)(0)----------
void TandW(){
  String reqRes = "/v3/weather/now.json?key=" + reqUserKey +
                  + "&location=" + reqLocation + 
                  "&language=en&unit=" +reqUnit;
  // 向心知天氣服務(wù)器服務(wù)器請求信息并對信息進(jìn)行解析
  httpRequest(reqRes,0);
  //延遲,需要低于20次/分鐘
  delay(5000);
}
void display_1(int results_0_now_temperature_int,String results_0_now_text_str);//聲明函數(shù),用于顯示溫度、天氣


//----------獲取3天預(yù)報(1)----------
void threeday(){
  // 建立心知天氣API當(dāng)前天氣請求資源地址
  String reqRes = "/v3/weather/daily.json?key=" + reqUserKey +
                  + "&location=" + reqLocation + "&language=en&unit=" +
                  reqUnit + "&start=0&days=3";

  // 向心知天氣服務(wù)器服務(wù)器請求信息并對信息進(jìn)行解析
  httpRequest(reqRes,1);
  delay(5000);
}

void clock_display(time_t prevDisplay){
    server.handleClient();
    dnsServer.processNextRequest();
    if (timeStatus() != timeNotSet)
    {
        if (now() != prevDisplay)
        { //時間改變時更新顯示
            prevDisplay = now();
            oledClockDisplay();
        }
    }
}


void setup(){
  Serial.begin(9600);          
  Serial.println("");
  initdisplay();
  // 連接WiFi

  
  
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_unifont_t_chinese2);
  u8g2.setCursor(0, 14);
  u8g2.print("Waiting for WiFi");
  u8g2.setCursor(0, 30);
  u8g2.print("connection...");
  u8g2.sendBuffer();
  connectWiFi();
  Udp.begin(localPort);
  setSyncProvider(getNtpTime);
  setSyncInterval(300); //每300秒同步一次時間

  
}

time_t prevDisplay = 0; //當(dāng)時鐘已經(jīng)顯示
 
void loop(){
    if (sta >=0 && sta<=250){
      clock_display(prevDisplay);
      
    }else if(sta == 251){
      TandW();
      
    }else{
      threeday();
      
    }

    ++sta;

    if(sta==253){
      sta = 0;
    }
}
 
// 向心知天氣服務(wù)器服務(wù)器請求信息并對信息進(jìn)行解析
void httpRequest(String reqRes,int stat){
  WiFiClient client;

  // 建立http請求信息
  String httpRequest = String("GET ") + reqRes + " HTTP/1.1rn" + 
                              "Host: " + host + "rn" + 
                              "Connection: closernrn";
  Serial.println(""); 
  Serial.print("Connecting to "); Serial.print(host);

  // 嘗試連接服務(wù)器
  if (client.connect(host, 80)){
    Serial.println(" Success!");
 
    // 向服務(wù)器發(fā)送http請求信息
    client.print(httpRequest);
    Serial.println("Sending request: ");
    Serial.println(httpRequest);  
 
    // 獲取并顯示服務(wù)器響應(yīng)狀態(tài)行 
    String status_response = client.readStringUntil('n');
    Serial.print("status_response: ");
    Serial.println(status_response);
 
    // 使用find跳過HTTP響應(yīng)頭
    if (client.find("rnrn")) {
      Serial.println("Found Header End. Start Parsing.");
    }
    if (stat == 0){
      
      // 利用ArduinoJson庫解析心知天氣響應(yīng)信息(實時數(shù)據(jù))
      parseInfo_now(client,1); 
    }else if(stat == 1){
      parseInfo_fut(client,1);
    }
  }
  else {
    Serial.println(" connection failed!");
    if (stat == 0){
      
      // 利用ArduinoJson庫解析心知天氣響應(yīng)信息(實時數(shù)據(jù))
      parseInfo_now(clientNULL,0); 
    }else if(stat == 1){
      parseInfo_fut(clientNULL,0);
    }
  }
    
  //斷開客戶端與服務(wù)器連接工作
  client.stop(); 
}

// 連接WiFi
void connectWiFi(){
  WiFi.begin(ssid, password);                  // 啟動網(wǎng)絡(luò)連接
  Serial.print("Connecting to ");              // 串口監(jiān)視器輸出網(wǎng)絡(luò)連接信息
  Serial.print(ssid); Serial.println(" ...");  // 告知用戶NodeMCU正在嘗試WiFi連接
  
  int i = 0;                                   // 這一段程序語句用于檢查WiFi是否連接成功
  while (WiFi.status() != WL_CONNECTED) {      // WiFi.status()函數(shù)的返回值是由NodeMCU的WiFi連接狀態(tài)所決定的。 
    delay(1000);                               // 如果WiFi連接成功則返回值為WL_CONNECTED                       
    Serial.print(i++); Serial.print(' ');      // 此處通過While循環(huán)讓NodeMCU每隔一秒鐘檢查一次WiFi.status()函數(shù)返回值
  }                                            // 同時NodeMCU將通過串口監(jiān)視器輸出連接時長讀秒。
                                               // 這個讀秒是通過變量i每隔一秒自加1來實現(xiàn)的。                                              
  Serial.println("");                          // WiFi連接成功后
  Serial.println("Connection established!");   // NodeMCU將通過串口監(jiān)視器輸出"連接成功"信息。
  Serial.print("IP address:    ");             // 同時還將輸出NodeMCU的IP地址。這一功能是通過調(diào)用
  Serial.println(WiFi.localIP());              // WiFi.localIP()函數(shù)來實現(xiàn)的。該函數(shù)的返回值即NodeMCU的IP地址。  
}

// 利用ArduinoJson庫解析心知天氣響應(yīng)信息(實時)
void parseInfo_now(WiFiClient client,int i){

  if(i==1){
  const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(1) + 2*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(6) + 230;
  DynamicJsonDocument doc(capacity);
  
  deserializeJson(doc, client);
  
  JsonObject results_0 = doc["results"][0];
  
  JsonObject results_0_now = results_0["now"];
  const char* results_0_now_text = results_0_now["text"]; // "Sunny"
  const char* results_0_now_code = results_0_now["code"]; // "0"
  const char* results_0_now_temperature = results_0_now["temperature"]; // "32"
  
  const char* results_0_last_update = results_0["last_update"]; // "2020-06-02T14:40:00+08:00" 
 
  // 通過串口監(jiān)視器顯示以上信息
  String results_0_now_text_str = results_0_now["text"].as< String >(); 
  int results_0_now_code_int = results_0_now["code"].as< int >(); 
  int results_0_now_temperature_int = results_0_now["temperature"].as< int >(); 
  String results_0_last_update_str = results_0["last_update"].as< String >();   
 
  Serial.println(F("======Weahter Now======="));
  Serial.print(F("Weather Now: "));
  Serial.print(results_0_now_text_str);
  Serial.print(F(" "));
  Serial.println(results_0_now_code_int);
  Serial.print(F("Temperature: "));
  Serial.println(results_0_now_temperature_int);
  Serial.print(F("Last Update: "));
  Serial.println(results_0_last_update_str);
  Serial.println(F("========================"));
  display_0(results_0_now_temperature_int,results_0_now_text_str);
  results_0_now_text_str_old = results_0_now_text_str;
  results_0_now_temperature_int_old = results_0_now_temperature_int;
  }
  else{
    display_0(results_0_now_temperature_int_old,results_0_now_text_str_old);
  }
 
}
//----------輸出實時天氣----------
void display_0(int results_0_now_temperature_int,String results_0_now_text_str){
  //顯示輸出
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_wqy16_t_gb2312);
  u8g2.setCursor(15, 14);
  u8g2.print("上海實時天氣");
  u8g2.setFont(u8g2_font_logisoso24_tr);
  u8g2.setCursor(45, 44);
  u8g2.print(results_0_now_temperature_int);
  u8g2.setCursor(15, 61);
  u8g2.setFont(u8g2_font_unifont_t_chinese2);
  u8g2.print(results_0_now_text_str);
  u8g2.sendBuffer();
}


// 利用ArduinoJson庫解析心知天氣響應(yīng)信息(預(yù)測)
void parseInfo_fut(WiFiClient client,int i){
  if(i==1){
    
  
  const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_ARRAY_SIZE(3) + JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(6) + 3*JSON_OBJECT_SIZE(14) + 860;
  
  DynamicJsonDocument doc(capacity);
  
  deserializeJson(doc, client);
  
  JsonObject results_0 = doc["results"][0];
  
  JsonArray results_0_daily = results_0["daily"];
  
  JsonObject results_0_daily_0 = results_0_daily[0];
  const char* results_0_daily_0_date = results_0_daily_0["date"]; 
  const char* results_0_daily_0_text_day = results_0_daily_0["text_day"]; 
  const char* results_0_daily_0_code_day = results_0_daily_0["code_day"];
  const char* results_0_daily_0_text_night = results_0_daily_0["text_night"]; 
  const char* results_0_daily_0_code_night = results_0_daily_0["code_night"]; 
  const char* results_0_daily_0_high = results_0_daily_0["high"];
  const char* results_0_daily_0_low = results_0_daily_0["low"]; 
  const char* results_0_daily_0_rainfall = results_0_daily_0["rainfall"];
  const char* results_0_daily_0_precip = results_0_daily_0["precip"]; 
  const char* results_0_daily_0_wind_direction = results_0_daily_0["wind_direction"]; 
  const char* results_0_daily_0_wind_direction_degree = results_0_daily_0["wind_direction_degree"];
  const char* results_0_daily_0_wind_speed = results_0_daily_0["wind_speed"];
  const char* results_0_daily_0_wind_scale = results_0_daily_0["wind_scale"];
  const char* results_0_daily_0_humidity = results_0_daily_0["humidity"];
  
  JsonObject results_0_daily_1 = results_0_daily[1];
  const char* results_0_daily_1_date = results_0_daily_1["date"];
  const char* results_0_daily_1_text_day = results_0_daily_1["text_day"];
  const char* results_0_daily_1_code_day = results_0_daily_1["code_day"];
  const char* results_0_daily_1_text_night = results_0_daily_1["text_night"]; 
  const char* results_0_daily_1_code_night = results_0_daily_1["code_night"]; 
  const char* results_0_daily_1_high = results_0_daily_1["high"];
  const char* results_0_daily_1_low = results_0_daily_1["low"]; 
  const char* results_0_daily_1_rainfall = results_0_daily_1["rainfall"]; 
  const char* results_0_daily_1_precip = results_0_daily_1["precip"]; 
  const char* results_0_daily_1_wind_direction = results_0_daily_1["wind_direction"];
  const char* results_0_daily_1_wind_direction_degree = results_0_daily_1["wind_direction_degree"]; 
  const char* results_0_daily_1_wind_speed = results_0_daily_1["wind_speed"];
  const char* results_0_daily_1_wind_scale = results_0_daily_1["wind_scale"];
  const char* results_0_daily_1_humidity = results_0_daily_1["humidity"]; 
  
  JsonObject results_0_daily_2 = results_0_daily[2];
  const char* results_0_daily_2_date = results_0_daily_2["date"];
  const char* results_0_daily_2_text_day = results_0_daily_2["text_day"];
  const char* results_0_daily_2_code_day = results_0_daily_2["code_day"];
  const char* results_0_daily_2_text_night = results_0_daily_2["text_night"];
  const char* results_0_daily_2_code_night = results_0_daily_2["code_night"];
  const char* results_0_daily_2_high = results_0_daily_2["high"]; 
  const char* results_0_daily_2_low = results_0_daily_2["low"]; 
  const char* results_0_daily_2_rainfall = results_0_daily_2["rainfall"];
  const char* results_0_daily_2_precip = results_0_daily_2["precip"]; 
  const char* results_0_daily_2_wind_direction = results_0_daily_2["wind_direction"]; 
  const char* results_0_daily_2_wind_direction_degree = results_0_daily_2["wind_direction_degree"]; 
  const char* results_0_daily_2_wind_speed = results_0_daily_2["wind_speed"];
  const char* results_0_daily_2_wind_scale = results_0_daily_2["wind_scale"]; 
  const char* results_0_daily_2_humidity = results_0_daily_2["humidity"]; 
  
  const char* results_0_last_update = results_0["last_update"]; 
  
  // 從以上信息中摘選幾個通過串口監(jiān)視器顯示
  String results_0_daily_0_date_str = results_0_daily_0["date"].as< String >();
  String  results_0_daily_0_text_day_str = results_0_daily_0["text_day"].as< String >(); 
  int results_0_daily_0_code_day_int = results_0_daily_0["code_day"].as< int >(); 
  String results_0_daily_0_text_night_str = results_0_daily_0["text_night"].as< String >(); 
  int results_0_daily_0_code_night_int = results_0_daily_0["code_night"].as< int >(); 
  int results_0_daily_0_high_int = results_0_daily_0["high"].as< int >();
  int results_0_daily_0_low_int = results_0_daily_0["low"].as< int >();
  String results_0_last_update_str = results_0["last_update"].as< String >();

  int results_0_daily_1_high_int = results_0_daily_1["high"].as< int >();
  int results_0_daily_1_low_int = results_0_daily_1["low"].as< int >();
  String results_0_daily_1_text_day_str = results_0_daily_1["text_day"].as< String >();
 
  Serial.println(F("======Today Weahter ======="));
  Serial.print(F("DATE: "));
  Serial.println(results_0_daily_0_date_str);
  Serial.print(F("Day Weather: "));
  Serial.print(results_0_daily_0_text_day_str);
  Serial.print(F(" "));
  Serial.println(results_0_daily_0_code_day_int);
  Serial.print(F("Night Weather: "));
  Serial.print(results_0_daily_0_text_night_str);
  Serial.print(F(" "));
  Serial.println(results_0_daily_0_code_night_int);
  Serial.print(F("High: "));
  Serial.println(results_0_daily_0_high_int);
  Serial.print(F("LOW: "));
  Serial.println(results_0_daily_0_low_int);
  Serial.print(F("Last Update: "));
  Serial.println(results_0_last_update_str);
  Serial.println(F("=============================="));
  display_1(results_0_daily_1_high_int,results_0_daily_1_low_int,results_0_daily_1_text_day_str);
  results_0_daily_1_high_int_old=results_0_daily_1_high_int;
  results_0_daily_1_low_int_old=results_0_daily_1_low_int;
  results_0_daily_1_text_day_str_old=results_0_daily_1_text_day_str;
}else{
  display_1(results_0_daily_1_high_int_old,results_0_daily_1_low_int_old,results_0_daily_1_text_day_str_old);
}


  
}
//----------預(yù)測明天天氣----------
void display_1(int results_0_daily_1_high_int,int results_0_daily_1_low_int,String results_0_daily_1_text_day_str){
  //顯示輸出
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_wqy16_t_gb2312);
  u8g2.setCursor(15, 14);
  u8g2.print("上海明天天氣");
  
  u8g2.setFont(u8g2_font_logisoso24_tr);
  u8g2.setCursor(20, 46);
  u8g2.print(results_0_daily_1_low_int);
  u8g2.setCursor(56, 46);
  u8g2.print("~");
  u8g2.setCursor(75, 46);
  u8g2.print(results_0_daily_1_high_int);
  
  u8g2.setCursor(30, 62);
  u8g2.setFont(u8g2_font_unifont_t_chinese2);
  u8g2.print(results_0_daily_1_text_day_str);
  u8g2.sendBuffer();
}


/*-------- NTP 代碼 ----------*/

const int NTP_PACKET_SIZE = 48;     // NTP時間在消息的前48個字節(jié)里
byte packetBuffer[NTP_PACKET_SIZE]; // 輸入輸出包的緩沖區(qū)

time_t getNtpTime()
{
    IPAddress ntpServerIP; // NTP服務(wù)器的地址

    while (Udp.parsePacket() > 0)
        ; // 丟棄以前接收的任何數(shù)據(jù)包
    Serial.println("Transmit NTP Request");
    // 從池中獲取隨機服務(wù)器
    WiFi.hostByName(ntpServerName, ntpServerIP);
    Serial.print(ntpServerName);
    Serial.print(": ");
    Serial.println(ntpServerIP);
    sendNTPpacket(ntpServerIP);
    uint32_t beginWait = millis();
    while (millis() - beginWait < 1500)
    {
        int size = Udp.parsePacket();
        if (size >= NTP_PACKET_SIZE)
        {
            Serial.println("Receive NTP Response");
            isNTPConnected = true;
            Udp.read(packetBuffer, NTP_PACKET_SIZE); // 將數(shù)據(jù)包讀取到緩沖區(qū)
            unsigned long secsSince1900;
            // 將從位置40開始的四個字節(jié)轉(zhuǎn)換為長整型,只取前32位整數(shù)部分
            secsSince1900 = (unsigned long)packetBuffer[40] < < 24;
            secsSince1900 |= (unsigned long)packetBuffer[41] < < 16;
            secsSince1900 |= (unsigned long)packetBuffer[42] < < 8;
            secsSince1900 |= (unsigned long)packetBuffer[43];
            Serial.println(secsSince1900);
            Serial.println(secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR);
            return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
        }
    }
    Serial.println("No NTP Response :-("); //無NTP響應(yīng)
    isNTPConnected = false;
    return 0; //如果未得到時間則返回0
}

// 向給定地址的時間服務(wù)器發(fā)送NTP請求
void sendNTPpacket(IPAddress &address)
{
    memset(packetBuffer, 0, NTP_PACKET_SIZE);
    packetBuffer[0] = 0b11100011; // LI, Version, Mode
    packetBuffer[1] = 0;          // Stratum, or type of clock
    packetBuffer[2] = 6;          // Polling Interval
    packetBuffer[3] = 0xEC;       // Peer Clock Precision
    // 8 bytes of zero for Root Delay & Root Dispersion
    packetBuffer[12] = 49;
    packetBuffer[13] = 0x4E;
    packetBuffer[14] = 49;
    packetBuffer[15] = 52;
    Udp.beginPacket(address, 123); //NTP需要使用的UDP端口號為123
    Udp.write(packetBuffer, NTP_PACKET_SIZE);
    Udp.endPacket();
}

void oledClockDisplay()
{
    int years, months, days, hours, minutes, seconds, weekdays;
    years = year();
    months = month();
    days = day();
    hours = hour();
    minutes = minute();
    seconds = second();
    weekdays = weekday();
    Serial.printf("%d/%d/%d %d:%d:%d Weekday:%dn", years, months, days, hours, minutes, seconds, weekdays);
    u8g2.clearBuffer();
    u8g2.setFont(u8g2_font_unifont_t_chinese2);
    u8g2.setCursor(0, 14);
    if (isNTPConnected)
    {
        if(timeZone >=0)
        {
            u8g2.print("當(dāng)前時間(UTC+");
            u8g2.print(timeZone);
            u8g2.print(")");
        }
        else
        {
            u8g2.print("當(dāng)前時間(UTC");
            u8g2.print(timeZone);
            u8g2.print(")");
        }
    }
    else
        u8g2.print("無網(wǎng)絡(luò)!"); //如果上次對時失敗,則會顯示無網(wǎng)絡(luò)
    String currentTime = "";
    if (hours < 10)
        currentTime += 0;
    currentTime += hours;
    currentTime += ":";
    if (minutes < 10)
        currentTime += 0;
    currentTime += minutes;
    currentTime += ":";
    if (seconds < 10)
        currentTime += 0;
    currentTime += seconds;
    String currentDay = "";
    currentDay += years;
    currentDay += "/";
    if (months < 10)
        currentDay += 0;
    currentDay += months;
    currentDay += "/";
    if (days < 10)
        currentDay += 0;
    currentDay += days;

    u8g2.setFont(u8g2_font_logisoso24_tr);
    u8g2.setCursor(0, 44);
    u8g2.print(currentTime);
    u8g2.setCursor(0, 61);
    u8g2.setFont(u8g2_font_unifont_t_chinese2);
    u8g2.print(currentDay);
    u8g2.drawXBM(80, 48, 16, 16, xing);
    u8g2.setCursor(95, 62);
    u8g2.print("期");
    if (weekdays == 1)
        u8g2.print("日");
    else if (weekdays == 2)
        u8g2.print("一");
    else if (weekdays == 3)
        u8g2.print("二");
    else if (weekdays == 4)
        u8g2.print("三");
    else if (weekdays == 5)
        u8g2.print("四");
    else if (weekdays == 6)
        u8g2.print("五");
    else if (weekdays == 7)
        u8g2.drawXBM(111, 49, 16, 16, liu);
    u8g2.sendBuffer();
}

實現(xiàn)效果如下
當(dāng)前時間.png
明天天氣.png
實時天氣.png

轉(zhuǎn)載請注明出處,感謝!

審核編輯 黃宇

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • OLED
    +關(guān)注

    關(guān)注

    119

    文章

    6198

    瀏覽量

    224085
  • 時鐘
    +關(guān)注

    關(guān)注

    10

    文章

    1733

    瀏覽量

    131446
  • Arduino
    +關(guān)注

    關(guān)注

    188

    文章

    6468

    瀏覽量

    186947
  • ESP8266
    +關(guān)注

    關(guān)注

    50

    文章

    962

    瀏覽量

    44956
收藏 人收藏

    評論

    相關(guān)推薦

    ESP8266 太空人動畫的 OLED 顯示

    ESP8266 太空人動畫的 OLED 顯示
    的頭像 發(fā)表于 10-08 15:06 ?262次閱讀
    <b class='flag-5'>ESP8266</b> 太空人動畫的 <b class='flag-5'>OLED</b> <b class='flag-5'>顯示</b>

    arduino(1)--ESP8266配置

    之后在打開開發(fā)板管理器。 之后輸入esp8266,安裝對應(yīng)的庫文件,因為我已經(jīng)安裝,故顯示為灰色,由于網(wǎng)絡(luò)原因,下載的時間可能比較久。 安裝完畢之后,選擇Generic ESP8266
    發(fā)表于 09-06 11:00

    STM32 ESP8266阿里云鏈接源碼

    stm32F103C8T6 ESP8266 物聯(lián)網(wǎng)電表 登錄阿里云
    發(fā)表于 08-29 14:21 ?6次下載

    esp32和esp8266代碼共用嗎

    本文將介紹ESP32和ESP8266兩款流行的微控制器在代碼共用性方面的可能性與差異性。 一、引言 隨著物聯(lián)網(wǎng)技術(shù)的飛速發(fā)展,越來越多的智能設(shè)備開始進(jìn)入我們的生活。其中,ESP32和ESP82
    的頭像 發(fā)表于 08-19 18:23 ?1109次閱讀

    esp8266esp32區(qū)別是什么

    以下是關(guān)于ESP8266ESP32的主要區(qū)別: 處理器和架構(gòu) : ESP8266 :使用一個Tensilica L106 80MHz的處理器,屬于Xtensa架構(gòu)。 ESP32 :使
    的頭像 發(fā)表于 08-19 18:16 ?5230次閱讀

    esp8266不燒錄可以使用嗎

    ESP8266是一款非常流行的Wi-Fi模塊,廣泛應(yīng)用于物聯(lián)網(wǎng)項目中。然而,如果不進(jìn)行燒錄,ESP8266將無法正常工作。 1. ESP8266簡介 ESP8266是一款由Espres
    的頭像 發(fā)表于 08-19 17:28 ?759次閱讀

    esp8266wifi模塊怎么連接手機

    ESP8266 : 使用USB轉(zhuǎn)TTL模塊將ESP8266模塊與電腦連接。 通過串口調(diào)試助手發(fā)送AT指令配置ESP8266,包括設(shè)置WiFi模式為STA模式(客戶端模式),連接指定的WiFi網(wǎng)
    的頭像 發(fā)表于 08-19 17:27 ?2623次閱讀

    Arduino Nano 和 NodeMCU ESP8266 讀取 DHT11 環(huán)境溫濕度數(shù)據(jù)及 OLED顯示

    Arduino Nano 和 NodeMCU ESP8266 讀取 DHT11 環(huán)境溫濕度數(shù)據(jù)及 OLED顯示
    的頭像 發(fā)表于 08-13 18:04 ?1109次閱讀
    Arduino Nano 和 NodeMCU <b class='flag-5'>ESP8266</b> 讀取 DHT11 環(huán)境溫濕度數(shù)據(jù)及 <b class='flag-5'>OLED</b><b class='flag-5'>顯示</b>

    是否可以創(chuàng)建一個包含多個esp8266模塊(esp-01)的網(wǎng)狀網(wǎng)絡(luò)?

    我想知道是否可以創(chuàng)建一個包含多個 esp8266 模塊(esp-01)的網(wǎng)狀網(wǎng)絡(luò)。如果是這樣,是否有任何指南/示例可以開始使用 esp8266 進(jìn)行網(wǎng)狀
    發(fā)表于 07-19 08:31

    ESP8266如何實現(xiàn)網(wǎng)狀網(wǎng)絡(luò)?

    ESP8266如何實現(xiàn)網(wǎng)狀網(wǎng)絡(luò)?
    發(fā)表于 07-09 08:06

    ESP8266網(wǎng)絡(luò)天氣時鐘OLED顯示

    OLED顯示
    jf_07365693
    發(fā)布于 :2024年06月28日 04:36:26

    國產(chǎn)低成本W(wǎng)i-Fi SoC解決方案芯片ESP8266ESP8285對比差異

    ESP8266ESP8285對比差異 ESP8285相當(dāng)于在ESP8266基礎(chǔ)上多加了1/2MB Flash, ESP8285與
    的頭像 發(fā)表于 05-17 11:44 ?1317次閱讀
    國產(chǎn)低成本W(wǎng)i-Fi SoC解決方案芯片<b class='flag-5'>ESP8266</b>與<b class='flag-5'>ESP</b>8285對比差異

    確定ESP8266固件下載成功的方法

    在物聯(lián)網(wǎng)設(shè)備的開發(fā)過程中,確定esp8266固件是否成功下載至設(shè)備十分關(guān)鍵。以下是一種簡單有效的確認(rèn)方法:機智云物聯(lián)網(wǎng)平臺首先,確保你的ESP8266模塊已經(jīng)正確連接至計算機,并通過串口調(diào)試助手進(jìn)行
    的頭像 發(fā)表于 05-16 08:10 ?586次閱讀
    確定<b class='flag-5'>ESP8266</b>固件下載成功的方法

    使用Wi-Fi ESP8266方案模組接入云平臺

    ESP8266的模塊芯片是基于無線通信協(xié)議的UARTWi-Fi透傳模塊芯片,支持802.11b/g/n的無線標(biāo)準(zhǔn),并帶有三種可選擇的工作模式。ESP8266模塊的控制是通過AT指令的形式控制,例如
    的頭像 發(fā)表于 05-10 08:20 ?1819次閱讀
    使用Wi-Fi <b class='flag-5'>ESP8266</b>方案模組接入云平臺

    STM32、ESP8266與MQTT連接阿里云物聯(lián)網(wǎng)的串口通信異常解析

    STM32、ESP8266與MQTT協(xié)議連接阿里云物聯(lián)網(wǎng)平臺時常見的串口通信異常介紹 在構(gòu)建物聯(lián)網(wǎng)應(yīng)用時,STM32、ESP8266與MQTT協(xié)議的結(jié)合是實現(xiàn)設(shè)備與網(wǎng)絡(luò)間穩(wěn)定通信的關(guān)鍵。然而,在連接
    的頭像 發(fā)表于 04-19 17:19 ?1437次閱讀
    RM新时代网站-首页