Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
Motivation
So, there was a project I was working on where I had to fetch the data from serial monitor in my node console and then using web sockets display the data in a web based application. But, I was having a hard time figuring out how to fetch the data, I explored and found serialport npm module, but the output it gave was in form of buffer, not the exact word I wanted. After spending some time, I was able to figure out how to do it. Hence, I am sharing this so that it can help anyone who is trying to do the same.
How does arduino and other peripherals communicate with Laptop?
In most of the cases, the answer is âSerial Communicationâ. With serial communication, the data can be sent bit by bit âin serialâ using a wire.The image given below is self explanatory, and gives a clear idea about serial and parallel communication.
Keeping in mind the main focus of this article, I will stop this discussion here and move to the point directly. However, if you want to read more about serial communication, you can refer wikipedia and if you want to read about serial communication in arduino, you can refer the official website of arduino.
About my project!
I made a tap water wastage detection system using arduino. It was based on a simple idea of combining rain water module with ultrasonic sensor.
What has ultrasonic sensor got to do with water wastage detection?Simple, using rain water module, I was sensing whether water drops fall on it, but, even if water drops fall over the rain water module, it would be wrong to conclude that water is being wasted. It can be a case that a person is drinking water from the tap, and somehow some water falls over the module. Clearly, I donât want to activate my water wastage detection system in this case. So, to prevent wrong output, Ultrasonic sensor is being used to detect whether a person is present nearby at the time when rain drop module senses water wastage. The idea is quite simple, if tap is off it means water is not being wasted (Genius!), if tap is on, and a person is in itâs proximity (say 70cm), again it would mean water is not being wasted (The person is probably using) . But, if tap is on, and there is no one nearby, it clearly implies that water is being wasted and the detector can give an alert now.
So, basically this project combines the usage of rain drop module and ultrasonic sensor to detect water wastage.Download the code here
Required OutputâŠ
This is the output on serial monitor which I intend to fetch on my Node server and then using web sockets, give real time updates about the tap. Letâs write a script for it :)
A good question that arises is can we combine hardware and JavaScript? Originally, JS was not intended to be used with hardware, but now much advances in the language have been made and yes, it can be used. Read this interesting article: NodeBotsâââThe Rise of JS Robotics.
Getting StartedâââInstalling the serialport library
The serialport library provides a great support for combining hardware projects and JavaScript.
Imagine a world where you can write JavaScript to control blenders, lights, security systems, or even robots. Thatâs rightââârobots! Thanks to Node Serialport, that world is here.
Visit this link for complete API documentation.
So, first of all, as we do with any node module, we will install it.
$ npm install --save serialport
After this node module has been downloaded, we can finally start :)
Opening port and reading values
Just like in arduino programs, before any real communication can take place, we need to actually open the port. It can be done by the following piece of code.
var SerialPort = require('serialport');
var serialPort = new SerialPort('/dev/ttyACM0', { baudrate: 9600});
Instead of /dev/ttyACM0 you must write the name of your port name. It would be something like COM17 on windows, or something like the above mentioned on UNIX based systems.
Now that the port is open, we can finally plug in our arduino and fetch the data using the following code.
// Switches the port into "flowing mode"serialPort.on('data', function (data) { console.log('Data:', data);});
// Read data that is available but keep the stream from entering //"flowing mode"serialPort.on('readable', function () { console.log('Data:', port.read());});
AWESOME!!! We are done with code, letâs try it out.
$ node filename.js
So, this was what I was talking about earlier. Instead of giving the text, it gives the data buffer.
No need to worry!The text data can be easily parsed and obtained. Use the code given below to do so!
// Require the serialport node modulevar serialport = require('serialport');var SerialPort = serialport.SerialPort;
// Open the portvar port = new SerialPort("/dev/ttyACM0", { baudrate: 9600, parser: serialport.parsers.readline("\n")});
// Read the port dataport.on("open", function () { console.log('open'); port.on('data', function(data) { console.log(data); });});
Okay, letâs run this code!
Conclusion
This article was mainly about JavaScript and hardware, and focused on communicating with serial port using NodeJs. Hope it will help you in case you are stuck at that data buffer thing just like IÂ was.
Thanks a lot for keeping your calm and reading till end. All the best and Happy coding!You can contact me in case of any doubts or if you need any assistance:Email: madhavbahl10@gmail.comWeb: http://www.madhavbahl.tech/Github: https://github.com/MadhavBahlMDLinkedIn: https://www.linkedin.com/in/madhavbahl/
Read Arduino Serial Monitor Using NodeJS was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.