【ESP8266之LUA开发】九、File操作,实现C#改变并存储工作模式,SSID与PWD的保存与读取

本文介绍ESP8266模块的文件操作方法,包括写入和读取模式设置,并通过C#上位机软件实现动态设置工作模式及WiFi连接方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文件操作的意义

文件操作进行数据的保存,可实现启动时工作在哪一种模式哪一种通信,以及其余需要保存在模块内部的信息


文件操作的API调用

API查阅<这里>

写入 mode.lua

if  file.open("mode.lua", "w+") then
    file.writeline("123")
    file.flush()
    file.writeline("456")
    file.flush()
    file.writeline("789")
    file.close()
else
    print("open mode.lua faild")    
end

注意,连续写之间要记得加上file.flush()

这里写图片描述

这里写图片描述

读取

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"

注意,读出来会多加一个回车符\r,因为读的时候是遇到\n作为结束符的,API也有说明的<这里>!

这里写图片描述


C#上位机协议说明

模式选择可直接在上位机上勾选相应的按钮,然后点击写入设置

这里写图片描述

上位机的C#部分代码

...
private void button2_Click(object sender, EventArgs e)
{
    byte[] sendbyte = new byte[6];
    sendbyte[0] = (byte)'+';//2B
    sendbyte[1] = (byte)'+';
    sendbyte[2] = (byte)'M';//4D
    sendbyte[3] = (byte)'D';//44
    sendbyte[4] = (byte)'0';//0

    if (radioButton5.Checked)
    {
        sendbyte[5] = (byte)'0';
    }
    else if (radioButton6.Checked)
    {
        sendbyte[5] = (byte)'1';
    }
    else if (radioButton7.Checked)
    {
        sendbyte[5] = (byte)'2';
    }
    if (radioButtonNetCon.Checked)
    {
        TcpSendDataMethod(sendbyte);
    }
    else if (radioButtonSerialCon.Checked)
    {
        SerialSend(sendbyte);
    }
}
...

上位机通过串口写入到8266时的数据协议说明

++MD00 AP模式

++MD01 Station模式

++MD02 AP+Station模式

最后还有CRC16校验码。

源码

配合C#上位机,实现工作模式的动态设置与保存。

init.lua

gpio.mode(4,gpio.OUTPUT)
gpio.mode(2,gpio.OUTPUT)
gpio.write(4,1)

if  adc.force_init_mode(adc.INIT_ADC) then
    node.restart()
    return
end

tmr.alarm(0, 1000, 1, function()
    gpio.write(4,1-gpio.read(4))
end)

tmr.alarm(1, 5000, 0, function()
    dofile("file.lua")
    dofile("wifi.lua")
end)

wifi.lua

if  file.open("mode.lua", "r") then
    local Mode = file.readline()
    if  Mode == nil then
        Mode="2"
    end
    if  Mode:byte(1) == 48 then
        print("Wifi MODE: SOFTAP")
        wifi.setmode(wifi.SOFTAP)
    elseif  Mode:byte(1) == 49 then
            print("Wifi MODE: STATION")
            wifi.setmode(wifi.STATION)  
    else
        print("Wifi MODE: STATIONAP")
        wifi.setmode(wifi.STATIONAP)         
    end
    file.close()
end

cfg={}
cfg = wifi.ap.getconfig(true)
if  cfg.ssid == nil then
    cfg.ssid="Hellow8266"
    cfg.pwd="11223344"
end
print("APssid: "..cfg.ssid)
if  cfg.pwd == nil then
    print("APpwd: nil")
else
    print("APpwd: "..cfg.pwd)
end 
cfg.save=true
wifi.ap.config(cfg)

apcfg={}
apcfg = wifi.sta.getconfig(true)
if  apcfg.ssid == nil then
    apcfg.ssid="360"
    apcfg.pwd="e903zcq567"
end
print("APssid: "..apcfg.ssid)
if  apcfg.pwd == nil then
    print("Stationpwd: nil")
else
    print("Stationpwd: "..apcfg.pwd)
end 
apcfg.save=true
wifi.sta.config(apcfg)
wifi.sta.autoconnect(1)


ConnectIP = "192.168.0.10"
ConnectPort = 8080

UdpSocket = net.createUDPSocket()   
UdpSocket:listen(ConnectPort)


UdpSocketTable={}
UdpIPTable={}
UdpPortTable={}
UdpConnectCnt = 0
UdpCanConnect = 0

UdpSocket:on("receive", function(socket, data, port, ip)
    UdpCanConnect = 1
    for i=0,2 do
        if  UdpIPTable[i] == ip and UdpPortTable[i] == port  then
            UdpCanConnect = 0
        end
    end

    if  ip == ConnectIP and port == ConnectPort  then
        UdpCanConnect = 0
    end

    if  UdpCanConnect == 1 then
        UdpSocketTable[UdpConnectCnt] = socket
        UdpIPTable[UdpConnectCnt] = ip 
        UdpPortTable[UdpConnectCnt] = port
        print("\r\n"..UdpConnectCnt.."-Connect")
        UdpConnectCnt = UdpConnectCnt + 1
    end
    
    if  UdpConnectCnt == 3 then
        UdpConnectCnt = 0
    end
    uart.write(0,data)
end)


UartReadCnt=0
UartReadCntCopy=0
UartReadData=""
UartReadDataCopy=""

uart.on("data",0,function(data) 
    UartReadCnt = UartReadCnt + 1
    UartReadData = UartReadData..data
        
end, 0)



tmr.alarm(2, 5, 1, function()
    if  UartReadCnt ~=0 then
        if  UartReadCnt == UartReadCntCopy  then
            UartReadCnt = 0
            UartReadCntCopy = 0
            UartReadDataCopy = UartReadData
            UartReadData=""
            Config(UartReadDataCopy)
            NetSend(UartReadDataCopy)
        else
            UartReadCntCopy = UartReadCnt  
        end         
    end

end)



function NetSend(data)
    if  UdpSocket ~= nil then
        UdpSocket:send(ConnectPort,ConnectIP,data)
    end
    
    for i=0,2 do
        if  UdpSocketTable[i] ~= nil then
            UdpSocketTable[i]:send(UdpPortTable[i],UdpIPTable[i],data) 
        end
    end
end



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


wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
   if printip == 0 then
      print("+IP"..T.IP)
   end
   printip = 1
end)

file.lua

接收上位机写入设置后发来的数据,并进行CRC校验,打印写入的工作模式!
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*256
    recrc = recrc + data:byte(RevLen-1)

    if  crc == recrc then
         --[[8266 Mode ]]
        if  RevLen == 8  then
            if  string.sub(data,1,5) == "++MD0" then----Mode
                if  file.open("mode.lua", "w+") then
                    file.writeline(string.sub(data,6,6))--MODE
                    print("MODE="..string.sub(data,6,6))
                    file.close()
                else
                    print("open mode.lua faild")    
                end
            end--[[8266 Mode ]]
        end
        
    end
end

wifi.lua中的Config(data)是定义在file.lua中的,可以跨文件直接调用的!

修改启动模式实验

这里写图片描述

这里写图片描述
这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

别忘了,每次写入后,复位一下!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ReCclay

如果觉得不错,不妨请我喝杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值