arduino-nodejs
v1.0.0
Published
Module for arduino-nodejs communication
Downloads
1
Readme
Module for arduino-nodejs communication
const arduino_node = require("arduino-nodejs")
// Returns a promise
// Example of result: ["COM9", "COM3", "COM1"]
// The lower on the list, the bigger is the chance
// Of it being a arduino board
arduino_node.list()
// COM_port should be a port returned by list()
// Baud_rate should be one of the rates
// Here: https://arduino.stackexchange.com/questions/296/
// Baud rates higher than 115200 may not work on
// Devices using CH430G/CH430 or other non-standard
// USB to Serial converters
// Delay should be a number in milliseconds to wait
// After sending a message.
// With lower baud rates it may be neccesary to include
// A number bigger than 10
arduino_node.connect(COM_port, Baud_Rate, Delay?)
// Sends a object to the Arduino
await arduino_node.write(object)
// Reads a value. Should be in a interval
// As small as possible to get the
// information fast
arduino_node.read()
// The arduino code should be like this:
#include <ArduinoJson.h>
void setup()
{
Serial.begin(115200); // Should be the same baud rate as using in the nodejs file
}
void loop()
{
if (Serial.available()) {
DynamicJsonDocument doc(1024);
deserializeJson(doc, Serial);
if (doc["fingerprint"] == "X-Node-Fingerprint") {
// Arduino detected the serial write and that it is from a nodejs script
// Do something cool here
}
}
}
// Example of code that displays the views and likes of a youtube video:
// NodeJS
await arduino_node.connect(list[0], 115200, 10)
const arduino_node = require("arduino-nodejs")
const gaxios = require("gaxios")
let video_id = "aKkVqmvs4NA"
let youtube_key = ""
let link = `https://www.googleapis.com/youtube/v3/videos?part=statistics&id=${id}&key=${youtube_key}`
function scrapeData(id) {
return new Promise((resolve, reject) => {
gaxios.request({ method: "GET", url: link})
.then((data) => {
resolve(data.data.items[0].statistics)
})
.catch((err) => {
reject(err)
})
})
}
function printToScreen(){
scrapeData(video_id)
.then(async (data) => {
await arduino_node.write({ type: "lcd_clear" })
await arduino_node.write({ type: "setCursor", "pos1": 0, "pos2": 0 })
await arduino_node.write({ type: "lcd_print", "text": `views: ${data.viewCount}` })
await arduino_node.write({ type: "setCursor", "pos1": 0, "pos2": 1 })
await arduino_node.write({ type: "lcd_print", "text": `likes: ${data.likeCount}` })
})
}
arduino_node.list().then(async (list) => {
arduino_node.connect(list[0], 115200, 10)
printToScreen()
setInterval(async () => {
printToScreen()
}, 8100);
}).catch((err) => {
console.log(err)
})
// Arduino
#include <LiquidCrystal_I2C.h>
#include <ArduinoJson.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // This is using the I2C version, change this if you want the normal one
void setup()
{
Serial.begin(115200);
lcd.init();
lcd.backlight();
}
void loop()
{
if (Serial.available()) {
DynamicJsonDocument doc(1024);
deserializeJson(doc, Serial);
if (doc["fingerprint"] == "X-Node-Fingerprint") {
if (doc["type"] == "lcd_print") {
lcd.print(doc["text"].as<String>());
} else if (doc["type"] == "lcd_clear") {
lcd.clear();
} else if (doc["type"] == "setCursor") {
lcd.setCursor(doc["pos1"].as<int>(), doc["pos2"].as<int>());
}
}
}
}