Wednesday 22 July 2015

ESP8266 net display

Using the previous lcdprint function here is a simple remote display. To display on it from your server you can use wget (or nc or whatever) eg.

a message
wget "http://xxx.xxx.xxx.xxx/?message=This%20is%20line%201&line=1&col=0" 
the time and date
wget "http://xxx.xxx.xxx.xxx/?message=`date +"%H:%M %a %d %b"`&line=2&col=0"
 
 
Here is the code based on the toggle_pin.lua webap (here)  Run node.compile() on this as well as the lcdprint or it runs out of heap.

--based on the webap toggle_pin.lua
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
 conn:on("receive", function(client,request)
   local buf = "ok";
   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%s%%]+)&*") do
        _GET[k] = v
      end
   end
 if (_GET.message ~= nil)then
  -- from http://lua-users.org/wiki/StringRecipes
  local function url_decode(str)
    str = string.gsub (str, "+", " ")
    str = string.gsub (str, "%%(%x%x)",function(h) return string.char(tonumber(h,16)) end)
    str = string.gsub (str, "\r\n", "\n")
    return str
  end 
 
  local line=1
  local col=0

  if (_GET.line~= nil) then  line=_GET.line+0 end
  if (_GET.col ~= nil) then  col=_GET.col+0 end
  local message=url_decode(_GET.message)
  dofile("lcdprint.lc").lcdprint(message,line,col)
 end
 client:send(buf);
 client:close();
 collectgarbage();
 end)
end) 
 

No comments: