三 入坑esp-01s 1.3寸OLED时钟及天气显示

先上图:
【三入坑esp-01s 1.3寸OLED时钟及天气显示】

三  入坑esp-01s 1.3寸OLED时钟及天气显示

文章插图
基本模样已经完成 。左右双屏各自显示的内容也已完成 。左边只显示时间,右边只显示天气 。
网上的方案是单屏循环显示时间和天气,屏幕小,我总觉得不过瘾,所以改成双屏,也变相地加大了屏幕面积,难度没怎么增加 。
三  入坑esp-01s 1.3寸OLED时钟及天气显示

文章插图
原来只显示英文,这也是的缺点,内存太小,只有1M,显示完全中文很难实现 。但我改成了显示星期,好看点,内存里面只增加“星期一二三四五六日”这9个汉字,占用内存小 。下一步打算反农历也加上去,研究了一下,C++计算农历还挺麻烦,不如,一个库就搞定了 。慢慢 研究,应该可以做完 。下面是左右屏的代码 。我是在别的代码上改的,肯定有些没用的代码,暂时还没精减,而且我的C++也是刚接触,很多代码我都不敢动,动一下可能就出问题 。大神不要笑!再次感谢开源的大神!
还有件事没说,打算买个3D打印机打印外壳,做完可能就更好看点,又要学习3D画图了 。我还有几个类似的东西,也一齐做出外壳,弄个全家福啥的 。
#include #include #include #include #include #include #include "SH1106Wire.h"//1.3寸//#include "SSD1306Wire.h"//0.96寸const char* WIFI_SSID = "******";//WIFI名称及密码const char* WIFI_PWD = "12345678";const String WDAY_NAMES[] = {"Sum", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};//星期#define TZ8// 时区为8#define DST_MN0// 默认为0#define TZ_MN((TZ)*60)#define TZ_SEC((TZ)*3600)#define DST_SEC((DST_MN)*60)time_t now; long timeSinceLastWUpdate = 0;//上次更新后的时间const int UPDATE_INTERVAL_SECS = 5 * 60; // 5分钟更新一次const int I2C_DISPLAY_ADDRESS = 0x3c;//I2c地址默认#if defined(ESP8266)const int SDA_PIN = 2;//引脚,如果显示屏不亮,就是引脚接反了 。const int SDC_PIN = 0;#endifSH1106Wiredisplay(I2C_DISPLAY_ADDRESS, SDA_PIN, SDC_PIN);//1.3寸//SSD1306Wiredisplay(I2C_DISPLAY_ADDRESS, SDA_PIN, SDC_PIN);// 0.96寸void drawDateTime();//函数声明void wificonnect() {//WIFI连接WiFi.begin(WIFI_SSID, WIFI_PWD);while (WiFi.status() != WL_CONNECTED) {Serial.print('.');delay(80);display.clear();display.setFont(ArialMT_Plain_24);display.setTextAlignment(TEXT_ALIGN_LEFT);display.drawString(10, 18, "wait");display.display();delay(80);display.clear();display.setFont(ArialMT_Plain_24);display.setTextAlignment(TEXT_ALIGN_LEFT);display.drawString(10, 18, "wait.");display.display();delay(80);display.clear();display.setFont(ArialMT_Plain_24);display.setTextAlignment(TEXT_ALIGN_LEFT);display.drawString(10, 18, "wait..");display.display();delay(80);display.clear();display.setFont(ArialMT_Plain_24);display.setTextAlignment(TEXT_ALIGN_LEFT);display.drawString(10, 18, "wait...");display.display();delay(80);display.clear();display.setFont(ArialMT_Plain_24);display.setTextAlignment(TEXT_ALIGN_LEFT);display.drawString(10, 18, "wait....");display.display();delay(80);display.clear();display.setFont(ArialMT_Plain_24);display.setTextAlignment(TEXT_ALIGN_LEFT);display.drawString(10, 18, "wait.....");display.display();delay(80);display.clear();display.setFont(ArialMT_Plain_24);display.setTextAlignment(TEXT_ALIGN_LEFT);display.drawString(10, 18, "wait......");display.display();delay(80);}Serial.println("");delay(500);}//汉字字模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 qi[] U8X8_PROGMEM = {0x44,0x00,0x44,0x3E,0xFE,0x22,0x44,0x22,0x44,0x22,0x7C,0x3E,0x44,0x22,0x44,0x22,0x7C,0x22,0x44,0x3E,0x44,0x22,0xFF,0x22,0x20,0x21,0x44,0x21,0x82,0x28,0x41,0x10};/*期*/const unsigned char yue[] U8X8_PROGMEM = {0x00,0x00,0xF8,0x1F,0x08,0x10,0x08,0x10,0x08,0x10,0xF8,0x1F,0x08,0x10,0x08,0x10,0x08,0x10,0xF8,0x1F,0x08,0x10,0x08,0x10,0x04,0x10,0x04,0x10,0x02,0x14,0x01,0x08/*"月"*/};const unsigned char ri[] U8X8_PROGMEM = {0x00,0x00,0xF8,0x0F,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0xF8,0x0F,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0xF8,0x0F,0x08,0x08};/*"日"*/const unsigned char yi[] U8X8_PROGMEM = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};/*"一"*/const unsigned char er[] U8X8_PROGMEM = {0x00,0x00,0x00,0x00,0x00,0x00,0xFC,0x1F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x7F,0x00,0x00,0x00,0x00,0x00,0x00};/*"二",0*/const unsigned char san[] U8X8_PROGMEM = {0x00,0x00,0x00,0x00,0xFE,0x3F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFC,0x1F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x7F,0x00,0x00,0x00,0x00};/*"三",1*/const unsigned char si[] U8X8_PROGMEM = {0x00,0x00,0x00,0x00,0xFE,0x3F,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x12,0x22,0x12,0x3C,0x0A,0x20,0x06,0x20,0x02,0x20,0xFE,0x3F,0x02,0x20,0x00,0x00};/*"四"*/const unsigned char wu[] U8X8_PROGMEM = {0x00,0x00,0xFE,0x3F,0x40,0x00,0x40,0x00,0x40,0x00,0x40,0x00,0xFC,0x0F,0x20,0x08,0x20,0x08,0x20,0x08,0x20,0x08,0x10,0x08,0x10,0x08,0x10,0x08,0xFF,0x7F,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};/*"六"*/void setup() {Serial.begin(115200);Serial.println(); //println换行输出Serial.println();// 屏幕初始化display.init();display.clear();display.display();display.flipScreenVertically(); //屏幕翻转display.setContrast(255); //屏幕亮度// 用固定密码连接wificonnect();display.flipScreenVertically(); //屏幕反转configTime(TZ_SEC, DST_SEC, "ntp.ntsc.ac.cn", "ntp1.aliyun.com"); //ntp获取时间delay(200);}void drawDateTime() {//显示时间int16_t x;int16_t y;now = time(nullptr);struct tm* timeInfo;timeInfo = localtime(&now);char buff[16];display.setTextAlignment(TEXT_ALIGN_CENTER);display.setFont(ArialMT_Plain_16);String date = WDAY_NAMES[timeInfo->tm_wday];sprintf_P(buff, PSTR("d/d"), timeInfo->tm_mon + 1, timeInfo->tm_mday);display.drawString(60 + x, 5 + y, String(buff));display.setFont(ArialMT_Plain_24);sprintf_P(buff, PSTR("d:d:d"), timeInfo->tm_hour, timeInfo->tm_min, timeInfo->tm_sec);display.drawString(64 + x, 22 + y, String(buff));display.setTextAlignment(TEXT_ALIGN_LEFT);display.drawXbm(62, 5, 16,16, xing);display.drawXbm(79, 5, 16,16, qi);if (WDAY_NAMES[timeInfo->tm_wday]=="Mon")display.drawXbm(96, 5, 16,16, yi);else if (WDAY_NAMES[timeInfo->tm_wday]=="Tue")display.drawXbm(96, 5, 16,16, er);else if (WDAY_NAMES[timeInfo->tm_wday]=="Wed")display.drawXbm(96, 5, 16,16, san);else if (WDAY_NAMES[timeInfo->tm_wday]=="Thu")display.drawXbm(96, 5, 16,16, si);else if (WDAY_NAMES[timeInfo->tm_wday]=="Fri")display.drawXbm(96, 5, 16,16, wu);else if (WDAY_NAMES[timeInfo->tm_wday]=="Sat")display.drawXbm(96, 5, 16,16, liu);else if (WDAY_NAMES[timeInfo->tm_wday]=="Sun")display.drawXbm(96, 5, 16,16, ri);}void loop() {display.clear();drawDateTime();display.display();if (millis() - timeSinceLastWUpdate > (1000L * UPDATE_INTERVAL_SECS)) { //屏幕刷新timeSinceLastWUpdate = millis();}}