Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions AgroSense_Soil Moisture_Sensor_LoRaWAN/TTN.js
Original file line number Diff line number Diff line change
@@ -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;
}

};
}
return { data: data };
}