Repository navigation
Expand file tree
/
Copy pathdataDoDB.sh
More file actions
66 lines (48 loc) · 1.45 KB
/
Copy pathdataDoDB.sh
File metadata and controls
66 lines (48 loc) · 1.45 KB
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
#!/bin/bash
# Path to database
DB="/var/www/html/sensordata.db"
# Function to read serial data from USB port
readSerialData(){
# Variables
local line # Store line
local data_started=false # Bool to know if data started
local count=0 # Count of data
local values=() # Read data
# Read data line by line, set Internal Field Separator to none so the line is as is, -r so no escape characters, read and put value to line
while IFS= read -r line; do
if [[ "$line" == "new" ]]; then # If new data arrived
data_started=true
count=0
values=()
continue
fi
if $data_started; then
if [[ -z "$line" ]]; then # Ignore blank lines
continue
fi
values+=("$line") # Add to values
count=$((count+1))
# If all data are present (13)
if [[ "$count" -eq 13 ]]; then
insert_data "$values[@]}" # Calling funtion to put values into SQL statement
data_started=false
sleep 0.1
fi
fi
done < <(cat < /dev/ttyACM0) # Specification of input for loop
}
# Function to insert data
insert_data(){
# SQL statement that doesnt change
sql="INSERT INTO data(datum, cas, intenzitaS, vstupVI, bocniVI, zatezVI, proudI, vstupV1, bocniV1, zatezV1, proud1, vstupV2, bocniV2, zatezV2, proud2) VALUES (CURRENT_DATE, CURRENT_TIME,"
# Add data to SQL statement
for value in "$@"; do
sql+="$value,"
done
# Trim last , from string
sql="${sql%,})"
# Execute sql statement
sqlite3 $DB "$sql"
}
# Call function to read Data
readSerialData