Simple standalone http server made with esp8266 and nodemcu

  Uncategorized

The following code is a standalone http server made with esp8266 and nodemcu written in lua.
The esp8266 is in wifi.SOFTAP mode (SoftAP is an abbreviated term for “software enabled access point.” This is software enabling a computer which hasn’t been specifically made to be a router into a wireless access point. It is often used interchangeably with the term “virtual router”.)

You can connect connect your smarthone with the esp8266 direct without any router or other network infrasructure. Just load the following code as init.lua on you eso8266 flash ram and reset.

print("WIFI control")

wifi.setmode(wifi.SOFTAP)
print("ESP8266 mode is: " .. wifi.getmode())
cfg={}
cfg.ssid="esp8266"
cfg.pwd="password"

if ssid and password then
  print("ESP8266 SSID is: " .. cfg.ssid .. " and PASSWORD is: " .. cfg.password)
end

wifi.ap.config(cfg)
print("ESP8266 AP IP now is: " .. wifi.ap.getip())

sv=net.createServer(net.TCP,30)
sv:listen(80,function(c)
c:on("receive", function(c, pl)
  print(pl)
  c:send("HTTP/1.1 200 OK\r\n") 
  c:send("Connection: close\r\n\r\n")
  c:send(" ")
  c:send(" ")
  c:send("

ESP8266 Wireless control setup

") majorVer, minorVer, devVer, chipid, flashid, flashsize, flashmode, flashspeed = node.info(); c:send("NodeMCU "..majorVer.."."..minorVer.."."..devVer.."

") bat = adc.readvdd33()/1000 c:send("Battery "..bat.."

") remain, used, total=file.fsinfo() c:send("File system info:
Total : "..total.." Bytes
Used : "..used.." Bytes
Remain: "..remain.." Bytes
") c:close() end) end)

The following circuit shows the min. configuration for your esp8266 to get the best stability.
ESP8266_improved_stability

If you ues a board with GPIO16 (Not ESP-01) you can connect the GPIO16 with REST to use the dsleep function. Otherwise your esp8266 will not wake up after execution of dsleep.