Close

Software on ESP8266

A project log for Minecraft server user count display

A simple display to show the kids (and me) how many that is logged in to our minecraft server.

kjetilKjetil 07/27/2015 at 21:060 Comments

The software on the ESP8266 module is simple too. It is an short script in LUA.

pl=1000
pin=4
tmr.alarm(0, 5000, 1, function()
    po=pl
    sk=net.createConnection(net.TCP, 0)
    sk:on("receive", function(sck, c)
      st,en= string.find (c, "Online: ")
      if (en ~= nil) then
        onl=string.sub (c,en+1,en+3)
        st,en= string.find (c, "Max: ")
        maon=string.sub (c,en+1,en+3)
        pl=1800-onl*1800/maon
	if (pl < 1000) then pl = 1000 end
	if (pl == 1800) then pl = 1900 end
      else
        if (pl < 2000) then print ("Offline") end
	pl=2000
      end
      if (pl ~= po) then
	print(onl,maon, pl)
      end
    end )
    sk:connect(80,"192.168.4.1")
    sk:send("GET /ESPing.php HTTP/1.1\r\nHost: 192.168.4.1\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
end)

gpio.mode(pin,gpio.OUTPUT)

tmr.alarm(1, 1000, 1, function()  
  gpio.write(pin,gpio.HIGH)
  tmr.delay(pl)
  gpio.write(pin,gpio.LOW)
end)

This script is uploaded as init.lua.

It consist of two main timers.

the first one is geting a webpage from my webserver every 5sek an calculates the server position in the variable "pl". There is also some if statements for spesial cases "pl <1000" makes everything abowe 10 players show as 10 players. "pl==1800" is if there is none players online to make this stand out. And if the search for the field "Online:" fails the pl is set to 2000 and pointing to offline.

the other timer send a pulse to the servo to set its position. A new pulse is sent every 1sek.

The calculations is ment to scale to the "Max:" player field, but is not testet with anything else than 20 players. And would probably fail, at least on the spesial cases the if statements is handeling. This is something to fix later.

I also want the ESP module to chec directly with the minecraft server, but when pinging the server from the module I always missed the first part of the response (the part with the number of players) With a network sniffer i did check an it was sent from the server. Tis is also somthing to fix later.

Tools used:

LUA Loader (to upload script to the ESP)

esp8266_flasher (to upload NodeMCU firmware)

And of course NodeMCU. I did use 0.9.6 version

Discussions