Spark Client that can receive data? (TCP)

What is the best way to communicate to the core.

I need more functionality than the Spark.function feature.

What I would prefer is that the spark act as a TCP server and receive JSON data. Is that possible / implemented?

Edit
I see that the function can take a string input. I’m going to give this more of a shot

Edit2
The functionality via the Spark API will work for now. However, I would eventually like to implement this. I think I may end up doing it via UDP

Have you actually tried a forum search on “tcp”? There are quite a few topics about it… I can see four topics if I just glance at the forum.
So yeah, I’m pretty sure it’s possible.

Well, it’s actually impossible to do it via the TCPServer object (I am pretty sure). I haven’t seen anybody do this, and it is what I would like to do.

On the client side, I suppose I could hack something together where I request from a server whether they want me to do something… but that isn’t really what I want either.

Yes, I have looked – as far as I can tell, no one is controlling their spark through TCP (plenty are sending data from their spark to a server though)

Would you care to elaborate a bit further on what it is that you’re trying to accomplish? Maybe a small example so that we get a basic idea of the general concept. Let’s ping, I dunno, @bko for this one, he’s fairly knowledgable in all things Spark related.

1 Like

Hi @CloudformDesign

I am not sure what you mean that TCPServer cannot provide a web control for the core–lots things do this already (without using the Spark cloud). The only thing the Spark core can’t really do is SSL/TLS HTTPS connections to secure web sites (like the Spark cloud) but if you have a server, you can just control the core with a HTTP page served by the core itself.

So I think a use-case describing what you want the user (or yourself) to do when they interact with the core and then what the core does in response is really the next step.

what I would like to do is something like:

if(server.data_available()){ // data is available (a client made a POST request)
    // Move data into a buffer
    for(int n = 0; n<buf_len; n++){
        buf[n] = server.read();
        if(not server.data_available()) break;
    }
    // Process buffer and do whatever I want with it.
}

something like that. It is not supported by the TCP Server at the moment.

From the documentation, the server.available() method returns a client instance. You use that client for reading the data.

1 Like

@dougalserver.available() lets you know that a client is connected so that you can send data to it. How do I get data back from the client?

Hi @CloudformDesign

client = server.available(); returns a client instance, so if there is one, you talk to that.

1 Like

As of what I did, I actually implemented just one Spark.function. The rest are called within that function or dispatched from inside loop()

I took a slightly different approach, passing the whatever data (in this case, strings) as a POST/GET parameters to my Spark.function. I think having a TCPClient or TCPServer to read JSON is totally possible, just that you might need some parsing to do to read it back as usable JSON from buffer of characters or strings.