Lua programming tutorial, use NodeMCU to display temperature and humidity on OLED
“OLED Display refers to Organic Light-Emitting Diode (OLED), which is self-luminous, so there is no need for a backlight, high contrast, thin thickness, wide viewing angle, fast response and other characteristics. It is considered to be the next generation The emerging application technology of flat panel displays.
OLED module introduction
OLED display refers to Organic Light-Emitting Diode (OLED), which is self-luminous, so there is no need for a backlight, high contrast, thin thickness, wide viewing angle, fast response and other characteristics. It is considered to be the next generation The emerging application technology of flat panel displays.
The author uses a 0.96 inch, white, I2C bus OLED module display with a resolution of 128×64. The common OLED interfaces include I2C and SPI. The adapter board is drawn by itself, and the interface of the module purchased on a certain treasure is basically the same. The module has a 5V to 3.3V circuit, which is compatible with external 3.3V and 5V voltages. I won’t say much about the hardware part here. After purchasing the oled module, the customer service will provide relevant information about the module.
Note: When purchasing an OLED module, check whether the module is supported by the u8g2 library. Only OELD screens supported by the u8g2 library can use the library.
Enable I2C and OLED modules
In the previous article “NodeMCU Firmware Compilation for ESP8266”, we have actually introduced how to enable the various modules supported by NodeMCU. After enabling the corresponding modules, you need to recompile the firmware and burn it to ESP8266 to use it, generally according to your needs. Turn on the corresponding module to reduce the size of the firmware.
1) To enable the I2C module, you need to open user_modules. LUA_USE_MODULES_I2C in the h file, using the firmware compiled by the author before, is already turned on by default.
The ESP8266 chip does not have a hardware I2C interface, so the module uses a software interface driver. It can be set on any GPIO pin including GPIO16. The module supports master mode, and each bus can have different speeds, up to 10 buses.
Slow speed: 100kHz;
Any clock can also be set. The GPIO16 pin can be used as an SCL pin, but the selected bus will be limited to not exceed FAST speed.
The module does not support high-speed mode (3.5MHz clock) and 10-bit addressing scheme.
2) To enable the OLED module, you need to open user_modules. The LUA_USE_MODULES_U8G2 and u8g2 modules in the h file have written common OLED screen drivers. The source code is open source and can be viewed on GitHub. The modules are as follows:
#Define LUA＿USE＿MODULES＿I2C #define LUA＿USE＿MODULES＿U8G2I2C related interface
Since the u8g2 library is used, only i2c is needed here. setup() interface.
i2c. setup (id, pinSDA, pinSCL, speed)
id: 0-9, bus number, NodeMCU supports 10 I2C buses.
pinSDA: 1～12, IO port number of SDA
pinSCL: 1～12, SCL IO port number
speed: i2c. SLOW (100kHz), i2c. FAST (400kHz), i2c. FASTPLUS (1MHz) or any clock frequency between 25000～1000000Hz. The FASTPLUS mode generates an I2C clock speed of 600kHz under the default CPU frequency of 80MHz. To obtain an I2C clock speed of 1MHz, the interface node is used. setcpufreq (node. CPU160MHZ) Change the CPU frequency to 160MHz.
speed: returns the set speed if it succeeds; returns 0 if it fails.
If you want to know about other interfaces, please refer to the official documentation of NodeMCU.
U8G2 library related interface SSD1306 driver initialization
Using the I2C interface, call the following code to initialize:
sla = 0x3c-I2C address of oled-id: id when I2C is initialized disp = u8g2. ssd1306_i2c_128×64_noname(id, sla)
For OLED initialization of other drive types, see the official documentation of NodeMCU.
u8g2. disp: drawStr(x, y, *str)
x: x-axis coordinate
y: y-axis coordinate
str: the string to be displayed
u8g2. disp: sendBuffer(void)
Send the content of the memory frame buffer to the display, execute this function, OLED will display the set content.
OLED font settings
The font is set in u8g2_fonts under the app/include directory. h file, what font is needed, just add the corresponding font macro after U8G2_FONT_TABLE_ENTRY (font_wqy16_t_chinese3). Because the Chinese font library is very large, the size of the compiled firmware will be very large, which may cause burning failure or ESP8266 not booting . I just used the default font directly. The font design of u8g2 is another technology. There are also tutorials on the Internet, and those who are interested can search for it.
／／ Add a U8G2＿FONT＿TABLE＿ENTRY for each font you want to compile into the image ／／See https：／／github． com/olikraus/u8g2/wiki/fntlistall for a complete list of // available fonts. Drop the ＇u8g2＿＇ prefix when you add them here. #Define U8G2＿FONT＿TABLE U8G2＿FONT＿TABLE＿ENTRY（font＿6×10＿tf) U8G2＿FONT＿TABLE＿ENTRY（font＿unifont＿t＿symbols）U8G2＿tABLE＿symbols) U8G2＿symbols（Chinese）
See u8g2 official website for fonts supported by u8g2.
Light up the OLED module
First of all, we must figure out the relationship between ESP8266 and NodeMCU pins, which is easy for many novices to confuse. The corresponding pin relationship between ESP8266 and NodeMCU is as follows:
Correspondence, such as the following table:
NodeMCU PinESP8266 PinD0GPIO16D1GPIO5D2GPIO4D3GPIO0D4GPIO2D5GPIO14……
The author’s OLED module has 4 pins (some IIC and SPI compatible modules may have 7 pins), and the connection relationship between the OLED module and NodeMCU is as follows:
OLED module NodeMCU PinESP8266VCC (with 3.3V regulator) VCC (3.3V or 5V) VCCGNDGNDGNDSCLD6GPIO12SDAD5GPIO14
The VCC of ESP8266 single module is 3.3V.
If the OLED module has RES, DC, and CS pins, you can control them in the program according to the instructions, or configure them directly on the hardware, see the oled module information for details.
If the OLED module does not have a 3.3V voltage regulator, the VCC of the OLED is connected to the 3.3V pin of the NodeMCU, because most of the 0.96 inch OLED screens are 3.3V power supplies, and the specific specifications of the OLED screen shall prevail.
Connection relationship between DHT11 module and NodeMCU:
DHT11 PinNodeMCU PinESP8266VCC3．3VVCCGNDGNDGNDDATAD4GPIO2
First connect the OLED and NodeMCU development board, then connect the usb to the computer, and upload the lua code that lights up the oled.
code show as below:
-Pin definition local sda = 5-GPIO14 local scl = 6-GPIO12 local sla = 0x3c-oled address, generally 0x3c-initialization function init_oled()-iic bus and oled initialization i2c. setup (0, sda, scl, i2c. SLOW) disp = u8g2. ssd1306_i2c_128×64_noname(0, sla)-Set the font disp: setFont (u8g2. font_unifont_t_symbols) disp: setFontRefHeightExtendedText()-disp: setDrawColor(Font Direct)-disp: Set Topion (1)-disp: Set Topion (1)-disp: －－Disp: drawFrame(0, 0, 128, 64) end －－ Display function function oled_show_msg() －－ Set the display content disp: drawStr(0, 0, “1 Hello OLED”) disp: drawStr(0, 16, “2234567890ABCDEF”) disp: drawStr(0, 32, “3234567890ABCDEF”) disp: drawStr(0, 48, “4234567890ABCDEF”)-Send content to oled disp: sendBuffer() end-main function function main() init_oled () oled_show_msg() end-run the program main()
Save the code to oled. Lua file, upload to ESP8266 module through ESPlorer tool, refresh ESPlorer file list, click oled. Lua can see the information displayed on the oled screen. As shown below:
Actual display effect:
Dynamically display temperature and humidity on OLED
The temperature and humidity module uses the DHT11 module. About the use of the DHT11 module, I wrote an article before.Just use it here