Arduino WiFi server -> PC client running Python

Greetings. I’ve been beating my head on this for too long now. I am setting up a suite of sensors running off an Arduino Rev3 and WifiShield (v1.1.0) and I want to send the data in real-time over our wifi to a PC, where a Python program will receive/log the data.

For now I just want to set up the network connection, send a message from the arduino, and have the python program receive it… which is where I’m stuck.

On the Arduino side, I’m simply using using the WiFiChatServer example. The wifi shield connects to our wifi just fine.

On the python side, I’m using a socket and trying to connect to the wifi shield ip and port 23. But the connection fails and I get the error: errno 10060: connected party failed to respond.

import socket

TCP_IP = '205.155.76.161' #Arduino WiFi IP
TCP_PORT = 23             #Arudino WiFi Port
BUFFER_SIZE = 1024

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP,TCP_PORT))
s.listen(1)

(conn,addr) = s.accept()
print 'Connection address: ',addr
while True:
    data = conn.recv(BUFFER_SIZE)
    if not data: break
    print 'received data: ',data
    conn.send('ECHO')
conn.close()
s.close()

I suspect I’m confused about the ports. I would be very grateful for some assistance.

Are you sending anything from the PC when it connects? It does not appear so. If not, the wifi server won't send anything. Here is a section of the WiFiChatServer code with the correct comments.

void loop() {
  // wait for a new client to send something
  WiFiClient client = server.available();


  // when the client sends the first byte, say hello:
  if (client) {

Thanks, though my Python program is hanging up at the 'connect' line.

s.connect((TCP_IP,TCP_PORT))

So I'm not even able to get to the send/receive message part.

Here is my updated Python code. I'm trying to send the message to Arduino, but Arduino isn't receiving it. Atleast, I'm not seeing the messaged received in the Serial Monitor. I also tried the WifiUDPSendReceiveString tutorial with no luck. Thoughts?

import socket

TCP_IP = '205.155.74.213' #Local Host
TCP_PORT = 3007            #Listening Port
BUFFER_SIZE = 12

SERVER_IP = '205.155.76.161'  #Arduino IP
SERVER_PORT = 23            #Server Port

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((TCP_IP,TCP_PORT))
#s.listen(1)

#(conn,addr) = s.accept()
#print 'Connection address: ',addr
while True:
    print 'Sending message'
    s.sendto('ECHO',(SERVER_IP,SERVER_PORT))
    print 'Waiting for response'
    data,addr = s.recvfrom(BUFFER_SIZE)
    if not data: break
    print 'received data: ',data
    print 'data recieved from: ',addr
    conn.close()
s.close()