Étape 8: Interface de serveur Web
Si tous ok, nous pouvons passer à l’étape suivante, comment faire pour rendre les données disponibles sur le web.
Pour celui-ci, nous aurons besoin de construire nos fonctions de serveur Web :
- Serveur WEB
srv=net.createServer(net.TCP) srv:listen(80, function(conn) conn:on("receive",function(conn,payload) print(payload) conn:send("HTTP/1.1 200 OK\n\n")
conn:send("<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"5\">") conn:send("<html><title>MCP9808 - Temperature Log Server - ESP8266</title><body>") conn:send("<h1>ESP8266 Temperature Log Server - MCP9808</h1><BR>") conn:send("Temperature : <b>" .. tp1 .. "°C</b><BR><BR>") conn:send("Node.HEAP : <b>" .. node.heap() .. "</b><BR><BR>") conn:send("IP ADDR : <b>".. wifi.sta.getip() .. "</b><BR>") conn:send("Node MAC : <b>" .. wifi.sta.getmac() .. "</b><br>") conn:send("TMR.NOW : <b>" .. tmr.now() .. "</b><BR<BR><BR>") conn:send("</html></body>") conn:on("sent",function(conn) conn:close() end) end) end)
- Fonction de lecture de température
function readTMP() mcp9808:init(sda, scl) tp1 = mcp9808:readTemp() print(tp1) end
- Minuterie - d’établir combien de fois nous voulons que le processus de lecture à faire.
-- read data every 1sec for direct web reading tmr.alarm(0, 1000, 1, function() readTMP() end )