1

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")
  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)

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

Am I doing something wrong or missing some steps here?

Your guidance is appreciated.

4
  • What is the NodeMCU output on the console? Commented Dec 30, 2016 at 9:27
  • the output on the console was Commented Dec 30, 2016 at 15:13
  • 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 Commented 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. Commented Dec 30, 2016 at 21:45

1 Answer 1

1

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

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

Sign up to request clarification or add additional context in comments.

3 Comments

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?.
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?
There's obviously no DNS component in your network that can resolve 'SyedAslam-PC' to an IP address.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.