Open API with clent

This commit is contained in:
Nomi Nonsense (Nonszy) 2024-03-10 19:13:28 +07:00
parent f1ccdb35bf
commit 4bc42d6ae9
5 changed files with 12 additions and 4 deletions

1
package-lock.json generated
View File

@ -12,6 +12,7 @@
"@serialport/bindings": "^9.2.9",
"@serialport/list": "^12.0.0",
"chalk": "^4.1.2",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.18.3",
"firmata": "^2.3.0",

View File

@ -15,6 +15,7 @@
"@serialport/bindings": "^9.2.9",
"@serialport/list": "^12.0.0",
"chalk": "^4.1.2",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.18.3",
"firmata": "^2.3.0",

View File

@ -1,20 +1,22 @@
import { Socket } from "socket.io";
import { Pin } from "johnny-five";
import { board } from "../setup";
export default (socket: Socket) => {
console.log(`${socket.id} | ${socket.client.request.headers.host} | Joined`);
socket.on("control-servo", (p: string, ang: string) => {
socket.on("servo", (p: string, ang: string) => {
const pin = Number.parseInt(p);
const angle = Number.parseInt(ang);
board.pinMode(pin, Pin.SERVO);
board.servoWrite(pin, angle);
console.log(socket.id, pin, angle);
})
socket.on("read-photoresistor", (pin: string) => {
socket.on("set-photoresistor", (pin: string) => {
board.analogRead(pin, (value) => {
socket.emit("read-photoresistor", value);
socket.emit("photoresistor", value);
console.log(socket.id, "A"+pin, value);
});
})

View File

@ -1,5 +1,6 @@
import http from 'node:http';
import express from 'express';
import cors from 'cors';
import { Server } from 'socket.io';
import chalk from 'chalk';
@ -23,6 +24,9 @@ const host: string = 'localhost';
const port: number = 3000;
// Express middleware
app.use(cors({
origin: "*"
}));
app.use(express.json());
app.use(express.static('client'));

View File

@ -2,7 +2,7 @@ import { Board, Pin, PinMode } from "johnny-five";
const board: Board = new Board({
port: '/dev/ttyUSB0',
debug: false,
debug: true,
repl: false
});