八,ESP8266 文件保存數據(基于Lua腳本語言)

應該是LUA介紹8266的最后一篇,,,,,,下回是直接用SDK,,然后再列個12345.......不過要等一兩個星期,先忙完朋友的事情

前面幾篇

用AT指令版本的

一,http://www.cnblogs.com/yangfengwu/p/5205570.html發AT指令,,其實AT指令就是事先用SDK內部寫好了

二,http://www.cnblogs.com/yangfengwu/p/5295632.html這個呢是配合路由器實現遠程,,其實可以買個域名,或者用云端

三,http://www.cnblogs.com/yangfengwu/p/5220919.html這是做了一個用手機控制的WIFI小車

四,http://www.cnblogs.com/yangfengwu/p/5212570.htmlAndroid TCP客戶端

五,http://www.cnblogs.com/yangfengwu/p/5294921.htmlAndroid TCP服務器

用LUA語言的,更靈活,功能更強

http://www.cnblogs.com/yangfengwu/p/7514336.html下載自己選擇功能的固件,刷固件,寫LUA的軟件

http://www.cnblogs.com/yangfengwu/p/7520260.htmlGPIO,串口,注意事項,空閑中斷

http://www.cnblogs.com/yangfengwu/p/7524297.htmlSPI通信介紹,,STM32 SPI 從機

http://www.cnblogs.com/yangfengwu/p/7524326.htmlTCP服務器,單個連接

http://www.cnblogs.com/yangfengwu/p/7531730.htmlTCP服務器,多個客戶端連接,CRC校驗

http://www.cnblogs.com/yangfengwu/p/7533302.htmlTCP Client 斷開自動重連,AD采集

http://www.cnblogs.com/yangfengwu/p/7533845.htmlUDP支持一路默認,3路動態連接,ssid 與 pwd 的讀取

還缺一樣....這篇...保存數據...這樣的話就可以隨意修改啟動時工作在哪一種模式,哪一種通信,以及其余需要保存在模塊內部的信息

就實現上位機軟件的第一個功能,,修改啟動模式

自己的上位機按鈕事件

privatevoidbutton2_Click(objectsender, EventArgs e)

{byte[] sendbyte =newbyte[6];

sendbyte[0] = (byte)'+';//2Bsendbyte[1] = (byte)'+';

sendbyte[2] = (byte)'M';//4Dsendbyte[3] = (byte)'D';//44sendbyte[4] = (byte)'0';//0if(radioButton5.Checked)

{

sendbyte[5] = (byte)'0';

}elseif(radioButton6.Checked)

{

sendbyte[5] = (byte)'1';

}elseif(radioButton7.Checked)

{

sendbyte[5] = (byte)'2';

}if(radioButtonNetCon.Checked)

{

TcpSendDataMethod(sendbyte);

}elseif(radioButtonSerialCon.Checked)

{

SerialSend(sendbyte);

}

}

++MD00? ? ? ? ? ? ? ? ?AP模式

++MD01? ? ? ? ? ? ? ? Station模式

++MD00? ? ? ? ? ? ? ? AP+Station模式

其實最后還有CRC16校驗碼

看文件操作

iffile.open("mode.lua","w+") then

file.writeline(string.sub(data,6,6))--MODE

print("MODE="..string.sub(data,6,6))

file.close()elseprint("open mode.lua faild")

end

注意

iffile.open("mode.lua","w+") then

file.writeline("123")

file.flush()//連續寫最好加上file.writeline("456")

file.flush()

file.writeline("789")

file.close()elseprint("open mode.lua faild")

end

像是這樣??mode.lua文件里面會是這樣

那么讀的時候

file.open("mode.lua","r") then

s1=file.readline()

s2=file.readline()

s3=file.readline()

file.close()

end

s1 ="123\r"

s2 ="456\r"

s3 ="789\r"

所以寫進去一行3個但是讀出來會多加一個換行符

iffile.open("mode.lua","r") then

local Mode=file.readline()ifMode ==nil then

Mode="2"endifMode:byte(1) ==48then

print("Wifi MODE: SOFTAP")

wifi.setmode(wifi.SOFTAP)

elseif? Mode:byte(1) ==49then

print("Wifi MODE: STATION")

wifi.setmode(wifi.STATION)elseprint("Wifi MODE: STATIONAP")

wifi.setmode(wifi.STATIONAP)

end

file.close()

end

直接上源碼吧

init.lua

gpio.mode(4,gpio.OUTPUT)

gpio.mode(2,gpio.OUTPUT)

gpio.write(4,1)ifadc.force_init_mode(adc.INIT_ADC) then

node.restart()returnend

tmr.alarm(0,1000,1, function()

gpio.write(4,1-gpio.read(4))

end)

tmr.alarm(1,5000,0, function()

dofile("file.lua")

dofile("UDP.lua")

end)

UDP.lua

iffile.open("mode.lua","r") then

local Mode=file.readline()ifMode ==nil then

Mode="2"endifMode:byte(1) ==48then

print("Wifi MODE: SOFTAP")

wifi.setmode(wifi.SOFTAP)

elseif? Mode:byte(1) ==49then

print("Wifi MODE: STATION")

wifi.setmode(wifi.STATION)elseprint("Wifi MODE: STATIONAP")

wifi.setmode(wifi.STATIONAP)

end

file.close()

end

cfg={}

cfg= wifi.ap.getconfig(true)ifcfg.ssid ==nil then

cfg.ssid="Hellow8266"cfg.pwd="11223344"end

print("APssid:"..cfg.ssid)ifcfg.pwd ==nil then

print("APpwd: nil")elseprint("APpwd:"..cfg.pwd)

end

cfg.save=truewifi.ap.config(cfg)

apcfg={}

apcfg= wifi.sta.getconfig(true)ifapcfg.ssid ==nil then

apcfg.ssid="qqqqq"apcfg.pwd="11223344"end

print("APssid:"..apcfg.ssid)ifapcfg.pwd ==nil then

print("Stationpwd: nil")elseprint("Stationpwd:"..apcfg.pwd)

end

apcfg.save=truewifi.sta.config(apcfg)

wifi.sta.autoconnect(1)

ConnectIP="192.168.1.103"ConnectPort=8080UdpSocket=net.createUDPSocket()

UdpSocket:listen(ConnectPort)

UdpSocketTable={}

UdpIPTable={}

UdpPortTable={}

UdpConnectCnt=0UdpCanConnect=0UdpSocket:on("receive", function(socket, data, port, ip)

UdpCanConnect=1fori=0,2doifUdpIPTable[i] == ip and UdpPortTable[i] ==port? then

UdpCanConnect=0end

endifip == ConnectIP and port ==ConnectPort? then

UdpCanConnect=0endifUdpCanConnect ==1then

UdpSocketTable[UdpConnectCnt]=socket

UdpIPTable[UdpConnectCnt]=ip

UdpPortTable[UdpConnectCnt]=port

print("\r\n"..UdpConnectCnt.."-Connect")

UdpConnectCnt= UdpConnectCnt +1endifUdpConnectCnt ==3then

UdpConnectCnt=0end

uart.write(0,data)

end)

UartReadCnt=0UartReadCntCopy=0UartReadData=""UartReadDataCopy=""uart.on("data",0,function(data)

UartReadCnt= UartReadCnt +1UartReadData=UartReadData..data

end,0)

tmr.alarm(2,5,1, function()ifUartReadCnt ~=0thenifUartReadCnt ==UartReadCntCopy? then

UartReadCnt=0UartReadCntCopy=0UartReadDataCopy=UartReadData

UartReadData=""Config(UartReadDataCopy)

NetSend(UartReadDataCopy)elseUartReadCntCopy=UartReadCnt

end

end

end)

function NetSend(data)ifUdpSocket ~=nil then

UdpSocket:send(ConnectPort,ConnectIP,data)

endfori=0,2doifUdpSocketTable[i] ~=nil then

UdpSocketTable[i]:send(UdpPortTable[i],UdpIPTable[i],data)

end

end

end

printip=0wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)

printip=0end)

wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)ifprintip ==0then

print("+IP"..T.IP)

end

printip=1end)

file.lua

就是接收串口的數據函數,檢測CRC對不對,,然后把數據寫進mode.lua,,,對了這樣設置了,,內部會自動生成一個mode.lua,,不用自己弄

function Config(data)

local RevLen=string.len (data)

local crc= ow.crc16(string.sub(data,1,RevLen-2))

local recrc= data:byte(RevLen)

recrc= recrc*256recrc= recrc + data:byte(RevLen-1)ifcrc ==recrc then--[[8266Mode ]]ifRevLen ==8thenifstring.sub(data,1,5) =="++MD0"then----Modeiffile.open("mode.lua","w+") then

file.writeline(string.sub(data,6,6))--MODE

print("MODE="..string.sub(data,6,6))

file.close()elseprint("open mode.lua faild")

end

end--[[8266Mode ]]

end

end

end

其實也沒有說的了,串口用空閑中斷接收的,前面說了,,,,剩下的主要是自己看著API文檔自己測試測試哈

現在把程序下進去用上位機試一試修改模式

復位重啟一下

對了我把UDP的修改了一個地方,,,寫的太著急寫錯了,

這樣才是判斷是新的連接,而且不和默認的一樣才執行后面的

完了一個大的心思

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,885評論 6 541
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,312評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,993評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,667評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,410評論 6 411
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,778評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,775評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,955評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,521評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,266評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,468評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,998評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,696評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,095評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,385評論 1 294
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,193評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,431評論 2 378

推薦閱讀更多精彩內容