|
1 | 1 | # arduino-serial-fetch
|
2 | 2 |
|
3 |
| -## Simple NodeJS app that converts serial messages to REST API requests |
| 3 | +This app converts serial messages from an Arduino to REST API requests. It has been developed to correspond specifically with the REST API service provided by [arduino-api-server](https://github.com/stephiescastle/arduino-api-server) and is structured solely to update pin data. |
4 | 4 |
|
5 |
| -This api will allow your arduino to send and receive data to a remote server. This means you can retrieve data from other arduinos and alternatively make your data available to others. Additionally, the endpoints for your arduino can be used as a data endpoint in other apps, i.e. p5.js, Max/MSP, etc. |
| 5 | +- [Requirements](#requirements) |
| 6 | +- [Getting Started](#getting-started) |
| 7 | +- [Arduino code and `Serial.print()` format](#arduino-code-and-serialprint-format) |
| 8 | +- [Adding more pins](#adding-more-pins) |
| 9 | +- [Test API endpoints](#test-api-endpoints) |
6 | 10 |
|
7 |
| -This is achievable by using two repos: |
8 |
| - |
9 |
| -1. [arduino-serial-fetch](https://github.com/stephiescastle/arduino-serial-fetch) - (aka this repo) - The node.js app that reads from and writes to your arduino board. The node.js app runs locally alongside your arduino board. |
10 |
| -2. [arduino-api-server](https://github.com/stephiescastle/arduino-api-server) - The server that provides the data endpoints and is deployed to heroku. |
11 |
| - |
12 |
| -This was developed to help facilitate the creation of interactive, physical computing-based works while we all live in quarantine. |
13 |
| - |
14 |
| -## Dependencies |
| 11 | +## Requirements |
15 | 12 |
|
16 | 13 | - [arduino-api-server](https://github.com/stephiescastle/arduino-api-server)
|
17 |
| -- node.js |
18 |
| -- arduino program must be printing data to the serial port with a delimiter (see [/arduinoSerial/arduinoSerial.ino](/arduinoSerial/arduinoSerial.ino)) |
19 |
| -- arduino must be tethered via USB |
| 14 | +- Arduino serial messages must following a specific format. See [Arduino code and `Serial.print()` format](#arduino-code-and-serialprint-format) |
| 15 | +- Arduino must be tethered to your computer |
20 | 16 |
|
21 |
| -### Getting Started |
| 17 | +## Getting Started |
22 | 18 |
|
23 |
| -1. Set up your [arduino-api-server](https://github.com/stephiescastle/arduino-api-server) |
24 |
| -2. Create your env file. |
| 19 | +1. First complete your [arduino-api-server](https://github.com/stephiescastle/arduino-api-server) setup |
| 20 | +2. Connect your Arduino to your computer and upload the corresponding Arduino program to it. See [Arduino code and `Serial.print()` format](#arduino-code-and-serialprint-format). |
| 21 | +3. Create your env file. |
25 | 22 |
|
26 | 23 | ```bash
|
27 | 24 | cp .env.dist .env
|
28 | 25 | ```
|
29 | 26 |
|
30 |
| -3. Update the `.env` file with your `API_HOST`. This corresponds to the URL of your API server. |
| 27 | +4. Update the `.env` file with values that match your configuration: |
31 | 28 |
|
32 |
| -4. Connect your arduino to your computer and upload the corresponding arduino program to it (you may need to modify this for the pins you are actually using) |
| 29 | + | var | default | description | |
| 30 | + | :----------- | :------------------------ | :--------------------------------------------------------------------------------------------------------------- | |
| 31 | + | `API_HOST` | `http://localhost:3000` | The host URL of your API server (see [arduino-api-server](https://github.com/stephiescastle/arduino-api-server)) | |
| 32 | + | `SERIALPORT` | `/dev/tty.SLAB_USBtoUART` | The serial port your Arduino is connected to. Should match the port name you use in the Arduino IDE. | |
| 33 | + | `BAUDRATE` | `9600` | Match the baudrate used in your Arduino code. Check `Serial.begin(9600);` in your Arduino `setup()` | |
| 34 | + | `INTERVAL` | `500` | Frequency of API requests (in milliseconds) | |
33 | 35 |
|
34 | 36 | 5. Install dependencies
|
35 | 37 |
|
36 | 38 | ```bash
|
37 | 39 | npm install
|
38 | 40 | ```
|
39 | 41 |
|
40 |
| -6. Run arduino-serial-fetch |
| 42 | +6. Run the app |
41 | 43 |
|
42 | 44 | ```bash
|
43 | 45 | npm start
|
44 | 46 | ```
|
45 | 47 |
|
46 |
| -### Test changing a value |
| 48 | + To stop the app, use ctrl-c (`^C`) |
| 49 | + |
| 50 | +## Arduino code and `Serial.print()` format |
| 51 | + |
| 52 | +This app assumes that your Arduino code is only printing one line to the Serial port per `loop()` iteration and is constructed like so: |
| 53 | + |
| 54 | +```js |
| 55 | +// for three pins |
| 56 | +<number>\t<number>\t<number>\t\n |
| 57 | +``` |
| 58 | + |
| 59 | +Each number represents pin data, with `\t` as the delimiter. `\n` signifies the end of the data reading for one iteration in `loop()`. It is up to the interpreter to know the order of the pins and how that should correspond to their usage (see [Adding more pins](#adding-more-pins)). |
| 60 | + |
| 61 | +You can use [/arduinoSerial/arduinoSerial.ino](/arduinoSerial/arduinoSerial.ino) as boilerplate for your Arduino code with pins modified as needed. |
| 62 | + |
| 63 | +## Adding more pins |
| 64 | + |
| 65 | +The app defaults to reading and sending the following pins: |
| 66 | + |
| 67 | +- `A0` |
| 68 | +- `A1` |
| 69 | +- `D2` |
| 70 | + |
| 71 | +This can be modified by updating both the: |
| 72 | + |
| 73 | +1. Arduino code: read the necessary pins and append to the `Serial.print` message |
| 74 | +2. `main.js`: modify the constructed `allPins` array that is used to generate API requests. |
| 75 | + |
| 76 | +For example, say I needed to read pins `A0`, `A3`, `D4`, and `D7`. I would need to modify my code like so: |
| 77 | + |
| 78 | +```c++ |
| 79 | +// arduinoSerial.ino |
| 80 | + |
| 81 | +int knobPin = A0; |
| 82 | +int knobValue = 0; |
47 | 83 |
|
48 |
| -If you don't have your arduino connected, you can manually update pin values via the `test.js` script. This is useful if you just want to test your endpoints. |
| 84 | +int sensorPin = A3; |
| 85 | +int sensorValue = 0; |
| 86 | + |
| 87 | +int button1Pin = D4; |
| 88 | +int button1Value = 0; |
| 89 | + |
| 90 | +int button2Pin = D7; |
| 91 | +int button2Value = 0; |
| 92 | + |
| 93 | +void setup() { |
| 94 | + pinMode(button1Pin, INPUT); |
| 95 | + pinMode(button2Pin, INPUT); |
| 96 | + Serial.begin(9600); |
| 97 | +} |
| 98 | + |
| 99 | +void loop() { |
| 100 | + // A0 will be pins[0] |
| 101 | + knobValue = analogRead(knobPin); |
| 102 | + Serial.print(knobValue); |
| 103 | + Serial.print("\t"); |
| 104 | + |
| 105 | + // A3 will be pins[1] |
| 106 | + sensorValue = analogRead(sensorPin); |
| 107 | + Serial.print(sensorValue); |
| 108 | + Serial.print("\t"); |
| 109 | + |
| 110 | + // D4 will be pins[2] |
| 111 | + buttonValue = digitalRead(button1Pin); |
| 112 | + Serial.print(button1Value); |
| 113 | + Serial.print("\t"); |
| 114 | + |
| 115 | + // D7 will be pins[3] |
| 116 | + buttonValue = digitalRead(button2Pin); |
| 117 | + Serial.print(button2Value); |
| 118 | + Serial.print("\t"); |
| 119 | + |
| 120 | + Serial.println(); |
| 121 | + |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +```js |
| 126 | +// main.js excerpt |
| 127 | +const allPins = [ |
| 128 | + { |
| 129 | + id: "A0", |
| 130 | + value: pins[0], |
| 131 | + }, |
| 132 | + { |
| 133 | + id: "A3", |
| 134 | + value: pins[1], |
| 135 | + }, |
| 136 | + { |
| 137 | + id: "D4", |
| 138 | + value: pins[2], |
| 139 | + }, |
| 140 | + { |
| 141 | + id: "D7", |
| 142 | + value: pins[3], |
| 143 | + }, |
| 144 | +]; |
| 145 | +``` |
| 146 | + |
| 147 | +> Note how the `pins[]` array index does not necessarily correspond to the Arduino pin number. |
| 148 | +
|
| 149 | +## Test API endpoints |
| 150 | + |
| 151 | +If you don't have an Arduino connected, you can mimic one with the `test.js` script. This is useful if you just want to test your endpoints. The test script will also send API requests at the same `INTERVAL` defined in your `.env` file. |
49 | 152 |
|
50 | 153 | ```bash
|
51 | 154 | npm test
|
52 | 155 | ```
|
| 156 | + |
| 157 | +To stop the test, use ctrl-c (`^C`) |
0 commit comments