Compare commits
6
Commits
a37c3ba7f9
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5482219975 | ||
|
|
78ee463f91 | ||
|
|
80e126a9f4 | ||
|
|
3aef5c642d | ||
|
|
c1d2600f92 | ||
|
|
096f60baa0 |
+41
@@ -0,0 +1,41 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Welcome file</title>
|
||||
<link rel="stylesheet" href="https://stackedit.io/style.css" />
|
||||
</head>
|
||||
|
||||
<body class="stackedit">
|
||||
<div class="stackedit__html">
|
||||
<h1 id="значение-ножки-сейчас--">Значение потенциометра сейчас - <span id="1"></span></h1>
|
||||
<h1 id="значение-ножки-сейчас--">Значение ножки сейчас - <span id="2"></span></h1>
|
||||
</div>
|
||||
<script>
|
||||
setInterval(()=>{
|
||||
fetch('/potentiometer')
|
||||
.then((response) => {
|
||||
return response.json();
|
||||
})
|
||||
|
||||
.then((data) => {
|
||||
document.getElementById("1").innerHTML = data;
|
||||
|
||||
})
|
||||
|
||||
fetch('/gpu')
|
||||
.then((response) => {
|
||||
return response.json();
|
||||
})
|
||||
|
||||
.then((data) => {
|
||||
document.getElementById("2").innerHTML = data;
|
||||
|
||||
})
|
||||
} , 200);
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -8,33 +8,87 @@
|
||||
// port.on('readable', function () {
|
||||
// console.log('Data:', port.read())
|
||||
// })
|
||||
|
||||
|
||||
// // Switches the port into "flowing mode"
|
||||
// port.on('data', function (data) {
|
||||
// console.log('Data:', data.toString('UTF8'))
|
||||
// })
|
||||
|
||||
const fs = require('fs');
|
||||
const http = require('node:http');
|
||||
const { SerialPort } = require('serialport')
|
||||
const { ReadlineParser } = require('@serialport/parser-readline')
|
||||
const {
|
||||
SerialPort
|
||||
} = require('serialport')
|
||||
const {
|
||||
ReadlineParser
|
||||
} = require('@serialport/parser-readline')
|
||||
|
||||
const port = new SerialPort({ path: '/com16', baudRate: 9600 })
|
||||
const port = new SerialPort({
|
||||
path: '/com11',
|
||||
baudRate: 9600
|
||||
})
|
||||
|
||||
var str = "";
|
||||
var obj;
|
||||
|
||||
const parser = port.pipe(new ReadlineParser({ delimiter: '\r\n' }))
|
||||
const parser = port.pipe(new ReadlineParser({
|
||||
delimiter: '\r\n'
|
||||
}))
|
||||
parser.on('data', (data) => {
|
||||
if (str != data) {
|
||||
console.log(data);
|
||||
if (str != data) {
|
||||
try {
|
||||
obj = JSON.parse(data);
|
||||
|
||||
console.log(obj);
|
||||
} catch {
|
||||
console.log("Ошибка");
|
||||
}
|
||||
str = data;
|
||||
|
||||
}
|
||||
str = data;
|
||||
})
|
||||
|
||||
|
||||
// Create a local server to receive data from
|
||||
const server = http.createServer((req, res) => {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(str);
|
||||
|
||||
console.log(req.url);
|
||||
if (req.url == '/') {
|
||||
fs.readFile('index.html', (err, data) => {
|
||||
if (err) {
|
||||
res.writeHead(404, "Not Found");
|
||||
res.write("No file found")
|
||||
res.end();
|
||||
} else {
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'text/html'
|
||||
})
|
||||
res.write(data)
|
||||
res.end();
|
||||
}
|
||||
})
|
||||
} else if (req.url == '/pin') {
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'application/json'
|
||||
});
|
||||
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 {
|
||||
res.writeHead(404, "Not Found");
|
||||
res.end();
|
||||
}
|
||||
});
|
||||
|
||||
server.listen(2048);
|
||||
@@ -1,16 +1,32 @@
|
||||
#include <Servo.h>
|
||||
|
||||
#define PIN_INPUT 7 //вход7
|
||||
|
||||
// Пин для сервопривода
|
||||
int servoPin = 3;
|
||||
// Создаем объект
|
||||
Servo Servo1;
|
||||
|
||||
void setup (){
|
||||
Serial.begin(9600);
|
||||
pinMode(PIN_INPUT, INPUT); //задаём вход
|
||||
Serial.begin(9600);
|
||||
pinMode(A0, INPUT); // к входу A0 подключаем потенциометр
|
||||
pinMode(A1, INPUT); // к входу A1 подключаем gpu
|
||||
Servo1.attach(servoPin);
|
||||
}
|
||||
|
||||
void loop (){
|
||||
if(digitalRead(PIN_INPUT) == HIGH) {
|
||||
Serial.println("1");
|
||||
} else {
|
||||
Serial.println("0");
|
||||
}
|
||||
}
|
||||
void loop (){
|
||||
int valuePotentiometer = analogRead(A0); // считываем данные с порта A0
|
||||
int valueGPU = digitalRead(A1);
|
||||
char varJson[64];
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user