基于ESP8266的NODEMCU气象站OLED显示,程序用LUA写成。话不多说上源码,有什么不懂的地方可以给我留言。或者关注我的公众号,会不定时推送一些东西。
OUTtemp=""
city=""
wea=""
lasttime =""
IO_BLINK = 4
TMR_WIFI = 4
TMR_BLINK = 5
gpio.mode(IO_BLINK, gpio.OUTPUT)
---------------------
-- blink
---------------------
blink = nil
print('Setting up WIFI...')
wifi.setmode(wifi.STATIONAP )
file.open("zhanghu.txt", "r")
mingzi=file.readline(1)
mingzi=string.sub(mingzi, 1, -2)
mima=file.readline(2)
mima=string.sub(mima, 1, -2)
file.close()
print(mingzi)
print(mima)
station_cfg={}
station_cfg.ssid=mingzi
station_cfg.pwd=mima
wifi.sta.config(station_cfg)
wifi.sta.autoconnect(1)
status=nil
-- http
---------------------
dofile('httpServer.lua')
httpServer:listen(80)
httpServer:use('/config', function(req, res)
if req.query.ssid ~= nil and req.query.pwd ~= nil then
station_cfg={}
station_cfg.ssid=req.query.ssid
station_cfg.pwd=req.query.pwd
wifi.sta.config(station_cfg)
file.open("zhanghu.txt", "w+")
file.writeline(req.query.ssid)
file.writeline(req.query.pwd)
file.close()
-- wifi.sta.config(req.query.ssid, req.query.pwd)
status = 'STA_CONNECTING'
status_sleep=0
tmr.alarm(TMR_WIFI, 1000, tmr.ALARM_AUTO, function()
status_sleep=status_sleep+1
if(status_sleep==10) then
res:type('application/json')
res:send('{"status":"timeout"}')
tmr.stop(TMR_WIFI)
end
if status ~= 'STA_CONNECTING' then
res:type('application/json')
res:send('{"status":"' .. status .. '"}')
tmr.stop(TMR_WIFI)
end
end)
end
end)
-- Get json
httpServer:use('/scanap', function(req, res)
wifi.sta.getap(function(table)
local aptable = {}
for ssid,v in pairs(table) do
local authmode, rssi, bssid, channel = string.match(v, "([^,]+),([^,]+),([^,]+),([^,]+)")
aptable[ssid] = {
authmode = authmode,
rssi = rssi,
bssid = bssid,
channel = channel
}
end
res:type('application/json')
res:send(sjson.encode(aptable))
end)
end)
---------get tianqiwendu
tmr.alarm(3, 15000, tmr.ALARM_AUTO,function() --每15秒获取天气信息conn=net.createConnection(net.TCP, 0)
conn:connect(80,"api.thinkpage.cn")
conn:on("receive", function(conn, payload)
--print("weather get"..payload)
--city=string.sub(payload,string.find(payload,"name")+7,string.find(payload,"country")-4)
wea=string.sub(payload,string.find(payload,"text")+7,string.find(payload,"code")-4)
OUTtemp=string.sub(payload,string.find(payload,"temperature")+14,string.find(payload,"last_update")-5)
lasttime=string.sub(payload,string.find(payload,"GMT")-9,string.find(payload,"GMT")-2)
--print("CITY:"..city.."\r\n")
print("WEATHER:"..wea)
print("TEMP:"..OUTtemp)
--print("TIME:"..lasttime.."\r\n")
hour=string.sub(lasttime,1,2)+8
minints=string.sub(lasttime,4,5)
nowtime=hour..":"..minints---..":"..seconds
print(nowtime)
end )
conn:on("connection", function(conn, payload)
conn:send("GET /v3/weather/now.json?key=lkvdhpaemyl8vgme&location=xian&language=en".." HTTP/1.1\r\n".."Host: ".."api.thinkpage.cn".. "\r\n".."Connection: close\r\n\r\n")
end
)
end)
-----------------get pm2.5
tmr.alarm(5, 30000, tmr.ALARM_AUTO,function()
conn1=net.createConnection(net.TCP, 0)
conn1:connect(80,"www.pm25.in")
conn1:on("receive", function(conn, payload1)
--print("pm25get"..payload1)
ii=string.find(payload1,"pm2")
if ii~=nil then
--print("pm weishu is:"..ii)
pm25=string.sub(payload1,ii+7,ii+9)
print("PM25:"..pm25.."\r\n")
end
end )
conn1:on("connection", function(conn, payload1)
conn1:send("GET /api/querys/aqi_details.json?city=xian&token=5j1znBVAsnSf5xQyNQyq&stations=no".." HTTP/1.1\r\n".."Host: ".."www.pm25.in".. "\r\n".."Connection: close\r\n\r\n")
end
)
end)
------------------------
tmr.alarm(2, 1000, tmr.ALARM_AUTO,
function()
if wifi.sta.getip()~= nil then
dofile("script2.lua")
tmr.stop(2)
else
print("wait connect..")
end
end)
这是init文件的内容。其中用到两个LUA文件,一个是dofile('httpServer.lua')。一个是dofile("script2.lua"),第二个文件是OLED显示用的,用来驱动0.96的oled显示器采用SPI驱动,接线可以看我其他有相关内容,pin6驱动DHT11采集温湿度信息。显示数据。下面是oled驱动文件源代码:script2.lua
cs = 8
dc = 2 -- D2
res = 0 -- D0
pin=6
--require"init"
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
gpio.mode(8, gpio.INPUT, gpio.PULLUP)
disp = u8g.ssd1306_128x64_hw_spi(cs, dc, res)
disp:setFont(u8g.font_6x10)
str = "AIRSTAION"
function task()
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
))
temp1=math.floor(temp)
humi1=math.floor(humi)
elseif status == dht.ERROR_CHECKSUM then
print("DHT Checksum error.")
elseif status ==dht.ERROR_TIMEOUT then
-- print("DHT timed out.")
end
end
function draw()
task()
if wifi.sta.getip()~= nil then
disp:drawStr(0, 60, "Net OK")
else
disp:drawStr(0, 60, "NO Net")
end
disp:drawStr(0, 15, str)
disp:drawStr(0, 30,"OutAir:")
if wea~=nil and OUTtemp~=nil then
disp:drawStr(43, 30,wea)
disp:drawStr(96, 30,OUTtemp.." C")
end
if nowtime~=nil then
disp:drawStr(80, 60,nowtime)
end
disp:drawStr(0,40,"InAir:")
disp:drawStr(43, 40,temp1.." C")
disp:drawStr(80, 40,humi1.." %")
disp:drawStr(0, 50, "PM2.5:")
if pm25~=nil then
disp:drawStr(45, 50, pm25)
end
end
tmr.alarm(6, 3000, tmr.ALARM_AUTO, function()
disp:firstPage()
while (disp:nextPage())
do
draw()
end
end)
有不懂可以问我,或者关注我的个人公众号“老沈物联”,一起交流
后附RAR文件下载。