2016-12-29 99 views
1

我试图将NodeMCU套接字客户端程序连接到Python服务器程序,但我无法建立连接。ESP8266 NodeMCU Lua将“套接字客户端”连接到“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 

难道我做错了什么或在这里失去了一些步骤?

您的指导表示赞赏。

+0

控制台上的NodeMCU输出是什么? –

+0

在控制台上输出了 – aslamengineer

+0

等待IP地址... 等待IP地址... 等待IP地址... 等待IP地址... 等待IP地址... 等待IP地址... 已建立WiFi连接,IP地址:192.168.0.103 您有3秒中止 正在等待... hi – aslamengineer

回答

1

当我第二次重新访问它时,它终于点击了。我一定是第一次扫描你的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是错误的,但它已经fixed in the dev branch

+0

Hello Marcel, 首先非常感谢您在本论坛中引导我和他人的努力,您提供的指导真正教育, After打破了我的头两天,甚至分析wireshark中的数据包, 我刚才解决了这个问题,问题不在于事件处理,而在于Python代码, 而不是host = socket.gethostname()我硬编码主机='192.168.0.104',它现在的作品(与我的卢阿和你提到的变化)。你认为这种行为的任何可能的原因是什么? – aslamengineer

+0

也在lua而不是IP时,当我尝试主机名srv:connect(12345,'SyedAslam-PC')时,它给了我DNS失败。任何建议为什么发生这种情况 – aslamengineer

+0

您的网络中显然没有可以将“SyedAslam-PC”解析为IP地址的DNS组件。 –

相关问题