Socket
Socket
Sign inDemoInstall

arduino-nodejs

Package Overview
Dependencies
15
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    arduino-nodejs

Module for arduino-nodejs communication


Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Install size
778 kB
Created
Weekly downloads
 

Readme

Source

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>());
      }
    }
  }
}

FAQs

Last updated on 29 Apr 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc