ESP8266模塊 - NodeMCU固件連接DHT11和OLED模塊(原創)


前篇,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\r\n",
			  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 ==


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM