ESP8266模块

前篇,ESP8266 相关固件的编译文章:
ESP-12F WIFI模块开发(NonOS, RTOS, NodeMCU固件编译)


NodeMCU连接DHT11模块

参考上面链接,,掌握ModeMCU固件的编译和烧录。使用最新版NodeMCU V3.0,自带DHT库,配置固件,开启DHT模块。

目录:nodemcu-firmware/app/include
修改文件:user_modules.h,该文件是管理各种模块的。
找到DHT宏定义,取消注释。

#define LUA_USE_MODULES_DHT

文件 user_config.h 涉及 Flash 大小,波特率,固件浮点或整形,SSL启动等。

配置文件修改完成后,进入nodemcu根目录,,输入make开始编译。
编译成功后,会在bin目录下有:0x00000.bin, 0x10000.bin 两个文件。
使用esp烧录工具,将固件烧录到esp8266模块。

使用ESPlorer工具连接ESP8266串口,开机如下:


DHT11测试代码:


-- 功能: 读取DHT11数据
function mydht11()
	pin = 4  -- GPIO2 与 DHT11的 DATA脚相连
	status, temp, humi, temp_dec, humi_dec = dht.read11(pin)
	if status == dht.OK then
		-- 数据获取, 针对整形固件
		print(string.format("DHT Temperature:%d.%03d;Humidity:%d.%03d
",
			  math.floor(temp),
			  temp_dec,
			  math.floor(humi),
			  humi_dec
		))

		-- 数据获取, 针对浮点固件
		print("DHT Temperature:"..temp..";".."Humidity:"..humi)

	elseif status == dht.ERROR_CHECKSUM then
		print( "DHT Checksum error." )
	elseif status == dht.ERROR_TIMEOUT then
		print( "DHT timed out." )
	end
end

ESPlorer工具左侧输入代码,发送到esp8266模块,在右侧下面命令行输入mydht11(),就可获取温湿度数据。

注: DHT11模块精度低,没有小数位,所以小数一直为0。


网页显示DHT11信息:

-- 设置连接 wifi
print("Connecting WIFI...")
wifi.setmode(wifi.STATION)
station_cfg={}
station_cfg.ssid = "SSID"      -- SSID 替换成自己的 WiFi名称
station_cfg.pwd  = "Password"  -- Password 替换成自己的 WiFi密码
station_cfg.save = false
wifi.sta.config(station_cfg)
wifi.sta.connect()

mytimer = tmr.create()
mytimer:alarm(1000, tmr.ALARM_AUTO, function() 
    if wifi.sta.getip() == nil then 
        print("IP unavaiable, Waiting...") 
    else
        mytimer:stop()
        print("Config done, IP is "..wifi.sta.getip())  -- ESPlorer中显示ESP8266的IP地址
    end 
end)


pin = 4 --GPIO2 设置和DHT11模块通信的引脚

-- 创建 HTTP服务器
srv = net.createServer(net.TCP)
srv:listen(80, function(conn)
    conn:on("receive", function(sck, payload)
        print(payload)
        local status, temp, humi, temp_dec, humi_dec = dht.read11(pin)   -- 获取温湿度
        local buf = "";
        buf = buf.."<meta http-equiv="refresh" content="2">";     -- html页面2秒刷新一次 
        buf = buf.."<meta charset="gbk">";
        buf = buf.."<h2>DHT11</h2>";  
        buf = buf.."<p>当前温度: "..temp.."."..temp_dec.."'C</p>";  
        buf = buf.."<p>当前湿度: "..humi.."."..humi_dec.."%</p>"; 

        sck:send(buf)
        
    end)
    conn:on("sent", function(sck) sck:close() end)
end)

在浏览器中输入ESP8266的IP地址,即可访问,网页端每隔3秒,刷新一次,如下图:


OLED模块

修改文件:user_modules.h,开启IIC, SPI, u8g2模块,

#define LUA_USE_MODULES_I2C
#define LUA_USE_MODULES_SPI
#define LUA_USE_MODULES_U8G2

OLED代码

OLED参数:
尺寸:0.96寸
驱动:SSD1306
颜色:单色(白)

支持IIC和SPI通信

u8g2库支持的oled模块还是比较多的,具体看u8g2官方网站。

-- OLED 初始化
function init_oled()
        -- 设置IIC通信的引脚
	local sda = 5 -- GPIO14
	local scl = 6 -- GPIO12
	local sla = 0x3c
	
	i2c.setup(0, sda, scl, i2c.SLOW)
	disp = u8g2.ssd1306_i2c_128x64_noname(0, sla)
        
        -- 设置字体,默认固件支持2种字体。
	disp:setFont(u8g2.font_6x10_tf)
	disp:setFontRefHeightExtendedText()
	disp:setDrawColor(1)
	disp:setFontPosTop()
	disp:setFontDirection(0)

	disp:drawFrame(0, 0, 128, 64)
end

-- 功能: oled显示温度和湿度
function oled_show_msg(temp, hum)
	if temp ~= nil then
		disp:drawStr(5, 5, "Temp: "..temp)
		disp:drawStr(5, 20, "Hum : "..hum)
		disp:sendBuffer()
	end
end

首先调用oled初始化函数(只需执行一次),然后根据自己需求调用oled显示函数。

默认 u8g2 模块只开启了2种字体,其他字体启用在: u8g2_fonts.h 文件里。
注: 字体比较占用空间,根据自己的flash大小,设置启用字体。

// 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_6x10_tf) 
  U8G2_FONT_TABLE_ENTRY(font_unifont_t_symbols) 

具体效果图:

== end ==

原文地址:https://www.cnblogs.com/wybliw/p/13578836.html