logo elektroda
logo elektroda
X
logo elektroda

[Solved] [ESP8266] How to format and process strings in LUA on ESP8266?

piterek-23 6285 52
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 15714384
    piterek-23
    Level 33  
    I am writing myself a program for the ESP-12 that is supposed to receive data from the UART, "slice" it and send it on over WiFi....

    I'm stuck with formatting the data and can't move on ;(

    To the point...
    The data I send to the ESP looks more or less like this:
    Code: VB.net
    Log in, to see the code

    the a, b, c and ":" is constant, and "123", "456", "789" are my variables I want to work on. The variables are 1 to 7 digits long sometimes there is a "-" sign

    How do I "slice" the string and substitute each variable further in the code?
    I think it was best to cut everything between the constant "a:" and the space, but how do I do that? :(

    If I do it this way I will send to ESP:
    Code: VB.net
    Log in, to see the code

    and in ESP:
    Code: Lua
    Log in, to see the code

    then the variables must be of fixed length, but this is not the case :(

    Thanks in advance for your help.
  • ADVERTISEMENT
  • #2 15714989
    piotr411
    Level 22  
    Variables are 1 to 7 digits long sometimes there is a "-" sign

    This notation I do not understand.
    Are there always or 3 characters after ":"? or is there a different number, from 1 to 7 characters?
    i.e. a:1 b:1234567 c:456
    is the array to be written string or dec?
  • #3 15714991
    piterek-23
    Level 33  
    Unfortunately, but there is a different number/length of characters (digits)
    This is temperature, pressure, etc. There is a minimum of one digit and a maximum of seven
  • #4 15714993
    piotr411
    Level 22  
    It doesn't matter how many there are, I'm just asking if there is a varying number, give all cases. Give some example frames
  • #5 15714996
    piterek-23
    Level 33  
    Yes, varying amounts. Examples of variables:
    - temperature from "-30.0" to "40.0"
    - pressure from "990.00" to "1100.00"
    - humidity from "1" to "100"

    i.e. ESP will receive e.g:
    temp: -25 pressure: 997.99 humidity: 55
  • #6 15714998
    piotr411
    Level 22  
    I think we have misunderstood each other, state exactly the frame you are sending to the ESP
  • ADVERTISEMENT
  • #7 15715000
    piterek-23
    Level 33  
    piotr411 wrote:
    I don't think we've misunderstood each other, specify exactly the frame you're sending to the ESP

    piterek-23 wrote:
    temp_external: -25 pressure: 997.99 humidity: 55
  • #8 15715002
    piotr411
    Level 22  
    ok, in the morning I will write an example
  • #9 15715007
    piterek-23
    Level 33  
    Thanks ;)
    Or maybe it is possible to send the data to ESP in a different way so that it would be easier to "process" it in LUA?

    I work with ESP with ATmega (BASCOM) and there I send the data like this:
    Code: VB.net
    Log in, to see the code
  • #10 15715012
    piotr411
    Level 22  
    Yes, it is easier as the frame will be in Bascom in this form:

    print variable1,",",variable2,", ", variable3, "x"
    i.e. you divide the variables by a comma, or another character, always the same.
    I just don't know why you need an AVR for this? isn't it better to do everything in ESP?
  • #11 15715013
    piterek-23
    Level 33  
    And this is no longer a problem ;)
    So I should send like this:
    Code: VB.net
    Log in, to see the code

    so that ESP receives:
    Code: Lua
    Log in, to see the code

    without spaces yes?

    piotr411 wrote:
    Only I don't know why to add AVR to it? wouldn't it be better to do everything in ESP?

    There will be a total of 15 variables
    AVR will do many other things ;)
  • ADVERTISEMENT
  • #12 15715017
    piotr411
    Level 22  
    Rather, this is the notation I had in mind

    (-25,55,997.99) or such (-25 55 997.99)

    No unnecessary characters sent to ESP

    Further I don't know why AVR, ESP is faster and has more memory, it can do much more, why complicate your electronics.
  • #13 15715018
    piterek-23
    Level 33  
    I need 6xADC i2c 8xGPIO :-)
    I like ESP, but in LUA I won't be able to do it all-C I don't know, and I understand BASCOM as-is and that's why this choice.
  • Helpful post
    #14 15715027
    piotr411
    Level 22  
    Please :

    Code: Lua
    Log in, to see the code


    I haven't checked the program on ESP, I wrote on a tablet in LUA Comet.
    I think Comet is very compatible with LUA ESP.

    Off topic, if you know Basic, why are you writing in LUA?
    ESP also has a very cool ESP Basic, this program is very easy to port to Basic.
    Link
  • Helpful post
    #15 15715189
    JacekCz
    Level 42  
    I assume you have read this: Link and then expand to adjacent links
  • Helpful post
    #16 15715206
    Anonymous
    Anonymous  
  • #17 15715347
    piterek-23
    Level 33  
    piotr411 wrote:
    The program on ESP I have not checked, I wrote on a tablet in LUA Comet.
    I think Comet is very compatible with LUA ESP.

    Thank you for the code. Now trying to somehow digest and understand it ;)
    piotr411 wrote:
    Behind the topic, if you know Basic, why are you writing in LUA?
    ESP also has a very cool ESP Basic, this program is very easy to port to Basic.
    [url=]http://www.esp8266basic.com[/url]

    I so honestly didn't know there was an ESP Basic ;)
    I have seen most things written in LUA or C. There are also projects that use AT commands (but that didn't work for me)
    I stayed with LUA because somehow I started to understand it.
    JacekCz wrote:
    I assume you have read this: Link and then expand to neighbouring links

    I had not seen this. Thank you for the link.
    turlam.dropsa wrote:
    People have invented something like I2C so connecting ADC and IO expanders is no problem for ESP.

    You're 100% right, but it's quicker to do something on AVR + BASCOM than to write code for ESP + all the devices connected to it.

    I know I'm asking about simple things. Many Things can be done better, differently.
    I am a Sunday electronics amateur and I do as I can, and I can't do too much ;)
    For the moment I'll stay with what I have, and as I grow I'll move onto ESP and more ambitious projects. You have to start somewhere.
  • Helpful post
    #18 15715399
    Anonymous
    Anonymous  
  • #19 15715449
    piotr411
    Level 22  
    In this example Link

    Array entry starts with ":" and ends with " "(space)
    It doesn't matter how many variables or how long, it was meant to be universal

    Now a simple example, also universal, it doesn't matter how many variables and how long they are, but you have to send the ESP the variables themselves, separated by a space, according to the formula"

    Bascom:
    Code: VB.net
    Log in, to see the code


    Lua:
    Code: Lua
    Log in, to see the code



    niveasoft - you have assumed too many constants in the example ;)


    JacekCz wrote:
    I assume you have read this: Link and then expand to adjacent links


    It is not easy to switch from Basic to LUA on the fly, there are too many characters in LUA for instructions that are not present in Basic. It is easier from C to LUA, because it is almost C but on a higher level. Therefore, beware of applications where high performance is needed and LUA does not provide for assembler insertions ;) To top it off, LUA ESP is extremely poorly optimised, which may not be feasible for certain projects. The added modules in LUA serve as in ARDUINO for fun, i.e. nothing serious. If one is to seriously exploit the potential of ESP then only Eclipse Mars with SDK and the C language ;) remains

    There are many ways to perform character operations in LUA. The next example will be, writing sequentially to an array of just numbers allocated with any characters, letters etc...in any number of characters. Here LUA has very wide possibilities, there is a multitude of operators available. After thinking about it for a while longer, I found 9 ways to solve the problem;) I will add more in my spare time. They will no longer be simple to interpret on Basic
  • ADVERTISEMENT
  • Helpful post
    #20 15715561
    Anonymous
    Anonymous  
  • #21 15716495
    piterek-23
    Level 33  
    piotr411 wrote:
    Now a simple example, also universal, does not matter the number of variables and their length, but to the ESP you must send the variables themselves, separated by a space, according to the formula."

    Bascom:

    Code: vbnet [expand] [select all]
    a$="-25 99.999 55" '- here we have 3 variables, you can give them 7 or 100 it doesn't matter, each one will be sequentially written into the array
    Print a$


    Lua:

    Code: lua [expand] [select all]
    str = "-25 55 99.999" -- LUA
    tab ={} a=0

    for i in string.gmatch(str, "%S+") do
    a=a+1
    tab[a]=i
    end

    -- here you read what you have written to the array
    print ("Number of variables found in string str:",a)
    print ("tab[1]) -- a 1 in the array means reading the data from position 1 -there you have written -25

    I'm trying to do this method and flop....
    If I send seven variables from the AVR it is OK, but if there are more then nothing ;(
    The ESP in the console is chugging along:
    Code: Bash
    Log in, to see the code
  • Helpful post
    #22 15716520
    JacekCz
    Level 42  
    well, unfortunately regular expressions cost money (gmatch, gsub),
    I sometimes argue with programmers who love to use them - the argument is not always about performance (on hyper machines), but e.g. wrong when you should use a mini-parser (the parser "understands" what it strips, w.r. just cuts and finds) . This is so outside the thread


    try the part of the article where it says "PHP-like explode". Lua itself has a pretty good set of string processing functions (it doesn't have the popular "split") and this article presents various approaches

    In most languages the "split" function/method does not use the regular expression engine, but I guess some do.

    EDIT I am aware that my advice has a bit of the rabbi who Ick cured a sick goat at a distance.... :)
  • #23 15716617
    piotr411
    Level 22  
    Have you tested the first example?
    It is less appetising to remember.

    A bit strange, with me after typing 20 variables it works and there is still, a free 17KB of memory.

    What Firmware do you have?
  • #24 15716688
    piterek-23
    Level 33  
    JacekCz wrote:
    EDIT I am aware that my advice has a little of the rabbi who Ick cured a sick goat at a distance.... :) [/i]

    And yes and no... For the moment I didn't understand much of what you said, but I often have that I'll ask for help and someone will hint at something and I don't really understand it, but after a while I go back to the statement in question and the glare of "I know what that person was trying to convey" :)
    piotr411 wrote:
    First example have you tested?
    It's less appetizing by memory.

    Not yet because I don't quite know how to use it :(
    piotr411 wrote:
    Some strange, at my place after entering 20 variables it works and there is still, free 17KB of memory.

    Then maybe (surely) I'm doing something wrong? :)

    Maybe I'll show the whole thing, it'll be easier....
    init.lua
    Code: Lua
    Log in, to see the code

    avr.lua
    Code: Lua
    Log in, to see the code

    piotr411 wrote:
    What Firmware do you have?

    Code: Bash
    Log in, to see the code
  • #25 15716756
    piotr411
    Level 22  
    There is something to optimise, a lot of memory is taken up by your text, think of one variable substituted and in part only modified.
  • #26 15716836
    piterek-23
    Level 33  
    @piotr411 Can you tell me how to use your code (post #14) i.e. what data to send from the AVR and how to substitute my uC01-uC20?
  • #27 15717620
    piotr411
    Level 22  
    Exactly the same as with example no. 2
    a=(data) - - what comes from uart

    Note the formatting of the variable data
    Recognition starts with : and ends with a space
    The rest is unchanged everything goes into the array.

    Get rid of the text from the file because it takes up too much memory. Write what is repeated to the function and refer to it. This will save a lot of RAM space. Flash has a lot of 4MB and you don't need to save here, but take care of the RAM because there will be messages like the one above. LUA leaves 20kb and so you have to combine to fit.

    instead of:
    SensorIp01 = "192.168.222.1"


    replace:
    SensorIp00 = "192.168.222."
    a, the following:
    SensorIp01 = "1"
    etc.......
    at the end you will combine both variables.
    Wherever you have repetitive text data do the same.

    This eats up all the memory :
    .. " HTTP/1.1Host: NettempIPHost"
    .. "Authorization: basic YWRtaW46MTk4MQ=="
    ... "Connection: ke ep-aliveAccept: */*")
    divide this into 3 variables and link when you send i.e. the easiest way is to refer to a function e.g:

    Code: Lua
    Log in, to see the code


    Originally it took 14kb, after this change it's already 3kb, and you can still rip off 1kb.

    Code: Lua
    Log in, to see the code
  • #28 15717764
    piterek-23
    Level 33  
    Oh dear... Thanks a lot for such explanations and codes :)
    I'm already getting to work.
  • Helpful post
    #29 15717828
    piotr411
    Level 22  
    Perhaps ;) more


    Code: Lua
    Log in, to see the code


    Beyond that, I don't think the programme is fully thought through.
    For what purpose do you check all the sensors at once?
    Some environmental conditions occur more slowly and there is no need to, ask yourself so often.

    The programme can still be heavily shortened and given more intelligence.
    Watching now what you want to build.
    I still say the AVR is unnecessary and only complicates the project.

    Added after 22 [minutes]:

    So now we are at 2kb


    Code: Lua
    Log in, to see the code
  • #30 15717992
    piterek-23
    Level 33  
    piotr411 wrote:
    Beyond that, I don't think the programme is entirely well thought out.
    For what purpose are you checking all the sensors at once?
    Some environmental conditions occur more slowly and there is no need to, ask yourself so often.

    You are right, but I have left such optimisation of the reading frequency for later.
    piotr411 wrote:
    The program can still be heavily shortened and given more intelligence.
    Watching now what you want to build.
    I still maintain that the AVR is unnecessary and only complicates the project.

    If I were to start writing this in LUA this ESP would have to have with 1GB of memory :D

    I don't know myself what I am doing wrong anymore because by doing according to your instructions I get:
    Code: Bash
    Log in, to see the code


    The code looks like this:
    Code: Lua
    Log in, to see the code


    Changed:
    Code: Bash
    Log in, to see the code

    to:
    Code: Bash
    Log in, to see the code

    because I was getting the message:
    Code: Bash
    Log in, to see the code

    supposedly I can send whatever I want, it does not necessarily have to be an IP

    Added after 1 [hour] 21 [minutes]:

    hmmm... tries your first code and uC sends:
    Code: Bash
    Log in, to see the code

    ESP doesn't shout anything that there is something wrong, but it doesn't send the data any further either :(

    Added after 5 [hours] 16 [minutes]:

    I think I need to look for an ESP with more memory....

    What I did this time was to get rid of avr.lua and move everything to init.lua because I thought maybe something would be freed up here or something, but of course nothing.
    At most it sends me 9 data and no more is possible :(

    init.lua
    Code: Lua
    Log in, to see the code
ADVERTISEMENT