Skip to content

Esp32MultiManager Class

The Esp32MultiManager class is a crucial component of UnderDeckLib for applications that need to interact with ESP32 devices via serial communication. It extends EventEmitter, allowing developers to listen for events such as connected, disconnected, data, and error, facilitating the construction of reactive and robust interfaces for embedded devices.

This class abstracts the complexity of discovering, connecting, and managing multiple serial ports, as well as sending and receiving JSON-formatted data. It is designed to automatically identify connected ESP32 devices and maintain a record of active connections, simplifying hardware integration in Node.js projects.

The Esp32MultiManager is typically instantiated within the UnderDeck class, but can be used independently if the only need is communication with ESP32:

import { Esp32MultiManager } from 'underdecklib/lib/esp32Manager.js';
const esp32Manager = new Esp32MultiManager(115200); // Optional: specify baud rate
  • Type: number
  • Description: The baud rate configured for serial communications. The default value is 115200.
  • Type: Map<string, object>
  • Description: A map that stores all active ESP32 connections. The key is the portPath (serial port path) and the value is an object containing the SerialPort instance and device information (deviceInfo).
new Esp32MultiManager(115200)
  • Description: The constructor for the Esp32MultiManager class. Initializes the manager with the specified baud rate. If no rate is provided, 115200 is used as default.
  • Parameters:
    • baudRate (number, optional): The baud rate for the serial ports. Default: 115200.
const devices = await esp32Manager.discoverDevices();
devices.forEach(device => {
console.log(`Device found: ${device.info.hostname} on port ${device.path}`);
});
  • Description: Scans all available serial ports on the system and attempts to identify connected ESP32 devices. It sends an identification command to each port and waits for a specific response from the ESP32. This method is asynchronous and returns a list of identified devices.
  • Returns: Promise<Array<{ path: string, info: object }>> A promise that resolves to an array of objects, where each object contains the port path (path) and device information (info).
// This method is internal and generally not called directly.
// Example of how it would be used internally:
// const result = await esp32Manager.probePort({ path: '/dev/ttyUSB0', manufacturer: 'FTDI' });
  • Description: An internal method used by discoverDevices() to test a single serial port. It opens the port, sends the identification command, and waits for a response within a timeout. If a valid ESP32 response is received, it resolves with the device information; otherwise, it resolves with null.
  • Parameters:
    • portInfo (object): An object containing information about the serial port (e.g., { path: string, manufacturer: string }).
  • Returns: Promise<{ path: string, info: object } | null> A promise that resolves with the device information or null.
esp32Manager.on("connected", ({ device, path }) => {
console.log(`Connected to ${device.hostname} on ${path}`);
});
esp32Manager.on("data", ({ device, data, path }) => {
console.log(`Data from ${device.hostname}:`, data);
});
esp32Manager.on("error", ({ device, error, path }) => {
console.error(`Error on ${device.hostname}:`, error.message);
});
esp32Manager.on("disconnected", ({ device, path }) => {
console.log(`Disconnected from ${device.hostname} on ${path}`);
});
// After discovering devices:
// const devices = await esp32Manager.discoverDevices();
// if (devices.length > 0) {
// await esp32Manager.connect(devices[0].path, devices[0].info);
// }
  • Description: Connects to a specific ESP32 device using the serial port path and device information. Once connected, it configures parsers to read data from the port and emits events such as connected, data, error, and disconnected.
  • Parameters:
    • portPath (string): The serial port path (e.g., COM1, /dev/ttyUSB0).
    • deviceInfo (object): The device information object obtained from discoverDevices().
  • Returns: Promise<void> A promise that resolves when the connection is established or rejects in case of failure.
// Assuming you have a device connected on '/dev/ttyUSB0'
// await esp32Manager.sendJson('/dev/ttyUSB0', { command: 'led_on', pin: 2 });
  • Description: Sends a JavaScript object as a JSON string to a connected ESP32 device. The object is serialized to JSON and a newline (\n) is added to indicate the end of the message. This method requires an active connection to the device.
  • Parameters:
    • portPath (string): The serial port path of the device to send data to.
    • payload (object): The JavaScript object to be sent.
  • Returns: Promise<void> A promise that resolves when the data is successfully sent or rejects in case of an error.
// Disconnect a specific device
// await esp32Manager.disconnect('/dev/ttyUSB0');
// Disconnect all devices
// await esp32Manager.disconnect();
  • Description: Disconnects from a specific serial port. If portPath is not provided, it disconnects from all actively connected ESP32 devices. This releases serial port resources.
  • Parameters:
    • portPath (string, optional): The serial port path to disconnect. If omitted, all active connections will be terminated.
  • Returns: Promise<void> A promise that resolves when the disconnection is complete.