Remote logging with Arduino and Node.js

Posted on by

In this tutorial I will show you how to save sensor data remotely with Arduino and Node.js. We will accomplish this with Arduino, Ethernet Shield and computer/server.
The basic idea is to set up UDP stream from arduino to remote computer where we have Node.js udp server writing data into a file.

Arduino setup

Put the ethernet shield on the arduino and plug in the ethernet cable from your network.
Connect arduino to your computer with usb cable and plug in additional 9V power adapter to make sure that shield will have enough power.
You are all set and should be looking at the same thing as seen on my photo.
(Photo is obviously missing a sensor of some-kind connected to arduino.)

Arduino sketch

In arduino sketch we have to set up the UDP client.
We will read the value of the sensor with analogRead and convert it to byte array before sending it out with UDP packet.
In my case I’m sending data to ip 192.168.1.135 which is my computer and I’ve chosen port 6000.

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

byte arduinoMac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress arduinoIP(192, 168, 1, 177); // desired IP for Arduino
unsigned int arduinoPort = 8888;      // port of Arduino

IPAddress receiverIP(192, 168, 1, 135); // IP of udp packets receiver
unsigned int receiverPort = 6000;      // port to listen on my PC

EthernetUDP Udp;

int sensorPin = A0; //define sensor pin
int sensorValue;

void setup() {
  Ethernet.begin(arduinoMac,arduinoIP);
  Udp.begin(arduinoPort);
}

void loop() {
  sensorValue = analogRead(sensorPin);//read sensor value from 0 to 1023 
  byte valueInBytes[2] = {lowByte(sensorValue), highByte(sensorValue)}; //convert it to byte array
  
  Udp.beginPacket(receiverIP, receiverPort); //start udp packet
  Udp.write(valueInBytes, 2); //write sensor data to udp packet
  Udp.endPacket(); // end packet

  delay(1000);
}

Upload the code to arduino.

Node.js UDP server

To receive packets sent form arduino we need an UDP server. Node.js is perfect for this because its easy to set up, lightweight and blazing fast. (Node.js works on Windows, Mac OS X, Linux, SunOS)
To install Node.js on Windows go to nodejs.org press the big green “Install” button and after you get the file run it.

Now we need to write an UDP server which listens to UDP traffic on port 6000 and writes to file. The code below is all you need. Save it to udpwrite.js file.

var dgram = require("dgram");
var server = dgram.createSocket("udp4");
var fs = require('fs');

var crlf = new Buffer(2);
crlf[0] = 0xD; //CR - Carriage return character
crlf[1] = 0xA; //LF - Line feed character

server.on("message", function (msg, rinfo) { //every time new data arrives do this:
  console.log("server got: " + msg.readUInt16LE(0) + " from " + rinfo.address + ":" + rinfo.port); // you can comment this line out
  fs.appendFile('mydata.txt', msg.readUInt16LE(0) + crlf, encoding='utf8');//write the value to file and add CRLF for line break
});

server.on("listening", function () {
  var address = server.address();
  console.log("server listening " + address.address + ":" + address.port);
});

server.bind(6000); //listen to udp traffic on port 6000

Run it

We have the code but the Node.js USP server is not running jet.
Easy way to run the code on windows is to make a bat file go4it.bat containing a line of code below and save it in the same folder as udpwrite.js file.
(If you don’t use windows then just run this command below in your console)

node udpwrite.js

Now run the go4it.bat file and you should see Node.js server working:

If you check your folder where you have go4it.bat and udpwrite.js you will see there is a new file mydata.txt. If you don’t see it press F5 to refresh.
In file you will see your sensor data:

To stop recording click on the black console window running Node.js and press Ctrl+C and then Y.

Download files

To make your life easier I made a zip file containing all the needed files: arduino_node.zip

Have fun ;)

Franci KapelFranci is an experienced web developer who spends most of his free time hacking gadgets. Site frenki.net was made to help others with their projects. Connect with Franci on Google+