From f2dbfe399fe33a8968cbf651f9e8007d5a22d08c Mon Sep 17 00:00:00 2001 From: Caleb Goodin <152418391+Faileb@users.noreply.github.com> Date: Sat, 2 Nov 2024 20:47:43 -0700 Subject: [PATCH 1/2] Add Moisture to TTN.js This commit adds an *optional* `Moisture` data value that calculates soil moisture percentage from user-defined `adcMax` and `adcMin` variables. The default behavior is opt-in. *If un-configured,* users will receive Makerfabs default data values of *only* the battery and ADC. Also adds `decimalPlaces` as a variable to limit data value output to two (2) decimal places by default, this variable is also configurable to suit the user's needs. Also changes data object keys to be more descriptive. There wasn't anything wrong with them as-is but this saves time when using the Home Assistant TTN integration. --- AgroSense_Soil Moisture_Sensor_LoRaWAN/TTN.js | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/AgroSense_Soil Moisture_Sensor_LoRaWAN/TTN.js b/AgroSense_Soil Moisture_Sensor_LoRaWAN/TTN.js index c259eef..6801f1c 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) + "V"; + 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 }; +} From d8a37944ee0a20e49a4f821f01efeb6e9828c558 Mon Sep 17 00:00:00 2001 From: Caleb Goodin <152418391+Faileb@users.noreply.github.com> Date: Wed, 6 Nov 2024 10:23:13 -0800 Subject: [PATCH 2/2] Remove measurement symbols from TTN.js In retrospect it was a bad idea to add the voltage and percent symbols to the payload formatter, it caused problems in Home Assistant with gauges. --- AgroSense_Soil Moisture_Sensor_LoRaWAN/TTN.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/AgroSense_Soil Moisture_Sensor_LoRaWAN/TTN.js b/AgroSense_Soil Moisture_Sensor_LoRaWAN/TTN.js index 6801f1c..8524c9e 100644 --- a/AgroSense_Soil Moisture_Sensor_LoRaWAN/TTN.js +++ b/AgroSense_Soil Moisture_Sensor_LoRaWAN/TTN.js @@ -10,7 +10,7 @@ function decodeUplink(input) { // Variables calculated from device payload // var num = input.bytes[0] * 256 + input.bytes[1] - var Battery = (input.bytes[4] / 10.0).toFixed(decimalPlaces) + "V"; + var Battery = (input.bytes[4] / 10.0).toFixed(decimalPlaces); var ADC = input.bytes[2] * 256 + input.bytes[3]; // Prepare the data object @@ -22,7 +22,7 @@ function decodeUplink(input) { // 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) + "%"); + .toFixed(decimalPlaces)); data.Moisture = Moisture; }