Browse Source

wifi auto link

master
DESKTOP-B25GA9E\W35 1 year ago
parent
commit
1c88cced26
  1. 3
      .vscode/settings.json
  2. 41
      EasySave.lua
  3. 10
      OLED_SSD1306.lua
  4. 3
      SaveData/Config.lua
  5. 5
      UDPService.lua
  6. 26
      WifiMgr.lua
  7. 1
      eus_params.lua
  8. 3
      init.lua

3
.vscode/settings.json vendored

@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
"u8g2",
"i2c",
"gpio",
"enduser_setup"
"enduser_setup",
"file"
]
}

41
EasySave.lua

@ -0,0 +1,41 @@ @@ -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

10
OLED_SSD1306.lua

@ -73,9 +73,7 @@ function PageLight(lightArr) @@ -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()

3
SaveData/Config.lua

@ -0,0 +1,3 @@ @@ -0,0 +1,3 @@
Config_Table = {
["Version"]="V0.1"
}

5
UDPService.lua

@ -2,6 +2,7 @@ @@ -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) @@ -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

26
WifiMgr.lua

@ -1,21 +1,41 @@ @@ -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,

1
eus_params.lua

@ -0,0 +1 @@ @@ -0,0 +1 @@

3
init.lua

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
--Start with this file
--这个文件初始化会执行
require("EasySave")
require("GPIO")
require("OLED_SSD1306")
require("WifiMgr")
@ -7,6 +8,6 @@ require("UDPService") @@ -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();

Loading…
Cancel
Save