Update to add soil sensor

This commit is contained in:
William Moore 2023-01-19 16:57:33 -06:00
parent a701efba2d
commit 8e27964021

View File

@ -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);
}