ESP8266 Monday Woes and a LIGHT at the end?

I’m not having a lot of luck with any of the firmware implementations at the minute for the little ESP8266 board. ESPRESSIF have gone quiet with one possible exception.

I sent queries about the attempt at a replacement for the Expressif v9.2.2 code – nothing all weekend and nothing this morning.

There is an IGGR update to his AT command set but I had problems with that – awaiting a response..

And then there is LUA – there is a new slight update out today (just blow the one firmware file in the folder onto your board) – NOT a fix for RAM issues but read on – you may find it worthwhile.

In my example I have a mobile phone app which sends 4 status requests GO1 GO2 GO3 and GO4 constantly when the App is displaying –  it’s asking for the status of 4 inputs – and expects a “1” or a “0” (in all cases followed by a single return “/n”).  It also might get a request to turn one of those outputs on or of. Requests are simple “YES1”, “NO1”, “YES2”,”NO2” etc. again followed by “\n” – I’ve used this kind of approach with 200ms polling for responses – for much of my home control using hardwired ETHERNET and my goal has always been to replicate this on the ESP8266  (I also need to grab the time but given RAM issues we’ll park that for now.

I tried the example of a port listener on port 4000 for one simple dummy output.
 

  l1=”0\n”
     sv=net.createServer(net.TCP, 90)    — 30s time out for a inactive client
     sv:listen(4000,function(c)
      c:on(“receive”, function(sck, pl)
                    print(pl)
                    if (pl==”GO1\n”) then c:send(l1)
                    elseif pl==”YES1\n” then l1=”1\n” c:send(“OK\n”)
                    elseif pl==”NO1\n” then l1=”0\n” c:send(“OK\n”)
                    else c:send(“0\n”)               
                    end
                    end)
      end)

Absolutely worked a TREAT over several minutes – returning the right information to my mobile phone All – and that’s on polls every 200ms. GREAT.  So I changed the code to handle 4 “devices”..

    l1=”0\n”
    l2=”0\n”
    l3=”0\n”
    l4=”0\n”
     sv=net.createServer(net.TCP, 90)    — 30s time out for a inactive client
     sv:listen(4000,function(c)
      c:on(“receive”, function(sck, pl)
                    print(pl)
                    if (pl==”GO1\n”) then c:send(l1)
                    elseif pl==”GO2\n” then c:send(l2)
                    elseif pl==”GO3\n” then c:send(l3)
                    elseif pl==”GO4\n” then c:send(l4)       
                    elseif pl==”YES1\n” then l1=”1\n” c:send(“OK\n”)
                    elseif pl==”NO1\n” then l1=”0\n” c:send(“OK\n”)
                    elseif pl==”YES2\n” then l2=”1\n” c:send(“OK\n”)
                    elseif pl==”NO2\n” then l2=”0\n” c:send(“OK\n”)
                    elseif pl==”YES3\n” then l3=”1\n” c:send(“OK\n”)
                    elseif pl==”NO3\n” then l3=”0\n” c:send(“OK\n”)
                    elseif pl==”YES4\n” then l4=”1\n” c:send(“OK\n”)
                    elseif pl==”NO4\n” then l4=”0\n” c:send(“OK\n”)
                    else c:send(“0\n”)               
                    end
                    end)
      end)

WELL – The interpreter never got to that last line in bold – instead – the ESP-01 exploded – sorry, rebooted – I tried this several times and it always reboots in the same place.  I’m thinking.. RAM!

And so here was my guess and a helpful fellow in the forums came to the same conclusion!!  I figured that as the code was being built up to be executed, it was being built up in RAM – and there’s a PAINFUL shortage of RAM – I’d seen a recommendation elsewhere to store things as files and run them from there…. using dofile   ie dofile(“fred.lua”)

So I stored the lot as a file (always ensure you remove any previous version first)

>
> file.remove(“mylistener.lua”)
> file.open(“mylistener.lua”,”w”)
> file.writeline([[l1=”0\n”]])
> file.writeline([[l2=”0\n”]])
> file.writeline([[l3=”0\n”]])
> file.writeline([[l4=”0\n”]])
> file.writeline([[sv=net.createServer(net.TCP, 90) ]])
> file.writeline([[sv:listen(4000,function(c)]])
> file.writeline([[c:on(“receive”, function(sck, pl) ]])
> file.writeline([[print(pl) ]])
> file.writeline([[if (pl==”GO1\n”) then c:send(l1) ]])
> file.writeline([[elseif pl==”GO2\n” then c:send(l2)]])
> file.writeline([[elseif pl==”GO3\n” then c:send(l3)]])
> file.writeline([[elseif pl==”GO4\n” then c:send(l4) ]])
> file.writeline([[elseif pl==”YES1\n” then l1=”1\n” c:send(“OK\n”) ]])
> file.writeline([[elseif pl==”NO1\n” then l1=”0\n” c:send(“OK\n”) ]])
> file.writeline([[elseif pl==”YES2\n” then l2=”1\n” c:send(“OK\n”) ]])
> file.writeline([[elseif pl==”NO2\n” then l2=”0\n” c:send(“OK\n”) ]])
> file.writeline([[elseif pl==”YES3\n” then l3=”1\n” c:send(“OK\n”)]])
> file.writeline([[elseif pl==”NO3\n” then l3=”0\n” c:send(“OK\n”)]])
> file.writeline([[elseif pl==”YES4\n” then l4=”1\n” c:send(“OK\n”)]])
> file.writeline([[elseif pl==”NO4\n” then l4=”0\n” c:send(“OK\n”)]])
> file.writeline([[else c:send(“0\n”)]])
> file.writeline([[end]])
> file.writeline([[end)]])
> file.writeline([[end)]])
> file.close()

That worked – looked like my RAM theory was correct.  All that remained was to say dofile(“mylistener.lua”)

So I added that to my startup..

file.remove(“init.lua”)
file.open(“init.lua”,”w”)
file.writeline([[print(“Pete’s LUA module 0.1”)]])
file.writeline([[tmr.alarm(4000, 0, function() dofile(“thelot.lua”) dofile(“mylistener.lua”) end )]])  
file.close()

The bit in bold is the bit I added. THELOT.LUA is another file that sets up the connection. And THAT worked.

Note the added dofile on the right.  Works – from power-up the board is now a listener able to make effective responses.!

I have hammered this to DEATH – I turned the phone off, swapped apps, left it off for 5 minutes at a time…unbelievably this will NOT fall over.

But what I really need is for the board to get one of those requests in – and then instead of sending out a fixed response I want it to wait for the serial to provide that response OR send a message to say no response was forthcoming!!! That’s so I can use it with an Arduino… flashing a couple of lights on the little board is FUN and if we get temperature sensing code for the little Dallas chips, a light and a sensor might indeed be all we need for some jobs – but the time will come – some of my setups have MANY I/O combinations – an Arduino would be the perfect companion here.  That is my next challenge – not a clue right now how to do that….  so despite the CHRONIC lack of RAM – and the DESPERATE NEED of this interpreter to crash… it DOES look as if it is possible to make it work… I am, right now, unable to crash this particular example!!!

Of course the REAL world tool will need to be WAY more complex… what if there is no connection? What if one has not been setup – we’ll need a screen to list the access points and select one… now THAT bit could be fun right now with the shortage of RAM and the heap management issues. BUT it’s a start!!! and all of this at 9600 serial (of course in a working scenario there would be no need for serial display).

And what of the memory bug? Ram slowly going down the tubes?  I’ve held off on writing this blog until I’d given it a hammering – the loop has run thousands of times now… still working. For the first time since I started with this little board and at times I’ve been THAT far off throwing it in the fire…   I think we might be onto something here. Much to be done but it’s a start.

05:06PM – There is a memory loss going on here if I turn the phone app on and off… repeatedly.. check this out…. the number is the amount of RAM left in bytes.

8456
GO1

8456
GO2

8456
GO3

8456
GO4

8456
GO1

8456
GO2

6112
GO1

6112
GO3

6112
GO4

6112
GO2

6112
GO1

6112
GO3

6112

Advertisements

2 thoughts on “ESP8266 Monday Woes and a LIGHT at the end?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s