[HELP] Unable to get display FLOAT variable correctly at TCP serial communicatio

Hi all,

please advise for below:

I am intend to send my temperature result via wireless connection.

The code is as below:

//declare variables
float tempC;
int tempPin = 0;

void setup()
{    
  Serial.begin(115200);
}

void loop()
{
tempC = analogRead(tempPin);           //read the value from the sensor
tempC = (5.0 * tempC * 100.0)/1024.0;  //convert the analog data to temperature
Serial.write("The reading: ");
Serial.write(tempC);
//Serial.write((byte)tempC);
//Serial.println("The reading: ");
//Serial.println(tempC);

delay(1000);                           //wait one second before sending new data
}

But there is a failure on this.

If i use Serial.println and with USB communication,
the result is displayed correctly (around 23.XX to 24.XX Celsius).

But~

Since when i use Serial.write and wireless communication (TCP),
the result displayed wrongly (character "#" or "$") ..

I am suspecting this is because the Serial.write is taking the result of temperate as the ascii code.
and thus display the corresponding character with the ascii code.

I have tried to cast the tempC to byte ... but still failed..
And did searching on casting float to string.. but
i failed to get correct result at the end...

Hereby wish to have your advise how can i fix the problem....

Many thanks!

Many thanks.

The Serial.write() function sends binary data. Why are you sending binary data to a TCP connection?

You really should be using Serial.print().

If i use Serial.println and with USB communication,
the result is displayed correctly (around 23.XX to 24.XX Celsius).

Well, whack yourself with a clue-by-4, then.

I am suspecting this is because the Serial.write is taking the result of temperate as the ascii code.
and thus display the corresponding character with the ascii code.

Not even close.

I have tried to cast the tempC to byte ... but still failed..

The tempC variable is a 4-byte data type. Casting that to a 1 byte data type means that you haven't a clue what casting does.