duino is a framework for working with Arduinos in node.js. Here show how to (with little bit modification) make it run on Ubuntu, to control the LED of Arduino Due using Node.js.
- Connect Arduino Due to your host PC with USB cable.
- In Arduino Due board -
Create a new sketch, copy the source code of du.ino here, https://github.com/ecto/duino/blob/master/src/du.ino.
You will be complained with error of 'int index' redeclared as different kind of symbol. To fix it, rename variable index, for example idx.
#include <Servo.h>
bool debug = false;
int idx = 0;
char messageBuffer[12];
char cmd[3];
char pin[3];
char val[4];
char aux[4];
Servo servo;
void setup() {
Serial.begin(115200);
}
void loop() {
while(Serial.available() > 0) {
char x = Serial.read();
if (x == '!') idx = 0; // start
else if (x == '.') process(); // end
else messageBuffer[idx++] = x;
}
}
/*
* Deal with a full message and determine function to call
*/
void process() {
idx = 0;
strncpy(cmd, messageBuffer, 2);
cmd[2] = '\0';
strncpy(pin, messageBuffer + 2, 2);
pin[2] = '\0';
if (atoi(cmd) > 90) {
strncpy(val, messageBuffer + 4, 2);
val[2] = '\0';
strncpy(aux, messageBuffer + 6, 3);
aux[3] = '\0';
} else {
strncpy(val, messageBuffer + 4, 3);
val[3] = '\0';
strncpy(aux, messageBuffer + 7, 3);
aux[3] = '\0';
}
if (debug) {
Serial.println(messageBuffer);
}
int cmdid = atoi(cmd);
// Serial.println(cmd);
// Serial.println(pin);
// Serial.println(val);
// Serial.println(aux);
switch(cmdid) {
case 0: sm(pin,val); break;
case 1: dw(pin,val); break;
case 2: dr(pin,val); break;
case 3: aw(pin,val); break;
case 4: ar(pin,val); break;
case 97: handlePing(pin,val,aux); break;
case 98: handleServo(pin,val,aux); break;
case 99: toggleDebug(val); break;
default: break;
}
}
/*
* Toggle debug mode
*/
void toggleDebug(char *val) {
if (atoi(val) == 0) {
debug = false;
Serial.println("goodbye");
} else {
debug = true;
Serial.println("hello");
}
}
/*
* Set pin mode
*/
void sm(char *pin, char *val) {
if (debug) Serial.println("sm");
int p = getPin(pin);
if(p == -1) { if(debug) Serial.println("badpin"); return; }
if (atoi(val) == 0) {
pinMode(p, OUTPUT);
} else {
pinMode(p, INPUT);
}
}
/*
* Digital write
*/
void dw(char *pin, char *val) {
if (debug) Serial.println("dw");
int p = getPin(pin);
if(p == -1) { if(debug) Serial.println("badpin"); return; }
pinMode(p, OUTPUT);
if (atoi(val) == 0) {
digitalWrite(p, LOW);
} else {
digitalWrite(p, HIGH);
}
}
/*
* Digital read
*/
void dr(char *pin, char *val) {
if (debug) Serial.println("dr");
int p = getPin(pin);
if(p == -1) { if(debug) Serial.println("badpin"); return; }
pinMode(p, INPUT);
int oraw = digitalRead(p);
char m[7];
sprintf(m, "%02d::%02d", p,oraw);
Serial.println(m);
}
/*
* Analog read
*/
void ar(char *pin, char *val) {
if(debug) Serial.println("ar");
int p = getPin(pin);
if(p == -1) { if(debug) Serial.println("badpin"); return; }
pinMode(p, INPUT); // don't want to sw
int rval = analogRead(p);
char m[8];
sprintf(m, "%s::%03d", pin, rval);
Serial.println(m);
}
void aw(char *pin, char *val) {
if(debug) Serial.println("aw");
int p = getPin(pin);
pinMode(p, OUTPUT);
if(p == -1) { if(debug) Serial.println("badpin"); return; }
analogWrite(p,atoi(val));
}
int getPin(char *pin) { //Converts to A0-A5, and returns -1 on error
int ret = -1;
if(pin[0] == 'A' || pin[0] == 'a') {
switch(pin[1]) {
case '0': ret = A0; break;
case '1': ret = A1; break;
case '2': ret = A2; break;
case '3': ret = A3; break;
case '4': ret = A4; break;
case '5': ret = A5; break;
default: break;
}
} else {
ret = atoi(pin);
if(ret == 0 && (pin[0] != '0' || pin[1] != '0')) {
ret = -1;
}
}
return ret;
}
/*
* Handle Ping commands
* fire, read
*/
void handlePing(char *pin, char *val, char *aux) {
if (debug) Serial.println("ss");
int p = getPin(pin);
if(p == -1) { if(debug) Serial.println("badpin"); return; }
Serial.println("got signal");
// 01(1) Fire and Read
if (atoi(val) == 1) {
char m[16];
pinMode(p, OUTPUT);
digitalWrite(p, LOW);
delayMicroseconds(2);
digitalWrite(p, HIGH);
delayMicroseconds(5);
digitalWrite(p, LOW);
Serial.println("ping fired");
pinMode(p, INPUT);
sprintf(m, "%s::read::%08d", pin, pulseIn(p, HIGH));
Serial.println(m);
delay(50);
}
}
/*
* Handle Servo commands
* attach, detach, write, read, writeMicroseconds, attached
*/
void handleServo(char *pin, char *val, char *aux) {
if (debug) Serial.println("ss");
int p = getPin(pin);
if(p == -1) { if(debug) Serial.println("badpin"); return; }
Serial.println("signal: servo");
// 00(0) Detach
if (atoi(val) == 0) {
servo.detach();
char m[12];
sprintf(m, "%s::detached", pin);
Serial.println(m);
// 01(1) Attach
} else if (atoi(val) == 1) {
// servo.attach(p, 750, 2250);
servo.attach(p);
char m[12];
sprintf(m, "%s::attached", pin);
Serial.println(m);
// 02(2) Write
} else if (atoi(val) == 2) {
Serial.println("writing to servo");
Serial.println(atoi(aux));
// Write to servo
servo.write(atoi(aux));
delay(15);
// TODO: Experiment with microsecond pulses
// digitalWrite(pin, HIGH); // start the pulse
// delayMicroseconds(pulseWidth); // pulse width
// digitalWrite(pin, LOW); // stop the pulse
// 03(3) Read
} else if (atoi(val) == 3) {
Serial.println("reading servo");
int sval = servo.read();
char m[13];
sprintf(m, "%s::read::%03d", pin, sval);
Serial.println(m);
}
}
Compile and upload to Arduino Due board.
- In Host PC, running Ubuntu -
Install duino for Node.js, run the command:
$ npm install duino
Create a .js file, testDuino.js. Copy the example code here, https://github.com/ecto/duino#usage.
You will be complained with error of Cannot open /dev/vboxusb.
To make it run on Ubuntu with usb port of ACMxx, modify arduino.Board to have device: "ACM".
var arduino = require('duino');
var board = new arduino.Board({
device: "ACM"
});
var led = new arduino.Led({
board: board,
pin: 13
});
led.blink();
- Now you can run your node.js code to toggle the LED on Arduino board.
$ node testDuino
Hi, please, I have a problem. I went exactly according to the procedure and at the end when I want to run JS file writes me this:
ReplyDeleteError: Error: Could not find Arduino
at /home/dominik/Downloads/dui/node_modules/duino/lib/board.js:25:15
at /home/dominik/Downloads/dui/node_modules/duino/lib/board.js:104:5
Im working in Ubuntu with Arduino Uno.
Thank you