Output LTC Dragino Temperature in Fahrenheit

Hi All,

I’ve found great help on this forum before with getting data flowing nicely into my Ubidots dashboard via TTN.

I’m unsure how to convert my data from C to F. I tried contacting Dragino, but havent received an answer. Unfortunately, i am only beginning to learn to code. Anyone able to help me understand how i can edit my decoder to convert Celsius to Fahrenheit?

Below is my current working decoder under Plugins(‘temp_channel1’ is the variable i use in dashboard):

function format_payload(args){
  var ubidots_payload = {};
  // Log received data for debugging purposes:
  // console.log(JSON.stringify(args));
  // Get RSSI and SNR variables using gateways data:
  var gateways = args['uplink_message']['rx_metadata'];
  for (const i in gateways) {  
    // Get gateway EUI and name
    var gw = gateways[i];
    var gw_eui = gw['gateway_ids']['eui'];
    var gw_id = gw['gateway_ids']['gateway_id'];
    // Build RSSI and SNR variables
    ubidots_payload['rssi-' + gw_id] = {
      "value": gw['rssi'],
      "context": {
        "channel_index": gw['channel_index'],
        "channel_rssi": gw['channel_rssi'],
        "gw_eui": gw_eui,
        "gw_id": gw_id,
        "uplink_token": gw['uplink_token']
      }
    }
    ubidots_payload['snr-' + gw_id] = gw['snr'];

  }
  
  // Get Fcnt and Port variables:
  ubidots_payload['f_cnt'] = args['uplink_message']['f_cnt'];
  ubidots_payload['f_port'] = args['uplink_message']['f_port'];
  
  // Get uplink's timestamp
  ubidots_payload['timestamp'] = new Date(args['uplink_message']['received_at']).getTime(); 
  
  // If you're already decoding in TTS using payload formatters, 
  // then uncomment the following line to use "uplink_message.decoded_payload".
  // PROTIP: Make sure the incoming decoded payload is an Ubidots-compatible JSON (See https://ubidots.com/docs/hw/#sending-data)
  //var decoded_payload = args['uplink_message']['decoded_payload'];
 
  // By default, this plugin uses "uplink_message.frm_payload" and sends it to the decoding function "decodeUplink".
  // For more vendor-specific decoders, check out https://github.com/TheThingsNetwork/lorawan-devices/tree/master/vendor
  ubidots_payload['temp_channel1'] = args['uplink_message']['decoded_payload']['Temp_Channel1']
  //let bytes =  Buffer.from(args['uplink_message']['frm_payload'], 'base64');
  //var decoded_payload = decodeUplink(bytes)['data'];
  // Merge decoded payload into Ubidots payload
  //Object.assign(ubidots_payload, decoded_payload);
  return ubidots_payload
}

function decodeUplink(bytes) {
  // Decoder for the RAK1906 WisBlock Environmental Sensor (https://store.rakwireless.com/products/rak1906-bme680-environment-sensor)
  var decoded = {};
  if (bytes[0] == 1) {
      // If received data is of Environment Monitoring type
      decoded.temperature = (bytes[1] << 8 | (bytes[2])) / 100;
      decoded.humidity = (bytes[3] << 8 | (bytes[4])) / 100;
      decoded.pressure = (bytes[8] | (bytes[7] << 8) | (bytes[6] << 16) | (bytes[5] << 24)) / 100;
      decoded.gas = bytes[12] | (bytes[11] << 8) | (bytes[10] << 16) | (bytes[9] << 24);
  } 
  return {"data": decoded};
}

module.exports = { format_payload };

Thanks!
William

got the answer from Dragino! The code i needed was this(put in your payload formatter under TTN):

function Decoder(bytes, port) {

var poll_message_status=(bytes[2]&0x40)>>6;

var decode = {};

decode.Ext= bytes[2]&0x0F;
decode.BatV= ((bytes[0]<<8 | bytes[1]) & 0x3FFF)/1000;

if(decode.Ext==0x01)
{
  decode.Temp_Channel1=parseFloat(((bytes[3]<<24>>16 | bytes[4])/100).toFixed(2)*1.8+32);
  decode.Temp_Channel2=parseFloat(((bytes[5]<<24>>16 | bytes[6])/100).toFixed(2)*1.8+32);
}
else if(decode.Ext==0x02)
{
  decode.Temp_Channel1=parseFloat(((bytes[3]<<24>>16 | bytes[4])/10).toFixed(1)*1.8+32);
  decode.Temp_Channel2=parseFloat(((bytes[5]<<24>>16 | bytes[6])/10).toFixed(1)*1.8+32);
}
else if(decode.Ext==0x03)
{
  decode.Res_Channel1=parseFloat(((bytes[3]<<8 | bytes[4])/100).toFixed(2));
  decode.Res_Channel2=parseFloat(((bytes[5]<<8 | bytes[6])/100).toFixed(2));
}

decode.Systimestamp=(bytes[7]<<24 | bytes[8]<<16 | bytes[9]<<8 | bytes[10] );


if(poll_message_status===0)
{
  if(bytes.length==11)
  {
    return decode;
  }
}

}

Hello, @wihvi

Thanks for reaching out and also for sharing the response from Dragino surely it will help other members of the community!

Indeed the best way was to update the value by multiplying the measurement by 1.8 with an offset of 32 in the decoder. Nonetheless, a similar output can be accomplished at Ubidots using the scale function in the variable itself as indicated in this article.

Regards,

-Santiago