Click here to Skip to main content
15,895,084 members
Articles / Web Development / HTML

Arduino and the Web using NodeJS and SerialPort2

Rate me:
Please Sign up or sign in to vote.
4.65/5 (22 votes)
18 Aug 2012CPOL6 min read 188.3K   3.1K   48  
This article focuses on how to listen for signals from an Arduino through the Serial Port (Linux, Mac) or COM Port for Windows using the SerialPort2 Node.JS module and serve signal data to the web in real time using Socket.IO.
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to

int sensorValue = 0;        // value read from the pot
int prevsensorValue = 0;

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);
  if (prevsensorValue != sensorValue) {
    
    // print the results to the serial monitor:
    Serial.print("A");
    Serial.print(sensorValue);
    Serial.print("B");
    
    prevsensorValue = sensorValue;
  }
  // wait 100 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(100);                     
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Kinalibangon Software
Philippines Philippines
Electronics and Communications Engineering student at our local university and a part time software developer for Kinalibangon Software.

Comments and Discussions