A Node.js tester module for Arduino devices using serial communication.
npm install @danidoble/serial-node-arduino-tester

A Node.js library for serial communication with Arduino devices. Provides a simple event-based interface to interact with Arduino devices through serial communication.
- 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
Important: This library requires the following peer dependencies that must be installed explicitly:
``bash`
npm install @danidoble/serial-node-arduino-tester serialport serial-core
Or using yarn:
`bash`
yarn add @danidoble/serial-node-arduino-tester serialport serial-core
Or using pnpm:
`bash`
pnpm add @danidoble/serial-node-arduino-tester serialport serial-core
- Node.js >= 18.0.0
- serialport ^13.0.0
- serial-core ^0.2.0-dev.3
`typescript
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);
});
`
`typescript
// Send predefined commands
await arduino.sayHi();
await arduino.sayCredits();
// Perform multiple actions
await arduino.doSomething();
// Stop the connection
await arduino.stop();
`
Received messages have the following structure:
`typescript`
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
- 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
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
- constructor(config: SerialConfig) - Creates an Arduino instance with configuration
- start(): void - Starts the serial service
- stop(): Promise
- sayHi(): Promise
- sayCredits(): Promise
- sayAra(): Promise
- doSomething(): Promise
`c
// 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?");
}
}
}
`
`bash`
npm install
`bash`
npm run test
`bash`
npm run build
`bash`
npm run format
`bash`
npm run lint
`bash``
npm run dev
Contributions are welcome. Please read CONTRIBUTING.md for more details about our code of conduct and the process for submitting pull requests.
This project is licensed under the GPL-3.0 License - see the LICENSE.md file for details.
Danidoble - danidoble@gmail.com