From 8e27964021487cc96d2e3cff5442fe32371f15a9 Mon Sep 17 00:00:00 2001 From: William Moore Date: Thu, 19 Jan 2023 16:57:33 -0600 Subject: [PATCH] Update to add soil sensor --- src/index.ts | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index ececc85..65160af 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,6 +12,7 @@ const { formatInTimeZone } = dateFnsTz; const RAIN_GAUGE_ENDPOINT = process.env.RAIN_GAUGE_ENDPOINT; +const SOIL_SENSOR_ENDPOINT = process.env.SOIL_SENSOR_ENDPOINT; const PORT = process.env.PORT || 3000; const RAIN_GAUGE_SLEEP = 100; @@ -63,22 +64,37 @@ const DataModel = mongoose.model('data', DataSchema); let rainfallTimestamp = ''; +const saveDataPoint = async (name: string, value: number) => { + const tempData = new DataModel(); + tempData.name = name; + tempData.value = value; + tempData.timestamp = new Date(); + tempData.save(); +} + const collectData = async () => { if (RAIN_GAUGE_ENDPOINT && RAIN_GAUGE_ENDPOINT.trim() !== '') { try { const response = await fetch(RAIN_GAUGE_ENDPOINT); const data: any = await response.json(); if (data.rainfall > 0) { - const tempData = new DataModel(); - tempData.name = "rainfall"; - tempData.value = data.rainfall; - tempData.timestamp = new Date(); - tempData.save(); + saveDataPoint('rainfall', data.rainfall); } } catch (e) { console.log(e); } } + + if (SOIL_SENSOR_ENDPOINT && SOIL_SENSOR_ENDPOINT.trim() !== '') { + try { + const response = await fetch(SOIL_SENSOR_ENDPOINT); + const data: any = await response.json(); + saveDataPoint('soil_temperature', data.temperature); + saveDataPoint('soil_capacitence', data.capacitive); + } catch (e) { + console.log(e); + } + } setTimeout(() => collectData(), RAIN_GAUGE_SLEEP); }