ESP32万年历 Arduino编程 U8g2库 OLED显示

课程设计 基于ESP32的万年历

###功能:从NTP网络时间服务器上获取当前时间,从聚合数据处获取阳历阴历时间。效果图如下。
ESP32万年历 Arduino编程 U8g2库 OLED显示_第1张图片ESP32万年历 Arduino编程 U8g2库 OLED显示_第2张图片

一些初始化

//包含一下相关头文件
#include 
#include 
#include 
#include 
#include 
#include 
#include 
//按照u8g2教程配置
U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, 22,  21,  U8X8_PIN_NONE); 
//实例化HTTP对象
HTTPClient http;

//WIFI相关参数
const char * ID = "填你的WIFIID";
const char * PASSWORD = "填你的WIFI密码";
const char *ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 8 * 3600;
const int daylightOffset_sec = 0;

//天气相关参数
String LunarURL = "";
String url_JuHe = "";
String Lunar = "0";
char Lunar1[10];
String Sun = "0";
String lunarYear = "0";
String Yeartime;

//时间相关参数
  struct tm timeinfo;
  int ss;
  int mm;
  int hh;
  int mday;
  int mon;
  int year;
  String sec;
  String minute;
  String hour;
  String day;
  String month;
  String yeart;
/* 创建实例 */

获取聚合API数据

String GitURL(String URL)
{
  url_JuHe =  "http://v.juhe.cn/calendar/day?date=";
  url_JuHe = url_JuHe+URL;
  url_JuHe = url_JuHe+"&key=可在聚合申请免费key";
  return url_JuHe;
}

解析获得的JSON数据

void ParseLunar(String url)
{  

  DynamicJsonDocument doc(1024); //分配内存,动态
  http.begin(url);
 
  int httpGet = http.GET();
  if(httpGet > 0)
  {
    Serial.printf("HTTPGET is %d",httpGet);
    
    if(httpGet == HTTP_CODE_OK)
    {
      String json = http.getString();
      Serial.println(json);
      
      deserializeJson(doc, json);
      
      Lunar = doc["result"]["data"]["lunar"].as<String>();
      Sun =   doc["result"]["data"]["date"].as<String>();
      lunarYear =   doc["result"]["data"]["lunarYear"].as<String>();
    }
    else
    {
      Serial.printf("ERROR1!!");
    }
  }
  else
  {
    Serial.printf("ERROR2!!");
  }
  http.end();
}
#include 
#include 
#include 
#include 
#include 
#include 
#include 

U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, 22,  21,  U8X8_PIN_NONE); 
HTTPClient http;

//WIFI相关参数
const char * ID = "填你的WIFIID";
const char * PASSWORD = "填你的WIFI密码";
const char *ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 8 * 3600;
const int daylightOffset_sec = 0;

//天气相关参数
String LunarURL = "";
String url_JuHe = "";
String Lunar = "0";
char Lunar1[10];
String Sun = "0";
String lunarYear = "0";
String Yeartime;

//时间相关参数
  struct tm timeinfo;
  int ss;
  int mm;
  int hh;
  int mday;
  int mon;
  int year;
  String sec;
  String minute;
  String hour;
  String day;
  String month;
  String yeart;
/* 创建实例 */
 

 
String GitURL(String URL)
{
  url_JuHe =  "http://v.juhe.cn/calendar/day?date=";
  url_JuHe = url_JuHe+URL;
  url_JuHe = url_JuHe+"&key=可在聚合申请免费key";
  return url_JuHe;
}

void ParseLunar(String url)
{  

  DynamicJsonDocument doc(1024); //分配内存,动态
  http.begin(url);
 
  int httpGet = http.GET();
  if(httpGet > 0)
  {
    Serial.printf("HTTPGET is %d",httpGet);
    
    if(httpGet == HTTP_CODE_OK)
    {
      String json = http.getString();
      Serial.println(json);
      
      deserializeJson(doc, json);
      
      Lunar = doc["result"]["data"]["lunar"].as<String>();
      Sun =   doc["result"]["data"]["date"].as<String>();
      lunarYear =   doc["result"]["data"]["lunarYear"].as<String>();
    }
    else
    {
      Serial.printf("ERROR1!!");
    }
  }
  else
  {
    Serial.printf("ERROR2!!");
  }
  http.end();
}

void setup() 
{
  pinMode(2,OUTPUT);
  pinMode(17,OUTPUT);
  Serial.begin(115200);
  //==================OLED==================
  //初始化屏幕
  u8g2.begin();
  //使能UTF8字体打印
  u8g2.enableUTF8Print(); 
  //==================OLED==================
  
  //==================wifi连接==================
  Serial.println("WiFi:");
  Serial.println(ID);
  Serial.println("PASSWORLD:");
  Serial.println(PASSWORD);
  WiFi.begin(ID,PASSWORD);
  
  while(WiFi.status()!=WL_CONNECTED)
  {
    delay(500);
    Serial.println("正在连接...");
  }
  Serial.println("连接成功!");
  //==================wifi连接==================
  //==================获取时间==================
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); 
  getLocalTime(&timeinfo,5000);
  delay(5000); 
  if (!getLocalTime(&timeinfo))
   {
      Serial.println("Failed to obtain time");
      return;
   }
  ss = timeinfo.tm_sec;
  mm = timeinfo.tm_min;
  hh = timeinfo.tm_hour;
  mday = timeinfo.tm_mday;
  mon = timeinfo.tm_mon;
  year = timeinfo.tm_year;
  //做类型转换是为了适配u8g2的显示
  sec = String(ss);
  minute = String(mm);
  hour = String(hh);
  day = String(mday);
  month = String(mon+1);
  yeart = String(year+1900);
  Yeartime = yeart + "-" + month +"-" + day;
  Serial.println(Yeartime);
  
  LunarURL = GitURL(Yeartime); 
  ParseLunar(LunarURL);
  Serial.println(LunarURL);
  //==================获取时间==================
}
 
void loop() 
{
  Serial.println(Lunar);
  Serial.println(Sun);
  u8g2.firstPage();
  do {
     //==================时间显示==================
    if (!getLocalTime(&timeinfo))
    {
        Serial.println("Failed to obtain time");
        return;
    }
    ss = timeinfo.tm_sec;
    mm = timeinfo.tm_min;
    hh = timeinfo.tm_hour;
    sec = String(ss);
    minute = String(mm);
    hour = String(hh);
    if(ss == 00&&mm == 00 &&hh ==16)
    {
      digitalWrite(17,HIGH);
    }
    if(ss == 10&&mm == 00 &&hh ==16)
    {
      digitalWrite(17,LOW);
    }
    u8g2.setFont(u8g2_font_ncenB14_tr);
    u8g2.setCursor(0, 20);
    u8g2.print(hour);

    u8g2.setCursor(24, 20);
    u8g2.print(":");
    
    u8g2.setCursor(31, 20);
    u8g2.print(minute);
    
    u8g2.setCursor(54, 20);
    u8g2.print(":");
    u8g2.setCursor(61, 20);
    u8g2.print(sec);
     //==================时间显示==================
     //==================日历显示==================
    u8g2.setFont(u8g2_font_unifont_t_chinese3); 
    u8g2.setCursor(0, 40);
    u8g2.print("阳历 ");
    u8g2.setCursor(36, 40);
    u8g2.print(Sun);
    u8g2.setFont(u8g2_font_unifont_t_chinese1); 
    u8g2.setCursor(0, 60);
    u8g2.print(lunarYear);
    u8g2.setCursor(56, 60);
    u8g2.print(Lunar);
    //==================日历显示==================
//    u8g2.setCursor(30, 60);
//    u8g2.print(LYeartime);
  } while ( u8g2.nextPage() ); 
}

字库方面,我找了本站的一个u8g2自制字库的教程,做了个可以显示“零壹贰叁肆伍陆柒捌玖拾廿卅卌一二三四五六七八九十丑寅卯辰巳午未申酉戌亥甲乙丙丁戊己庚辛壬癸年月日鼠牛虎兔龙蛇马羊猴鸡狗猪”的字库 以供农历显示。有需要可留言

参考资料https://blog.csdn.net/qq_41650023/article/details/124697234?ops_request_misc=&request_id=&biz_id=102&utm_term=esp32%E5%A4%A9%E6%B0%94&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-1-124697234.142v50control,201v3control_2&spm=1018.2226.3001.4187,《心知天气天气状况获取,ESP32获取天气信息》

你可能感兴趣的:(单片机,iot)