Communicates with Arduino via Website with NodeJS

  1. Install Node JS
  2. Install Node Serial Port: https://github.com/voodootikigod/node-serialport
  3. Install Coffee Script: npm install -g coffee-script
  4. Copy the following coffee script and compile it
{SerialPort} = require('serialport')
fs = require 'fs'

port = '/dev/ttyACM0'
serial = null
value = 0x00

toggle = =>
value = if value == 0x00 then 0x01 else 0x00
serial.write new Buffer([value])

console.log "Starting..."
fs.stat port, (err, stats) ->
   if err?
     console.log "Couldn't stat #{port}"
     process.exit()

console.log "Started."

serial = new SerialPort port, baudrate: 9600
setInterval toggle, 1000
5. Upload the following code to your Arduino Device
const int outputPin = 13;
 
#define LED 11

void setup()
{
	pinMode(LED, OUTPUT);
	Serial.begin(9600);
}

void loop()
{
	if (Serial.available() > 0) {
		int incomingByte = Serial.read();

		if (incomingByte == 0x01) {
			digitalWrite(LED, HIGH);
		} else if (incomingByte == 0x00) {
			digitalWrite(LED, LOW);
		}
	}
}

 

(Visited 330 times, 1 visits today)