-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhub.m
76 lines (63 loc) · 1.77 KB
/
hub.m
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
71
72
73
74
75
76
%% Script to read constantly the UART sychronously
clear all
close all
delete(instrfind)
addpath('./AES/AES');
[s_box, inv_s_box, w, poly_mat, inv_poly_mat] = aes_init;
COM_PORT_NUMBER = "/dev/tty.usbmodem2121103";
baud_rate = 9600;
data_bits = 8;
comm_conn = serial(COM_PORT_NUMBER,'BaudRate',baud_rate,'DataBits',data_bits);
datain = '';
dataout = '1';
count = 1;
fopen(comm_conn);
start_time = datetime;
fwrite(comm_conn,dataout);
prev_v = '';
hold on;
while (1)
% Read the available UART data as characters and store the result
ciphertext = fread(comm_conn,16,'uint8');
datain = inv_cipher (ciphertext, w, inv_s_box, inv_poly_mat);
clc;
if count ==1
prev_v = char(datain(7));
else
if char(datain(7)) ~= prev_v
prev_v = datain(7);
end
end
if prev_v == 83
mov_rec_str = "standing";
elseif prev_v == 87
mov_rec_str = "walking";
elseif prev_v == 82
mov_rec_str = "running";
else
mov_rec_str = "fell";
end
mov_rec_print_str = sprintf('Movement: %s', mov_rec_str);
disp(mov_rec_print_str);
if datain(6) == 1 is_in_h_comf_str = "Yes"; else is_in_h_comf_str = "No"; end
h_comf_print_str = sprintf('Is in human comfort: %s', is_in_h_comf_str);
disp(h_comf_print_str);
if(datain(1) > 0)
sign = 1;
else
sign = -1;
end
temps(count)= sign * (datain(2)*100 + datain(3))/100;
hums(count)= (datain(4)*100 + datain(5))/100;
times(count) = datetime - start_time;
subplot(2, 1, 1);
plot(times(1:count) , temps(1:count));
xlabel("Time");
ylabel("Temperture");
subplot(2, 1, 2);
plot(times(1:count) , hums(1:count));
xlabel("Time");
ylabel("Humidity");
drawnow;
count = count + 1;
end