Post your best Lua script examples here

User avatar
By fcattaneo
#38631 Goodmorning everyone.

I have a esp 8266 board ( esp-201 ) with Nodemcu firmware..

I need to set some variabiles with value caming from serial port (@ standard 9600 baud speed, in ascii format )

A pic microcontroller send to esp-201 3 variabiles in this format :

x;y;z

where x, y and z can by integer or float ascii representation .

example :

127;1944;1.82

How i can do this in Lua language ?

Thanks at all,
Fabrizio.
User avatar
By jankop
#38707 Hi Fabrizio,
here is very smal demo for you, but you must add an eof character to string end. Example: 127;1944;1.82<CR>

Code: Select allbaud=9600
databits=8
parity=0
stopbits=1
echo=0
uart.setup( 0,baud,databits,parity,stopbits,echo )

    uart.on("data", "\r",
      function(data)
        print("receive from uart:", data)
   end, 0)
User avatar
By fcattaneo
#38712 Tanks jankop for your example.

Yes.. my software under PIC send eof for each line.. and there is 1 new line every 10 seconds. ( every line contain 3 number send as string in ascii )

like this :

127;14;1.08
... after 10 sec...
128;12;1.18
ecc.ecc.

....I'm trying to understand your code .. but in variable 'data' there is all string ?


I need to set variable for each number coming from serial.. each number coming as ascii string separated by ';' caracters.

sometings like this... ( not correct lua code.. )

tr = data[1] -- set variable tr with the first value coming from serial interfaces
print tr
-- i see 127

tp = data[2] -- set variable tp with the second value coming from serial interfaces
print tp
-- i see 14

ta = data[3] -- set variable ta with the last value coming from serial interfaces
print ta
-- i see 1.08

I did not understand .. :?:

Thanks in advance,
Fabrizio.
User avatar
By jankop
#38783
Code: Select all--***************************************************
EOF="\r"  -- set your EOF, here is <CR> !!!
--***************************************************
baud=9600
databits=8
parity=0
stopbits=1
echo=0
tr, tp, ta = 0
uart.setup( 0,baud,databits,parity,stopbits,echo )

uart.on("data", EOF,
  function(data)
    print("receive from uart:", data)
    start=1
   tr=tonumber(string.sub(data,start,string.find (data, ";", start, string.len (data))-1))
    start=string.find (data, ";", start, string.len (data))+1
   tp=tonumber(string.sub(data,start,string.find (data, ";", start, string.len (data))-1))
    start=string.find (data, ";", start, string.len (data))+1
   ta=tonumber(string.sub(data,start,string.find (data, EOF, start, string.len (data))-1))
    print("parsed data:", tr,tp,ta)
    data="nil"
  end, 0)
Attachments
pars.jpg