基于NodeMCU的智能小车控制器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.9 KiB

require("GPIO");
function StartUDPService()
if UDPSocket == nil then
UDPSocket = net.createUDPSocket();
UDPSocket:listen(6000);
UDPSocket:on("receive", UDPService);
UDPPort, UDPIP = UDPSocket:getaddr();
else
UDPSocket:listen(6000);
UDPSocket:on("receive", UDPService);
UDPPort, UDPIP = UDPSocket:getaddr();
end
--print(string.format("local UDP socket address / port: %s:%d", UDPIP, UDPPort))
end
function UDPService(s, data, port, ip)
local commandSet = {};
for word in string.gmatch(data, "[^-]+") do
table.insert(commandSet,word);
end
print("get data count" .. #commandSet);
-- if string.match(data,"^Light") then
-- if #commandSet==3 then
-- print(tonumber(commandSet[2]).."-"..commandSet[3])
-- if commandSet[3]=="On" and tonumber(commandSet[2])~=nil then
-- GPIOOn(tonumber(commandSet[2]));
-- s:send(port, ip, "Light "..commandSet[2].." "..commandSet[3]);
-- elseif commandSet[3]=="Off" and tonumber(commandSet[2])~=nil then
-- GPIOOff(tonumber(commandSet[2]));
-- s:send(port, ip, "Light "..commandSet[2].." "..commandSet[3]);
-- else
-- s:send(port, ip, "CommandFaild");
-- end
-- end
-- end
if string.match(data,"^Go") then
if #commandSet==2 then
print(tonumber(commandSet[2]))
Is_AutoClose = false;
GPIOOn(0);
GPIOOn(1);
GPIOOn(2);
s:send(port, ip, "1-1-1");
end
end
if string.match(data,"^Back") then
if #commandSet==2 then
print(tonumber(commandSet[2]))
Is_AutoClose = false;
GPIOOn(0);
GPIOOff(1);
GPIOOn(2);
s:send(port, ip, "1-0-1");
end
end
if string.match(data,"^Stop") then
Is_AutoClose = false;
GPIOOff(0);
GPIOOn(1);
GPIOOff(2);
s:send(port, ip, "0-1-0");
end
if string.match(data,"^Ready") then
Is_AutoClose = false;
GPIOOff(0);
GPIOOn(1);
GPIOOn(2);
s:send(port, ip, "0-1-1");
end
if string.match(data,"^Status") then
local result = "";
if(LightArr[0] == true){
result = "1-"
}else{
result = "0-"
}
if(LightArr[1] == true){
result = result .. "1-"
}else{
result = result .. "0-"
}
if(LightArr[2] == true){
result = result .. "1"
}else{
result = result .. "0"
}
s:send(port, ip, result);
end
if string.match(data,"^Addr") then
s:send(port, ip, "Addr-"IpCfg.ip);
end
end
function StopUDPService()
if UDPSocket ~= nil then
UDPSocket:close();
end
end
--StartUDPService();