Arduino Node SocketIO Setup Tutorial
Arduino is a cool thing - people making all kinds of interesting stufff with it - from robots to synthesizers and so on. It happened that friend of mine gave me one to play with. After some experiments and googling I’ve made a connection between Arduino and webpage. It was really easy, and below is my little tutorial on how to make such thing. Hope you’ll find it useful and will make something bigger from it.
You can download source code here:
In this tutorial you will learn how to connect data from potentiometer on Arduino, transfer in through serial port to Node server and then connect in to the webpage through Socket IO - so webpage will know about changes on potentiometer and reacted accordingly
Let’s start with Arduino setup. I’m using nano but it shouldn’t matter at all. Here is my wiring:
The code for arduino is dead simple:
Now you can connect Arduino to computer through usb and it will be constantly sending data to serial. Now let’s start node server to listen for it. You’ll need node and npm (I’m using node 8.7 at the moment of tutorial)
Create new folder and initialize it
We’ll need only three packages: Express - for easy setting up server, SerialPort - for port listening and Socket.IO for connecting node server and web page. Let’s install them!
Create file server.js at the root of the project and add these lines:
Add start script to package.json:
Now, running
Should welcome you with “Express server started on 3000” in terminal. And if you go to localhost:3000 in your browser you should see
Congratulations! Let’s add socket.io integration. Create folder public in the root directory and add there file index.html with following code:
Add index.js to the same folder with
Add following code to server.js (root directory)
Check it first - now if you restart server and navigate to localhost:3000 you should see in terminal “a user connected” and in browser console “Socket Connected”. Click on any place on the page - you shold see coordinates of click printed in terminal and “server registered click event” in browser console.
Sockets are a bit confusing at the first moment, but it’s a really simple concept - we listen and emit events from both server and browser side. In code above, we start server with “io” listener - after user connect we emit “connected” event, for which already listens socket client, and so on. Socket’s itself is a very broad topic, and I would like to concentrate on arduino part now. Please leave a comment below if you would like to get more in depth socket tutorial
So let’s connect it all together!
We already setup arduino part and plugged it by usb to computer. Now we need to listen to serial port. We’ll use library SerialPort
server.js:
index.js
Now we creating a new serialPort listener which will listens for data from particular port. You’ll need to check to which port connected your Arduino manually - there is many ways to do so, for example “serialport-list” command in terminal if you’ll install serialport package globally.
When we are connected to port and to socket, we emit “connected” event to let browser know that everything is ok and start to parse data from serial port. Once these data is not the same as before (to avoid passing it every 100ms) we sendind data event. Once we recieved event on browser side we simply setting background attribute for 1024 / 3 possible variants.
And now, if everything is works as intended - we can change potentiometer value on Arduino and these will change background of our page. Isn’t it cool? There is a thousands possibilities to play with this thing, enjoy!
Thank you for reading! Check source code for a tutorial
Please leave a comment below - What ideas you came up with? Should I make more tutorials like these one? What kind of?