0

I was send data from Arduino server to my node.js client, when I receive it , I am not getting it as a whole String but instead as chars, in my console I got something like this

Received: h Received: e Received: l Received: l Received: o

Instead of receiving
Received: 'hello'

Any help please

Down bellow is my node.js receiving data client and my Arduino sending the data

client.on('data', function(data) {
    console.log('Received: ' + data);
});
 // listen for incoming clients
      EthernetClient clientA = serverA.available();
      if (clientA) {
          Serial.println("Client A connected.");

          while(clientA.available() > 0) {
              char dataA = clientA.read(); // 
              Serial.print(dataA);
              //clientA.write(dataA); // echo
              serverB.write(dataA); // forward
          }
      }

Client A is another node.js client sending the that to Arduino and Arduino re sending the data.

  • Please spend time to read some of the concepts mentioned in https://stackoverflow.com/a/41521743/4902099 – hcheung Mar 22 '20 at 01:44

1 Answers1

0

The problem is you read char by char:

    char dataA = clientA.read(); // 
    Serial.print(dataA);

you could do a loop, place all received chars into a buffer and then trigger the emptying/printing the buffer. Some pseudocode tpo get you started:

    char dataA;
    char buffer [32] = '\0'; // Buffer for 31 chars and the null terminator
    uint8_t i = 0;        
    while(clientA.available() > 0) {
          dataA = clientA.read();  
          buffer [i] = dataA; // we put the char into the buffer
         if (dataA != '/0') i++; // put the "pointer" to the next space in buffer
         else {               
          Serial.print(buffer);

         ... do something else with the buffer ....

          }
        }

Read about concepts of serial communication and learn to use them Asa starter:
https://www.oreilly.com/library/view/arduino-cookbook/9781449399368/ch04.html

Codebreaker007
  • 2,911
  • 1
  • 10
  • 22