Need Help: Feather M0 Programming


I’m using Adafruit LoRa Feather M0 and I would like to send temperature and battery level to my gateway. I need a little bit help on coding. I can easily send an integer number but I can not combine two numbers in a packet. Also I’m having trouble on how to send a floating point number. Here is what I have so far:

    float measuredvbat = analogRead(VBATPIN);
    measuredvbat *= 2; // we divided by 2, so multiply back
    measuredvbat *= 3.3; // Multiply by 3.3V, our reference voltage
    measuredvbat /= 1024; // convert to voltage
    Serial.print("VBat: " ); Serial.println(measuredvbat);
    float tempC= getTempC();
    char radiopacket[3];
    Serial.print("Temp is: " ); Serial.println(tempC);
    Serial.print("Temp is: " ); Serial.println(radiopacket);
    LMIC_setTxData2(1, (uint8_t *)radiopacket, sizeof(radiopacket)-1, 0);
    Serial.println(F("Packet queued"));

Any help would be appreciated.
Thank you!

If you are not too much in programming, consider using the Cayenne LPP library
(And you will get a decoder for free at the other end :wink: )

itoa yields text; don’t send text.

Instead, see for example How to use the DHT22 with LMiC? and Decrypting messages for dummies which then for you might be something like:

// Keep 1 decimal; the true accuracy is probably even less
// Signed
int16_t temp = 10 * tempC;
// Unsigned
uint16_t batt = 10 * measuredvbat;

uint8_t radiopacket[4];
radiopacket[0] = temp >> 8;
radiopacket[1] = temp;
radiopacket[2] = batt >> 8;
radiopacket[3] = batt;
// Do NOT subtract 1 from the size here; that's only for text
LMIC_setTxData2(1, radiopacket, sizeof(radiopacket), 0);

…along with:

function Decoder(bytes, port) {
  // Sign-extend 16 bits to 32 bits to support negative temperatures
  var temp = bytes[0]<<24>>16 | bytes[1];
  // Battery cannot be negative
  var batt = bytes[2]<< 8 | bytes[3];
  return {
    temperature: temp / 10,
    battery: batt / 10

Please see the extended explanation in the posts I linked above. All untested.


Thanks so much!! Helped a lot and now I understand how that works. You guys are awesome!