
In this tutorial, the author 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 the photo.
(Photo is obviously missing a sensor of some-kind connected to arduino.)
For more detail about Arduino sketch, Node.js UDP server setting and etc, visit the author’s blog “FRANKIE.NET”
Code
#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); }
COMMENTS