forked from ArduinoJS2023/ArduinoJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
89 lines (72 loc) · 2.73 KB
/
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
require("events").EventEmitter.defaultMaxListeners = 10000;
const express = require("express");
const app = express();
const { createServer } = require("http");
const { Server } = require("socket.io");
const { Board, Led, Servo } = require("johnny-five");
const opn = require("opn");
const startUrl = "http://localhost:3000";
const port = 3000;
const boardPort = "COM4";
// pin setup
const blue = 3;
const green = 5;
const red = 6;
const servoA = 9;
const servoB = 10;
const servoC = 11;
const servoD = 12;
let board = new Board({ repl: false, port: boardPort });
// opn(startUrl, { app: "Google Chrome" });
const dev = process.env.NODE_ENV !== "production";
const next = require("next");
const nextApp = next({ dev });
const nextHandler = nextApp.getRequestHandler();
board.on("ready", () => {
const rgb = new Led.RGB([red, green, blue]);
const servo1 = new Servo(servoA); // Create a new Servo object. Assume the servo is attached to pin 10.
const servo2 = new Servo(servoB); // Create a new Servo object. Assume the servo is attached to pin 10.
const servo3 = new Servo(servoC); // Create a new Servo object. Assume the servo is attached to pin 10.
const servo4 = new Servo(servoD); // Create a new Servo object. Assume the servo is attached to pin 10.
nextApp.prepare().then(() => {
app.get("*", (req, res) => {
return nextHandler(req, res);
});
const server = createServer(app);
const io = new Server(server);
io.on("connection", (socket) => {
console.log("a user connected");
socket.on("disconnect", () => {
console.log("user disconnected");
});
socket.on("color change", (color) => {
// console.log("(back) color changed to:" + color);
rgb.color(color);
});
socket.on("servo1 move", (angle) => {
// Listen to "servo1 move" event
// console.log("(back) servo moved to:" + angle);
servo1.to(angle); // Move the servo to the received angle
});
socket.on("servo2 move", (angle) => {
// Listen to "servo2 move" event
// console.log("(back) servo moved to:" + angle);
servo2.to(angle); // Move the servo to the received angle
});
socket.on("servo3 move", (angle) => {
// Listen to "servo3 move" event
// console.log("(back) servo moved to:" + angle);
servo3.to(angle); // Move the servo to the received angle
});
socket.on("servo4 move", (angle) => {
// Listen to "servo4 move" event
// console.log("(back) servo moved to:" + angle);
servo4.to(angle); // Move the servo to the received angle
});
});
server.listen(port, (err) => {
if (err) throw err;
console.log(`> Ready on http://localhost:${port}`);
});
});
});