Complete API Docs with examples

This commit is contained in:
Nomi Nonsense (Nonszy) 2024-03-17 11:18:37 +07:00
parent 9470845bad
commit 2e205ec1c1
2 changed files with 295 additions and 30 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
example-client/
# Logs # Logs
logs logs
*.log *.log

323
README.md
View File

@ -72,8 +72,8 @@ Arduino assembly guide also available in [johnny-five](https://github.com/rwaldr
- [Analog Read](#analog-read) - [Analog Read](#analog-read)
- [Analog Write](#analog-write) - [Analog Write](#analog-write)
- [PIN](#pin) - [PIN](#pin)
- [Read PIN mode](#read-pin-mode) - [Read PIN Mode](#read-pin-mode)
- [Set PIN mode](#set-pin-mode) - [Set PIN Mode](#set-pin-mode)
- [LED](#led) - [LED](#led)
- [Read LED state](#read-led-state) - [Read LED state](#read-led-state)
- [Set LED state](#set-led-state) - [Set LED state](#set-led-state)
@ -83,6 +83,7 @@ Arduino assembly guide also available in [johnny-five](https://github.com/rwaldr
- [Piezo](#piezo) - [Piezo](#piezo)
- [Piezo Tone](#piezo-tone) - [Piezo Tone](#piezo-tone)
- [Piezo Note](#piezo-note) - [Piezo Note](#piezo-note)
- [Piezo Play Music](#piezo-play-music)
## Common HTTP Responses ## Common HTTP Responses
Some HTTP responses are sent with JSON, otherwise an HTML body will be sent which is the default express.js response Some HTTP responses are sent with JSON, otherwise an HTML body will be sent which is the default express.js response
@ -112,11 +113,19 @@ Some HTTP responses are sent with JSON, otherwise an HTML body will be sent whic
| message | Descriptive message of the changed state | | message | Descriptive message of the changed state |
## Example Request ## Example Request
This is an example of javascript code sending a request to turn on the LED light, [See LED API](#set-led-state) This is an example of sending a request to turn on the LED light, [See LED API](#set-led-state)
**Using curl**
```bash
curl -X PATCH http://localhost:3000/api-arduino/led/13/on
```
**Using fetch async/await javascript**
```javascript ```javascript
async function turnOnLed() { async function turnOnLed() {
const res = await fetch("http://localhost:3000/led/13/on"); // Set LED to HIGH const res = await fetch("http://localhost:3000/api-arduino/led/13/on", { method: 'PATCH' }); // Set LED to HIGH
const data = await res.json(); const data = await res.json();
console.log(data); // { status: 200, pin_state: { 13: high }, message: "Pin 13 Set to HIGH" } console.log(data); // { status: 200, pin_state: { 13: high }, message: "Pin 13 Set to HIGH" }
} }
@ -127,7 +136,7 @@ async function turnOnLed() {
### Digital Read ### Digital Read
- **URL Endpoint** - **URL Endpoint**
/pin/:p/ `/api-arduino/digital/:pin`
- **URL Params** - **URL Params**
@ -139,7 +148,15 @@ async function turnOnLed() {
`GET` `GET`
- **Sample Response** - **Example Request**
```javascript
fetch("http://localhost:3000/api-arduino/digital/13")
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error('error', err));
```
- **Example Response**
```json ```json
{ {
@ -153,7 +170,7 @@ async function turnOnLed() {
### Digital Write ### Digital Write
- **URL Endpoint** - **URL Endpoint**
/pin/:p/:m `/api-arduino/digital`
- **Body** - **Body**
@ -168,7 +185,30 @@ async function turnOnLed() {
`PATCH` `PATCH`
- **Sample Response** - **Example Request**
```javascript
var data = {
pin: 13,
state: 'LOW'
}
var options = {
method: 'PATCH',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json'
},
redirect: 'follow'
}
fetch("http://localhost:3000/api-arduino/digital", options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error('error', err));
```
- **Example Response**
```json ```json
{ {
@ -184,7 +224,7 @@ async function turnOnLed() {
### Analog Read ### Analog Read
- **URL Endpoint** - **URL Endpoint**
/pin/:p/ `/api-arduino/analog/:p`
- **URL Params** - **URL Params**
@ -196,7 +236,15 @@ async function turnOnLed() {
`GET` `GET`
- **Sample Response** - **Example Request**
```javascript
fetch("http://localhost:3000/api-arduino/analog/A0")
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error('error', err));
```
- **Example Response**
```json ```json
{ {
@ -210,7 +258,7 @@ async function turnOnLed() {
### Analog Write ### Analog Write
- **URL Endpoint** - **URL Endpoint**
/pin/:p/:m `/api-arduino/analog`
- **Body** - **Body**
@ -225,7 +273,30 @@ async function turnOnLed() {
`PATCH` `PATCH`
- **Sample Response** - **Example Request**
```javascript
var data = {
pin: 'A0',
value: 1023
}
var options = {
method: 'PATCH',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json'
},
redirect: 'follow'
}
fetch("http://localhost:3000/api-arduino/analog", options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error('error', err));
```
- **Example Response**
```json ```json
{ {
@ -241,10 +312,10 @@ async function turnOnLed() {
## PIN ## PIN
### Read PIN mode ### Read PIN Mode
- **URL Endpoint** - **URL Endpoint**
/pin/:p/ `/api-arduino/pin/:p/`
- **URL Params** - **URL Params**
@ -256,7 +327,16 @@ async function turnOnLed() {
`GET` `GET`
- **Sample Response** - **Example Request**
```javascript
fetch("http://localhost:3000/api-arduino/pin/13")
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error('error', err));
```
- **Example Response**
```json ```json
{ {
@ -271,10 +351,10 @@ async function turnOnLed() {
} }
``` ```
### Set PIN mode ### Set PIN Mode
- **URL Endpoint** - **URL Endpoint**
/pin/:p/:m `/api-arduino/pin/:p/:m`
- **URL Params** - **URL Params**
@ -287,7 +367,16 @@ async function turnOnLed() {
`PATCH` `PATCH`
- **Sample Response** - **Example Request**
```javascript
fetch("http://localhost:3000/api-arduino/pin/13/output", { method: 'PATCH' })
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error('error', err));
```
- **Example Response**
```json ```json
{ {
@ -308,7 +397,7 @@ async function turnOnLed() {
### Read LED state ### Read LED state
- **URL Endpoint** - **URL Endpoint**
/led/:p/ `/api-arduino/led/:p/`
- **URL Params** - **URL Params**
@ -320,7 +409,16 @@ async function turnOnLed() {
`GET` `GET`
- **Sample Response** - **Example Request**
```javascript
fetch("http://localhost:3000/api-arduino/led/13")
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error('error', err));
```
- **Example Response**
```json ```json
{ {
@ -335,7 +433,7 @@ async function turnOnLed() {
### Set LED state ### Set LED state
- **URL Endpoint** - **URL Endpoint**
/led/:p/:a `/api-arduino/led/:p/:a`
- **URL Params** - **URL Params**
@ -348,7 +446,16 @@ async function turnOnLed() {
`PATCH` `PATCH`
- **Sample Response** - **Example Request**
```javascript
fetch("http://localhost:3000/api-arduino/led/13/output", { method: 'PATCH' })
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error('error', err));
```
- **Example Response**
```json ```json
{ {
@ -367,7 +474,7 @@ async function turnOnLed() {
### Read RGB state ### Read RGB state
- **URL Endpoint** - **URL Endpoint**
/rgb-led `/api-arduino/rgb-led`
- **Body** - **Body**
@ -383,7 +490,31 @@ async function turnOnLed() {
`POST` `POST`
- **Sample Response** - **Example Request**
```javascript
var data = {
r: 7,
g: 6,
b: 5
}
var options = {
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json'
},
redirect: 'follow'
}
fetch("http://localhost:3000/api-arduino/rgb-led", options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error('error', err));
```
- **Example Response**
```json ```json
{ {
@ -399,7 +530,7 @@ async function turnOnLed() {
### Set RGB state ### Set RGB state
- **URL Endpoint** - **URL Endpoint**
/rgb-led `/api-arduino/rgb-led`
- **Body** - **Body**
@ -424,7 +555,40 @@ async function turnOnLed() {
`PATCH` `PATCH`
- **Sample Response** - **Example Request**
```javascript
var data = {
r: {
pin: 7,
value: true
},
g: {
pin: 6,
value: true
},
b: {
pin: 5,
value: false
}
}
var options = {
method: 'PATCH',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json'
},
redirect: 'follow'
}
fetch("http://localhost:3000/api-arduino/rgb-led", options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error('error', err));
```
- **Example Response**
```json ```json
{ {
@ -448,7 +612,7 @@ async function turnOnLed() {
### Piezo Tone ### Piezo Tone
- **URL Endpoint** - **URL Endpoint**
/led/:p/:f `/api-arduino/piezo/:p/:f`
- **URL Params** - **URL Params**
@ -461,7 +625,16 @@ async function turnOnLed() {
`PATCH` `PATCH`
- **Sample Response** - **Example Request**
```javascript
fetch("http://localhost:3000/api-arduino/piezo/6/300", { method: 'PATCH' })
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error('error', err));
```
- **Example Response**
```json ```json
{ {
@ -476,7 +649,7 @@ async function turnOnLed() {
### Piezo Note ### Piezo Note
- **URL Endpoint** - **URL Endpoint**
/led/:p/:a `/api-arduino/piezo/note`
- **Body** - **Body**
@ -491,7 +664,30 @@ async function turnOnLed() {
`PATCH` `PATCH`
- **Sample Response** - **Example Request**
```javascript
var data = {
pin: 6,
note: "B3"
}
var options = {
method: 'PATCH',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json'
},
redirect: 'follow'
}
fetch("http://localhost:3000/api-arduino/piezo/note", options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error('error', err));
```
- **Example Response**
```json ```json
{ {
@ -507,3 +703,70 @@ async function turnOnLed() {
``` ```
### Piezo Play Music
- **URL Endpoint**
`/api-arduino/piezo/music`
- **Body**
```typescript
{
pin: number,
notes: string[],
beats: number,
tempo: number
}
```
Example
```javascript
{
pin: 6,
notes: ["F4", "G4", "F4", "G4"],
beats: 1/4,
tempo: 100
}
```
- **Method**
`PATCH`
- **Example Request**
```javascript
var data = {
pin: 6,
notes: ["E4", "E4", "F4", "G4", "G4", "F4", "E4", "D4", "C4", "C4", "D4", "E4", "D4", "-", "C4", "C4"], // ode to joy notes
beats: 1/2,
tempo: 100
}
var options = {
method: 'PATCH',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json'
},
redirect: 'follow'
}
fetch("http://localhost:3000/api-arduino/piezo/music", options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error('error', err));
```
- **Example Response**
```json
{
"status": 200,
"pin_notes": {
"6": [ "E4", "E4", "F4", "G4", "G4", "F4", "E4", "D4", "C4", "C4", "D4", "E4", "D4", "-", "C4", "C4" ]
},
"message": "Piezo 6 play notes E4, E4, F4, G4..."
}
```