ESP8266 Arduino OLED

一、参考

  1. Adafruit的SSD1306驱动https://blog.csdn.net/tiandiren111/article/details/122019747
  2. U8G2参考https://blog.csdn.net/qq_40531588/article/details/89515149

二、步骤

  1. 参考上面链接的大佬,先成功显示

  2. 我们这里选择Adafruit的U8G2库,不需要用到原始的
    在这里插入图片描述

  3. 按照库上面的More info查看使用说明 https://github.com/olikraus/U8g2_for_Adafruit_GFX

  4. 我这里加载了一中文显示,使用带的字库就OK了,但是只能显示小部分汉字

    /*
     * oled_infor
     * OLED显示连网信息
     */
    #include <ESP8266WiFi.h>
    #include <Wire.h>
    #include <Adafruit_GFX.h>
    #include <Adafruit_SSD1306.h>
    #include <U8g2_for_Adafruit_GFX.h>
     
    const char* ssid     = "your-ssid";//连接WIFI名(SSID)
    const char* password = "your-password";//WIFI密码
     
    Adafruit_SSD1306 oled(128, 32, &Wire,-1);
    U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;
     
    void setup()
    {
      oled.begin(SSD1306_SWITCHCAPVCC,0x3C);
      /*
      oled.setTextColor(WHITE);//开像素点发光
      oled.clearDisplay();//清屏
      
      oled.setTextSize(1); //设置字体大小  
      oled.setCursor(15, 5);//设置显示位置
      oled.println("WiFi Information");
      oled.setCursor(2, 20);//设置显示位置 
     
      WiFi.begin(ssid,password);//启动网络连接
     
      while (WiFi.status() != WL_CONNECTED)//检测网络是否连接成功
      {
        delay(500);
        oled.print(".");//设置显示位置
        oled.display(); // 开显示
      }
      
      oled.setTextSize(1);//设置字体大小  
      oled.setCursor(2, 35);//设置显示位置
      oled.println("Connected,IP address:");
      oled.println();
      oled.println(WiFi.localIP());
      oled.display(); // 开显示
    */
    
    
      u8g2_for_adafruit_gfx.begin(oled);
      
    }
     
    void loop() 
    {
    
      oled.clearDisplay();                               // clear the graphcis buffer  
      u8g2_for_adafruit_gfx.setFontMode(1);                 // use u8g2 transparent mode (this is default)
      u8g2_for_adafruit_gfx.setFontDirection(0);            // left to right (this is default)
      u8g2_for_adafruit_gfx.setForegroundColor(WHITE);      // apply Adafruit GFX color
      u8g2_for_adafruit_gfx.setFont(u8g2_font_wqy12_t_chinese2);  // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall  u8g2_font_wqy12_t_chinese2
      u8g2_for_adafruit_gfx.setCursor(0,16);                // start writing at this position
      u8g2_for_adafruit_gfx.print(("这真的好吗"));
      //u8g2_for_adafruit_gfx.setCursor(0,32);                // start writing at this position
      //u8g2_for_adafruit_gfx.print(("I'm FC"));            // UTF-8 string with german umlaut chars
      oled.display();                                    // make everything visible
      delay(2000);
    
      
      }
    

三、库漏洞修改

  1. 对于这个库,可以使用u8g2自带的GBK库 u8g2_font_wqy12_t_gb2312 ,但是使用后,会提示

    out section `.rodata' will not fit in region `dram0_0_seg'
    

    这个编译错误,查看源码后发现,是因为定义数组没有放到 TXT段中导致的…这里直接就丢到Dram了,很不合理

  2. 所以作出下面的修改 对8266做特殊处理 (这个问题是参考u8g2库做的修改,现在用这个for_adafruit实在是旧…BUG都没修复
    C:\Users\Administrator\Documents\Arduino\libraries\U8g2_for_Adafruit_GFX\src\u8g2_fonts.h中增加放TXT段,并且加入读取TXT段的函数(必加,否则会死机复位)

    #if defined(ESP8266)
    uint8_t u8x8_pgm_read_esp(const uint8_t * addr);   /* u8x8_8x8.c */
    #  define U8X8_FONT_SECTION(name) __attribute__((section(".text." name)))
    #  define u8x8_pgm_read(adr) u8x8_pgm_read_esp(adr)
    #endif
    

    C:\Users\Administrator\Documents\Arduino\libraries\U8g2_for_Adafruit_GFX\src\U8g2_for_Adafruit_GFX.cpp中增加

    #if defined(ESP8266)
    uint8_t u8x8_pgm_read_esp(const uint8_t * addr) 
    {
        uint32_t bytes;
        bytes = *(uint32_t*)((uint32_t)addr & ~3);
        return ((uint8_t*)&bytes)[(uint32_t)addr & 3];
    }
    #endif
    
    
    

四、测试结果

在这里插入图片描述

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
ESP8266是一款功能强大的微控制器,可以通过Arduino开发环境进行编程控制。而OLED(Organic Light-Emitting Diode)是一种能够发光的有机LED显示技术。结合这两者,我们可以使用ESP8266连接到互联网,并在OLED显示屏上显示当前的时间。 首先,我们需要连接ESP8266OLED显示屏。通过使用合适的引脚,将ESP8266的GPIO引脚与OLED显示屏的相应引脚连接起来。 然后,我们可以使用Arduino编程语言来编写代码,以实现ESP8266联网和OLED显示时间。首先,我们需要确保ESP8266能够成功连接到Wi-Fi网络。我们可以使用ESP8266的WiFi库来连接到网络,并提供Wi-Fi网络的名称和密码。 之后,我们可以使用NTP(Network Time Protocol)来获取当前的时间。我们可以使用ESP8266的NTPClient库来向NTP服务器发出请求,并得到当前的时间。 接下来,将获取到的时间数据转换为我们所需要的格式,例如年、月、日、小时、分钟和秒。然后,将这些时间数据使用OLED库来显示在OLED显示屏上。 最后,我们可以使用循环结构来实现定时更新时间的功能。通过在程序中添加适当的延时,可以让OLED显示屏上的时间持续更新,并保持正确。 通过上述步骤,我们可以使用ESP8266Arduino来实现联网的OLED显示屏,用于显示当前的时间。这样,我们就可以通过一个简单的硬件和软件组合,实现这一功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值