Reading float data from SerialPort in Node.js

I am sending measurement data from arduino to Node.js blynk-client via serialport

float mA, Volt = 0.0f;
UART_writeFloat(Volt);
UART_writeFloat(mA);

void UART_writeFloat (float value)
  {
     const byte * p = (const byte*) &value;
     for (byte i = 0; i < 4; i++)
          Serial.write(*p++);
  }

And accept in the server on Ubuntu with NodeJS. I don’t know anything about NodeJS, but the code from the example of working with the serialport receives only raw data into the buffer.

var buffer = '';
var Voltage = 0.0;
var Current = 0.0;
port.on('data', function(buffer) {
        console.log(buffer);
    });

and so I get

<Buffer 6f 83 60 40 c1 14 74 c1>

help get my sent data into two float variables Voltage and Current.

Obviously HEX. Define your data as a specific number, decode the resulting HEX and see if you can make sense of it.

6f = 111
83 = 131
60 = 96
40 = 64
c1 = 191
14 = 18
74 = 116
c1 = 193

:man_shrugging:

Ohh… sorry, but I am not familiar with JS. and I did not understand anything from what you wrote …
I just need help with the code, how to load data from the buffer into a float variable.
I have problem with JS code only…

That makes two of us.

What does this function output on the serial?


void UART_writeFloat (float value)
  {
     const byte * p = (const byte*) &value;
     for (byte i = 0; i < 4; i++)
          Serial.write(*p++);
  }

It output 4 bytes of float-var
I don’t know how to concat them back to float in JS

Found !!!

var vVolt = new blynk.VirtualPin(25);
var vIrms = new blynk.VirtualPin(26);
var vPWR = new blynk.VirtualPin(27);

    port.on('data', function(chunk) {

       Voltage = chunk.readFloatLE(0);
       Current = chunk.readFloatLE(4);
       Power   = chunk.readFloatLE(8);

        vVolt.write(Voltage);
        vIrms.write(Current);
        vPWR.write(Power);
    });
1 Like