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

User avatar
By Ben17
#72768 Hi guys,
I am starting a project using two ESP8266-01 to transfer data via WiFi between 2 Arduinos. I know that this is not ideal, but I have to do it. The prblem is: after a week of googling, I am still having trouble programming in Lua, and my codes won't work. If you could just take a look at them and let me know what I'm doing wrong or point me in the right direction, I would appreciate it.

ESP CLIENT
wifi.sta.disconnect()
wifi.setmode(wifi.STATION)
wifi.sta.config("test","12345678") -- connecting to server
wifi.sta.connect()
uart.setup(0,9600,8,0,1,1)
tmr.alarm(1, 2000, 1, function()
if(wifi.sta.getip()~=nil) then
tmr.stop(1)
cl=net.createConnection(net.TCP, 0)
cl:connect(80,"192.168.4.1")
uart.on("data", "}", --the string ends on }
function(data)
tmr.alarm(2, 5000, 1, function()
cl:send(data)
end)
end, 0)

end
end)

ESP SERVER
wifi.setmode(wifi.STATIONAP);
wifi.ap.config({ssid="test",pwd="12345678"});
sv = net.createServer(net.TCP)
uart.setup(0, 9600, 8, uart.PARITY_NONE, uart.STOPBITS_1)-- setup UART1 i.e. pin GPIO2
sv:listen(80, function(conn)
conn:on("receive", function(conn, receivedData) --data received
print(receivedData)
uart.write(0, receivedData)
end)
conn:on("sent", function(conn)
collectgarbage()
end)
end)

tmr.delay(1000000)

P.S. I already checked and Client reads the serial from the first Arduino perfectly. So the problem is in the communication between the 2 ESPs.
User avatar
By marcelstoer
#72788 It would help if you put your code between [code][/code] tags (better legibility). I suspect you're doing this on an old NodeMCU version?

Ben17 wrote:my codes won't work


In what way? What happens?

I would stick to the code samples at https://en.wikipedia.org/wiki/NodeMCU#HTTP_request and https://en.wikipedia.org/wiki/NodeMCU#HTTP_server for starters. You can only send once the connection was established (i.e. in the 'connection' event callback). You're creating a memory leak because of closed upvalues in the callbacks ('conn' parameter in the callback); stick to the example to avoid that. 'collectgarbage' shouldn't be necessary and the use of tmr.delay is a real no-go here: https://nodemcu.readthedocs.io/en/dev/e ... g-tmrdelay
User avatar
By Ben17
#72794 Thank you very much! I had forgotten to include the trigger for the connection event. The snippets in Wikipedia helped because they are pretty straightforward. The samples I had previously gotten were quite messy.
I can only test the codes on Tuesday, but I think they will work just fine. Thanks again!