|
1 | 1 | # arduino-to-nodejs
|
2 | 2 |
|
3 |
| -Using JavaScript, Node.js, and an Arduino to communicate from an Arduino to the browser. |
| 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 | + |
| 5 | +## HTML and JavaScript |
| 6 | + |
| 7 | +Create an HTML file called `index.html`. Add the following code: |
| 8 | + |
| 9 | +```javascript |
| 10 | +<!doctype html> |
| 11 | +<html> |
| 12 | + <head> |
| 13 | + |
| 14 | + <script src='https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js'></script> |
| 15 | + |
| 16 | + <script> |
| 17 | + |
| 18 | + var socket = io(); |
| 19 | + |
| 20 | + socket.on('data', function(data) { |
| 21 | + console.log(data); |
| 22 | + document.getElementById('sample').style.opacity = data/100+"%"; |
| 23 | + }); |
| 24 | + |
| 25 | + </script> |
| 26 | + |
| 27 | + <style> |
| 28 | + |
| 29 | + #sample { |
| 30 | + background-color: red; |
| 31 | + width: 300px; |
| 32 | + height: 300px; |
| 33 | + } |
| 34 | + |
| 35 | + </style> |
| 36 | + |
| 37 | + </head> |
| 38 | + <body> |
| 39 | + |
| 40 | + <h1>Communicating from an Arduino to Node.js</h1> |
| 41 | + <div id="sample"></div> |
| 42 | + |
| 43 | + </body> |
| 44 | +</html> |
| 45 | +``` |
| 46 | + |
| 47 | +The above code creates a webpage with a red square. Whn the dial is turned on the Arduino device, the red square will fade in and out. |
| 48 | + |
| 49 | +## Node.js Server |
| 50 | + |
| 51 | +Before we setup the Node.js server we need to know the name of the serialport your Arduino is attached to. You can find the name of your serialport, it will look something like `/dev/tty.wchusbserialfa1410`. On a Mac using the Terminal and entering the following command: |
| 52 | + |
| 53 | +``` |
| 54 | +ls /dev/{tty,cu}.* |
| 55 | +``` |
| 56 | + |
| 57 | +On a PC you can use the command line and the following command: |
| 58 | + |
| 59 | +``` |
| 60 | +
|
| 61 | +``` |
| 62 | + |
| 63 | +Or you can find the name in [Arduino Create](https://create.arduino.cc/editor) in the drop down menu used to select your Arduino. |
| 64 | + |
| 65 | +Create a file called `app.js` and add the following code: |
| 66 | + |
| 67 | +```javascript |
| 68 | +var http = require('http'); |
| 69 | +var fs = require('fs'); |
| 70 | +var index = fs.readFileSync( 'index.html'); |
| 71 | + |
| 72 | +var SerialPort = require('serialport'); |
| 73 | +const parsers = SerialPort.parsers; |
| 74 | + |
| 75 | +const parser = new parsers.Readline({ |
| 76 | + delimiter: '\r\n' |
| 77 | +}); |
| 78 | + |
| 79 | +var port = new SerialPort('/dev/tty.wchusbserialfa1410',{ |
| 80 | + baudRate: 9600, |
| 81 | + dataBits: 8, |
| 82 | + parity: 'none', |
| 83 | + stopBits: 1, |
| 84 | + flowControl: false |
| 85 | +}); |
| 86 | + |
| 87 | +port.pipe(parser); |
| 88 | + |
| 89 | +var app = http.createServer(function(req, res) { |
| 90 | + res.writeHead(200, {'Content-Type': 'text/html'}); |
| 91 | + res.end(index); |
| 92 | +}); |
| 93 | + |
| 94 | +var io = require('socket.io').listen(app); |
| 95 | + |
| 96 | +io.on('connection', function(socket) { |
| 97 | + |
| 98 | + console.log('Node is listening to port'); |
| 99 | + |
| 100 | +}); |
| 101 | + |
| 102 | +parser.on('data', function(data) { |
| 103 | + |
| 104 | + console.log('Received data from port: ' + data); |
| 105 | + io.emit('data', data); |
| 106 | + |
| 107 | +}); |
| 108 | + |
| 109 | +app.listen(3000); |
| 110 | +``` |
| 111 | + |
| 112 | +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. |
| 113 | + |
| 114 | +> Note: Make sure to change the name of the serialport. |
| 115 | +
|
| 116 | +## The Arduino |
| 117 | + |
| 118 | +Using [Arduino Create](https://create.arduino.cc/editor) create the following sketch and upload it to your Arduino. |
| 119 | + |
| 120 | +```csharp |
| 121 | +int percent = 0; |
| 122 | +int prevPercent = 0; |
| 123 | + |
| 124 | +void setup() { |
| 125 | + Serial.begin( 9600 ); |
| 126 | +} |
| 127 | + |
| 128 | +void loop() { |
| 129 | + |
| 130 | + percent = round(analogRead(0) / 1024.00 * 100); |
| 131 | + |
| 132 | + if(percent != prevPercent) { |
| 133 | + Serial.println(percent); |
| 134 | + prevPercent = percent; |
| 135 | + } |
| 136 | + |
| 137 | + delay(100); |
| 138 | + |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +The previous code will generate a percentage pased on the dial and pass the number on to the Node.js server using the serialport. |
| 143 | + |
| 144 | +You will need to setup the following circuit using your Arduino: |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | +[View the Circuit on Tinkercad](https://www.tinkercad.com/things/5Siec0jdhZo-arduinotobrowser) |
| 149 | + |
| 150 | +## Launch Application |
| 151 | + |
| 152 | +1. Using the Terminal start your Node.js app using `node app.js`. |
| 153 | +2. Open up a browser and enter the URL `http://localhost:3000/`. |
| 154 | +3. Using [Arduino Create](https://create.arduino.cc/editor) upload the sketch to your Arduino. |
| 155 | +4. Turn the dial on the Arduino device and watch the red square in the browser.t. |
| 156 | + |
| 157 | +## Tutorial Requirements: |
| 158 | + |
| 159 | +* [Visual Studio Code](https://code.visualstudio.com/) or [Brackets](http://brackets.io/) (or any code editor) |
| 160 | +* [Filezilla](https://filezilla-project.org/) (or any FTP program) |
| 161 | +* [Arduino Create](https://create.arduino.cc/editor) |
| 162 | + |
| 163 | +Full tutorial URL: https://codeadam.ca/learning/arduino-to-nodejs.html |
| 164 | + |
| 165 | +<a href="https://codeadam.ca"> |
| 166 | +<img src="https://codeadam.ca/images/code-block.png" width="100"> |
| 167 | +</a> |
| 168 | + |
| 169 | + |
0 commit comments