-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino-server.js
119 lines (99 loc) · 3.12 KB
/
arduino-server.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// express 불러오기
const express = require("express");
// express 인스턴스 생성
const app = express();
// 포트 정보
const httpPort = 3000;
// cors 불러오기
const cors = require("cors");
// CORS 미들웨어 사용
app.use(cors());
// serialport 및 websocket 불러오기
const { SerialPort } = require("serialport");
const WebSocket = require("ws");
const { exec } = require("child_process");
function uploadArduinoSketch(filePath) {
const compileCommand =
"arduino-cli compile --fqbn arduino:avr:uno " + filePath;
const uploadCommand =
"arduino-cli upload -p /dev/cu.usbmodem1401 --fqbn arduino:avr:uno " +
filePath;
exec(compileCommand, (error, stdout, stderr) => {
if (error) {
console.error(`Compile error: ${error.message}`);
return;
}
console.log(`Compile output: ${stdout}`);
exec(uploadCommand, (error, stdout, stderr) => {
if (error) {
console.error(`Upload error: ${error.message}`);
return;
}
console.log(`Upload output: ${stdout}`);
});
});
}
// 아두이노 포트 찾기
async function findArduinoPort() {
try {
const ports = await SerialPort.list();
const arduinoPort = ports.find(
(port) => port.manufacturer && port.manufacturer.includes("Arduino")
);
return arduinoPort ? arduinoPort.path : null;
} catch (error) {
console.error("Error listing ports:", error);
return null;
}
}
// 웹소켓 서버 설정
(async () => {
const arduinoPath = await findArduinoPort();
if (!arduinoPath) {
console.error("Arduino not found");
return;
}
const serialPort = new SerialPort({ path: arduinoPath, baudRate: 9600 });
const wss = new WebSocket.Server({ port: 8080 });
let latestData = ""; // Buffer to store the latest data
serialPort.on("data", (data) => {
latestData = data.toString(); // Update the buffer with the latest data
});
wss.on("connection", (ws) => {
console.log("Client connected");
const interval = setInterval(() => {
if (latestData) {
console.log("Sending data:", latestData);
ws.send(latestData);
}
}, 5000); // Send data every 5 seconds
ws.on("close", () => {
console.log("Client disconnected");
clearInterval(interval); // Clear interval when client disconnects
});
});
console.log("WebSocket server running on ws://localhost:8080");
})();
// 라우트 설정
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.post("/upload-sketch", (req, res) => {
uploadArduinoSketch("arduino/hci_2ne1ping_start/hci_2ne1ping_start.ino");
res.send("Arduino sketch upload initiated.");
});
app.post("/upload-sketch/:path", (req, res) => {
const path = req.params.path;
if (path === "camera") {
uploadArduinoSketch("arduino/hci_2ne1ping_camera/hci_2ne1ping_camera.ino");
} else if (path === "muse2") {
uploadArduinoSketch("arduino/hci_2ne1ping_muse2/hci_2ne1ping_muse2.ino");
} else {
return res.status(404).send("Path not found");
}
res.send("Arduino sketch upload initiated.");
});
// 서버 실행
app.listen(httpPort, () => {
console.log(`App running on port ${httpPort}...`);
});