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 @@
"u8g2", "u8g2",
"i2c", "i2c",
"gpio", "gpio",
"enduser_setup" "enduser_setup",
"file"
] ]
} }

41
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

10
OLED_SSD1306.lua

@ -73,9 +73,7 @@ function PageLight(lightArr)
--Disp:updateDisplayArea(0,0,64,64); --Disp:updateDisplayArea(0,0,64,64);
end end
--PageMainScene("DF2","192.168.1.111","V1.0") --PageMainScene("DF2","192.168.1.115","V1.0")
--LightArr = {[1]=true,[2]=false,[3]=true,[4]=true} --local lightArr = {[1]=true,[2]=false,[3]=true,[4]=true}
--PageLight(LightArr) --PageLight(lightArr)
--LightArr[1] = false; --PageAPNet()
--PageLight(LightArr)
PageAPNet()

3
SaveData/Config.lua

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

5
UDPService.lua

@ -2,6 +2,7 @@
-- UDPIP = nil; -- UDPIP = nil;
-- UDPPort = nil; -- UDPPort = nil;
require("GPIO"); require("GPIO");
LightArr = {[1]=false,[2]=false,[3]=false,[4]=false}
function StartUDPService() function StartUDPService()
if UDPSocket == nil then if UDPSocket == nil then
UDPSocket = net.createUDPSocket(); UDPSocket = net.createUDPSocket();
@ -27,10 +28,14 @@ function UDPService(s, data, port, ip)
print(tonumber(commandSet[2]).."-"..commandSet[3]) print(tonumber(commandSet[2]).."-"..commandSet[3])
if commandSet[3]=="On" and tonumber(commandSet[2])~=nil then if commandSet[3]=="On" and tonumber(commandSet[2])~=nil then
GPIOOn(tonumber(commandSet[2])); GPIOOn(tonumber(commandSet[2]));
LightArr[tonumber(commandSet[2])] = true;
s:send(port, ip, "Light "..commandSet[2].." "..commandSet[3]); s:send(port, ip, "Light "..commandSet[2].." "..commandSet[3]);
PageLight(LightArr);
elseif commandSet[3]=="Off" and tonumber(commandSet[2])~=nil then elseif commandSet[3]=="Off" and tonumber(commandSet[2])~=nil then
GPIOOff(tonumber(commandSet[2])); GPIOOff(tonumber(commandSet[2]));
LightArr[tonumber(commandSet[2])] = false;
s:send(port, ip, "Light "..commandSet[2].." "..commandSet[3]); s:send(port, ip, "Light "..commandSet[2].." "..commandSet[3]);
PageLight(LightArr);
else else
s:send(port, ip, "CommandFaild"); s:send(port, ip, "CommandFaild");
end end

26
WifiMgr.lua

@ -1,21 +1,41 @@
StationCfg = {} StationCfg = {}
IpCfg = {} IpCfg = {}
WifiSaved = {}
--Load Config.lua and get value from Config_Table
function AutoLink() 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) wifi.setmode(wifi.STATION)
StationCfg.ssid="DF2" StationCfg.ssid=ssid
StationCfg.pwd="wodepasstne?" StationCfg.pwd=passwd
StationCfg.save=true StationCfg.save=true
wifi.sta.config(StationCfg) wifi.sta.config(StationCfg)
wifi.sta.connect() wifi.sta.connect()
PageMainScene(ssid,wifi.sta.getip().."","V1.0");
local lightArr = {[1]=false,[2]=false,[3]=false,[4]=false}
PageLight(lightArr)
end end
--mobile setting ap and passwd --mobile setting ap and passwd
function AutoLinkByMobile() function AutoLinkByMobile()
wifi.setmode(wifi.STATIONAP) 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.manual(true)
enduser_setup.start( enduser_setup.start(
"DY-Light-Controller",
function() function()
print("Connected to WiFi as:" .. wifi.sta.getip()) print("Connected to WiFi as:" .. wifi.sta.getip())
end, end,

1
eus_params.lua

@ -0,0 +1 @@

3
init.lua

@ -1,5 +1,6 @@
--Start with this file --Start with this file
--这个文件初始化会执行 --这个文件初始化会执行
require("EasySave")
require("GPIO") require("GPIO")
require("OLED_SSD1306") require("OLED_SSD1306")
require("WifiMgr") require("WifiMgr")
@ -7,6 +8,6 @@ require("UDPService")
function Init() function Init()
SetIP("192.168.1.115","255.255.255.0","192.168.1.1"); SetIP("192.168.1.115","255.255.255.0","192.168.1.1");
AutoLink(); AutoLink();
StartUDPService(); --StartUDPService();
end end
Init(); Init();

Loading…
Cancel
Save