diff --git a/.vscode/settings.json b/.vscode/settings.json index d9124e7..1f4708c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,6 +5,7 @@ "u8g2", "i2c", "gpio", - "enduser_setup" + "enduser_setup", + "file" ] } \ No newline at end of file diff --git a/EasySave.lua b/EasySave.lua new file mode 100644 index 0000000..bbc6de8 --- /dev/null +++ b/EasySave.lua @@ -0,0 +1,41 @@ +Files = file.list(); +if Files["Config.lua"] then + dofile("Config.lua"); +end +--Get data from config files +function GetData(key) + if Config_Table[key] ~=nil then + return Config_Table[key]; + end + return nil; +end + +--Save data to config files +function SaveData(key,value) + file.open("Config.lua", "w+") + + Config_Table[key] = value; + local tableStr = TableToStr(Config_Table); + file.write(tableStr); + file.close() +end + +function TableToStr(table) + local stringData = "{\n" + for key, value in pairs(table) do + local keyValue = "[\"" .. tostring(key) .. "\"]=" + local valueString = "\"" .. tostring(value) .. "\"" + stringData = stringData .. " " .. keyValue .. valueString .. ",\n" + end + stringData = stringData:sub(1, #stringData - 2) .. "\n}\n" + stringData = "Config_Table = "..stringData; + print(stringData) + return stringData +end + +if Files["Config.lua"] == nil then + if Config_Table == nil then + Config_Table = {} + end + SaveData("Version","V0.1"); +end diff --git a/OLED_SSD1306.lua b/OLED_SSD1306.lua index 1710197..8f4dd5a 100644 --- a/OLED_SSD1306.lua +++ b/OLED_SSD1306.lua @@ -73,9 +73,7 @@ function PageLight(lightArr) --Disp:updateDisplayArea(0,0,64,64); end ---PageMainScene("DF2","192.168.1.111","V1.0") ---LightArr = {[1]=true,[2]=false,[3]=true,[4]=true} ---PageLight(LightArr) ---LightArr[1] = false; ---PageLight(LightArr) -PageAPNet() +--PageMainScene("DF2","192.168.1.115","V1.0") +--local lightArr = {[1]=true,[2]=false,[3]=true,[4]=true} +--PageLight(lightArr) +--PageAPNet() diff --git a/SaveData/Config.lua b/SaveData/Config.lua new file mode 100644 index 0000000..cce38bb --- /dev/null +++ b/SaveData/Config.lua @@ -0,0 +1,3 @@ +Config_Table = { + ["Version"]="V0.1" + } \ No newline at end of file diff --git a/UDPService.lua b/UDPService.lua index 39842f3..7264b86 100644 --- a/UDPService.lua +++ b/UDPService.lua @@ -2,6 +2,7 @@ -- UDPIP = nil; -- UDPPort = nil; require("GPIO"); +LightArr = {[1]=false,[2]=false,[3]=false,[4]=false} function StartUDPService() if UDPSocket == nil then UDPSocket = net.createUDPSocket(); @@ -27,10 +28,14 @@ function UDPService(s, data, port, ip) print(tonumber(commandSet[2]).."-"..commandSet[3]) if commandSet[3]=="On" and tonumber(commandSet[2])~=nil then GPIOOn(tonumber(commandSet[2])); + LightArr[tonumber(commandSet[2])] = true; s:send(port, ip, "Light "..commandSet[2].." "..commandSet[3]); + PageLight(LightArr); elseif commandSet[3]=="Off" and tonumber(commandSet[2])~=nil then GPIOOff(tonumber(commandSet[2])); + LightArr[tonumber(commandSet[2])] = false; s:send(port, ip, "Light "..commandSet[2].." "..commandSet[3]); + PageLight(LightArr); else s:send(port, ip, "CommandFaild"); end diff --git a/WifiMgr.lua b/WifiMgr.lua index e8590a1..063aedd 100644 --- a/WifiMgr.lua +++ b/WifiMgr.lua @@ -1,21 +1,41 @@ StationCfg = {} IpCfg = {} +WifiSaved = {} +--Load Config.lua and get value from Config_Table function AutoLink() + --get key from eus_params.lua + if Files["eus_params.lua"] then + WifiSaved = dofile("eus_params.lua"); + else + AutoLinkByMobile(); + PageAPNet(); + return nil; + end + --SampleLink + ManualLink(WifiSaved.wifi_ssid,WifiSaved.wifi_password) +end + +function ManualLink(ssid,passwd) wifi.setmode(wifi.STATION) - StationCfg.ssid="DF2" - StationCfg.pwd="wodepasstne?" + StationCfg.ssid=ssid + StationCfg.pwd=passwd StationCfg.save=true wifi.sta.config(StationCfg) wifi.sta.connect() + PageMainScene(ssid,wifi.sta.getip().."","V1.0"); + local lightArr = {[1]=false,[2]=false,[3]=false,[4]=false} + PageLight(lightArr) + end --mobile setting ap and passwd function AutoLinkByMobile() wifi.setmode(wifi.STATIONAP) - wifi.ap.config({ssid="MyPersonalSSID", auth=wifi.OPEN}) + wifi.ap.config({ssid="DY-Light-Controller", auth=wifi.OPEN}) enduser_setup.manual(true) enduser_setup.start( + "DY-Light-Controller", function() print("Connected to WiFi as:" .. wifi.sta.getip()) end, diff --git a/eus_params.lua b/eus_params.lua new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/eus_params.lua @@ -0,0 +1 @@ + diff --git a/init.lua b/init.lua index 082e21c..64dc632 100644 --- a/init.lua +++ b/init.lua @@ -1,5 +1,6 @@ --Start with this file --这个文件初始化会执行 +require("EasySave") require("GPIO") require("OLED_SSD1306") require("WifiMgr") @@ -7,6 +8,6 @@ require("UDPService") function Init() SetIP("192.168.1.115","255.255.255.0","192.168.1.1"); AutoLink(); - StartUDPService(); + --StartUDPService(); end Init();