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

User avatar
By Yak54x
#5309 Hi Guys,
I'm playing around with buttons on a Website to switch a led on pin 9. My code works well. But I have a question. The time between pushing a button and led switching is about 2 seconds, but the response time to show the updated website on pc or tablet is about 35 seconds. Is this normal? I found that there is only a little difference of time between the tested scripts out of the forum and my own code. What is your experience?
User avatar
By ThomasW
#5315
Yak54x wrote:Hi Guys,
I'm playing around with buttons on a Website to switch a led on pin 9. My code works well. But I have a question. The time between pushing a button and led switching is about 2 seconds, but the response time to show the updated website on pc or tablet is about 35 seconds. Is this normal?

I would say no. Maybe post your code?

Thomas
User avatar
By Yak54x
#5329 Here is my code:

Code: Select allled = 9   -- pin 9
led_stat = "off"         
gpio.mode(led,gpio.OUTPUT)

print("wait on input...")

srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
    conn:on("receive",function(conn,payload)
   
    print("Input:"..payload)
 
    if string.find(payload,"led_on") then
        print("input led_on: - ")
        gpio.write(led,gpio.LOW)
        led_stat = "on"
    end
     
    if string.find(payload,"led_off") then
        print("input led_off - ")
        gpio.write(led,gpio.HIGH)
        led_stat = "off"
    end
       -- html-output
       conn:send("HTTP/1.0 200 OK\r\nContent-type: text/html\r\nServer: ESP8266\r\n\n")
       conn:send("</html><head></head><body>")
       conn:send("Served from ESP8266-".. node.chipid().." - NODE.HEAP: ".. node.heap().." ")
       if led_stat == "off" then
         conn:send("<font color=\"red\">")
       else
          conn:send("<font color=\"green\">")
       end
       conn:send("<h1> Led is "..led_stat.."</h1></font>")
       conn:send("<FORM action=\"\" method=\"POST\">")
       conn:send("<button style=\"background-color:green\" name=\"led_on\" type=\"submit\" value=\"led_on\">LED on</button><br>")
       conn:send("<br></br>")
       conn:send("<button style=\"background-color:yellow\" name=\"led_off\" type=\"submit\" value=\"led_on\">LED off</button><br>")
       conn:send("</form>")
       conn:send("</body>")
       conn:send("</html>")   
    end)   
    end)


I have tried some other similar scripts out of the forum, the response time is in the same range and independent from target device (PC - Win7(64) Firefox or MS InetExplorer, Tablet or Smartphone - Android). Maybe it's a problem with the router?
User avatar
By ThomasW
#5333
Yak54x wrote:Maybe it's a problem with the router?

No, you're just not closing the connection (or send a 'Content-length header') and the browser waits until it's timeout before displaying anything. If you're not planning to send a larger page, you should be fine by just adding
Code: Select allconn:on("sent",function(conn) conn:close() end)


Thomas

Edit: You should also fix the output:
conn:send("HTTP/1.0 200 OK\r\nContent-type: text/html\r\nServer: ESP8266\r\n\n")
conn:send("</html><head></head><body>")

- End of the headers should be marked mit "\r\n\r\n"
- html should not start with </html> ;)