應(yīng)該是LUA介紹8266的最后一篇,,,,,,下回是直接用SDK,,然后再列個(gè)12345.......不過(guò)要等一兩個(gè)星期,先忙完朋友的事情
前面幾篇
用AT指令版本的
一,http://www.cnblogs.com/yangfengwu/p/5205570.html發(fā)AT指令,,其實(shí)AT指令就是事先用SDK內(nèi)部寫(xiě)好了
二,http://www.cnblogs.com/yangfengwu/p/5295632.html這個(gè)呢是配合路由器實(shí)現(xiàn)遠(yuǎn)程,,其實(shí)可以買(mǎi)個(gè)域名,或者用云端
三,http://www.cnblogs.com/yangfengwu/p/5220919.html這是做了一個(gè)用手機(jī)控制的WIFI小車(chē)
四,http://www.cnblogs.com/yangfengwu/p/5212570.htmlAndroid TCP客戶端
五,http://www.cnblogs.com/yangfengwu/p/5294921.htmlAndroid TCP服務(wù)器
用LUA語(yǔ)言的,更靈活,功能更強(qiáng)
一http://www.cnblogs.com/yangfengwu/p/7514336.html下載自己選擇功能的固件,刷固件,寫(xiě)LUA的軟件
二http://www.cnblogs.com/yangfengwu/p/7520260.htmlGPIO,串口,注意事項(xiàng),空閑中斷
三http://www.cnblogs.com/yangfengwu/p/7524297.htmlSPI通信介紹,,STM32 SPI 從機(jī)
四http://www.cnblogs.com/yangfengwu/p/7524326.htmlTCP服務(wù)器,單個(gè)連接
五http://www.cnblogs.com/yangfengwu/p/7531730.htmlTCP服務(wù)器,多個(gè)客戶端連接,CRC校驗(yàn)
六http://www.cnblogs.com/yangfengwu/p/7533302.htmlTCP Client 斷開(kāi)自動(dòng)重連,AD采集
七http://www.cnblogs.com/yangfengwu/p/7533845.htmlUDP支持一路默認(rèn),3路動(dòng)態(tài)連接,ssid 與 pwd 的讀取
還缺一樣....這篇...保存數(shù)據(jù)...這樣的話就可以隨意修改啟動(dòng)時(shí)工作在哪一種模式,哪一種通信,以及其余需要保存在模塊內(nèi)部的信息
就實(shí)現(xiàn)上位機(jī)軟件的第一個(gè)功能,,修改啟動(dòng)模式

自己的上位機(jī)按鈕事件

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模式
其實(shí)最后還有CRC16校驗(yàn)碼
看文件操作

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()//連續(xù)寫(xiě)最好加上file.writeline("456")
file.flush()
file.writeline("789")
file.close()elseprint("open mode.lua faild")
end

像是這樣??mode.lua文件里面會(huì)是這樣

那么讀的時(shí)候

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"
所以寫(xiě)進(jìn)去一行3個(gè)但是讀出來(lái)會(huì)多加一個(gè)換行符

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
就是接收串口的數(shù)據(jù)函數(shù),檢測(cè)CRC對(duì)不對(duì),,然后把數(shù)據(jù)寫(xiě)進(jìn)mode.lua,,,對(duì)了這樣設(shè)置了,,內(nèi)部會(huì)自動(dòng)生成一個(gè)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

其實(shí)也沒(méi)有說(shuō)的了,串口用空閑中斷接收的,前面說(shuō)了,,,,剩下的主要是自己看著API文檔自己測(cè)試測(cè)試哈
現(xiàn)在把程序下進(jìn)去用上位機(jī)試一試修改模式

復(fù)位重啟一下





對(duì)了我把UDP的修改了一個(gè)地方,,,寫(xiě)的太著急寫(xiě)錯(cuò)了,

這樣才是判斷是新的連接,而且不和默認(rèn)的一樣才執(zhí)行后面的
完了一個(gè)大的心思