As the title says... Chat on...

User avatar
By promy
#13428 I am using an esp-01 to get my arduino nano a wireless connection. I want the esp-01 to pass commands (from the url) to the arduino and send the response back. Sending the command is no problem (just use print), but getting the response back seems a bit more tricky since i want to be able to catch other serial responses from the arduino as well (the arduino loops reading sensors, and when they change it activates a function on the esp-01 via serial (the arduino just prints functionname(parameters)

The lua code for the webpage i am using: (sorry for the dutch words in it)

Code: Select alluart.setup(0,9600,8,0,1,0)
hoogte_vijver = 1
srv=net.createServer(net.TCP) srv:listen(80,function(conn2)
   conn2:on("receive",function(conn2,payload)
      print(payload)
      if string.find(payload,"comm=") ~= nil then --we ontvangen een commando
         if string.find(payload,"vijver") ~= nil then
            print('H\n')  --commando H doorsturen, in arduino vraagt dit de hoogte op van de vijver
            teller = 0
            while hoogte_vijver < 2 do
               print ("wachten op data")
               if teller > 100  then break end
               teller = teller + 1
            end
            if hoogte_vijver == 1 then conn2:send("fout geen hoogte bekend") else conn2:send(hoogte_vijver) end
            hoogte_vijver = 1
         end
             end
   end)
   conn2:on("sent",function(conn2) conn2:close() end)
end)


When the command H is send to the arduino it measures the distance and sends the result back by changing the lua variable ( ex println(hoogte_vijver=60) in arduino)
However it seems that i cannot change the variable while i am in the while loop, so it does not work.

Can anyone tell me how to solve this?
User avatar
By cal
#13437
promy wrote:The lua code for the webpage i am using: (sorry for the dutch words in it)

...
Code: Select all            while hoogte_vijver < 2 do
               print ("wachten op data")
               if teller > 100  then break end
               teller = teller + 1
            end

...
Can anyone tell me how to solve this?


You seem to assume some magic here that changes "hoogte_vijver".
You send something to arduino using "print" but you don't have code to "receive" values from arduino.
Maybe "uart.on" is what you need to set that global variable?
I would also think about using a delay in the loop.
It should be possible to replace that loop with more event driven design but thats beyond me.

Carsten
User avatar
By promy
#13440 It's not magic, but when you don't have an uart.on (just setup the serial port) you can send lua code over the serial.
eg: just sending hoogte_vijver = 10 sets the variabele. You can even call functions like that.
However it seems that its not processed while in the loop.
I tried the version with uart.on, but it has the same problem: it seems that while lua is in the while loop, nothing else is processed?
User avatar
By cal
#13442
promy wrote:It's not magic, but when you don't have an uart.on (just setup the serial port) you can send lua code over the serial.
eg: just sending hoogte_vijver = 10 sets the variabele. You can even call functions like that.
However it seems that its not processed while in the loop.
I tried the version with uart.on, but it has the same problem: it seems that while lua is in the while loop, nothing else is processed?


Cool.
Sounds like magic for me ;-)
Is there some doc/info on that?
I expected a solution like viewtopic.php?f=24&t=2286.
Some low level (interrupt?) code has to get in charge nevertheless and needs to get "into" the loop.
I would try to enter a "delay" inside the code.
Does lua have/need something like "memory barriers" that makes changes of one piece of code visible to others?

Hmm thinking about that more:
The setting of the variable is just the communication with the lua interpreter like what I do using a terminal program.
Give it a chance to get through using a delay or IMHO even better without being tried: use callbacks.

Still learning,
Carsten