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

User avatar
By scargill
#3277 I may be missing something here but...

I have a socket listener running - so that a phone all can send queries to the ESP-01 and I can respond accordingly. It works very well but of course the ESP-01 is very limited in what it can do in terms of I/O.

So what I'd like to do is take in text from the remote APP - and send it out of the serial line - I can do that already... but then I want any input from the serial line to be send straight out to the remote app over TCP/IP, rather than the LUA interpreter interpreting what I send into the serial line.

I'm sure it's simple but I have no idea how to do this.

So - my example...(without worrying about files at this point - just the basic function

sv=net.createServer(net.TCP, 30) -- that 30 seconds timeout doesn't seem to do anything
sv:listen(4000,function(c)
c:on("receive", function(sck, pl)
print(pl)
c:send("My response but how to wait for and grab this from the serial line???")
end)
end)

I tried this demo example from the web but the interpreter does not seem to like it....

io.write("continue with this operation (y/n)?")
answer=io.read()
if answer=="y" then
--(put what you want it to do if you say y here)
elseif answer=="n" then
--(put what you want to happen if you say n)
end

and this - both fail

local answer
repeat
io.write("continue with this operation (y/n)? ")
io.flush()
answer=io.read()
until answer=="y" or answer=="n"
User avatar
By gerardwr
#3281
scargill wrote:So what I'd like to do is take in text from the remote APP - and send it out of the serial line - I can do that already... but then I want any input from the serial line to be send straight out to the remote app over TCP/IP, rather than the LUA interpreter interpreting what I send into the serial line.


Hi,
I asked zeroday but reading input from the serial port is currently not supported.
http://www.esp8266.com/viewtopic.php?f=23&t=588&hilit=serial+input&start=50#p2716

Please support this "new feature" topic.
http://www.esp8266.com/viewtopic.php?f=21&t=614
User avatar
By ThomasW
#3321 Agreed, reading/writing serial from lua would be great..

Meanwhile, maybe the following hack could work for you as a starting point:
Code: Select all-- device on the serial end gets "xxxx:yyyy" where xxxx is the id of
-- the connection and yyyy is the message/command which comes in over the net
-- device should answer with 'MSG("xxxx:whatever")\n'
-- this goes directly to the lua-interpreter, so be careful

SOCKTAB={}  -- global table that holds id:socket pairs

-- device will invoke the following function:
MSG = function(cmd)
    local forID,msg = cmd:match("(.-):(.*)$") -- extract target and message
    -- processing the message from the device goes here (if needed)

    -- finally, send something back over the net:
    if  SOCKTAB[forID]
    then
        SOCKTAB[forID]:send(msg)
    else
        print("attempt to write to closed socket") -- remove this for production!
    end
end

function setup_listener(conn,ID)
    conn:on("receive",function(conn,payload)
        print(ID .. ":" .. payload) -- send "xxxxx:" + payload over serial
    end)
    conn:on("disconnection",function(conn)
        SOCKTAB[ID]=nil     -- cleanup global table
    end)
end

srv=net.createServer(net.TCP,180)
srv:listen(80,function(conn)
    local ID=tostring(tmr.now())    -- assign an ID to this connection
    SOCKTAB[ID]=conn                -- and save in global table
    print("opening " .. ID)         -- remove this for production!
    setup_listener(conn,ID)
end)


Thomas
User avatar
By RichardS
#3347 Very interesting...... need to try that one.

Richard.