ESP8266 NodeMCU Lua "Socket client" 到 "Python Server" 连接不可能
ESP8266 NodeMCU Lua "Socket client" to "Python Server" connection not possible
我试图将 NodeMCU Socket 客户端程序连接到 Python 服务器程序,但无法建立连接。
我测试了一个简单的 Python 客户端服务器代码,它运行良好。
Python 服务器代码
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
while True:
c, addr = s.accept() # Establish connection with client.
print 'Got connection from', addr
print c.recv(1024)
c.send('Thank you for connecting')
c.close() # Close the connection
Python客户端代码(我用这个测试了上面的代码)
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
s.connect((host, port))
s.send('Hi i am aslam')
print s.recv(1024)
s.close # Close the socket when done
输出服务器端是
Got connection from ('192.168.99.1', 65385)
Hi i am aslam
NodeMCU代码
--set wifi as station
print("Setting up WIFI...")
wifi.setmode(wifi.STATION)
--modify according your wireless router settings
wifi.sta.config("xxx", "xxx")
wifi.sta.connect()
function postThingSpeak()
print("hi")
srv = net.createConnection(net.TCP, 0)
srv:on("receive", function(sck, c) print(c) end)
srv:connect(12345, "192.168.0.104")
srv:on("connection", function(sck, c)
print("Wait for connection before sending.")
sck:send("hi how r u")
end)
end
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())
print("You have 3 seconds to abort")
print("Waiting...")
tmr.alarm(0, 3000, 0, postThingSpeak)
end
end)
但是当我 运行 NodeMCU 时 Python 服务器没有响应。
ESPlorer 控制台中的输出看起来像
Waiting for IP address...
Waiting for IP address...
Waiting for IP address...
Waiting for IP address...
Waiting for IP address...
Waiting for IP address...
WiFi connection established, IP address: 192.168.0.103
You have 3 seconds to abort
Waiting...
hi
我是不是做错了什么或者漏掉了一些步骤?
感谢您的指导。
在我第二次重新访问它之后,它终于点击了。我一定是第一次扫描你的 Lua 代码太快了。
您需要在 建立连接之前设置所有事件处理程序(srv:on
)。否则它们可能不会触发 - 取决于建立连接的速度。
srv = net.createConnection(net.TCP, 0)
srv:on("receive", function(sck, c) print(c) end)
srv:on("connection", function(sck)
print("Wait for connection before sending.")
sck:send("hi how r u")
end)
srv:connect(12345,"192.168.0.104")
example in our API documentation is wrong but it's already fixed in the dev
branch.
我试图将 NodeMCU Socket 客户端程序连接到 Python 服务器程序,但无法建立连接。
我测试了一个简单的 Python 客户端服务器代码,它运行良好。
Python 服务器代码
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
while True:
c, addr = s.accept() # Establish connection with client.
print 'Got connection from', addr
print c.recv(1024)
c.send('Thank you for connecting')
c.close() # Close the connection
Python客户端代码(我用这个测试了上面的代码)
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
s.connect((host, port))
s.send('Hi i am aslam')
print s.recv(1024)
s.close # Close the socket when done
输出服务器端是
Got connection from ('192.168.99.1', 65385)
Hi i am aslam
NodeMCU代码
--set wifi as station
print("Setting up WIFI...")
wifi.setmode(wifi.STATION)
--modify according your wireless router settings
wifi.sta.config("xxx", "xxx")
wifi.sta.connect()
function postThingSpeak()
print("hi")
srv = net.createConnection(net.TCP, 0)
srv:on("receive", function(sck, c) print(c) end)
srv:connect(12345, "192.168.0.104")
srv:on("connection", function(sck, c)
print("Wait for connection before sending.")
sck:send("hi how r u")
end)
end
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())
print("You have 3 seconds to abort")
print("Waiting...")
tmr.alarm(0, 3000, 0, postThingSpeak)
end
end)
但是当我 运行 NodeMCU 时 Python 服务器没有响应。
ESPlorer 控制台中的输出看起来像
Waiting for IP address...
Waiting for IP address...
Waiting for IP address...
Waiting for IP address...
Waiting for IP address...
Waiting for IP address...
WiFi connection established, IP address: 192.168.0.103
You have 3 seconds to abort
Waiting...
hi
我是不是做错了什么或者漏掉了一些步骤?
感谢您的指导。
在我第二次重新访问它之后,它终于点击了。我一定是第一次扫描你的 Lua 代码太快了。
您需要在 建立连接之前设置所有事件处理程序(srv:on
)。否则它们可能不会触发 - 取决于建立连接的速度。
srv = net.createConnection(net.TCP, 0)
srv:on("receive", function(sck, c) print(c) end)
srv:on("connection", function(sck)
print("Wait for connection before sending.")
sck:send("hi how r u")
end)
srv:connect(12345,"192.168.0.104")
example in our API documentation is wrong but it's already fixed in the dev
branch.