วันอังคารที่ 3 มีนาคม พ.ศ. 2558

ESP8266 GPIO Http Server

ในบทความนี้จะต่อจากครั้งที่แล้วโดยจะใช้ ESP8266-01 เป็นตัวทดสอบ

เราใจใช้ GPIO0(3) กับ GPIO2(4)


ซึ่งผมได้ปรับปรุง Codeจากครั้งที่แล้วเพื่อให้สามารถควบคุมการทำงานของ GPIO ได้

Source Code

gpio.mode(3,gpio.OUTPUT)
gpio.mode(4,gpio.OUTPUT)
function split(s, delimiter)
    result = {};
    for match in (s..delimiter):gmatch("(.-)"..delimiter) do
        table.insert(result, match);
    end
    return result;
end
function urldecode(payload)
    result = {};
    list=split(payload,"\r\n")
    list=split(list[1]," ")
    list=split(list[2],"\/")
    table.insert(result, list[1]);
    table.insert(result, list[2]);
    table.insert(result, list[3]);
    return result;
end

function index(conn)

    gpio3 = 'ON'
    gpio4 = 'ON'
    if (gpio.read(3) == 1) then
       gpio3 = 'OFF'
    end
    if (gpio.read(4) == 1) then
       gpio4 = 'OFF'
    end
    conn:send('HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\n\r\n\
     <!DOCTYPE HTML><html><head><style>input[type=submit] {font-size:large;width:8em;height:4em;}</style>\
     <meta content="text/html;charset=utf-8"><title>ESP8266</title>\
     <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">\
     <body bgcolor="#ffe4c4"><h2>ESP8266-01</h2><hr>\
     <form action="/digital/3/'..gpio.read(3)..'" method="POST">\
     <input type="submit" value="GPIO3 '..gpio3..'"/></form><br><br>\
     <form action="/digital/4/'..gpio.read(4)..'" method="POST">\
     <input type="submit" value="GPIO4 '..gpio4..'" /></form>\
     </body></html>')
end
function notfound(conn)
     conn:send('HTTP/1.1 404 Not Found\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\n\r\n\
          <!DOCTYPE HTML>\
         <html><head><meta content="text/html;charset=utf-8"><title>ESP8266</title></head>\
          <body bgcolor="#ffe4c4"><h2>Page Not Found</h2>\
          </body></html>') 
end

srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
    conn:on("receive",function(conn,payload)
      print("Http Request..\r\n")
      --print (payload)
      list=urldecode(payload) 
      if ((list[2]=="") or (list[2]=="index.html")) then 
          index(conn)   
      elseif (list[2]=="digital") then
          local pin = tonumber(list[3]) 
          local status = tonumber(list[4]) 
          if (status == 1) then
               gpio.write(pin, 0)   
          else
               gpio.write(pin, 1)
          end  
          --print(gpio.read(pin))        
          index(conn)
      else
          notfound(conn)   
      end      
      conn:close() 
    end)
end)


อุปกรณ์ที่ใช้ในการทดสอย





Donate

8 ความคิดเห็น:

  1. คือผมมีคำถามครับ เราสามารถนำ esp8266 หลายๆตัว มาทำเป็นระบบเครือข่ายแบบ local ได้ไหมครับ

    ตอบลบ
    คำตอบ
    1. ได้ครับ ถ้าหมายถึง จากตัวหนึ่ง ไปเรียกให้อีกตัวหนึ่งทำงาน หรือไปอ่านข้อมูลจากอีกตัวหนึ่งมา

      ลบ
    2. ขอบคุนมากครับ แล้วพี่พอจะมีลิ้งให้ผมไปศึกษาไหมครับ

      ลบ
  2. คำตอบ
    1. Thank you!! I use this very good. But I have a problem for program.ESP8266 WEBServer http page button don't like youtube video
      can show (GPIO3 ON) ->(GPIO3 OFF)

      ลบ