Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I was trying to connect a NodeMCU Socket client program to a Python server program, but I was not able to establish a connection.

I tested a simple Python client server code and it worked well.

Python Server Code

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 client code (with this I tested the above code)

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     

The output server side was

Got connection from ('192.168.99.1', 65385)
Hi i am aslam

NodeMCU code

--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")
tmr.alarm(1, 1000, 1, function()
  if wifi.sta.getip() == nil then
    print("Waiting for IP address...")
    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)

But when I run the NodeMCU there is no response in the Python server.

The Output in the ESPlorer console looks like

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...

Am I doing something wrong or missing some steps here?

Your guidance is appreciated.

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 – aslamengineer Dec 30, 2016 at 15:17 So, for whatever reason the connection to 192.168.0.104:12345 cannot be established. Did you try listening for the other events? Did you try connecting to a public host somewhere on the internet? Try host httpbin.org and path /get on port 80. – Marcel Stör Dec 30, 2016 at 21:45

After I revisited this for the second time it finally clicked. I must have scanned your Lua code too quickly the first time.

You need to set up all event handlers (srv:on) before you establish the connection. They may not fire otherwise - depending on how quickly the connection is established.

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")
srv:connect(12345,"192.168.0.104")

The example in our API documentation is wrong but it's already fixed in the dev branch.

Hello Marcel, Firstly Thank you very very much for the effort to guide me and others in this forum, The guidance you provide really educates, After breaking my head for two days and even analysing the data packets in wireshark, i solved the problem just now, the issue is not with event handling but in python code, instead of host = socket.gethostname() i hard coded the host = '192.168.0.104' , it works now(with both my lua and changes you mentioned). any possible reasons you think for this behavior?. – aslamengineer Jan 2, 2017 at 17:09 also when in lua instead of IP when i tried host name srv:connect(12345,'SyedAslam-PC'), it gave me DNS failure. any suggestions why this happened? – aslamengineer Jan 2, 2017 at 17:10 There's obviously no DNS component in your network that can resolve 'SyedAslam-PC' to an IP address. – Marcel Stör Jan 2, 2017 at 19:27

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.