Update to add soil sensor
This commit is contained in:
parent
a701efba2d
commit
8e27964021
26
src/index.ts
26
src/index.ts
@ -12,6 +12,7 @@ const { formatInTimeZone } = dateFnsTz;
|
|||||||
|
|
||||||
|
|
||||||
const RAIN_GAUGE_ENDPOINT = process.env.RAIN_GAUGE_ENDPOINT;
|
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 PORT = process.env.PORT || 3000;
|
||||||
const RAIN_GAUGE_SLEEP = 100;
|
const RAIN_GAUGE_SLEEP = 100;
|
||||||
|
|
||||||
@ -63,22 +64,37 @@ const DataModel = mongoose.model('data', DataSchema);
|
|||||||
|
|
||||||
let rainfallTimestamp = '';
|
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 () => {
|
const collectData = async () => {
|
||||||
if (RAIN_GAUGE_ENDPOINT && RAIN_GAUGE_ENDPOINT.trim() !== '') {
|
if (RAIN_GAUGE_ENDPOINT && RAIN_GAUGE_ENDPOINT.trim() !== '') {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(RAIN_GAUGE_ENDPOINT);
|
const response = await fetch(RAIN_GAUGE_ENDPOINT);
|
||||||
const data: any = await response.json();
|
const data: any = await response.json();
|
||||||
if (data.rainfall > 0) {
|
if (data.rainfall > 0) {
|
||||||
const tempData = new DataModel();
|
saveDataPoint('rainfall', data.rainfall);
|
||||||
tempData.name = "rainfall";
|
|
||||||
tempData.value = data.rainfall;
|
|
||||||
tempData.timestamp = new Date();
|
|
||||||
tempData.save();
|
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(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);
|
setTimeout(() => collectData(), RAIN_GAUGE_SLEEP);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user