Update API docs
This commit is contained in:
parent
737a2e2fd3
commit
76e9ae14e5
217
README.md
217
README.md
@ -1,28 +1,225 @@
|
|||||||
# Lunar Vein: Arduino
|
# Lunar Vein: Arduino
|
||||||
|
|
||||||
A REST API based serial communication, assisted Firmata protocol and johnny-five API, enabling software communication with the Arduino board using the Server API., that's it. Idk about electronics and networking actually. But that piece of knowledge motivated me to interact it with other software. Enjoy (づ ̄ ³ ̄)づ
|
A **SIMPLE** REST API based serial communication. Assisted Firmata protocol and johnny-five API, enabling software communication with the Arduino board using the Server API. that's it. Idk about electronics and networking actually. But that piece of knowledge motivated me to make some IoT stuff. Enjoy (づ ̄ ³ ̄)づ
|
||||||
|
|
||||||
## Setup
|
## Setup
|
||||||
|
Tools that are required
|
||||||
|
- Arduino Board
|
||||||
|
- [Arduino IDE](https://www.arduino.cc/en/software)
|
||||||
|
- [Node.js](https://nodejs.org/en) v16.0.0^ or other javascript runtime
|
||||||
|
|
||||||
### Board
|
### Board
|
||||||
Arduino assembly guide also available in [johnny-five](https://github.com/rwaldron/johnny-five?tab=readme-ov-file#setup-and-assemble-arduino) documentation
|
Arduino assembly guide also available in [johnny-five](https://github.com/rwaldron/johnny-five?tab=readme-ov-file#setup-and-assemble-arduino) documentation\
|
||||||
1. Download [Arduino IDE](https://www.arduino.cc/en/software)
|
1. Plug in your Board via USB
|
||||||
2. Plug in your Board via USB
|
2. Open Arduino IDE then select your Board
|
||||||
3. Open Arduino IDE then select your Board
|
3. Go to `File > Examples > Firmata` Select `StandarFirmataPlus`
|
||||||
4. Go to `File > Examples > Firmata` Select `StandarFirmataPlus`
|
4. Upload the sketch
|
||||||
5. Upload the sketch
|
|
||||||
|
|
||||||
### Server
|
### Server
|
||||||
1. Clone project and Install
|
1. Clone project and Install
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
> git clone https://github.com/norman-andrians/lunar-vein-arduino.git && cd lunar-vein-arduino
|
git clone https://github.com/norman-andrians/lunar-vein-arduino.git && cd lunar-vein-arduino
|
||||||
> npm i
|
npm i
|
||||||
```
|
```
|
||||||
|
|
||||||
2. Add `.env` file in the project root directory with the `SERIAL_PORT` variable
|
2. Add `.env` file in the project root directory with the `SERIAL_PORT` variable
|
||||||
|
|
||||||
```
|
```
|
||||||
SERIAL_PORT=/dev/ttyUSB0
|
SERIAL_PORT=/dev/ttyUSB0
|
||||||
```
|
```
|
||||||
|
|
||||||
3. Run the project
|
3. Run the project
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
> npm start
|
npm start
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Other scripts
|
||||||
|
- You can see what ports are connected to the device by running `npm run ports` script.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run ports
|
||||||
|
```
|
||||||
|
```
|
||||||
|
> lunar-vein-arduino@1.0.0 ports
|
||||||
|
> ts-node src/ports/print.ts
|
||||||
|
|
||||||
|
3 Ports available
|
||||||
|
1. /dev/ttyUSB0
|
||||||
|
2. /dev/ttyUSB1
|
||||||
|
3. /dev/ttyACM0
|
||||||
|
```
|
||||||
|
|
||||||
|
# API Documentation
|
||||||
|
|
||||||
|
## Table of contents
|
||||||
|
- [Common HTTP Responses](#common-http-responses)
|
||||||
|
- [JSON Response](#json-response)
|
||||||
|
- [Example Request](#example-request)
|
||||||
|
- [PIN](#pin)
|
||||||
|
- [Read pin mode](#read-pin-mode)
|
||||||
|
- [Set pin mode](#set-pin-mode)
|
||||||
|
- [LED](#led)
|
||||||
|
- [Read led state](#read-led-state)
|
||||||
|
- [Set led state](#set-led-state)
|
||||||
|
|
||||||
|
## Common HTTP Responses
|
||||||
|
Some HTTP responses are sent with JSON, otherwise an HTML body will be sent which is the default express.js response
|
||||||
|
|
||||||
|
| HTTP Status | Marks |
|
||||||
|
|-------------|-------|
|
||||||
|
| 200 | The request `act` was successful |
|
||||||
|
| 400 | You may be making an invalid request, try to check the payload or recheck the documentation. |
|
||||||
|
| 404 | The resource wass not found |
|
||||||
|
| 405 | Method not allowed, servers usually only accept `GET` and `PATCH` methods |
|
||||||
|
| 500 | An error in the app server, if it continues please raise an issue or ask to contribute. |
|
||||||
|
|
||||||
|
## JSON Response
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"status": 200,
|
||||||
|
"pin_state": {
|
||||||
|
"13": "HIGH"
|
||||||
|
},
|
||||||
|
"message": "Pin 13 Set to HIGH"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
| Property | Description |
|
||||||
|
|----------|-------------|
|
||||||
|
| status | HTTP Status code |
|
||||||
|
| pin_state | The state value of the pin that has been changed |
|
||||||
|
| message | Descriptive message of the changed state |
|
||||||
|
|
||||||
|
## Example Request
|
||||||
|
This is an example of javascript code sending a request to turn on the LED light, [See LED API](#set-led-state)
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
async function turnOnLed() {
|
||||||
|
const res = await fetch("http://localhost:3000/led/13/on"); // Set LED to HIGH
|
||||||
|
const data = await res.json();
|
||||||
|
console.log(data);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## PIN
|
||||||
|
|
||||||
|
### Read pin mode
|
||||||
|
- **URL Endpoint**
|
||||||
|
|
||||||
|
/pin/:p/
|
||||||
|
|
||||||
|
- **URL Params**
|
||||||
|
|
||||||
|
| Params | Mark | Type | Required | Description |
|
||||||
|
|--------|------|------|----------|-------------|
|
||||||
|
| p | pin | `string` | true | Seleced pin |
|
||||||
|
|
||||||
|
- **Method**
|
||||||
|
|
||||||
|
`GET`
|
||||||
|
|
||||||
|
- **Sample Response**
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"status": 200,
|
||||||
|
"pins": [
|
||||||
|
{
|
||||||
|
"pin": 13,
|
||||||
|
"mode": "OUTPUT"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"message": "Pin 13 is OUTPUT"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Set pin mode
|
||||||
|
- **URL Endpoint**
|
||||||
|
|
||||||
|
/pin/:p/:m
|
||||||
|
|
||||||
|
- **URL Params**
|
||||||
|
|
||||||
|
| Params | Mark | Type | Required | Description |
|
||||||
|
|--------|------|------|----------|-------------|
|
||||||
|
| p | pin | `string` | true | Seleced pin |
|
||||||
|
| m | mode | `'input'`, `'output'`, `'servo'` | true | Pin Mode |
|
||||||
|
|
||||||
|
- **Method(s)**
|
||||||
|
|
||||||
|
`PATCH`
|
||||||
|
|
||||||
|
- **Sample Response**
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"status": 200,
|
||||||
|
"pins": [
|
||||||
|
{
|
||||||
|
"pin": 13,
|
||||||
|
"mode": "OUTPUT"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"message": "Pin 13 setted as OUTPUT"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## LED
|
||||||
|
|
||||||
|
### Read led state
|
||||||
|
- **URL Endpoint**
|
||||||
|
|
||||||
|
/led/:p/
|
||||||
|
|
||||||
|
- **URL Params**
|
||||||
|
|
||||||
|
| Params | Mark | Type | Required | Description |
|
||||||
|
|--------|------|------|----------|-------------|
|
||||||
|
| p | pin | `string` | true | Seleced pin |
|
||||||
|
|
||||||
|
- **Method**
|
||||||
|
|
||||||
|
`GET`
|
||||||
|
|
||||||
|
- **Sample Response**
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"status": 200,
|
||||||
|
"pin_state": {
|
||||||
|
"13": "HIGH"
|
||||||
|
},
|
||||||
|
"message": "Led pin 13 is HIGH"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Set led state
|
||||||
|
- **URL Endpoint**
|
||||||
|
|
||||||
|
/pin/:p/:a
|
||||||
|
|
||||||
|
- **URL Params**
|
||||||
|
|
||||||
|
| Params | Mark | Type | Required | Description |
|
||||||
|
|--------|------|------|----------|-------------|
|
||||||
|
| p | pin | `string` | true | Seleced pin |
|
||||||
|
| a | act | `'on'`, `'off'` | true | Action |
|
||||||
|
|
||||||
|
- **Method(s)**
|
||||||
|
|
||||||
|
`PATCH`
|
||||||
|
|
||||||
|
- **Sample Response**
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"status": 200,
|
||||||
|
"pin_state": {
|
||||||
|
"13": "HIGH"
|
||||||
|
},
|
||||||
|
"message": "Led pin 13 setted HIGH"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
@ -4,7 +4,8 @@ import { ChannelPins, digitalValue, voltage } from ".";
|
|||||||
import { Led } from "johnny-five";
|
import { Led } from "johnny-five";
|
||||||
|
|
||||||
|
|
||||||
export async function readLed (req: Request, res: Response) {
|
export function readLed (req: Request, res: Response): Response<string | any> {
|
||||||
|
try {
|
||||||
const { p } = req.params;
|
const { p } = req.params;
|
||||||
const pin: number = Number.parseInt(p);
|
const pin: number = Number.parseInt(p);
|
||||||
|
|
||||||
@ -16,9 +17,17 @@ export async function readLed (req: Request, res: Response) {
|
|||||||
[pin]: pinState
|
[pin]: pinState
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
return res.status(500).json({
|
||||||
|
status: 200,
|
||||||
|
message: "Internal server error"
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function writeLed (req: Request, res: Response) {
|
export function writeLed (req: Request, res: Response): Response<string | any> {
|
||||||
const { p, a } = req.params;
|
const { p, a } = req.params;
|
||||||
const act: string = a.toLocaleLowerCase();
|
const act: string = a.toLocaleLowerCase();
|
||||||
const pin: number = Number.parseInt(p);
|
const pin: number = Number.parseInt(p);
|
||||||
@ -63,11 +72,12 @@ export async function writeLed (req: Request, res: Response) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
export async function readRgbLed (req: Request, res: Response) {
|
export function readRgbLed (req: Request, res: Response): Response<string | any> {
|
||||||
const r: number = Number.parseInt(req.body.r);
|
const r: number = Number.parseInt(req.body.r);
|
||||||
const g: number = Number.parseInt(req.body.g);
|
const g: number = Number.parseInt(req.body.g);
|
||||||
const b: number = Number.parseInt(req.body.b);
|
const b: number = Number.parseInt(req.body.b);
|
||||||
|
|
||||||
|
try {
|
||||||
const rgbPins: number[] = Object.values({ r, g, b });
|
const rgbPins: number[] = Object.values({ r, g, b });
|
||||||
|
|
||||||
for (let i = 0; i < rgbPins.length; i++) {
|
for (let i = 0; i < rgbPins.length; i++) {
|
||||||
@ -101,9 +111,17 @@ export async function readRgbLed (req: Request, res: Response) {
|
|||||||
[b]: led.blue.isOn
|
[b]: led.blue.isOn
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
return res.status(500).json({
|
||||||
|
status: 200,
|
||||||
|
message: "Internal server error"
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function writeRgbLed (req: Request, res: Response) {
|
export function writeRgbLed (req: Request, res: Response): Response<string | any> {
|
||||||
const r: ChannelPins = req.body.r;
|
const r: ChannelPins = req.body.r;
|
||||||
const g: ChannelPins = req.body.g;
|
const g: ChannelPins = req.body.g;
|
||||||
const b: ChannelPins = req.body.b;
|
const b: ChannelPins = req.body.b;
|
||||||
@ -140,14 +158,14 @@ export async function writeRgbLed (req: Request, res: Response) {
|
|||||||
const pins: string = rgbLeds.map(c => c.pin.toString()).join(", ");
|
const pins: string = rgbLeds.map(c => c.pin.toString()).join(", ");
|
||||||
const values: string = rgbLeds.map(c => `${c.value}`).join(", ");
|
const values: string = rgbLeds.map(c => `${c.value}`).join(", ");
|
||||||
|
|
||||||
res.status(200).json({
|
return res.status(200).json({
|
||||||
status: 200,
|
status: 200,
|
||||||
message: `Success changed pins ${pins} to state ${values}`
|
message: `Success changed pins ${pins} to state ${values}`
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
res.status(500).json({
|
return res.status(500).json({
|
||||||
status: 500,
|
status: 500,
|
||||||
message: "Internal Server Error"
|
message: "Internal Server Error"
|
||||||
});
|
});
|
||||||
|
@ -10,9 +10,12 @@ export function readPin (req: Request, res: Response) {
|
|||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
status: 200,
|
status: 200,
|
||||||
state: {
|
pins: [
|
||||||
used: true
|
{
|
||||||
},
|
pin: pin,
|
||||||
|
mode: mode
|
||||||
|
}
|
||||||
|
],
|
||||||
message: `Pin ${pin} is ${mode}`
|
message: `Pin ${pin} is ${mode}`
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -28,6 +31,12 @@ export function setPin (req: Request, res: Response) {
|
|||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
status: 200,
|
status: 200,
|
||||||
|
pins: [
|
||||||
|
{
|
||||||
|
pin: pin,
|
||||||
|
mode: mode
|
||||||
|
}
|
||||||
|
],
|
||||||
message: `Pin ${pin} setted as ${mode}`
|
message: `Pin ${pin} setted as ${mode}`
|
||||||
});
|
});
|
||||||
}
|
}
|
@ -8,19 +8,18 @@ import { board, suBoard, comport } from './setup';
|
|||||||
import { isBoardConnected } from './middleware/connection';
|
import { isBoardConnected } from './middleware/connection';
|
||||||
import view from './routes/view';
|
import view from './routes/view';
|
||||||
import api from './routes/api';
|
import api from './routes/api';
|
||||||
import { selectPort } from './ports';
|
|
||||||
import socketHandler from './handlers/socketHandler';
|
import socketHandler from './handlers/socketHandler';
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const server = http.createServer(app);
|
const server: http.Server = http.createServer(app);
|
||||||
const io = new Server(server, {
|
const io: Server = new Server(server, {
|
||||||
cors: {
|
cors: {
|
||||||
origin: "*"
|
origin: "*"
|
||||||
}
|
}
|
||||||
}); // I have no experience at WebSocket, so.. forgive me :)
|
}); // I have no experience at WebSocket, so.. forgive me :)
|
||||||
|
|
||||||
const host = 'localhost';
|
const host: string = 'localhost';
|
||||||
const port = 3000;
|
const port: number = 3000;
|
||||||
|
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
app.use(express.static('client'));
|
app.use(express.static('client'));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user