Sending data to an IP on the network

Hi, I have a particle photon which is connected to a local network. I need the photon to send its data to a particular IP on the network which will further handle it.
Can I just add the IP details in the WiFi.config and it should work or is it something else?

You would most likely use TCPClient or UDP for that

1 Like

Thanks. And how can I send the device ID of my device? I couldn’t find any function to give me the device ID inside the application program

Always check the docs for references.
https://docs.particle.io/reference/device-os/firmware/boron/

3 Likes

There is a search feature in the reference docs and it’s there to be used :wink:

2 Likes

Thanks

I did try that but it didn’t give me what I needed.

Really?

The fourth entry in that list is referencing the very topic @Mjones has linked above (for Photon tho’)

2 Likes

Hey, I encountered a very peculiar problem. At the time of sending a data array through the TCPClient, if the array is not full, it is not sending it over. However, if I reduce the content length parameter for the server, it is accepting the data.

Ex: char jsonCloud[500];
At the time of sending, all the data in aggregated in this array. However, this array is not always full. So, at the time of TCPClient declaration if I give this:
client.println(“Content-length: 500”);
It is not able to send data and it is saying bad request. However, if I reduce the content length to some value what it is sending at that moment, it is able to send it. Like:
client.println(“Content-length: 300”);
Is it expected or there is a bug somewhere on my part?
Also, how to measure the occupied length of an array here?

If you can show your code we might be able to understand better what’s happening.

Especially in order to answer questions like these

if (client.connect("requestbin.net", 80))
		{
			Serial.println("connected");
			//client.println("Hey There");
			client.println("POST /r/1p6jgcy1 HTTP/1.0");
			client.println("Host: requestbin.net");
			client.println("Content-Length: 300");
			client.println("Cache-Control: no-cache");
			client.println("Content-Type: application/json");
			client.println();
			client.println(jsonCloud);
			Serial.println("Data sent");
			Serial.println(client.read());
		}

This is my POST request code

When jsonCloud is a valid string then you should use strlen() to determin the length and pass as content length (plus the extra bytes for the ln in println() - I’d rather go with client.print() tho’).
client.println(jsonCloud) (and client.print(jsonCloud)) will only transmit the data up to the first zero-terminator (\0) in the buffer.

If you pass a content length and then fail to deliver all the data you promised (which you wouldn’t with a JSON not filling the enitre array) the server will keep waiting for more to come and enventually timeout.

On the other hand, if you actually wanted to transfer the entire buffer, you’d not use client.println() but client.write(jsonCloud, sizeof(jsonCloud)).

Thanks for all your help. I ultimately did strlen() and it seemed to work. Also, I don’t know why I had to add a new line just before writing the json file.
My other question is what protocol does the particle use by default to communicate? http or https?

Because the standard requires a valid HTTP request (header) to be ended/separated with an empty line before adding the (optional) body.
https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_message

CoAP