Trying to send a string to an ip address


I am trying to send a string to a web device, it connects as it gives an error when unplugged but does not when it is plugged in, i have read lots of articles and tried a lot of the fixes but it will not work. Appreciate any advice.

I need to send this {21,100,0,~} to ip port 2081. Could you suggest why it does not work, i hope i have supplied all i need to help anyone figure it out.

I have defined this at the top

TCPClient client;
byte server[] = { 192, 168, 0, 100 }; 
const char command[] = "{21,100,0,~}";

i then in my code run this to connect and send.

if (client.connect(server, 2081))
        client.write((uint8_t *)command, sizeof(command));
       Serial.println("connection failed");



I have tried client.println("{21,100,0,~}"); also with no luck


Hi @tomtom

Have you made sure your router will route this port (2081) from another host such as a laptop on the same network? A lot of times the problem is not on the Particle device, but in the router.


Yes, ive tested using hercules utility and i can send it via that.


I would add a line right after client.stop();:

Serial.println("Command sent.");

Just to make sure the code processes to that point. That would rule out the Particle device (and you didn’t state which device type but I’m guessing Xenon on an Ethernet Featherwing since you say “unplugged”). Are you able to run Wireshark to make sure the packet actually makes it onto the wire?


That is good advise from @ninjatill so I would try that.

You could also try adding a small delay after client.write(...) and before client.stop() in case the connection is getting torn down prematurely. In a perfect world you would use 'client.flush()` but that does nothing right now.


thanks for the suggestion,

I have tried that and can confirm that it print via serial, I assume the connection is working or it would crash or give an error or something.

I think the issue might be what its sending or how its sending, i need it to send this -> {21,100,0,~} <- as a string in text format, i dont know 100% if my code is right to send that like that as a text format to the ip address.

Also to clear up potential confusion its a particle photon chip and im sending it to a wifi device, i.e that text im trying to send {21,100,0,~} once received by the wifi device does an action. I know the action works when sending via Hercules so i assume its not sending in the correct format?



Using the sizeof() operator you will send the null terminator on your command string. Is the receiving device expecting the null terminator? Can it handle it correctly if not? Just something to consider.


OK. “Photon on wireless” gives some other ideas for trouble shooting. Make sure your wireless allows peer-to-peer communication. Some access points allow you to disable peer-to-peer communication. This is more common in internet cafes or public hot spots so that malicious actors with a wireless device can’t communicate with an unsuspecting victim’s device.

The declaration of your command[] looks Ok to my bit of C++ knowledge. But the cast is not something I’m familiar with. Why are you casting to a uint8_t here: (uint8_t *)command? This example in the docs would lead me to write it a bit different:

Docs example:

client.write(buf, len);

What I would write:

client.write(command, sizeof(command));


Thanks for the suggestion, yes the receiving device should receive {21,100,0,~} including the {} brackets and the null terminator.



Thanks for your reply, as far as im aware p2p is enabled as i can send the command via my laptop on the same network, i send the uint8_t here: (uint8_t *)command? purely as its something i read from another forum post my concern was that it might be recieveing {21,100,0,~} but as a different character set such as ascii or hex, im fairly new so i thought that might be an issue. I it to be send and received as “{21,100,0,~}”.



@tomtom, hex is not a character set, just a numerical representation. The command you want to send is ASCII. Are you saying you need to include the double quotes as part of the message?


hi, sorry i meant in a different format of any kind, in the docs it states you can set an optional base value “(optional): the base in which to print numbers: BIN for binary (base 2), DEC for decimal (base 10), OCT for octal (base 8), HEX for hexadecimal (base 16).”

This is what i was referring to, as for your question no its just the {21,100,0,~}



Ive just re-read your suggestion as i miss read it, ive tried that but i get a " invalid conversion from ‘const char*’ to ‘const uint8_t* {aka const unsigned char*}’ [-fpermissive]" when doing that



Yes. You are right, it won’t compile. Like I said, my c++ skills are weak :disappointed_relieved:. I tried to get an example going and I think it’s working. Using wireshark, I just couldn’t see what the data was on the TCP packet. I don’t know if the data isn’t received or that I just don’t know how to use wireshark. So I attempted to also send the same data via UDP. The UDP packet clearly shows the payload is working.

#include <Particle.h>

TCPClient client;
UDP udpClient;
byte server[] = { 192, 168, 0, 186 }; 
int tcpPort = 2081;
int udpPort = 2082;
const char command[] = "ZZZ Hello World!";

void setup() {

void loop() {

    if (client.connect(server, tcpPort))
        client.write((uint8_t *)command, sizeof(command));
        Serial.println("Command Sent TCP.");
        Serial.println("connection failed");

    //Send again using UDP.
    if (udpClient.sendPacket(command, sizeof(command), server, udpPort) < 0) {
    } else {
        Serial.println("Command Sent UDP."); 




Apparently the 2 TCP packets in the above post, before the UDP packet, are from the connection attempt… not sending the payload. I’m thinking the payload should go on another TCP packet that is never sent by the Photon. Looking at the serial output, the TCP connection returns false every attempt. Only the UPD command gets sent.



Thanks for your help, I will have to have a go at the UDP connection to test if it works for me via UDP instead of TCP.



I don’t know if it’s a bug or not but I can’t get TCPClient.connected() to return true. The TCP SYN packet is received and my PC sends back a TCP RST packet. Then the TCPClient.connected() returns false. Every time. I don’t know why. Maybe an Elite knows if this is a bug or just my lack of understanding… @bko, @ScruffR, @Moors7, @peekay123?

Come to think of it, I suppose you need a TCP server on the other end to receive the packets. It’s not like UDP which is a, mostly, one-way communication. UDP doesn’t care if the packet gets received. TCP requires two peers.