1
+ <!DOCTYPE html>
2
+ < html >
3
+ < head >
4
+ < title > Edge based Human Activity Recognition</ title >
5
+ < meta name ="viewport " content ="width=device-width, initial-scale=1 ">
6
+ < meta name ="author " content ="Francesco Colasante ">
7
+ < meta charset ="UTF-8 ">
8
+ <!-- CSS only -->
9
+ < link rel ="stylesheet " href ="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css " integrity ="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk " crossorigin ="anonymous ">
10
+
11
+ </ head >
12
+ < body >
13
+
14
+ < div class ="jumbotron jumbotron-fluid ">
15
+ < div class ="container ">
16
+ < h1 class ="display-4 "> Crowd sensing</ h1 >
17
+ < small > Francesco Colasante</ small >
18
+ </ div >
19
+ </ div >
20
+ < h7 > Your local IP address is: </ h7 >
21
+ < h7 id ="ip "> ip address</ h7 >
22
+ <!--Get device public ip address -->
23
+ < script type ="application/javascript ">
24
+ function getIP ( json ) {
25
+ let ip = document . getElementById ( "ip" ) ;
26
+ ip . innerHTML = json . ip ;
27
+ }
28
+ </ script >
29
+ < script type ="application/javascript " src ="https://api.ipify.org?format=jsonp&callback=getIP "> </ script >
30
+
31
+ < div class ="card ">
32
+ < div class ="card-header ">
33
+ Edge-based
34
+ </ div >
35
+ < div class ="card-body ">
36
+ < h5 class ="card-title "> Linear Accelerometer raw data</ h5 >
37
+ < p > </ p >
38
+ < div class ="card-text " id ="lad "> No data</ div >
39
+ < div class ="progress ">
40
+ < div id ="x " class ="progress-bar bg-success " role ="progressbar " style ="width: 0% " aria-valuenow ="100 "
41
+ aria-valuemin ="0 " aria-valuemax ="100 "> </ div >
42
+ </ div >
43
+ < div class ="progress ">
44
+ < div id ="y " class ="progress-bar bg-info " role ="progressbar " style ="width: 0% " aria-valuenow ="100 "
45
+ aria-valuemin ="0 " aria-valuemax ="100 "> </ div >
46
+ </ div >
47
+ < div class ="progress ">
48
+ < div id ="z " class ="progress-bar bg-warning " role ="progressbar " style ="width: 0% " aria-valuenow ="100 "
49
+ aria-valuemin ="0 " aria-valuemax ="100 "> </ div >
50
+ </ div >
51
+ < div id ="k "> Can't compute standard deviation</ div >
52
+ < div id ="stat " class ="alert alert-success " role ="alert ">
53
+ Waiting for data
54
+ </ div >
55
+
56
+ < a href ="https://demo.thingsboard.io:/dashboard/b61584a0-9466-11ea-9a66-358c6522b855?publicId=d2ff5950-6a96-11ea-8e0a-7d0ef2a682d3 ""
57
+ class="btn btn-primary"> Live Dashboard</ a >
58
+ </ div >
59
+ </ div >
60
+
61
+ < div class ="embed-responsive embed-responsive-4by3 ">
62
+ < iframe class ="embed-responsive-item "
63
+ src ="https://demo.thingsboard.io:/dashboard/7c023c00-99ab-11ea-ba29-93c92b718da1?publicId=c81c0600-994e-11ea-8945-557a14320ccf "
64
+ allowfullscreen > </ iframe >
65
+ </ iframe >
66
+ </ div >
67
+ Console Log:
68
+ < div id ="cnsl " class ="alert alert-dark " style ="height: 100px; " role ="alert ">
69
+ </ div >
70
+
71
+ < script >
72
+ let lad = document . getElementById ( 'lad' ) ;
73
+ let x = document . getElementById ( 'x' ) ;
74
+ let y = document . getElementById ( 'y' ) ;
75
+ let z = document . getElementById ( 'z' ) ;
76
+ let k = document . getElementById ( 'k' ) ;
77
+ let stat = document . getElementById ( 'stat' ) ;
78
+ let ip = document . getElementById ( 'ip' ) . innerHTML ;
79
+
80
+ let cnsl = document . getElementById ( 'cnsl' ) ;
81
+ function print ( text , append = true ) {
82
+ if ( append )
83
+ cnsl . innerHTML += text + '<br>' ;
84
+ else
85
+ cnsl . innerHTML = text ;
86
+ }
87
+ print ( "Console is ready" ) ;
88
+
89
+ //thingsboard device access token
90
+ let token = "BApw7f9Vxs5AhofNrRdf" ;
91
+
92
+ //check sensor on device
93
+ if ( 'LinearAccelerationSensor' in window ) {
94
+ //a window is formed by 4 relevation
95
+ let win = 4 ;
96
+
97
+ //array for saving retrived data locally
98
+ let data = [ ] ;
99
+
100
+ let i = 0 ;
101
+
102
+ //initialize sensor with frequency 1Hz
103
+ let las = new LinearAccelerationSensor ( { frequency : 1 } ) ;
104
+ //function exectued at each detection
105
+ las . addEventListener ( 'reading' , function ( la ) {
106
+ // write values on html page
107
+ x . style . width = la . target . x * 100 + "%" ;
108
+ y . style . width = la . target . y * 100 + "%" ;
109
+ z . style . width = la . target . z * 100 + "%" ;
110
+
111
+ lad . innerHTML = 'x: ' + la . target . x + '<br> y: ' + la . target . y + '<br> z: ' + la . target . z ;
112
+ print ( 'x: ' + la . target . x + '<br> y: ' + la . target . y + '<br> z: ' + la . target . z ) ;
113
+ if ( i < win ) {
114
+ //save values in the arrays
115
+ data [ i ] = { x : Math . abs ( la . target . x ) , y : Math . abs ( la . target . y ) , z : Math . abs ( la . target . z ) } ;
116
+ i ++ ;
117
+ } else {
118
+ //compute standard deviation
119
+ let j ;
120
+ print ( "Calculating data" , false ) ;
121
+ print ( "Data" + JSON . stringify ( data ) ) ;
122
+ //l_sma = 1/win * sum(x_i+y_i+z_i)
123
+ let l_sma = data . map ( val => val . x + val . y + val . z ) . reduce ( ( acc , val ) => acc + val ) ;
124
+ l_sma /= win ;
125
+ print ( "LSMA:" + JSON . stringify ( l_sma ) ) ;
126
+ let tmp = data . map ( val => val . x + val . y + val . z - l_sma ) . map ( x => Math . pow ( x , 2 ) ) ;
127
+ let sosd = tmp . reduce ( ( acc , val ) => acc + val ) ;
128
+ print ( "sosd:" + sosd ) ;
129
+ sosd /= win ;
130
+ sosd = Math . sqrt ( sosd ) ;
131
+ //print standard deviation on html page
132
+ k . innerHTML = "standard deviation: " + sosd ;
133
+
134
+ //empirical treshold for standard deviation is 0.5
135
+ if ( sosd < 0.5 ) {
136
+ stat . innerHTML = "Still" ;
137
+ stat . className = 'alert alert-danger'
138
+ }
139
+ else {
140
+ stat . innerHTML = "Moving" ;
141
+ stat . className = 'alert alert-success'
142
+ }
143
+
144
+ //cerate message to be sent
145
+ let msg = `{\"user\":${ ip } ,\"status\":\"${ stat . innerHTML } \"}` ;
146
+ console . log ( "msg" + msg ) ;
147
+
148
+ //TODO: send the message
149
+
150
+ const Http = new XMLHttpRequest ( ) ;
151
+ const url = `https://demo.thingsboard.io/api/v1/${ token } /telemetry` ;
152
+ Http . open ( "POST" , url ) ;
153
+ Http . send ( msg ) ;
154
+
155
+ //restert the loop
156
+ i = 0 ;
157
+ data [ i ] = { x : Math . abs ( la . target . x ) , y : Math . abs ( la . target . y ) , z : Math . abs ( la . target . z ) } ;
158
+ }
159
+ } ) ;
160
+ //start the sensor
161
+ las . start ( ) ;
162
+ }
163
+
164
+ //error messgae
165
+ else lad . innerHTML = 'Linear Accelerometer not supported' ;
166
+ </ script >
167
+ </ body >
168
+ </ html >
0 commit comments