You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+30-36Lines changed: 30 additions & 36 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Communicating Between an Arduino and a Node.js Server
2
2
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.
4
4
5
5
## HTML and JavaScript
6
6
@@ -19,7 +19,7 @@ Create an HTML file called `index.html`. Add the following code:
In my Node.js I would use `COM3` as my serialport string.
72
72
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.
74
74
75
75
Or you can find the name in [Arduino Create](https://create.arduino.cc/editor) in the drop down menu used to select your Arduino.
76
76
77
77
Create a file called `app.js` and add the following code:
78
78
79
79
```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");
83
83
84
-
var SerialPort =require('serialport');
84
+
var SerialPort =require("serialport");
85
85
constparsers=SerialPort.parsers;
86
86
87
87
constparser=newparsers.Readline({
88
-
delimiter:'\r\n'
88
+
delimiter:"\r\n",
89
89
});
90
90
91
-
var port =newSerialPort('/dev/tty.wchusbserialfa1410',{
91
+
var port =newSerialPort("/dev/tty.wchusbserialfa1410", {
92
92
baudRate:9600,
93
93
dataBits:8,
94
-
parity:'none',
94
+
parity:"none",
95
95
stopBits:1,
96
-
flowControl:false
96
+
flowControl:false,
97
97
});
98
98
99
99
port.pipe(parser);
100
100
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"});
103
103
res.end(index);
104
104
});
105
105
106
-
var io =require('socket.io').listen(app);
106
+
var io =require("socket.io").listen(app);
107
107
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");
112
110
});
113
111
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);
119
115
});
120
116
121
117
app.listen(3000);
122
118
```
123
119
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.
125
121
126
122
> Note: Make sure to change the name of the serialport.
127
123
128
124
## The Arduino
129
125
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.
131
127
132
128
```csharp
133
129
intpercent=0;
@@ -138,16 +134,16 @@ void setup() {
138
134
}
139
135
140
136
voidloop() {
141
-
137
+
142
138
percent=round(analogRead(0) /1024.00*100);
143
-
139
+
144
140
if(percent!=prevPercent) {
145
141
Serial.println(percent);
146
142
prevPercent=percent;
147
143
}
148
-
144
+
149
145
delay(100);
150
-
146
+
151
147
}
152
148
```
153
149
@@ -157,7 +153,7 @@ The previous code will generate a percentage pased on the dial and pass the numb
157
153
158
154
You will need to setup the following circuit using your Arduino:
0 commit comments