Skip to content

Commit 6e74d70

Browse files
committed
Organized readme
1 parent 8a2b183 commit 6e74d70

File tree

2 files changed

+30
-36
lines changed

2 files changed

+30
-36
lines changed

README.md

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Communicating Between an Arduino and a Node.js Server
22

3-
This tutorial will walkthrough the process of creating an Arduino IOT device that controls aspects fo a website. In this example the website will simpy display a red block that will fade when a dial on an Arduino is adjusted.
3+
This tutorial will walkthrough the process of creating an Arduino IOT device that controls aspects fo a website. In this example the website will simpy display a red block that will fade when a dial on an Arduino is adjusted.
44

55
## HTML and JavaScript
66

@@ -19,7 +19,7 @@ Create an HTML file called `index.html`. Add the following code:
1919

2020
socket.on('data', function(data) {
2121
console.log(data);
22-
document.getElementById('sample').style.opacity = data+"%";
22+
document.getElementById('sample').style.opacity = data+"%";
2323
});
2424

2525
</script>
@@ -70,64 +70,60 @@ COM3 = \Device\Serial2
7070

7171
In my Node.js I would use `COM3` as my serialport string.
7272

73-
If you're not sure which one is your Arduino, just disconnet your Arduino and execute the cpommand again and take note of which port is no longer on the list.
73+
If you're not sure which one is your Arduino, just disconnet your Arduino and execute the cpommand again and take note of which port is no longer on the list.
7474

7575
Or you can find the name in [Arduino Create](https://create.arduino.cc/editor) in the drop down menu used to select your Arduino.
7676

7777
Create a file called `app.js` and add the following code:
7878

7979
```javascript
80-
var http = require('http');
81-
var fs = require('fs');
82-
var index = fs.readFileSync( 'index.html');
80+
var http = require("http");
81+
var fs = require("fs");
82+
var index = fs.readFileSync("index.html");
8383

84-
var SerialPort = require('serialport');
84+
var SerialPort = require("serialport");
8585
const parsers = SerialPort.parsers;
8686

8787
const parser = new parsers.Readline({
88-
delimiter: '\r\n'
88+
delimiter: "\r\n",
8989
});
9090

91-
var port = new SerialPort('/dev/tty.wchusbserialfa1410',{
91+
var port = new SerialPort("/dev/tty.wchusbserialfa1410", {
9292
baudRate: 9600,
9393
dataBits: 8,
94-
parity: 'none',
94+
parity: "none",
9595
stopBits: 1,
96-
flowControl: false
96+
flowControl: false,
9797
});
9898

9999
port.pipe(parser);
100100

101-
var app = http.createServer(function(req, res) {
102-
res.writeHead(200, {'Content-Type': 'text/html'});
101+
var app = http.createServer(function (req, res) {
102+
res.writeHead(200, {"Content-Type": "text/html"});
103103
res.end(index);
104104
});
105105

106-
var io = require('socket.io').listen(app);
106+
var io = require("socket.io").listen(app);
107107

108-
io.on('connection', function(socket) {
109-
110-
console.log('Node is listening to port');
111-
108+
io.on("connection", function (socket) {
109+
console.log("Node is listening to port");
112110
});
113111

114-
parser.on('data', function(data) {
115-
116-
console.log('Received data from port: ' + data);
117-
io.emit('data', data);
118-
112+
parser.on("data", function (data) {
113+
console.log("Received data from port: " + data);
114+
io.emit("data", data);
119115
});
120116

121117
app.listen(3000);
122118
```
123119

124-
The above code listend for a message from the Arduino over the USD port and then passes a message onto the HTML/JavaScript using Socket.io.
120+
The above code listend for a message from the Arduino over the USD port and then passes a message onto the HTML/JavaScript using Socket.io.
125121

126122
> Note: Make sure to change the name of the serialport.
127123
128124
## The Arduino
129125

130-
Using [Arduino Create](https://create.arduino.cc/editor) create the following sketch and upload it to your Arduino.
126+
Using [Arduino Create](https://create.arduino.cc/editor) create the following sketch and upload it to your Arduino.
131127

132128
```csharp
133129
int percent = 0;
@@ -138,16 +134,16 @@ void setup() {
138134
}
139135

140136
void loop() {
141-
137+
142138
percent = round(analogRead(0) / 1024.00 * 100);
143-
139+
144140
if(percent != prevPercent) {
145141
Serial.println(percent);
146142
prevPercent = percent;
147143
}
148-
144+
149145
delay(100);
150-
146+
151147
}
152148
```
153149

@@ -157,7 +153,7 @@ The previous code will generate a percentage pased on the dial and pass the numb
157153

158154
You will need to setup the following circuit using your Arduino:
159155

160-
![Tinkercad Circuit](https://raw.githubusercontent.com/codeadamca/arduino-to-nodejs/master/tinkercad-to-nodejs.png)
156+
![Tinkercad Circuit](_readme/tinkercad-to-nodejs.png)
161157

162158
[View the Circuit on Tinkercad](https://www.tinkercad.com/things/5Siec0jdhZo-arduinotobrowser)
163159

@@ -170,15 +166,13 @@ You will need to setup the following circuit using your Arduino:
170166

171167
## Tutorial Requirements:
172168

173-
* [Visual Studio Code](https://code.visualstudio.com/) or [Brackets](http://brackets.io/) (or any code editor)
174-
* [Arduino Create](https://create.arduino.cc/editor)
175-
* [SerialPort NPM](https://www.npmjs.com/package/serialport)
176-
* [Socket.io](https://socket.io/)
169+
- [Visual Studio Code](https://code.visualstudio.com/) or [Brackets](http://brackets.io/) (or any code editor)
170+
- [Arduino Create](https://create.arduino.cc/editor)
171+
- [SerialPort NPM](https://www.npmjs.com/package/serialport)
172+
- [Socket.io](https://socket.io/)
177173

178174
Full tutorial URL: https://codeadam.ca/learning/arduino-to-nodejs.html
179175

180176
<a href="https://codeadam.ca">
181177
<img src="https://codeadam.ca/images/code-block.png" width="100">
182178
</a>
183-
184-
File renamed without changes.

0 commit comments

Comments
 (0)