基于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.

85 lines
2.2 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,"^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);
GPIOOff(1);
GPIOOff(2);
s:send(port, ip, "0-1-1");
end
if string.match(data,"^Status") then
local result = "";
if LightArr[0] == true then
result = "1-"
else
result = "0-"
end
if LightArr[1] == true then
result = result .. "1-"
else
result = result .. "0-"
end
if LightArr[2] == true then
result = result .. "1"
else
result = result .. "0"
end
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();