conn:send conn:on trying to find info on how to use.

xtal

Joined Oct 21, 2008
13
ok I found a simple way to refresh page
<head><meta http-equiv="refresh"content="30"/></head>
this would refres every 30 seconds,,

I still would like to trigger on certain serial input :eek:
 

djsfantasi

Joined Apr 11, 2010
9,163
If you can accept a refresh every few seconds, code a meta tag in your web page. Here's the code. Copy and paste it into the document you wish to reload. Once in there, change the number of seconds you wish the page to wait before starting the reloading process. The example is set to 15 seconds. This code goes in between the HEAD tags.
<META HTTP-EQUIV="refresh" CONTENT="15">​
 
I still havn't figure out conn:send. The lua documentation has some information around it.

Code:
 -- A simple http server
    srv=net.createServer(net.TCP)
    srv:listen(80,function(conn)
      conn:on("receive",function(conn,payload)
        print(payload)
        conn:send("<h1> Hello, NodeMcu.</h1>")
      end)
      conn:on("sent",function(conn) conn:close() end)
    end)
https://github.com/nodemcu/nodemcu-firmware

I have been working on this project, and have been trying to style the HTML. I think inline styling is the only option for the conn:send. If anyone has any information on how it reads/rends let me know.

As an FYI, I came across this project on the ESP8266 forums (http://www.esp8266.com/viewtopic.php?f=19&t=990).

They fixed the double in their code with the following:

Code:
wifi.setmode(wifi.STATION)
wifi.sta.config("<SSID>","<code>")
outpin=4 -- Select right IO index !! Here is settings for GPIO2 (Lua build 20141219)

gpio.mode(outpin,gpio.OUTPUT)
gpio.write(outpin,gpio.LOW)
status = 'OFF'

function ctrlpower(kdesi,payload)
   pwm.close(outpin)
   gpio.mode(outpin,gpio.OUTPUT)
   dotaz=string.sub(payload,kdesi[2]+1,#payload)
   status = dotaz
   if dotaz=="ON"  then gpio.write(outpin,gpio.HIGH) return end
   if dotaz=="OFF" then gpio.write(outpin,gpio.LOW) return end
   if dotaz=="FLC" then pwm.setup(outpin,2,512)pwm.start(outpin) return end
   pwm.setup(outpin,1000,dotaz*10)
   pwm.start(outpin)
end

function sendPage(conn)
   conn:send('HTTP/1.1 200 OK\n\n')
   conn:send('<!DOCTYPE HTML>')
   conn:send('<html>')
   conn:send('<head><meta content="text/html; charset=utf-8"><style>input{width: 100px; height: 100px;}</style>')
   conn:send('<title>ESP8266</title></head>')
   conn:send('<body><h1>LED Controller</h1>')
   conn:send('Status: <b>')
   if (status == "ON") then      conn:send('ON')
   elseif (status == "OFF") then    conn:send('OFF')
   elseif (status == "FLC") then    conn:send('Flickering')
   else                     
      conn:send(status)
      conn:send('%')
   end
   conn:send('</b><br /><br />')
   conn:send('<form action="/" method="POST">')
   conn:send('<input type="submit" name="pwmi" value="OFF"/>')
   conn:send('<input type="submit" name="pwmi" value="ON"/><br /><br />')
   conn:send('<input type="submit" name="pwmi" value="10"/>')
   conn:send('<input type="submit" name="pwmi" value="20"/>')
   conn:send('<input type="submit" name="pwmi" value="30"/>')
   conn:send('<input type="submit" name="pwmi" value="40"/>')
   conn:send('<input type="submit" name="pwmi" value="50"/>')
   conn:send('<input type="submit" name="pwmi" value="60"/>')
   conn:send('<input type="submit" name="pwmi" value="70"/>')
   conn:send('<input type="submit" name="pwmi" value="80"/>')
   conn:send('<input type="submit" name="pwmi" value="90"/> % of power<br /><br />')
   conn:send('<input type="submit" name="pwmi" value="FLC"/> HW blinker</form>')
   conn:send('</body></html>')
end

srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
   conn:on("receive", function(conn,payload)
      --next row is for debugging output only
      --print(payload)
      if (string.find(payload, "GET / HTTP/1.1") ~= nil) then
         print("GET received")
         sendPage(conn)
      else
         kdesi={string.find(payload,"pwmi=")}
         --If POST value exist, set LED power
         if kdesi[2]~=nil then
            print("Command received: " .. payload)
            ctrlpower(kdesi,payload)
            sendPage(conn)
         end
      end
   end)
   conn:on("sent", function(conn)
      conn:close()
      print("Connection closed")
   end)
end)
 
Top