Skip to content

Instantly share code, notes, and snippets.

@eelcocramer
Last active April 14, 2022 11:52
Show Gist options
  • Save eelcocramer/4117108 to your computer and use it in GitHub Desktop.
Save eelcocramer/4117108 to your computer and use it in GitHub Desktop.
Arduino LED controlled over Bluetooth with NodeJS
// This code uses the SoftwareSerial library.
// It can be obtained here: http://arduino.cc/en/Reference/SoftwareSerial
unsigned int timeout=0;
unsigned char state=0;
char val; // variable to receive data from the serial port
int ledpin = 13; // LED connected to pin 13
// Timer2 service
ISR(TIMER2_OVF_vect) {
TCNT2 = 0;
timeout++;
if (timeout>61) {
state=1;
timeout=0;
}
}
// initialize the timer 2 service
void init_timer2(void) {
TCCR2A |= (1 << WGM21) | (1 << WGM20);
TCCR2B |= 0x07; // by clk/1024
ASSR |= (0<<AS2); // Use internal clock - external clock not used in Arduino
TIMSK2 |= 0x01; //Timer2 Overflow Interrupt Enable
TCNT2 = 0;
sei();
}
// sets up the program
void setup() {
// open the serial port
Serial.begin(9600);
// bind the ledpin as output
pinMode(ledpin, OUTPUT);
// bind pin 2 as input
pinMode(2,INPUT);
// interrupt for reading from the bluetooth connection
attachInterrupt(0, cleantime, FALLING);
init_timer2();
}
// function for controlling the led
void control(void) {
if (Serial.available()) { // if data is available to read
val = Serial.read(); // read it and store it in 'val'
}
if (val == '1') { // if '1' was received
Serial.println('1'); // display the new value
digitalWrite(ledpin, HIGH); // turn ON the LED
} else if (val == '0') {
Serial.println('0'); // display the new value
digitalWrite(ledpin, LOW); // otherwise turn it OFF
} else if (val == 's') { // if 's' is received display the current status of the led
if (digitalRead(ledpin) == HIGH) {
Serial.println('1');
} else {
Serial.println('0');
}
}
val = ' ';
delay(100); // wait 100ms for next reading
}
// control loop for the program
void loop() {
switch(state) {
case 0:
// no bt connection, do nothing
break;
case 1:
// when there is a bt connection enter the control function
control();
break;
}
}
void cleantime() {
timeout=0;
state=0;
}
var BTSP = require('bluetooth-serial-port');
var serial = new BTSP.BluetoothSerialPort();
serial.on('found', function(address, name) {
// you might want to check the found address with the address of your
// bluetooth enabled Arduino device here.
serial.findSerialPortChannel(address, function(channel) {
serial.connect(bluetoothAddress, channel, function() {
console.log('connected');
process.stdin.resume();
process.stdin.setEncoding('utf8');
console.log('Press "1" or "0" and "ENTER" to turn on or off the light.')
process.stdin.on('data', function (data) {
serial.write(data);
});
serial.on('data', function(data) {
console.log('Received: ' + data);
});
}, function () {
console.log('cannot connect');
});
});
});
serial.inquire();
@trinhnam99
Copy link

var BTSP = require('bluetooth-serial-port');
var serial = new BTSP.BluetoothSerialPort();

serial.on('found', function(address, name) {

// you might want to check the found address with the address of your
// bluetooth enabled Arduino device here.

serial.findSerialPortChannel(address, function(channel) {
    serial.connect(bluetoothAddress, channel, function() {
        console.log('connected');
        process.stdin.resume();
        process.stdin.setEncoding('utf8');
        console.log('Press "1" or "0" and "ENTER" to turn on or off the light.')

        process.stdin.on('data', function (data) {
            serial.write(data);
        });

        serial.on('data', function(data) {
            console.log('Received: ' + data);
        });
    }, function () {
        console.log('cannot connect');
    });
});

});

serial.inquire();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment