Problem with argon client/python server

I’m trying to create a simple client/server but it the client won’t connect to the server once it is running. I’ve looked at similar problems and people have suggested looking at the method for defining IP addresses but I think i’m addressing that in my code? Any help would be appreciated and apologies if it is something obvious.
Code:

Client

TCPClient client;
int analogPin = A0;
int port = 9999;

void setup() {
    // Initialize the Serial
    Serial.begin(9600);
    // IPAddress myIP = WiFi.localIP();
    // Serial.println(myIP);
    
    uint8_t server[] = { 192, 168, 25, 1};
    IPAddress IPfromBytes( server );
    //byte server[] = { 192, 168, 25, 1};
    
    // Connect to the remote server
    if(client.connect(IPfromBytes, port)) {
        Serial.println("Connected");
    } else {
        // if we can’t connect, then we display an error.
        Serial.println("error");
    }
}

void loop() {
    int val = analogRead(analogPin);
    // No type set and the default one is char. Meaning in this case our Int will
    // be converted to UTF8
    client.print(val);
    client.print(',');
    Serial.println(val);
    delay(100);
}

Server:

import socket
import sys

# grab the host name so that I can grab the ip address
IP = socket.gethostbyname(socket.gethostname())
#IP = socket.gethostname()
PORT = 9999

# create a TCP / IP socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# bind the socket to a public host host and a port
server_socket.bind((IP, PORT))

# tell the socket to listen
# allows up to 5 queued connections
server_socket.listen(5)

# print out that we are listening
print("listening on: \n IP: " + IP + "\n PORT: " + str(PORT))

# accept connections from outside
(connection, client_address) = server_socket.accept()
print("waiting for a connection")

# main loop
while True:
    try: 
        # let the user know it is connected to an photon
        print("connected to argon: " + client_address)
        data = connection.recv(2048)
        # recieve the data in small chunks

    except socket.error as e:
        print(e)

    finally:
        # clean up the connection and close it
        server_socket.close()

I have reformatted your code blocks but in the process I noticed that your Python code did not indent correctly (in the original version), but Python is paying close attention to the indentation.
Hence I adjusted the indentation as I’d assume you’d want it done.

Can you confirm whether this is the Python code (including indentation) you are using or not?

However, this is how I would setup the IP address in your Argon code

    IPAddress IPfromBytes( 192, 168, 25, 1 );

Thanks for the fast reply. I copied the code wrong but I do have it indented correctly on the python file. The code isn’t mine it was taken from here: Connecting photon client to a python server
I changed the IP address setup to your suggestion but same result unfortunately. I’ll attach the output of the serial terminal and server:
server%20output serial%20output

You may want to retry a connection in loop() once you find that client.connect() is not true.
For that I’d make the IP address a global variable and move the client.connect() call into a fitting conditional block inside loop().

Assuming this code is okay, it hasn’t worked.

void loop() {
  if(!(client.connect(IPfromBytes, port))) {
      client.connect(IPfromBytes, port);
  }

    int val = analogRead(analogPin);

I had a previous problem with my network where I couldn’t flash code onto the device OTA. Could this be related to that?

This code unconditionally tries to connect again irrespective of the current connection state.
You should check if (!client.connected()).

1 Like

I changed it but same result unfortunately.

I tried your code now and it seems to be an issue with your Python code.
This is what it throws at me

waiting for a connection
Traceback (most recent call last):
  File ".\Server.py", line 29, in <module>
    print("connected to argon: " + client_address)
TypeError: can only concatenate str (not "tuple") to str

However, I’ve altered the code for the Argon to allow for different IP addresses without the need to rebuild and flash.

const int analogPin = A0;

int port = 9999;
IPAddress ip;
TCPClient client;


void setup() {
  Serial.begin(9600);
  Particle.function("setServer", setServer);
}

void loop() {
  if (client.connected()) {
    int val = analogRead(analogPin);
    // No type set and the default one is char. Meaning in this case our Int will
    // be converted to UTF8
    client.printf("%d, ", val);
    Serial.print('.');
  }
  else if (ip) {
    client.connect(ip, port);
  }
  delay(1000);
}

int setServer(const char* cmd) {
  uint8_t octet[4];
  int     retVal = sscanf(cmd, "%d.%d.%d.%d:%d", &octet[0], &octet[1], &octet[2], &octet[3], &port);
  
  Serial.printlnf("Input: %s -> Parsed: %d.%d.%d.%d:%d", cmd, octet[0], octet[1], octet[2], octet[3], port);
  if (retVal >= 4) {
    client.stop();
    ip = IPAddress(octet[0], octet[1], octet[2], octet[3]);
    retVal = octet[3];
  }
  else {
    retVal *= -1;
    ip.clear();
  }
  
  return retVal;
}
1 Like

Thank you for that function its very handy. However I am not receiving that error you got as I can’t get that far in the first place. The server never accepts the connection in the first place. I set up a node.js server and still can’t connect the argon over my network to the server so either I am doing something really fundamental wrong or there is a problem with my network. I understand if you’re too fed up to look into it any further although any other feedback would be welcome

What OS are you running on the host computer?
Could it be that the firewall is blocking the communication?

this should actually be

        print("connected to argon: ", client_address)
        # or
        print("connected to argon: " + client_address[0])

I updated my Argon code above to also allow for the port to be sent in from outside.

Yurrtel,

I saw a thread you posted on Particle community about your argon failing to connect to a python tcp server.

I have the same problem.

I did not find ScruffR’s suggestions helpful and am wondering if you ever found the cause of the problem. If so, could you explain to me how you fixed it?

Thanks
Gary

In the end it was a simple error on the server side. The gethostbyname() function I was using to grab my machines IP address was actually grabbing my VMware virtual network adapters address instead, so I hard coded in the correct address. Maybe you can post some code and either I or someone else might be able to help?

Yurrtel,

Thx for your quick response. It got me thinking, and I reviewed my notes. Turns out I had ‘leftover’ Wifi credentials in the Argon, and when I let it connect automatically, it connected to the wrong network, which happened to be available. I fixed credentials and problem solved.

Thanks again
Gary

1 Like

Strange thing is that my unhelpful suggestions couldn’t help with an issue with issues entirely out of the scope of them (for both cases).

1 Like

No offense meant dude