Spark Core TCP Server

Hello,

I have a TCP client written in Visual Studio C# and I want it to be able to talk to the TCP server program in my Spark Core. The server is simply going to echo back the text the TCP client is going to sent. But since my home network is DHCP, the Spark Core IP address keeps changing.

After power cycling the Spark Core, how do I determine the IP address? Can I use the Spark Core Device ID (for example 53ff75065075535112345678) or the Core name to determine the IP address?

I know I can determine the IP address of the Spark Core using the following code. But the TCP client has no way in knowing this IP address as this information is only available in Spark Core.

void setup()
{
        server.begin();
        // Make sure your Serial Terminal app is closed before powering your Core
        Serial.begin(9600);
        // Now open your Serial Terminal, and hit any key to continue!
        while(!Serial.available()) SPARK_WLAN_Loop();
        Serial.println(Network.localIP());
        Serial.println(Network.subnetMask());
        Serial.println(Network.gatewayIP());
        Serial.println(Network.SSID());
}

Is there a way to assign a static IP address in the Spark Core?

Below is my TCP Client code for your reference.

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Sockets;

public class clnt {
    public static void Main() {
        try {
            TcpClient tcpclnt = new TcpClient();
            Console.WriteLine(“Connecting…”);

tcpclnt.Connect(“192.168.88.99”,2488);    //  IP ADDRESS & PORT # OF TCP SERVER IN SPARK CORE
            
            Console.WriteLine(“Connected”);
            Console.Write("Enter the string to be transmitted : ");
            String str=Console.ReadLine();
            Stream stm = tcpclnt.GetStream();
            ASCIIEncoding asen= new ASCIIEncoding();
            byte[] ba=asen.GetBytes(str);
            Console.WriteLine(“Transmitting…”);
            stm.Write(ba,0,ba.Length);
            byte[] bb=new byte[100];
            int k=stm.Read(bb,0,100);
            for (int i=0;i<k;i++) Console.Write(Convert.ToChar(bb[i]));   
            tcpclnt.Close();
        }
        catch (Exception e) {
            Console.WriteLine("Error… " + e.StackTrace);
        }
    }
}

Thanks.

@n1849778, you can avoid having to set a static IP on the Core by “reserving” a fixed IP on your router! All you need is to know the MAC of the Core which you can find in your router’s DHCP allocated IP table and then reserve an IP of your choice using that MAC. Anytime your Spark connects, the DHCP will send it that reseverd IP. Voila! Static without the hassles! :smile:

Thanks for the quick response.
My router does not do address reservation cos it is kinda old (I think). I have a Linksys WRT54GS. I will give it a try once I get a new router. Thanks.

You could use the Spark cloud - in your setup() function in spark firmware code use Spark.variable() to assign a spark variable with the value of the local IP Address of the spark WiFi.localIP(). You can then fetch this variable in your TCP client using the cloud’s REST API, so you know which IP to connect to.

2 Likes

I would imagine the REST API requires the Internet connection?

Yes it does, both from the spark (breathing cyan) and your TCP Server will need access to the internet. Do you have internet access?

No not on the wifi network that the Spark Core is connected to.

I read it here that Spark Core can handle static IP address but I need to dig into the source code (which I believe the firmware source code). Step 4 in the link below highlights about static IP address.

So I setup my laptop to compile Spark Core firmware through the link below.

I can compile and load the firmware to the Spark Core now but I don’t seem to find the location where I can assign the static IP address in the firmware source.