diff --git a/AgroSense_Soil Moisture_Sensor_LoRaWAN/TTN.js b/AgroSense_Soil Moisture_Sensor_LoRaWAN/TTN.js index c259eef..8524c9e 100644 --- a/AgroSense_Soil Moisture_Sensor_LoRaWAN/TTN.js +++ b/AgroSense_Soil Moisture_Sensor_LoRaWAN/TTN.js @@ -1,14 +1,30 @@ function decodeUplink(input) { + + // Optionally define max and min values for ADC + // Un-comment the variable value and change to suit your needs + var adcMax; //= 1500; // adcMax is "dry" soil + var adcMin; //= 1200; // adcMin is "wet" soil + // User defined number of decimal places as a variable + var decimalPlaces = 2; + + // Variables calculated from device payload // var num = input.bytes[0] * 256 + input.bytes[1] - var bat = input.bytes[4] / 10.0 - var adc = input.bytes[2] * 256 + input.bytes[3] + var Battery = (input.bytes[4] / 10.0).toFixed(decimalPlaces); + var ADC = input.bytes[2] * 256 + input.bytes[3]; + + // Prepare the data object + var data = { + Battery: Battery, + ADC: ADC + }; - return { - data: { - field1: bat, - field2: adc, - }, + // Only calculate and add Moisture if adcMax and adcMin are defined + if (adcMax !== undefined && adcMin !== undefined) { + var Moisture = ((Math.max(0, Math.min(100, ((adcMax - ADC) / (adcMax - adcMin)) * 100))) + .toFixed(decimalPlaces)); + data.Moisture = Moisture; + } - }; -} \ No newline at end of file + return { data: data }; +}