TCPClient problem connecting - help please

Hi,

I hope you can point out to me what I did wrong. For the past few days I have trying to use TCPClient in various way communicate with my server. The only thing I have succeeded in doing is to send UDP data from my Core to my server (not receiving from the server as I can see in other posts due to problem with Core, etc…) It looks like I am having trouble just to get TCPClient just to connect to the server.

On the server side I run Node.js with the below script (which is tested to be up and running, I can connect to it via PuTTY per example). Here is the simple script:


var HOST = "MyHostIPAddress"; // In the script it's my actual host's IP' address
var PORT = 88;

net.createServer(function(socket)
   {
   var rAddress = socket.remoteAddress;
   var rPort = socket.remotePort;
   console.log("Connected: " + rAddress + ":" + rPort);
   socket.write("Welcome!\r\n");

   socket.on("data", function(data)
      {
      console.log("Data " + rAddress + ":" + data);
      socket.write("You said '" + data + "'\r"); // Echo back to client
      });

   socket.on("close", function()
      {
      console.log("Closed: " + rAddress + ":" + rPort);
      });
   }). listen(PORT, HOST);

In the Core, I have these codes:


TCPClient client;

void setup()
   {
   Serial1.begin(9600); // I am using Serial1 and not Serial
   delay(2000); // delay to ensure Core acquires Network

   Serial1.println(WiFi.localIP());
   Serial1.println(WiFi.SSID());
   Serial1.println(Spark.deviceID());
   }

int k=0;

void loop()
   {
   if(k==0) // Only try to connect once
      {
      k++;
      client.connect("MyServerIPAddress", 88); // In code it is my actual server's IP address
      if(client.connected())
         {
         Serial1.println("Connected.");
         client.flush();
         client.stop();
         }
      else
         {
         Serial1.println("Connection fails.");
         }
      }
   }

When I loaded the Core I see this:

192.168.1.15
MyNetworkSSID
51ff6d065082554926580887
Connection fails.

On the server side I see this:
Server listening on MyServerIPAddress:88

(If I try to connect with PuTTY then the server displays the data incoming etc…")

All the time… can someone help me getting pass this connection problem?

Thanks!!!

If by “MyServerIPAddress” you mean something like “10.0.1.10” then that won’t work. The DNS service on Spark does not translate dotted IP addresses from strings, so you have to use an actual IP address datatype.

Try this:

...
client.connect( IPAddress(10,0,1,10), 88);
...
2 Likes

Hi bko,

Yes. And I did tried also like you suggested to put client.connect(IPAddress(x,y,z,j), 88);
Same results… I also tried byte server[]={x,y,z,j} and client.connect(server, 88);
As well, I tried different port number: 2088, 2080, 8022, etc… nothing seems to work for me.
Do I need to include some libraries or something?

Thanks

Two things come to mind:

Can you connect to your server from a host other than your Spark? This ensures the server works and that your router is setup to allow peer-to-peer connections on the port you are using.

Secondly, you could try using TCPClient to connect to Google or some other well known host over HTTP. The example in the doc is OK but not setup for real usage with volume data.

1 Like

Hi bko,

Thank-you for your prompt reply.
My PC and the Core is on the same WiFi network… so for that we can deduce that if my PC can connect to the server then the problem is not the network. Having said that, I did connect to the server using TeraTerm or PuTTY.

Example on PuTTY (with connection to MyServerIPAddress port 88):

It connects right the way and on PuTTY it displays: Welcome!
On server. It displays: Connected: 67.68.160.157:50477 (which is the IP address of the WiFi access router…)
Then when I typed “test” on PuTTY the server will display "Data 67.68.160.157: test"
And in PuTTY the echo back displays “You said ‘test’”. The script on Server works fine, and the network is ok.

I recompiled the Core with www.google.com port 80

I tried this line on the coding above:


client.connect("www.google.com", 80);

or


client.connect("173.194.43.113", 80);

or


client.connect(IPAdress(173,194,43,113), 80);

In all three cases, I got the same results… Connection fails.

I hope you can find out what I did wrong, because after a few days of coding and recoding I am kind out of idea… :frowning:

PS: I am trying to create multiple applications but they all require little information to be sent… Either HTTP Post request or TCP with small exchange of data sent/data received will do find for me… Just need to get it to connected… I did try the example of HTTPCLient… I never get it works - hence I back down to see if the problem is with TCP level… and it seems to be TCP connection is “my” problem… :frowning:

Hi @Kiminator

If you are connecting to your server from the same PC that the server is running on, that is not a real test–you need another host on that network to test.

As I said, don’t bother trying “173.194.43.113” as that will not work.

What happens when you run code here in the doc on the right side:

http://docs.spark.io/firmware/#communication-tcpclient

If you cannot connect to google with the doc example, something is wrong in your network.

Hi bko,

My PC is not on the same network as my server.
My PC is on the same network as my Core.
The Server is in New Jersey… hosted server.

1 Like

Hi bko,

Here are some more information…

The Core is on the same WiFi as my PC.
Core IP: 192.168.1.15
My PC: 192.168.1.16
The WiFi access point has these:

192.168.1.1 with DHCP activated (the Core and my PC got their IP address from this DHCP
67.68.160.157 as its public IP address (as we can see when I connected to my server in New Jersey - I am, the Core and my PC are in Montreal).

Additional testing. I have a Apache server at 192.168.1.10:80 (which I use for scripting php)…
My PC can connect to the 192.168.1.10:80 through browser and through PuTTY in raw mode, etc…
The Core cannot…

I changed the code to:


client.connect(IPAdress(192,168,1,10), 80);

I still have the same results… Connection fails.

I am almost certain my problem is with my Core…

This is why I suggested that you run a known-good program like the one in the doc I linked to above.

Did that run successfully for you?

Hi,
I just run the code you suggested…

Here is what I got on my Serial1 from Core:

connecting…
connection failed

disconnecting.

OK, what TI patch level is your core at? That is have you applied “deep update” to it?

You can also check in the webIDE.

Can you do normal cloud things like run the Tinker app (factory reset) and control the D7 LED?

Hi.
I was reading about that I patch you posted on other messages. I don’t know how to do that.
In fact, I don’t even know how to find that on weIDE (which I will do right now to find out).

Yes, I can run Tinker app… lighting the LED with my iPhone Tinker to the Core… and I did do a few factory reset on to it.

So how do I apply the ‘deep update’?

On the webIDE
It says:

You are building with
Spark Firmware v0.3.4 (Oct 21)
core-firmware 6c98a17
core-communication-lib d824a68
core-common-lib 3cdaed3

@Kiminator, if you’ve got Google Chrome you could use this

view-source:https://api.spark.io/v1/devices/<YOUR_CORE_ID>/events/?access_token=<YOUR_ACCESS_TOKEN>

then you’ll get some response like this, once your Core goes online

event: spark/status
data: {"data":"online","ttl":"60","published_at":"2015-04-17T22:24:25.961Z","coreid":"<YOUR_CORE_ID>"}

event: spark/cc3000-patch-version
data: {"data":"1.29","ttl":"60","published_at":"2015-04-17T22:24:26.070Z","coreid":"<YOUR_CORE_ID>"}
1 Like

In addition to what @ScruffR said, you can look in the “targets” menu of the web IDE (click the little target scope down on the bottom left) and if you see an up-arrow next to the core name, that means it needs to be updated.

1 Like

I just did that…
only thing I see is

: ok
and Chrome is still trying to load… after 2-3 minutes already.

My previous reply was to @ScruffR

Have you reset your Core to have it reconnect?
It will only publish this info, whenever it goes online to the cloud.

1 Like

@bko

In web IDE I clicked on target… I got to the Core name… there is a right > arrow but no up-arrow.
Clicking on > give me the unit ID… :frowning:
And I am using IE11 on Windows 8

As per @ScruffR suggestion I did switched to Chrome to try his suggestion… and by the way after long period of time the Chrome still have : ok and still trying to load…