As the title says... Chat on...

User avatar
By xlaser
#43396 I would like to display the contents of variable 'vars' in the code below either with a print to consul window or as part of the HTML page served .
A print statement outside the trm.alarm clause is proving impossible to implement and if I try to embed a html statement like
buf = buf.."<p style='color:black'>" ..vars.."</p>";
I get a Lua error message about trying to concat a NILL value.
Any ideas on how to display the content of 'vars' would be welcome.

wifi.setmode(wifi.STATION)
wifi.sta.config("virginmedia7991485","cduqmqdp")
wifi.sta.connect()
tmr.alarm(1, 1000, 1, function() -- start timer 1
cfg =
{
ip="192.168.0.201",
netmask="255.255.255.0",
gateway="192.168.0.1"
}
wifi.sta.setip(cfg) -- overwrite the DHCP info
if wifi.sta.getip()== nil then
print("IP unavaiable, Waiting...")
else
tmr.stop(1) -- stop timer 1
print("Config done, IP is "..wifi.sta.getip())
end
end)
led1 = 3
led2 = 4
gpio.mode(led1, gpio.OUTPUT)
gpio.mode(led2, gpio.OUTPUT)
srv=net.createServer(net.TCP)
srv:listen(82,function(conn) -- Listen on port 82
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.."<html>";
buf = buf.."<head>";
buf = buf.."<title>ESP8266 LED Remote Control</title>";
buf = buf.."</head>";
buf = buf.."<body style='background-color:#FFFF77'>";
buf = buf.."<h1 style='color:blue'> ESP8266 Web Server</h1>";
buf = buf.."<p style='color:green'>GPIO0_LED GPIO0 <a href=\"?pin=ON1\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF1\"><button>OFF</button></a></p>";
buf = buf.."<p style='color:red'>GPIO2_LED GPIO2 <a href=\"?pin=ON2\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF2\"><button>OFF</button></a></p>";
buf = buf.."Last Command Status: ";
if(_GET.pin == "ON1")then
gpio.write(led1, gpio.HIGH);
buf = buf.."GPIO_0 is ON </p>";
elseif(_GET.pin == "OFF1")then
gpio.write(led1, gpio.LOW);
buf = buf.."GPIO_0 is OFF </p>";
elseif(_GET.pin == "ON2")then
gpio.write(led2, gpio.HIGH);
buf = buf.."GPIO_2 is ON </p>";
elseif(_GET.pin == "OFF2")then
gpio.write(led2, gpio.LOW);
buf = buf.."GPIO_2 is OFF </p>";
else
buf = buf.."Undefined</p>";
end
buf = buf.."</body>";
buf = buf.."</html>";
client:send(buf);
client:close();
collectgarbage();
end)
end)
User avatar
By TerryE
#43447 Use a current dev custom build., so you can avoid loads of issues that we've cleaned up. This also ups the default bit rate to 115200 which will help print timeout issues. Edit your post to add a code block to that those wanting to help can read it easily.
Why not just keep your code simple and read the SDK: don't send and close in the same callback.:
Code: Select all      local status
      if(_GET.pin == "ON1")then
        gpio.write(led1, gpio.HIGH)
        status = "GPIO 0 is ON"
      elseif(_GET.pin == "OFF1")then
        gpio.write(led1, gpio.LOW)
        status = "GPIO 0 is OFF"
      elseif(_GET.pin == "ON2")then
        gpio.write(led2, gpio.HIGH)
        status = "GPIO 2 is ON"
      elseif(_GET.pin == "OFF2")then
        gpio.write(led2, gpio.LOW)
        status = "GPIO 2 is OFF"
      else
        status = "Undefined"
      end
     
      client:send(([[<html>
  <head><title>ESP8266 LED Remote Control</title></head>
  <body style='background-color:#FFFF77'>
  <h1 style='color:blue'> ESP8266 Web Server</h1>
  <p style='color:green'>GPIO0_LED GPIO0 <a href="?pin=ON1"><button>ON</button></a>&nbsp;<a href="?pin=OFF1"><button>OFF</button></a></p>
  <p style='color:red'>GPIO2_LED GPIO2 <a href="?pin=ON2"><button>ON</button></a>&nbsp;<a href="?pin=OFF2"><button>OFF</button></a></p>
  <p>Last Command Status: %s</p>
  </body></html>]]):format(status))
      collectgarbage()
    end)
     
    conn:on("sent",function(client) client:close() collectgarbage() end
  end)
User avatar
By AdrianM
#43505 TerryE shows you a neat way to format the status string into the html string literal. This literal is enclosed by double square brackets [[ ... ]] and is a more WYSIWYG way of forming a string. If you've not come across it before you might want to read up on it here http://lua-users.org/wiki/StringsTutorial