Post your best Lua script examples here

User avatar
By ieglover
#43049 Greetings all. I am trying to create a module that will allow me to read some voltage and current values from some hardware I have created and I hooked up on of the GPIOs to send a shutdown signal to a piccolo chip and eventually to a relay.

I am getting a string of data from the piccolo chip using the uart module, that I can reprint back out to serial without an issue. However, I am looking to add it to my webserver that includes the toggle switch to shutdown the system. On the uart module at the bottom, I can easily read it as needed so I have that string saved and can manipulate it, I just need to figure out how to get it updated onto the webserver. I am not concerned with how often it needs to refresh as I can control how often I receive the serial data.

--wifi.setmode(wifi.STATION)
--wifi.sta.config("HOME-CD31-2.4", "XXXXX")
--ip = wifi.sta.getip()
--print(ip)
gpio.mode(4, gpio.OUTPUT)
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive", function(client,request)
local buf = "";
local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
if(method == nil)then
_, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
end
local _GET = {}
if (vars ~= nil)then
for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
_GET[k] = v
end
end

buf = buf.."<h1> Power Line Monitoring</h1><form src=\"/\">Shutdown <select name=\"pin\" onchange=\"form.submit()\">";
local _on,_off = "",""
if(_GET.pin == "ON")then
_on = " selected=true";
gpio.write(4, gpio.HIGH);
elseif(_GET.pin == "OFF")then
_off = " selected=\"true\"";
gpio.write(4, gpio.LOW);
end

buf = buf.."<option".._on..">ON</opton><option".._off..">OFF</option></select></form>";
buf = buf.."<h1> Current and Voltage Data</h1>";

client:send(buf);
client:close();
collectgarbage();
end)
end)

uart.setup(0, 9600, 8, 0, 1,0)
uart.on("data", '|',
function(data)
print("receive from uart:", data)
end,0)
User avatar
By Donquixote2u
#44559 Well fwiw I have just done the same thing, and the web update was just done by setting up a connection and sending a request to the web server (address in variable HOST) embedding the data as a normal GET type argument in the url, which is basically how your code is set up to receive; in this case the GET agument is Windspeed, and "data" is the value passed:

Code: Select all--- Get data and send to host web server
function sendData()
print("Sending data to "..HOST)
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload) print(payload) end)
conn:connect(80,HOST)
conn:send("GET /?Windspeed="..string.format("%04d",data).." HTTP/1.1\r\n")
conn:send("Host: "..HOST.."\r\n")
conn:send("Accept: */*\r\n")
conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
conn:send("\r\n")
conn:on("sent",function(conn)
conn:close()
end)
end
-- send data every X ms to thing speak
tmr.alarm(0, 20000, 1, function() sendData() end )


hope that is relevant to you.