-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
70 lines (58 loc) · 2.17 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//importing packages
const { createClient } = require('@supabase/supabase-js');
const SerialPort = require('serialport');
const ReadLine = require('@serialport/parser-readline');
//supabase API key and URL
const supabaseUrl = '';
const supabaseKey = '';
const supabase = createClient(supabaseUrl, supabaseKey);
const port = new SerialPort('COM7', {baudeRate: 9600});
const parser = port.pipe(new ReadLine({ delimiter: '\r\n' }));
//method that saves humidity data to database
async function saveHumidity(humidity) {
const { data, error } = await supabase .from('humidity_log')
.insert([{ humidity_value: humidity, timestap: new Date() }]);
if (error) {
console.error('Error inserting humidity data:', error.message);
}
else {
console.log('Humidity data saved:', data);
}
}
//method that saves moisture data to database
async function saveMoisture(moisture) {
const { data, error } = await supabase .from('moisture_data')
.insert([{ moisture_value: moisture, timestap: new Date() }]);
if (error) {
console.error('Error inserting moisture data:', error.message);
}
else {
console.log('Moisture data saved:', data);
}
}
//method that saves temperature data to database
async function saveTemperature(temperature) {
const { data, error } = await supabase .from('temperature_data')
.insert([{ temperature_value: temperature, timestap: new Date() }]);
if (error) {
console.error('Error inserting temperature data:', error.message);
}
else {
console.log('Moisture data saved:', data);
}
}
//getting data from arduino
parser.on('data', (data) => {
console.log('Received data:', data);
const parts = data.split(',');
const humidity = parts[0].split(':')[1];
const moisture = parts[1].split(':')[1];
const temperature = parts[2].split(':')[1];
///saving the collected data
saveHumidity(humidity);
saveMoisture(moisture);
saveTemperature(temperature);
});
port.on('error', (err) => {
console.error('Error with serial port:', err.message);
});