working servo and photoresistor with websocket

This commit is contained in:
2024-03-05 00:26:06 +07:00
parent f897b3bf50
commit 80bedd3cb3
11 changed files with 93 additions and 31 deletions

View File

@@ -0,0 +1,15 @@
import { Board, Pin } from "johnny-five";
const board: Board = new Board({
port: '/dev/ttyUSB0',
debug: false,
repl: false
});
board.on("ready", async () => {
board.pinMode(14, Pin.INPUT);
board.analogRead(0, (val) => {
console.log(val);
})
});

19
test/serial/servo.ts Normal file
View File

@@ -0,0 +1,19 @@
import { Board, Pin, PinMode } from "johnny-five";
const board: Board = new Board({
port: '/dev/ttyUSB0',
debug: false,
repl: false
});
board.on("ready", async () => {
board.pinMode(9, Pin.SERVO);
board.servoWrite(9, 0);
while (true) {
board.servoWrite(9, 180);
await new Promise(resolve => setTimeout(resolve, 1000));
board.servoWrite(9, 0);
await new Promise(resolve => setTimeout(resolve, 1000));
}
});

19
test/websocket/common.ts Normal file
View File

@@ -0,0 +1,19 @@
import http from 'node:http';
import express from 'express';
import { Server } from "socket.io";
const app = express();
const server = http.createServer(app);
const io = new Server(server);
io.on("connection", (socket) => {
console.log("Connected");
socket.on("message", (message) => {
console.log(message);
})
})
server.listen(3001, "localhost", () => {
console.log("Common server is running");
})