WiFiClient->write does not send complte message

mariusl
Posts: 2
Joined: Wed Jan 03, 2024 2:20 pm

WiFiClient->write does not send complte message

Postby mariusl » Wed Jan 03, 2024 2:37 pm

Hi,
I am using an ESP32 Dev board and I am trying to get TCP messages sent to Node-Red. All the connectivity is working but my messages are consistently broken up or not send completely.
The project is meant to connect to a LoRaWan board via a serial port and then send the message to Node-Red via TCP.

The message looks like this.

Code: Select all

 {" type":"sensor","unique_id":"TopTankWaterLevel","sensor":2,"payload":4325}
The result from WiFiClient->write is a number of bytes sent. That is the number at the end of each string. So I write the complete string but only two bytes are sent and then the rest.

Code: Select all

{"2
type":"sensor","unique_id":"TopTankWaterLevel","sensor":1,"payload":100}
74
This is the output from Node-Red and that reflects what I see from the transmit side.

Code: Select all

msg : Object
object
_msgid: "63c458aa1560dad9"
payload: "{""
1/3/2024, 4:31:06 PMnode: 1febf83796b23888
msg : Object
object
_msgid: "63c458aa1560dad9"
payload: "type":"sensor","unique_id":"TopTankWaterLevel","sensor":2,"payload":4325}↵"
And this is the piece of cose that writes the message out.

Code: Select all

uint8_t ccnt = 0;
                    for (byte cln = 0; cln < MAX_NMEA_CLIENTS; cln++)
                    {
                        if (TCPClient[num][cln])
                        {
                            TCPClient[num]->flush();
                            // this writes the message out
                             ccnt = TCPClient[num][cln].write(buf2[num], i2[num]);
                             // this is s print of the message to show what it looks like
                             COM[0]->write(buf2[num], i2[num]); //// debug only
                             COM[0]->println(ccnt); //// debug only
                          
                            COM[0]->flush();
                        }
                    }

I am sure that I am missing something stupid so some help will be appreciated.

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: WiFiClient->write does not send complte message

Postby lbernstone » Thu Jan 04, 2024 10:02 pm

You should write the string out to console before you send it off to verify your output is as expected.
Serial.printf("payload: %s\tsize: %d\n", buf2[num], i2[num]);

mariusl
Posts: 2
Joined: Wed Jan 03, 2024 2:20 pm

Re: WiFiClient->write does not send complte message

Postby mariusl » Fri Jan 05, 2024 6:54 am

I do write it to console before sending. The samples provided in my first post is copied from the console. The message is correct and complete.
I catch and print the number of chars sent by the write function and it shows that it sends 2 chars first and then the rest. The result in Node-Red confirms this as well. The message is broken up into two pieces and it seems to be constant.

this statement

Code: Select all

COM[0]->write(buf2[num], i2[num]); //// debug only
is the same as Serial.write.

Who is online

Users browsing this forum: No registered users and 78 guests