Problem transmitting negative numbers [SOLVED]

Started by DonpK, January 04, 2018, 09:03:32 AM

DonpK

I have an outside node transmitting temperature readings to an inside gateway. The temperature readings are sent as integers and are received correctly until the temperature goes negative, ie, from 1 to 0 to -1, -2 etc. When the negative numbers are received and printed they display not as -1, -2 etc, but as 4294967295, 4294967294 etc.  4294967295 as a positive binary number is 32-bits long all 1's. The twos compliment of this number is -1. Somewhere I seem to be picking up an extra 16 bits and not doing the twos compliment for a negative integer.

Here are some snippets of my code. For the data payload I'm using the struct example:

typedef struct {  //Note: Size of this struct should match Receive struct size
  int nodeId;     //store this nodeId
  int tempF;      //Senerion temperature in F
  int humidity;   //Senerion humidity
} Payload;
Payload theData;


then I load the current data:

   
theData.nodeId = NODEID;
    theData.tempF = getTemperature(); 
    theData.humidity = getHumidity();


Up to this point, the negative numbers look okay in the Serial Monitor.
They are then  sent with:

   
if (radio.sendWithRetry(GATEWAYID, (const void*)(&theData), sizeof(theData)))


On the receiving end I use the same struct and:

   
if (radio.receiveDone())


followed by:

   theData = *(Payload*)radio.DATA; 
   Serial.print("Temp = ");
   Serial.print(theData.tempF, 1);Serial.println("F");
   Serial.print("Humidity = "); 
   Serial.print(theData.humidity); Serial.println("%");


It's at this point that a -1 at the sending end prints out as 4294967295. At both the sending and receiving ends, the DATALEN is six bytes.

What am I doing wrong?

Thanks for any suggestions

TomWS

What is the second parameter in your tempF serial print?
Serial.print(theData.tempF, 1);Serial.println("F");

I have no idea what this is doing, but that's the only thing out of order that I see.

Tom

DonpK

Thank you, Tom. The second parameter specifies the number of decimal places, but, of course, it doesn't make sense with integers. I must have copied and pasted that line from some place where I was displaying a floating point number.

Removing the second parameter fixed the problem. Just in time to measure the sub-zero temperatures expected in my part of the country!



TomWS

Well I'm glad that you can now tell that it's COLD outside!   :)

Enjoy,
Tom