應該是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的修改了一個地方,,,寫的太著急寫錯了,
這樣才是判斷是新的連接,而且不和默認的一樣才執行后面的
完了一個大的心思