init on both ESPs (runs on soft restart or hard reset)
gets router DHCP assigned ipaddress
======================================================
--init.lua
print("set up wifi mode")
wifi.setmode(wifi.STATION)
wifi.sta.config("ret13x","XXXXXXXXXX")
--here SSID and PassWord should be modified according your wireless router
wifi.sta.connect()
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()== nil then
print("IP unavaiable, Waiting...")
else
tmr.stop(1)
print("Config done, IP is "..wifi.sta.getip())
print("ESP MAC Address: " .. wifi.sta.getmac())
--dofile("yourfile.lua")
end
end)
Data Sent from ESP1 to ESP2 (same for ESP2 to ESP1, diff. Ipddress below)
===============================================
uart.on("data", 4,
function(data)
local conn1 = nil
conn1=net.createConnection(net.TCP, 0)
--connect
conn1:connect(port,'Ipaddress_ESP2')
--send data
conn1:send("data")
--close connection
conn1:socket:close()
end, 0) --end function
Server on both ESPs
===================
-- start server (the ESP knows its the Ipaddress from init)
srv=net.createServer(net.TCP)
srv:listen(5666,function(conn) --port 5666
--receive from other ESP
conn:on("receive",function(conn,payload)
--show what was received
print(payload) -- out serial port on this ESP
end) -- conn:on("receive" ...
-- Close server connection and continue to Listen for request
conn:close() conn = nil
end) -- srv:listen(5666,function(conn)