@danidoble/serial-node-arduino-tester
v0.0.2
Published
A Node.js tester module for Arduino devices using serial communication.
Readme
Serial Node Arduino
A Node.js library for serial communication with Arduino devices. Provides a simple event-based interface to interact with Arduino devices through serial communication.
Features
- Event-based serial communication
- Automatic connection/disconnection management
- Command sending with custom aliases
- Support for timeouts and asynchronous responses
- Typed and structured messages
- Support for CommonJS and ES Modules
- Native TypeScript with included type definitions
Installation
Important: This library requires the following peer dependencies that must be installed explicitly:
npm install @danidoble/serial-node-arduino-tester serialport serial-coreOr using yarn:
yarn add @danidoble/serial-node-arduino-tester serialport serial-coreOr using pnpm:
pnpm add @danidoble/serial-node-arduino-tester serialport serial-coreRequirements
- Node.js >= 18.0.0
- serialport ^13.0.0
- serial-core ^0.2.0-dev.3
Usage
Basic Setup
import { Arduino } from '@danidoble/serial-node-arduino-tester';
// Configure Arduino connection
const arduino = new Arduino({
path: '/dev/ttyUSB0', // Your Arduino serial port
baudRate: 9600,
autoOpen: true
});
// Listen to connection events
arduino.on('serial:connected', info => {
console.log('Arduino connected:', info);
});
arduino.on('serial:disconnected', reason => {
console.log('Arduino disconnected:', reason);
});
// Listen to Arduino messages
arduino.on('serial:message', message => {
console.log('Message received:', message);
});
// Listen to errors
arduino.on('serial:error', error => {
console.error('Error:', error);
});Sending Commands
// Send predefined commands
await arduino.sayHi();
await arduino.sayCredits();
// Perform multiple actions
await arduino.doSomething();
// Stop the connection
await arduino.stop();Message Structure
Received messages have the following structure:
interface SerialMessage {
code: Buffer | string; // Raw code received
name: string; // Message name
description: string; // Message description
request: string; // Alias of the command that generated the response
no_code: number; // Numeric message code
}Predefined Message Types:
connected(code: 100) - Connection establishedthanks(code: 101) - Credits message from Arduinohello there(code: 102) - Greeting messageara ara(code: 404) - Easter egg messageunknown(code: 400) - Unknown/unrecognized message
Available Events
serial:status- Serial connection status changesserial:connected- Connection established with the deviceserial:disconnected- Device disconnectedserial:message- Structured message received from Arduinoserial:data- Raw data received from Arduinoserial:error- Communication errors
API Documentation
The library includes comprehensive JSDoc comments for all public methods and classes. For detailed information about the Arduino class and its methods, refer to the source code:
- Arduino class - Main class for serial communication
- Types - TypeScript type definitions
Arduino Class Methods
constructor(config: SerialConfig)- Creates an Arduino instance with configurationstart(): void- Starts the serial servicestop(): Promise<void>- Stops and disconnects from the ArduinosayHi(): Promise<void>- Sends HI command to ArduinosayCredits(): Promise<void>- Sends CREDITS command to ArduinosayAra(): Promise<void>- Sends OTHER command (ara ara message)doSomething(): Promise<void>- Executes all commands concurrently
Arduino Code Example
// serial.ino
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) { // Check if data to read is available
String comando = Serial.readStringUntil('\n'); // read the data until a new line is found
comando.trim(); // remove leading/trailing whitespace and \r characters
if(comando == "CONNECT"){
Serial.println("connected");
} else if (comando == "CREDITS") {
Serial.println("created by danidoble");
} else if (comando == "HI") {
Serial.println("hello there");
} else {
Serial.println("ara ara, what are you doing?");
}
}
}Development
Install Dependencies
npm installRun Tests
npm run testBuild the Project
npm run buildCode Formatting
npm run formatLinting
npm run lintDevelopment Mode
npm run devContributing
Contributions are welcome. Please read CONTRIBUTING.md for more details about our code of conduct and the process for submitting pull requests.
License
This project is licensed under the GPL-3.0 License - see the LICENSE.md file for details.
Author
Danidoble - [email protected]
