2016-12-29 78 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組件。 –

相關問題