goddamit idk it will be repository
This commit is contained in:
commit
9b30e597e9
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
node_modules/
|
||||||
|
dist/
|
||||||
|
build/
|
||||||
|
builds/
|
5
nodemon.json
Normal file
5
nodemon.json
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"watch": ["src"],
|
||||||
|
"ext": ".ts,.js",
|
||||||
|
"exec": "ts-node ./src/index.ts"
|
||||||
|
}
|
2492
package-lock.json
generated
Normal file
2492
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
27
package.json
Normal file
27
package.json
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
{
|
||||||
|
"name": "lunar-vein-arduino",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "A test to arduino",
|
||||||
|
"main": "index.ts",
|
||||||
|
"author": "Norman Andrians",
|
||||||
|
"license": "MIT",
|
||||||
|
"private": true,
|
||||||
|
"scripts": {
|
||||||
|
"start": "nodemon",
|
||||||
|
"test-blink": "nodemon test/blink.js"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@serialport/bindings": "^9.2.9",
|
||||||
|
"express": "^4.18.3",
|
||||||
|
"firmata": "^2.3.0",
|
||||||
|
"nodemon": "^3.1.0",
|
||||||
|
"serialport": "^12.0.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/express": "^4.17.21",
|
||||||
|
"@types/firmata": "^0.19.8",
|
||||||
|
"@types/nodemon": "^1.19.6",
|
||||||
|
"ts-node": "^10.9.2",
|
||||||
|
"typescript": "^5.3.3"
|
||||||
|
}
|
||||||
|
}
|
16
src/client/index.html
Normal file
16
src/client/index.html
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Firmata Arduino Client</title>
|
||||||
|
<link rel="stylesheet" href="style/main.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<section>
|
||||||
|
<h2>LED</h2>
|
||||||
|
<button id="toggleLed1">ON/OFF</button>
|
||||||
|
</section>
|
||||||
|
<script src="./scripts/main.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
17
src/client/scripts/main.js
Normal file
17
src/client/scripts/main.js
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
document.getElementById('toggleLed1').addEventListener('mousedown', async () => {
|
||||||
|
const res = await fetch('/api-arduino/led/13/on', {
|
||||||
|
method: 'PATCH'
|
||||||
|
});
|
||||||
|
const data = await res.json();
|
||||||
|
|
||||||
|
console.log(data);
|
||||||
|
})
|
||||||
|
|
||||||
|
document.getElementById('toggleLed1').addEventListener('mouseup', async () => {
|
||||||
|
const res = await fetch('/api-arduino/led/13/off', {
|
||||||
|
method: 'PATCH'
|
||||||
|
});
|
||||||
|
const data = await res.json();
|
||||||
|
|
||||||
|
console.log(data);
|
||||||
|
})
|
0
src/client/style/main.css
Normal file
0
src/client/style/main.css
Normal file
0
src/controller/led.ts
Normal file
0
src/controller/led.ts
Normal file
91
src/index.ts
Normal file
91
src/index.ts
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
import express, { Request, Response } from 'express';
|
||||||
|
import Firmata from 'firmata';
|
||||||
|
import promises from './promises';
|
||||||
|
const app = express();
|
||||||
|
|
||||||
|
const board = new Firmata('/dev/ttyUSB0');
|
||||||
|
|
||||||
|
const host = 'localhost';
|
||||||
|
const port = 3000;
|
||||||
|
|
||||||
|
const PIN = {
|
||||||
|
servo: 0,
|
||||||
|
rgb_led: {
|
||||||
|
r: 10,
|
||||||
|
g: 9,
|
||||||
|
b: 8
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
app.use(express.static('client'));
|
||||||
|
|
||||||
|
app.get('/', (req: Request, res: Response) => {
|
||||||
|
res.sendFile('./client/index.html', { root: __dirname });
|
||||||
|
})
|
||||||
|
|
||||||
|
app.get('/api-arduino/led/:p', async (req: Request, res: Response) => {
|
||||||
|
const { p } = req.params;
|
||||||
|
const pin = Number.parseInt(p);
|
||||||
|
|
||||||
|
if (Number.isNaN(pin)) {
|
||||||
|
return res.status(400).json({
|
||||||
|
status: 400,
|
||||||
|
message: 'Invalid pin param, it should be integer'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const pinState = board.pins[pin].value;
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
status: 200,
|
||||||
|
message: `Pin ${pin} state ${pinState}`
|
||||||
|
});
|
||||||
|
})
|
||||||
|
|
||||||
|
app.patch('/api-arduino/led/:p/:a', (req: Request, res: Response) => {
|
||||||
|
const { p, a } = req.params;
|
||||||
|
const act = a.toLocaleLowerCase();
|
||||||
|
const pin = Number.parseInt(p);
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (Number.isNaN(pin)) {
|
||||||
|
return res.status(400).json({
|
||||||
|
status: 400,
|
||||||
|
message: 'Invalid pin param, it should be integer'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (act) {
|
||||||
|
case 'on':
|
||||||
|
board.digitalWrite(pin, board.HIGH);
|
||||||
|
console.log(`${req.hostname} | ${pin} | ${act.toLocaleUpperCase()}`);
|
||||||
|
break;
|
||||||
|
case 'off':
|
||||||
|
board.digitalWrite(pin, board.LOW);
|
||||||
|
console.log(`${req.hostname} | ${pin} | ${act.toLocaleUpperCase()}`);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
console.log(`${req.hostname} | ${pin} | INVALID ACT`);
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(200).json({
|
||||||
|
status: 200,
|
||||||
|
message: `Success changed pin ${pin} to state ${act}`
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
res.status(500).json({
|
||||||
|
status: 500,
|
||||||
|
message: "Internal Server Error"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.listen(port, host, () => {
|
||||||
|
console.log(`Server is running in ${host} at port ${port}`);
|
||||||
|
console.log("Connecting Board");
|
||||||
|
board.on('ready', () => {
|
||||||
|
console.log(`Board ${board.ports[0]} Connected`);
|
||||||
|
})
|
||||||
|
});
|
9
src/promises/index.js
Normal file
9
src/promises/index.js
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
function digitalRead (board, pin) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
board.digitalRead(pin, (val) => {
|
||||||
|
resolve(val);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { digitalRead };
|
2
src/routes/api.ts
Normal file
2
src/routes/api.ts
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
import { Router } from "express";
|
||||||
|
|
0
src/routes/client.ts
Normal file
0
src/routes/client.ts
Normal file
17
src/test/blink.js
Normal file
17
src/test/blink.js
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
const Firmata = require('firmata');
|
||||||
|
const board = new Firmata('/dev/ttyUSB0');
|
||||||
|
|
||||||
|
board.on('ready', async () => {
|
||||||
|
const led = 12;
|
||||||
|
|
||||||
|
board.pinMode(led, board.MODES.OUTPUT);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
board.digitalWrite(led, board.HIGH);
|
||||||
|
console.log("ON");
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 300));
|
||||||
|
board.digitalWrite(led, board.LOW);
|
||||||
|
console.log("OFF");
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 300));
|
||||||
|
}
|
||||||
|
});
|
20
src/test/read.js
Normal file
20
src/test/read.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
const Firmata = require('firmata');
|
||||||
|
const board = new Firmata('/dev/ttyUSB0');
|
||||||
|
|
||||||
|
board.on('ready', async () => {
|
||||||
|
const led = 12;
|
||||||
|
|
||||||
|
board.pinMode(led, board.MODES.OUTPUT);
|
||||||
|
|
||||||
|
let state = board.LOW;
|
||||||
|
|
||||||
|
board.digitalWrite(led, board.HIGH);
|
||||||
|
|
||||||
|
state = board.pins[led].value;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
console.log(state);
|
||||||
|
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||||
|
}
|
||||||
|
});
|
10
tsconfig.json
Normal file
10
tsconfig.json
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"module": "NodeNext",
|
||||||
|
"moduleResolution": "NodeNext",
|
||||||
|
"target": "ES2020",
|
||||||
|
"sourceMap": false,
|
||||||
|
"outDir": "dist"
|
||||||
|
},
|
||||||
|
"include": ["src/**/*"]
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user