Tcpclient not connected to tcpserver on the same network as particle photon

On my computer I have tcp server running , I ensure using the netstat command:

$ netstat -vanp tcp  | grep 7138
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 192.168.5.165:7138      0.0.0.0:*               LISTEN      -

and in my particle photon

    TCPClient client;
    String tcp_serer_ip = "192.168.5.165";
    int  serverPort = 7138;
    client.connect(tcp_serer_ip, serverPort);

    if (client.connected()) {
        char data[256];
        // Prepare a buffer with JSON data
        snprintf(data, sizeof(data), "{\"device\":\"photon\",\"temp\":%.2f,\"height\":%.2f,\"pressure\":%.2f}", cTemp, height, pressure);
        // Send to the server. Send only a LF line terminator because it makes parsing easier.
        client.printf("%s\n", data);
        is_connected = 1;
        }
    else{
        is_connected = -1;
    }

I introduced the variable is_connected initialized as 0 for debugging. All I get is -1.

alright I added rule in my ubuntu running tcpserver to allow port 7138/tcp incomming connections but now its getting connected tcp server register connection is established but the data is there.

so something went wrong in sending data ?

I think the primary issue is that you are using a String for your ip address where you should be using a byte array. You likely got a compiler warning about this.

byte tcp_server_ip[] = {192, 168, 5, 165};

The example in the docs should help:

https://docs.particle.io/reference/device-os/firmware/photon/#tcpclient

2 Likes