Compare commits
3 Commits
3aef5c642d
...
5482219975
Author | SHA1 | Date | |
---|---|---|---|
5482219975 | |||
78ee463f91 | |||
80e126a9f4 |
23
index.html
23
index.html
@ -9,19 +9,32 @@
|
|||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body class="stackedit">
|
<body class="stackedit">
|
||||||
<div class="stackedit__html"><h1 id="значение-ножки-сейчас--">Значение ножки сейчас - <span id="wertyui"></span></h1>
|
<div class="stackedit__html">
|
||||||
|
<h1 id="значение-ножки-сейчас--">Значение потенциометра сейчас - <span id="1"></span></h1>
|
||||||
|
<h1 id="значение-ножки-сейчас--">Значение ножки сейчас - <span id="2"></span></h1>
|
||||||
</div>
|
</div>
|
||||||
<script>
|
<script>
|
||||||
setInterval(()=>
|
setInterval(()=>{
|
||||||
fetch('/pin')
|
fetch('/potentiometer')
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
return response.json();
|
return response.json();
|
||||||
})
|
})
|
||||||
|
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
document.getElementById("wertyui").innerHTML = data;
|
document.getElementById("1").innerHTML = data;
|
||||||
|
|
||||||
})
|
})
|
||||||
, 2000);
|
|
||||||
|
fetch('/gpu')
|
||||||
|
.then((response) => {
|
||||||
|
return response.json();
|
||||||
|
})
|
||||||
|
|
||||||
|
.then((data) => {
|
||||||
|
document.getElementById("2").innerHTML = data;
|
||||||
|
|
||||||
|
})
|
||||||
|
} , 200);
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
|
27
index.js
27
index.js
@ -23,18 +23,26 @@ const {
|
|||||||
} = require('@serialport/parser-readline')
|
} = require('@serialport/parser-readline')
|
||||||
|
|
||||||
const port = new SerialPort({
|
const port = new SerialPort({
|
||||||
path: '/com9',
|
path: '/com11',
|
||||||
baudRate: 9600
|
baudRate: 9600
|
||||||
})
|
})
|
||||||
|
|
||||||
var str = "";
|
var str = "";
|
||||||
|
var obj;
|
||||||
|
|
||||||
const parser = port.pipe(new ReadlineParser({
|
const parser = port.pipe(new ReadlineParser({
|
||||||
delimiter: '\r\n'
|
delimiter: '\r\n'
|
||||||
}))
|
}))
|
||||||
parser.on('data', (data) => {
|
parser.on('data', (data) => {
|
||||||
if (str != data) {
|
if (str != data) {
|
||||||
console.log(data);
|
try {
|
||||||
|
obj = JSON.parse(data);
|
||||||
|
|
||||||
|
console.log(obj);
|
||||||
|
} catch {
|
||||||
|
console.log("Ошибка");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
str = data;
|
str = data;
|
||||||
})
|
})
|
||||||
@ -42,6 +50,7 @@ parser.on('data', (data) => {
|
|||||||
|
|
||||||
// Create a local server to receive data from
|
// Create a local server to receive data from
|
||||||
const server = http.createServer((req, res) => {
|
const server = http.createServer((req, res) => {
|
||||||
|
|
||||||
console.log(req.url);
|
console.log(req.url);
|
||||||
if (req.url == '/') {
|
if (req.url == '/') {
|
||||||
fs.readFile('index.html', (err, data) => {
|
fs.readFile('index.html', (err, data) => {
|
||||||
@ -62,6 +71,20 @@ const server = http.createServer((req, res) => {
|
|||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
});
|
});
|
||||||
res.end(str);
|
res.end(str);
|
||||||
|
} else if (req.url == '/potentiometer') {
|
||||||
|
res.writeHead(200, {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
});
|
||||||
|
if (obj) {
|
||||||
|
res.end(obj.valuePotentiometer.toString());
|
||||||
|
}
|
||||||
|
} else if (req.url == '/gpu') {
|
||||||
|
res.writeHead(200, {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
});
|
||||||
|
if (obj) {
|
||||||
|
res.end(obj.valueGPU.toString());
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
res.writeHead(404, "Not Found");
|
res.writeHead(404, "Not Found");
|
||||||
res.end();
|
res.end();
|
||||||
|
@ -1,16 +1,32 @@
|
|||||||
|
#include <Servo.h>
|
||||||
|
|
||||||
#define PIN_INPUT 7 //вход7
|
// Пин для сервопривода
|
||||||
|
int servoPin = 3;
|
||||||
|
// Создаем объект
|
||||||
|
Servo Servo1;
|
||||||
|
|
||||||
void setup (){
|
void setup (){
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
pinMode(PIN_INPUT, INPUT); //задаём вход
|
pinMode(A0, INPUT); // к входу A0 подключаем потенциометр
|
||||||
|
pinMode(A1, INPUT); // к входу A1 подключаем gpu
|
||||||
|
Servo1.attach(servoPin);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop (){
|
void loop (){
|
||||||
if(digitalRead(PIN_INPUT) == HIGH) {
|
int valuePotentiometer = analogRead(A0); // считываем данные с порта A0
|
||||||
Serial.println("1");
|
int valueGPU = digitalRead(A1);
|
||||||
} else {
|
char varJson[64];
|
||||||
Serial.println("0");
|
sprintf(varJson, "{\"valuePotentiometer\": %d, \"valueGPU\": %d}", valuePotentiometer, valueGPU);
|
||||||
}
|
Serial.println(varJson);
|
||||||
|
|
||||||
|
Servo1.write(valuePotentiometer / 12);
|
||||||
|
// // 0 градусов
|
||||||
|
// Servo1.write(0);
|
||||||
|
// delay(1000);
|
||||||
|
// // 90 градусов
|
||||||
|
// Servo1.write(90);
|
||||||
|
// delay(1000);
|
||||||
|
// // 180 градусов
|
||||||
|
// Servo1.write(180);
|
||||||
|
// delay(1000);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user