Skip to content

Commit 01ce85a

Browse files
authored
Add files via upload
1 parent 9a00034 commit 01ce85a

File tree

7 files changed

+424
-0
lines changed

7 files changed

+424
-0
lines changed

WebSocketServer.ino

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#include <SPI.h>
2+
#include <Ethernet.h>
3+
4+
// Enabe debug tracing to Serial port.
5+
#define DEBUG
6+
7+
// Here we define a maximum framelength to 64 bytes. Default is 256.
8+
#define MAX_FRAME_LENGTH 64
9+
10+
#include <WebSocket.h>
11+
12+
byte mac[] = { 0x52, 0x4F, 0x43, 0x4B, 0x45, 0x54 };
13+
byte ip[] = { 192, 168, 1 , 10 };
14+
15+
const int voiceSensor = A0;
16+
const int shockSensor = 3;
17+
const int led = 13;
18+
unsigned long last_time;
19+
bool led_flag = 0;
20+
21+
const int max_voice = 1000;
22+
const int min_voice = 300;
23+
24+
const int max_size = 20;
25+
float values[max_size];
26+
int i = 0;
27+
28+
void add(float new_val){
29+
if (i>max_size-1) i=0;
30+
values[i] = new_val;
31+
i++;
32+
}
33+
34+
float mid(){
35+
float result = 0;
36+
for(int j=0;j<max_size;j++){
37+
result+=values[j];
38+
}
39+
return result/(float)max_size;
40+
}
41+
42+
float get_voice(){
43+
float var = mid();
44+
if ((analogRead(voiceSensor)>min_voice)&&(analogRead(voiceSensor)<max_voice))
45+
{
46+
add(analogRead(voiceSensor));
47+
if (abs(var - analogRead(voiceSensor))>20){
48+
led_flag = 1;
49+
last_time = millis();
50+
digitalWrite(led, HIGH);
51+
}
52+
else {
53+
if ((millis() - last_time > 1000) && led_flag)
54+
{
55+
digitalWrite(led, LOW);
56+
led_flag = 0;
57+
}
58+
}
59+
}
60+
return var;
61+
}
62+
63+
int get_shock(){
64+
return digitalRead(shockSensor);
65+
}
66+
67+
// Create a Websocket server
68+
WebSocketServer wsServer;
69+
70+
void onConnect(WebSocket &socket) {
71+
Serial.println("onConnect called");
72+
}
73+
74+
75+
// You must have at least one function with the following signature.
76+
// It will be called by the server when a data frame is received.
77+
void onData(WebSocket &socket, char* dataString, byte frameLength) {
78+
79+
#ifdef DEBUG
80+
Serial.print("Got data: ");
81+
Serial.write((unsigned char*)dataString, frameLength);
82+
Serial.println();
83+
#endif
84+
85+
// Just echo back data for fun.
86+
socket.send(dataString, strlen(dataString));
87+
}
88+
89+
void onDisconnect(WebSocket &socket) {
90+
Serial.println("onDisconnect called");
91+
}
92+
93+
void setup() {
94+
#ifdef DEBUG
95+
Serial.begin(19200);
96+
#endif
97+
Ethernet.begin(mac, ip);
98+
wsServer.registerConnectCallback(&onConnect);
99+
wsServer.registerDataCallback(&onData);
100+
wsServer.registerDisconnectCallback(&onDisconnect);
101+
wsServer.begin();
102+
103+
pinMode(voiceSensor, INPUT);
104+
pinMode(led, OUTPUT);
105+
digitalWrite(led, LOW);
106+
107+
delay(10); // Give Ethernet time to get ready
108+
}
109+
110+
void loop() {
111+
wsServer.listen();
112+
float sound_sensor_val = get_voice();
113+
int sound_shock_val = get_shock();
114+
115+
char str_voice_val[5] = {' '} ;
116+
char str_shock_val[1];
117+
char response[6];
118+
119+
int val_int = (int)sound_sensor_val;
120+
float val_float = (abs(sound_sensor_val) - abs(val_int)) * 10000;
121+
int val_fra = (int)val_float;
122+
sprintf (str_voice_val, "%d.%d", val_int, val_fra);
123+
124+
125+
if (sound_shock_val == 1){
126+
str_shock_val[0] = '1';
127+
}
128+
else{
129+
str_shock_val[0] = '0';
130+
}
131+
132+
response[0]= str_shock_val[0];
133+
for(int i=1; i<7;i++){
134+
response[i] = str_voice_val[i-1];
135+
}
136+
137+
138+
Serial.println(response);
139+
delay(10);
140+
141+
if (wsServer.connectionCount() > 0) {
142+
//wsServer.send("Hello to Aleksey Vladimirovich!!!", 34);
143+
wsServer.send(response, 7);
144+
Serial.println("successfully sent");
145+
}
146+
else{
147+
Serial.println("NO CONNECTION WITH CLIENT");
148+
}
149+
}

data.csv

Whitespace-only changes.

requirements.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
annotated-types==0.6.0
2+
anyio==4.3.0
3+
fastapi==0.110.0
4+
idna==3.6
5+
Jinja2==3.1.3
6+
MarkupSafe==2.1.5
7+
pydantic==2.6.4
8+
pydantic_core==2.16.3
9+
sniffio==1.3.1
10+
starlette==0.36.3
11+
typing_extensions==4.10.0
12+
websockets==12.0

templates/index.html

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
2+
<!DOCTYPE html>
3+
<html lang="en">
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>Arduino Connect</title>
7+
<!--suppress JSUnresolvedLibraryURL -->
8+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.1/css/bootstrap.min.css"
9+
integrity="sha512-T584yQ/tdRR5QwOpfvDfVQUidzfgc2339Lc8uBDtcp/wYu80d7jwBgAxbyMh0a9YM9F8N3tdErpFI8iaGx6x5g=="
10+
crossorigin="anonymous" referrerpolicy="no-referrer"/>
11+
<!--suppress JSUnresolvedLibraryURL -->
12+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.css"
13+
integrity="sha512-/zs32ZEJh+/EO2N1b0PEdoA10JkdC3zJ8L5FTiQu82LR9S/rOQNfQN7U59U9BC12swNeRAz3HSzIL2vpp4fv3w=="
14+
crossorigin="anonymous" referrerpolicy="no-referrer"/>
15+
</head>
16+
<body>
17+
<div class="container">
18+
<div class="row">
19+
<div class="col-12">
20+
<div class="card">
21+
<div class="card-body">
22+
<canvas id="canvas_1"></canvas>
23+
<canvas id="canvas_2"></canvas>
24+
</div>
25+
</div>
26+
</div>
27+
</div>
28+
</div>
29+
<!--suppress JSUnresolvedLibraryURL -->
30+
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"
31+
integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ=="
32+
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
33+
<!--suppress JSUnresolvedLibraryURL -->
34+
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.1/js/bootstrap.bundle.min.js"
35+
integrity="sha512-mULnawDVcCnsk9a4aG1QLZZ6rcce/jSzEGqUkeOLy0b6q0+T6syHrxlsAGH7ZVoqC93Pd0lBqd6WguPWih7VHA=="
36+
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
37+
<!--suppress JSUnresolvedLibraryURL -->
38+
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"
39+
integrity="sha512-d9xgZrVZpmmQlfonhQUvTR7lMPtO7NkZMkA0ABN3PHCbKA5nqylQ/yWlFAyY6hYgdF1Qh6nYiuADWwKB4C2WSw=="
40+
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
41+
<script>
42+
$(document).ready(function () {
43+
const config_1 = {
44+
type: 'line',
45+
data: {
46+
labels: Array(200).fill("0000-00-00 00:00:00"),
47+
datasets: [{
48+
label: "voice_sensor",
49+
backgroundColor: 'rgb(255, 99, 132)',
50+
borderColor: 'rgb(255, 99, 132)',
51+
data: Array(210).fill(null),
52+
fill: false,
53+
}],
54+
},
55+
options: {
56+
responsive: true,
57+
title: {
58+
display: true,
59+
text: 'Датчик звука'
60+
},
61+
tooltips: {
62+
mode: 'index',
63+
intersect: false,
64+
},
65+
hover: {
66+
mode: 'nearest',
67+
intersect: true
68+
},
69+
scales: {
70+
xAxes: [{
71+
display: true,
72+
scaleLabel: {
73+
display: true,
74+
labelString: 'Time'
75+
}
76+
}],
77+
yAxes: [{
78+
display: true,
79+
scaleLabel: {
80+
display: true,
81+
labelString: 'Value'
82+
},
83+
ticks: {
84+
min: 530,
85+
max: 545,
86+
stepSize: 1
87+
}
88+
}]
89+
}
90+
}
91+
}
92+
93+
const config_2 = {
94+
type: 'line',
95+
data: {
96+
labels: Array(200).fill("0000-00-00 00:00:00"),
97+
datasets: [{
98+
label: "shock_sensor",
99+
backgroundColor: 'rgb(255, 99, 132)',
100+
borderColor: 'rgb(255, 99, 132)',
101+
data: Array(210).fill(null),
102+
fill: false,
103+
}],
104+
},
105+
options: {
106+
responsive: true,
107+
title: {
108+
display: true,
109+
text: 'Датчик вибрации'
110+
},
111+
tooltips: {
112+
mode: 'index',
113+
intersect: false,
114+
},
115+
hover: {
116+
mode: 'nearest',
117+
intersect: true
118+
},
119+
scales: {
120+
xAxes: [{
121+
display: true,
122+
scaleLabel: {
123+
display: true,
124+
labelString: 'Time'
125+
}
126+
}],
127+
yAxes: [{
128+
display: true,
129+
scaleLabel: {
130+
display: true,
131+
labelString: 'Value'
132+
},
133+
ticks: {
134+
min: 0,
135+
max: 1,
136+
stepSize: 0.5
137+
}
138+
}
139+
]}
140+
}
141+
};
142+
143+
const context_1 = document.getElementById('canvas_1').getContext('2d');
144+
const lineChart_1 = new Chart(context_1, config_1);
145+
146+
const context_2 = document.getElementById('canvas_2').getContext('2d');
147+
const lineChart_2 = new Chart(context_2, config_2);
148+
149+
150+
const source = new EventSource("/chart-data");
151+
152+
source.onmessage = function (event) {
153+
const data = JSON.parse(event.data);
154+
if (config_1.data.labels.length === 210) {
155+
config_1.data.labels.shift();
156+
config_1.data.datasets[0].data.shift();
157+
}
158+
if (config_2.data.labels.length === 210) {
159+
config_2.data.labels.shift();
160+
config_2.data.datasets[0].data.shift();
161+
}
162+
163+
config_1.data.labels.push(data.time);
164+
config_1.data.datasets[0].data.push(data.voice_sensor);
165+
config_2.data.labels.push(data.time);
166+
config_2.data.datasets[0].data.push(data.shock_sensor);
167+
lineChart_1.update();
168+
lineChart_2.update();
169+
}
170+
});
171+
</script>
172+
</body>
173+
</html>

0 commit comments

Comments
 (0)