Post your best Lua script examples here

User avatar
By Sri1234
#62577 Hello all

I am new to uart programming with nodemcu and lua scripts. I am trying to do serial communication between esp8266 and avr microcontroller. For this i am using UART functions in nodemcu. I used uart.write command and got the response in terminal window. However when i tried to read the data sent from terminal software like Hterm, i was not able to make it work. I dont understand about the basics of the uart.on function. i could not see much of example codes for the same. It would be of great help to me if someone can explain how to do serial read using esp8266. I have attached the small code i am working on. I am writing a serial data to microcontroller and in turn need to read two bytes of incoming serial data and display it. Is there any command to check if there is data available at read buffer ?. kindly help
Code: Select alldevice = 5
uart.setup(0,9600,8,0,1,0)

tmr.alarm(1, 2000,1, function()
   
            A= adcintech0()
            print(A)
     
      end)

function adcintech0()
        D=0
        uart.write(0,2)
        uart.on("data", 1,
  function(data)
    print("receive from uart:", data)
    intech0low=data
    print("intech0low", intech0low)
       end, 0)
       
uart.on("data", 1,
  function(data)
    print("receive from uart:", data)
    intech0hi=data
    print("intech0high", intech0hi)
   
    end, 0)
    intech0=(intech0hi*255)+intech0low
     
return intech0
end
User avatar
By aterocana
#64882 I don't have time right now so I haven't tried your code. I'm working too in making esp8266 speaks with another microcontroller on serial UART.
Code: Select alluart.on("data", 1,
  function(data)
    print("receive from uart:", data)
    intech0hi=data
    print("intech0high", intech0hi)
   
    end, 0)

in this snippet from your code you should print, for each char you get from uart (the second parameter is 1), "receive from uart: %c". To debug it I'm using the multiplexed TX and RX on the board and the microUSB port. I open both with Minicom (in my case /dev/ttyUSB0 and /dev/ttyUSB1), opening ttyUSB1 in write mode. After each char the function should be triggered.
You can also specify the string which ends the buffer, in the documentation the example is with "\r" (return on the keyboard): https://nodemcu.readthedocs.io/en/maste ... rt/#uarton
Code: Select all-- when '\r' is received.
uart.on("data", "\r",
  function(data)
    print("receive from uart:", data)
    if data=="quit\r" then
      uart.on("data") -- unregister callback function
    end
end, 0)


Let me know if I've been helpful.
User avatar
By aterocana
#64898 HI, I couldn't replicate your code, but I'm working on making esp8266 talk with another microcontroller too.
Code: Select alluart.on("data", 1,
  function(data)
    print("receive from uart:", data)
    intech0hi=data
    print("intech0high", intech0hi)
   
    end, 0)

This part of code takes every 1 byte (1 char) and print "receive from uart: %c", it should work.
For example, this code:
Code: Select alluart.on("data", 5,
    function(data)
       print("received: :", data)
        if data=="hello" then
            print("hi")
        end
    end
, 0)

Every 5 char it prints them down. If the 5 chars are "hello" it answers back with "hi".
I hope I've been useful, let me know.