Created
April 19, 2017 00:38
-
-
Save ScottyTM/8d067a42543219ab2cf764cebe0eeaa9 to your computer and use it in GitHub Desktop.
Webserver on NodeMCU
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- based on http://www.beerandchips.net/2015/12/26/making-nodemcu-lua-esp8266-weather-station/ | |
-- with modifications for float firmware and dht11, also it now close the connection after data was sent | |
PIN = 4 -- data pin, GPIO2 | |
chipserial = node.chipid() | |
-- run the server | |
srv=net.createServer(net.TCP, 4) | |
print("Server created on " .. wifi.sta.getip()) | |
srv:listen(80,function(conn) | |
conn:on("receive",function(conn,request) | |
print(request) | |
status, temp, humi = dht.read11(PIN) | |
if string.match(request, "machine") then | |
conn:send("\"Temperature\": \""..temp.."\", \"Humidity\": \""..humi.."\"") | |
else | |
conn:send('<html>') | |
conn:send('<title>On Deck</title></head>') | |
conn:send('<body bgcolor=\"#ffffff\">') | |
conn:send('<center>') | |
conn:send('<table bgcolor=\"#0000ff\" width=\"90%\" border=\"0\">') | |
conn:send('<tr>') | |
conn:send(' <td><font size=\"2\" face=\"arial, helvetica\" color=\"#ffffff\"><center>Temperature</center></font></td>') | |
conn:send('</tr>') | |
conn:send('<tr>') | |
conn:send(' <td><font size=\"7\" face=\"arial, helvetica\" color=\"#ffffff\"><center>'..temp..'°C</center></font></td>') | |
conn:send('</tr>') | |
conn:send('<tr>') | |
conn:send(' <td><font size=\"2\" face=\"arial, helvetica\" color=\"#ffffff\"><center>Humidity</center></font></td>') | |
conn:send('</tr>') | |
conn:send('<tr>') | |
conn:send(' <td><font size=\"5\" face=\"arial, helvetica\" color=\"#ffffff\"><center>'..humi..'%</center></font></td>') | |
conn:send('</tr>') | |
conn:send('</table>') | |
conn:send('</center>') | |
conn:send('</body></html>') | |
end | |
conn:on("sent",function(conn) conn:close() end) | |
end) | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment