More ESP8266 Lua Depression

Update: Fixed….. missing counter increment filled up Flash – I’ve asked Zeroday (the author of the ESP8266 if he can have a look at putting checks in to stop overflow.

I was beginning to think my new TCP/IP listener was the cause of being unable to even delete a file – but no.. Same issue with a fresh board.

If you’re into playing with the Lua Interpreter try this..

Put a new copy of the Interpreter on your board – so brand new, nothing else in there.

This is an attempt to write 3 variables into a file… then to read the file into a temporary place (as you can’t open 2 files at once)… and while doing that, amend one of the values.

Then delete the file and re-write it with the variable set… hence we get non-volatile variables.

Zeroday has suggested a better way – write to a second file and point to the one last updated – that’s a great idea and I’ll implement that later– but not until we get to the bottom of why this failed SPECTACULARLY.

Not only does it bomb, but even after a reboot you can’t re-run that or any other file operation – it needs the board to be reflashed. SURELY that should not happen in an interpreted environment.

So—here’s the code – please by all means test or comment on – or better yet, tell me I’ve done something wrong because that’s easier no doubt than an operating system fault? Once this is working (please…) I’ll publish a version using 2 files and an explanation of how to create and maintain non-volatile variables. What do you want those for? Example: simply App that turns a light on and off – do you REALLY want to forget it’s state after a reboot?

 

file.remove(“mtest.lua”)
file.open(“mtest.lua”,”w”)
    file.writeline(“test1=1”)
    file.writeline(“test2=2”)
    file.writeline(“test3=3”)
file.close()

newitem=”test2″
newvalue=”Rubbish”
   
counter=1
items={}
values={}

file.open(“mtest.lua”,”r”)
x=file.readline()
while x~=nil do
  items[counter],values[counter]=string.match(x, “(%w+)=(%w+)”)
  if items[counter]==newitem then values[counter]=newvalue .. “\n” end
  x=file.readline()
end
file.close()
file.remove(“mtest.lua”)
file.open(“mtest.lua”,”w”)
   counter=1
   while items[counter]~=nil do
     file.writeline(items[counter] .. “=” .. values[counter])
   end
file.close()
   
items=nil
values=nil
counter=nil

dofile(“mtest.lua”)

print(test1)
print(test2)
print(test3)

Advertisements

16 thoughts on “More ESP8266 Lua Depression

  1. some issues in your code:
    1, counter should be +1 in loops, otherwise it writes to a file many same lines and reboot.
    2, values[counter]=newvalue .. “\n”
    will produce test2=Rubbish in file, where Rubbish is not defined(nil).
    should be
    values[counter]='”‘..newvalue .. ‘”\n’
    produce test2=”Rubbish”.

    –code
    file.remove(“mtest.lua”)
    file.open(“mtest.lua”,”w”)
    file.writeline(“test1=1”)
    file.writeline(“test2=2”)
    file.writeline(“test3=3″)
    file.close()

    newitem=”test2″
    newvalue=”Rubbish”

    counter=1
    items={}
    values={}

    file.open(“mtest.lua”,”r”)
    x=file.readline()
    while x~=nil do
    items[counter],values[counter]=string.match(x, “(%w+)=(%w+)”)
    if items[counter]==newitem then values[counter]='”‘..newvalue .. ‘”\n’ end
    x=file.readline()
    counter=counter+1
    end
    file.close()
    file.remove(“mtest.lua”)
    file.open(“mtest.lua”,”w”)
    counter=1
    while items[counter]~=nil do
    file.writeline(items[counter] .. “=” .. values[counter])
    counter=counter+1
    end
    file.close()

    items=nil
    values=nil
    counter=nil

    dofile(“mtest.lua”)

    print(test1)
    print(test2)
    print(test3)

  2. You are absolutely right – and thanks for that – I would suggest that there is an issue when simple mistakes like that can result in having to reflash the interpreter. Worth putting in some checks? I’m onto the next stage now and will comment back in here. Thank you.

  3. the reboot maybe caused by a watch dog, because the write to file forever.
    not sure which one fires first. the watch dog or flash used up.
    when writing to flash file system, a suddenly reboot will cause problem.
    will re-check this part. Thank you for testing.

  4. If it is possible to check – also to have a function to say how much left… but yes I realised a minute ago that the flash would just fill up. I’d love to know how much I have available. Working on 2-file code now. This is great – you’re doing a great job.

  5. And that brings me to another issue – for me to worry about but feel free to help… all AMENDED variables are going to get saves as strings with quotes as things stand. Hmmmmmmmmmmm.

  6. ALMOST – NEARLY THERE…

    Ok, here is the version with two files and a selector – at the top for a demo I’ve set the selector to 0 and clearly the stuff would be in a function but for the purposes of demo.. here it is – BUT at the very last minute, and error. Can you spot? It’s putting something wrong in globals2

    file.remove(“globals1.lua”)
    file.open(“globals1.lua”,”w”)
    file.writeline(“test1=1”)
    file.writeline(“test2=2”)
    file.writeline(“test3=3”)
    file.close()

    file.remove(“globals2.lua”)
    file.open(“globals2.lua”,”w”)
    file.writeline(“test1=1”)
    file.writeline(“test2=2”)
    file.writeline(“test3=3”)
    file.close()

    file.remove(“whichglob.lua”)
    file.open(“whichglob.lua”,”w”)
    file.writeline(“globnum=0”)
    file.close()

    dofile(“whichglob.lua”)

    newitem=”test2″
    newvalue=”Rubbish”

    counter=1
    items={}
    values={}

    if globnum==0 then file.open(“globals1.lua”,”r”) else file.open(“globals2.lua”,”r”) end
    x=file.readline()
    while x~=nil do
    items[counter],values[counter]=string.match(x, “(%w+)=(%w+)”)
    if items[counter]==newitem then
    if type(newvalue)==string then
    values[counter]='”‘..newvalue .. ‘”\n’
    else
    values[counter]=newvalue .. ‘\n’
    end

    end
    counter=counter+1
    x=file.readline()
    end
    file.close()
    if globnum==1 then file.open(“globals1.lua”,”w”) else file.open(“globals2.lua”,”w”) end

    file.open(“mtest.lua”,”w”)
    counter=1
    while items[counter]~=nil do
    file.writeline(items[counter] .. “=” .. values[counter])
    counter=counter+1
    end
    file.close()

    if whichglob==0 then whichglob=1 else whichglob=0 end
    file.open(“whichglob.lua”,”w”)
    file.writeline(“whichglob=” .. whichglob)
    file.close()

    items=nil
    values=nil
    counter=nil

    dofile(“globals1.lua”)

    print(test1)
    print(test2)
    print(test3)

    dofile(“globals2.lua”)

    print(test1)
    print(test2)
    print(test3)

  7. Oh the result was..

    > dofile(“globals1.lua”)
    > print(test1)
    1
    > print(test2)
    2
    > print(test3)
    3
    > dofile(“globals2.lua”)
    globals2.lua:4: unexpected symbol near ‘?’
    > print(test1)
    1
    > print(test2)
    2
    > print(test3)
    3
    >

  8. YYYYYYEEEESSSSS

    Just needs wrapping up in a function and it works – thank you Zeroday.

    Here is the working test for anyone interested.. the question is – will that and all my other code fit – we’ll soon find out…

    file.remove(“globals1.lua”)
    file.open(“globals1.lua”,”w”)
    file.writeline(“test1=1”)
    file.writeline(“test2=2”)
    file.writeline(“test3=3”)
    file.close()

    file.remove(“globals2.lua”)
    file.open(“globals2.lua”,”w”)
    file.writeline(“test1=1”)
    file.writeline(“test2=2”)
    file.writeline(“test3=3”)
    file.close()

    file.remove(“whichglob.lua”)
    file.open(“whichglob.lua”,”w”)
    file.writeline(“globnum=0”)
    file.close()

    dofile(“whichglob.lua”)

    newitem=”test2″
    newvalue=”Rubbish”

    counter=1
    items={}
    values={}

    if globnum==0 then file.open(“globals1.lua”,”r”) else file.open(“globals2.lua”,”r”) end
    x=file.readline()
    while x~=nil do
    items[counter],values[counter]=string.match(x, “(%w+)=(%w+)”)
    if items[counter]==newitem then
    if type(newvalue)==”string” then
    values[counter]='”‘..newvalue .. ‘”\n’
    else
    values[counter]=newvalue .. ‘\n’
    end

    end
    counter=counter+1
    x=file.readline()
    end
    file.close()
    if globnum==1 then file.open(“globals1.lua”,”w”) else file.open(“globals2.lua”,”w”) end
    counter=1
    while items[counter]~=nil do
    file.writeline(items[counter] .. “=” .. values[counter])
    counter=counter+1
    end
    file.close()

    if whichglob==0 then whichglob=1 else whichglob=0 end
    file.open(“whichglob.lua”,”w”)
    file.writeline(“whichglob=” .. whichglob)
    file.close()

    items=nil
    values=nil
    counter=nil

    dofile(“globals1.lua”)

    print(test1)
    print(test2)
    print(test3)

    dofile(“globals2.lua”)

    print(test1)
    print(test2)
    print(test3)

  9. Also, if you are parsing, rather than the following loops:

    x=file.readline()
    while x~=nil do
    — …
    x=file.readline()
    end

    Have a go at using the readline function as an iterator:

    for x in file.readline do
    — …
    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