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.");
}
}
}
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.
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?
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.
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.
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…
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:
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…
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.
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.
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…
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…