TCP Communication through nodemcu esp8266

1 year 9 months ago #3745 by Dr. Sana
Hi!
My project is based on two esp8266 module labview app running on PC. I have to establish TCP Communication between Labview and the two esp8266 modules without using external router. In my present network one of my nodemcu is configured in AP mode and the other is in station mode and obviously my PC (server) is another station which is listening for some tcp request. The labview (software running on PC) is making tcp connection with the one in AP mode but not with one in station mode. So anyone who could guide me where the problem is and what could be the solution.
The esp8266 modules are configure in lua, Code is given below:

Access Point

wifi.setmode(wifi.SOFTAP)
cfg={}; cfg.ssid="myssid"; cfg.pwd="87654321"; wifi.ap.config(cfg)
dhcp_config ={}
dhcp_config.start = "192.168.4.1"
wifi.ap.dhcp.config(dhcp_config)
wifi.ap.dhcp.start()
conn=net.createConnection(net.TCP, 0)
conn:on("connection", function(connected)
print("$TCP^")
uart.on("data",'#', function(data)
conn:send(data);
end, 0)
end ,0)
conn:on("receive", function(conn, payload)
print(payload)
end)
conn:on("disconnection",function(disconnected)
print("$DIS^")
node.restart()
end)

Station:

uart.setup(0,9600,8,0,1)
print("ready to start")
print("Connecting to WiFi access point...")
wifi.setmode(wifi.STATION)
wifi.sta.config("myssid","87654321")
wifi.sta.autoconnect(1)
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip() == nil then
print("Waiting for IP address...")
else
tmr.stop(1)
print("WiFi connection established, IP address: " .. wifi.sta.getip())
end
end)
print("Open connection...")
conn=net.createConnection(net.TCP, 0)
--port and ip at which labview app is listening
conn:connect(4001,'192.168.4.2')
conn:on("connection", function(connected)
print("#")
uart.on("data",'#', function(data)
conn:send(data);
end, 0)
end ,0)
conn:on("receive", function(conn, payload)
print(payload)
end)
conn:on("disconnection",function(disconnected)
print("*")
end)

Please Log in or Create an account to join the conversation.

1 year 9 months ago #3746 by EasyIoT

Dr. Sana wrote: Hi!
My project is based on two esp8266 module labview app running on PC. I have to establish TCP Communication between Labview and the two esp8266 modules without using external router. In my present network one of my nodemcu is configured in AP mode and the other is in station mode and obviously my PC (server) is another station which is listening for some tcp request. The labview (software running on PC) is making tcp connection with the one in AP mode but not with one in station mode. So anyone who could guide me where the problem is and what could be the solution.
The esp8266 modules are configure in lua, Code is given below:

Access Point

wifi.setmode(wifi.SOFTAP)
cfg={}; cfg.ssid="myssid"; cfg.pwd="87654321"; wifi.ap.config(cfg)
dhcp_config ={}
dhcp_config.start = "192.168.4.1"
wifi.ap.dhcp.config(dhcp_config)
wifi.ap.dhcp.start()
conn=net.createConnection(net.TCP, 0)
conn:on("connection", function(connected)
print("$TCP^")
uart.on("data",'#', function(data)
conn:send(data);
end, 0)
end ,0)
conn:on("receive", function(conn, payload)
print(payload)
end)
conn:on("disconnection",function(disconnected)
print("$DIS^")
node.restart()
end)

Station:

uart.setup(0,9600,8,0,1)
print("ready to start")
print("Connecting to WiFi access point...")
wifi.setmode(wifi.STATION)
wifi.sta.config("myssid","87654321")
wifi.sta.autoconnect(1)
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip() == nil then
print("Waiting for IP address...")
else
tmr.stop(1)
print("WiFi connection established, IP address: " .. wifi.sta.getip())
end
end)
print("Open connection...")
conn=net.createConnection(net.TCP, 0)
--port and ip at which labview app is listening
conn:connect(4001,'192.168.4.2')
conn:on("connection", function(connected)
print("#")
uart.on("data",'#', function(data)
conn:send(data);
end, 0)
end ,0)
conn:on("receive", function(conn, payload)
print(payload)
end)
conn:on("disconnection",function(disconnected)
print("*")
end)


If I understand you correctly you are working without router. One ESP8266 is in AP mode and other in station mode. I guess PC WiFi is connected to ESP8266 operating as AP. Even ESP8266 is in AP mode it can not route IP traffic from ESP8266 in station mode to PC. This is the reason you can not connect ESP8266 in station mode to PC.

Please Log in or Create an account to join the conversation.

Time to create page: 0.194 seconds