五、增加OLED显示功能
前面已经完成了串口打印天气的功能,还是不够NB方便,NodeMCU的固件支持各种OLED的库,开发非常方便。
不过在网上搜寻了一波,发现用LUA开发的例程还是比较少的。
第一步首先刷入带u8g和IIC的固件,在https://nodemcu-build.com/上定制自己需要的固件,我选择的是旧版的固件,新版的都是u8g2,资料太少不会用
刷完固件后连接开发板,串口可以看到mudules模块信息:
然后就可以编写程序测试OLED好不好用了,代码如下
- -- OLED Display demo
- -- March, 2016
- -- @kayakpete | pete@hoffswell.com
- -- Hardware:
- -- ESP-12E Devkit
- -- 4 pin I2C OLED 128x64 Display Module
- -- Connections:
- -- ESP -- OLED
- -- 3v3 -- VCC
- -- GND -- GND
- -- D1 -- SDA
- -- D2 -- SCL
- -- Variables
- sda = 1 -- SDA Pin
- scl = 2 -- SCL Pin
- function init_OLED(sda,scl) --Set up the u8glib lib
- sla = 0x3c
- i2c.setup(0, sda, scl, i2c.SLOW)
- disp = u8g.ssd1306_128x64_i2c(sla)
- disp:setFont(u8g.font_6x10)
- -- disp:setFontRefHeightExtendedText()
- -- disp:setDefaultForegroundColor()
- disp:setFontPosTop()
- --disp:setRot180() -- Rotate Display if needed
- end
- function print_OLED()
- disp:firstPage()
- repeat
- disp:setColorIndex(1);
- disp:drawBox(0, 0,128,20)
- -- disp:setDefaultForegroundColor()
- disp:setColorIndex(0);
- disp:setFont(u8g.font_6x10)
- disp:drawStr(20, 0, "Weather Station")
- disp:drawStr(0, 10, "Powered By YunJin")
-
- disp:setDefaultForegroundColor()
- disp:drawStr(0, 30, str1)
- disp:drawStr(0, 40, str2)
- until disp:nextPage() == false
-
- end
- -- Main Program
- str1="Hello Yunjin!!"
- str2=" @zhongli"
- init_OLED(sda,scl)
- print_OLED()
|