TCP connection to private server (spark core as tcp client)

Why on my video spark core some times connects to my server, and sometimes not? does anybody know?

It’s just too hard to help if you won’t format your code :frowning:

What should i format?

Can’t read it. But that’s OK.

Folks, I have tried many variations of the above code but I am unable to connect the core to my Mac which is on same network and write some data to a port using tcpclient.

Below is the code of core and then the code (python) which is listening on mac. Please note that when I use Telnet to write to the port from my windows box to Mac on the same port for testing, it works great. Please help

TCPClient client;
byte server[] = { 192, 168, 1, 122 };
byte c;
int port = 5005;
void setup()
{
  Serial.begin(9600);
  delay(1000);
  Serial.println("connecting...");
}

void loop()
{
      Serial.println("connecting...");
  if (client.connect(server, port))
  {
    Serial.println("connected");
    client.println("connected");
//    с = client.read();
    Serial.print(client.read());
  }
  else
  {
    Serial.println("connection failed");
  }
  Serial.println();
  Serial.println("disconnecting.");
}

Python code running on Mac

!/usr/bin/env python

import socket


TCP_IP = '192.168.1.122'
TCP_PORT = 5005
BUFFER_SIZE = 10  # Normally 1024, but we want fast response

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

conn, addr = s.accept()
print 'Connection address:', addr
while 1:
    data = conn.recv(BUFFER_SIZE)
    if not data: break
    print "received data:", data
    conn.send(data)  # echo
conn.close()
~                 

Hi @lkhosla

I would try changing this

 byte server[] = { 192, 168, 1, 122 };

to this

IPAddress server(192, 168, 1, 122);

See is that is better.

Also you can format code blocks here in the forum using three grave accent marks on a single line at the start of the code second and then again at the end of the code section. I will fix up your post above for you so if you edit again (click the little pencil) you can see how to do it.

1 Like

Thanks but no luck even after swapping byte server with IpAddress. I don’t see any data being written to port 5005. Any other suggestions?

Hi @lkhosla

What serial port debug message do you get from the code? Just the “connection failed” message?

I am new to core debug…how would I check the message when nothing is coming to my listener?

in screen on the modem I get following message

connecting…
connection failed

disconnecting.
connecting…

I have another arduino core and that works just fine connecting and writing the data to a tcp socket…It is a bit frustrating to work with spark core (although as easy it was to set it up and get going). Any next steps or suggestions?

I guess I would start checking basics, like are you on the right wireless network etc. In setup, you could try:

IPAddress localAddr = Network.localIP();
Serial.print(localAddr);
Serial.print(" on ");
Serial.println(Network.SSID());
Serial.print("Pinging 5 times: ");
Serial.println(Network.ping(server));

Also you can try doing client.stop() when you disconnect and adding a delay(50); at the bottom of the loop function. Others have found a delay is required there for reasons that are not fully debugged.

1 Like

Thanks. Everything seems to be on same network and ping works too. Please look at the output below for your suggestion. The problem of not getting any data still exist on port 5005

192.168.1.125 on RedPenguin
Pinging 5 times: 5
connecting…
connection failed

disconnecting.
192.168.1.125 on RedPenguin
Pinging 5 times: 5
connecting…

One more thing to try: port is declared int in your code above but the connect method has uint16_t as the type of the port–can you change port to uint16_t port = 5005; On Arduino int is 16-bits but on the Spark core it is 32-bits.

no luck with uint16_t either.

I think the issue seems to be that Spark Core seems very moody. I disconnected Spark core and plugged it back in and once it rebooted and ran same code but this time the PINGS are returning 0 instead of 5 times. I pinged the server from another client (Windows Laptop) and pings are 100% successful so something within the core which is unable to make connection INTERMITTENTLY (more times failure than success).

disconnecting.
192.168.1.125 on RedPenguin
Pinging 5 times: 0
connecting…
connection failed

disconnecting.
192.168.1.125 on RedPenguin
Pinging 5 times: 0
connecting…
connection failed

Does other firmware like Tinker work OK for you? Maybe you have a router issue.

There is not a lot that can go wrong with ping on the Spark core.

1 Like

well if it was a network issue then I would see similar pattern from other TELNET connection to same IP and PORT and I don’t have that issue. TELNET from another windows box connects just fine to the MAC @ port 5005.

I think this maybe a bug somewhere here. The Core reboots itself sometimes (has happened 3 times in last 2 hrs). When it came back up Pings started to work and I did see some data coming in tcpdump on port 5005. Has anyone else experienced this much issue?

ok, I now have pings working consistently but it still is not able make the client connection.

disconnecting.
192.168.1.104 on RedPenguin
Pinging 5 times: 5
connecting…
connection failed
disconnecting.
192.168.1.104 on RedPenguin
Pinging 5 times: 5
connecting…
connection failed

disconnecting.
192.168.1.104 on RedPenguin
Pinging 5 times: 5
connecting…
connection failed

disconnecting.
192.168.1.104 on RedPenguin
Pinging 5 times: 5
connecting…
connection failed

disconnecting.
192.168.1.104 on RedPenguin
Pinging 5 times: 5
connecting…
connection failed

disconnecting.
192.168.1.104 on RedPenguin
Pinging 5 times: 5
connecting…
connection failed

I was able to connect finally. Didn’t change any code but issue is this now.

I have a listener (python) given above listening at port 5005. When Core connects in the loop and does client.read() it prints -1 on debug serial port and then after that (2 tries) it stops connecting… Any idea what is -1 and why does it stop connecting after 2 connections?