SerialPipe
Serial-based communication pipe for WLED devices. Communicates over USB serial, sending and receiving newline-delimited JSON.
Implements the WledPipe interface.
Package
robowled.wledpipe.SerialPipe
Constructor
SerialPipe(SerialPort.Port usbPort, int baudRate)
Creates a SerialPipe connected to a WLED device via USB serial.
Parameters:
| Parameter | Type | Description |
|---|---|---|
usbPort |
SerialPort.Port |
The WPILib serial port (e.g., SerialPort.Port.kUSB) |
baudRate |
int |
The baud rate for serial communication (e.g., 115200) |
Example:
import robowled.wledpipe.SerialPipe;
import edu.wpi.first.wpilibj.SerialPort;
// Create a serial connection on USB port at 115200 baud
SerialPipe wled = new SerialPipe(SerialPort.Port.kUSB, 115200);
Available Ports:
| Port | Description |
|---|---|
SerialPort.Port.kUSB |
Primary USB port on roboRIO |
SerialPort.Port.kUSB1 |
Secondary USB port |
SerialPort.Port.kUSB2 |
Tertiary USB port |
SerialPort.Port.kMXP |
MXP expansion port serial |
SerialPort.Port.kOnboard |
Onboard RS-232 port |
Methods
All methods are defined by the WledPipe interface.
sendGson(JsonElement json)
Sends a Gson JsonElement as JSON over the serial port, followed by a newline.
Parameters:
| Parameter | Type | Description |
|---|---|---|
json |
JsonElement |
The Gson JSON element to send |
Throws: Exception if sending fails
Example:
import com.google.gson.JsonObject;
// Build a JSON command using Gson
JsonObject command = new JsonObject();
command.addProperty("on", true);
command.addProperty("bri", 255);
wled.sendGson(command);
// Sends: {"on":true,"bri":255}\n
sendString(String str)
Sends a raw string over the serial port.
Parameters:
| Parameter | Type | Description |
|---|---|---|
str |
String |
The string to send |
Throws: Exception if sending fails
Example:
// Send a raw JSON command
wled.sendString("{\"ps\":1}\n");
// Activate an effect
wled.sendString("{\"seg\":[{\"fx\":42}]}\n");
tryReadString()
Non-blocking read operation. Call periodically to check for incoming data. Returns a complete line when available.
Returns: String - A complete line (without newline), or null if no complete line is available
Throws: Exception if reading fails
Example:
// In a periodic method
@Override
public void periodic() {
try {
String response = wled.tryReadString();
if (response != null) {
System.out.println("WLED response: " + response);
}
} catch (Exception e) {
// Handle error
}
}
tryReadGson()
Non-blocking read that parses the response as a Gson JsonElement.
Returns: JsonElement - A parsed JSON element, or null if no complete line is available
Throws: Exception if reading or parsing fails
Example:
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
// In a periodic method
@Override
public void periodic() {
try {
JsonElement response = wled.tryReadGson();
if (response != null && response.isJsonObject()) {
JsonObject state = response.getAsJsonObject();
boolean isOn = state.get("on").getAsBoolean();
int brightness = state.get("bri").getAsInt();
System.out.println("WLED on=" + isOn + ", bri=" + brightness);
}
} catch (Exception e) {
// Handle error
}
}
Complete Example
import robowled.wledpipe.SerialPipe;
import edu.wpi.first.wpilibj.SerialPort;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
public class LedSubsystem extends SubsystemBase {
private final SerialPipe wled;
public LedSubsystem() {
wled = new SerialPipe(SerialPort.Port.kUSB, 115200);
}
public void setPreset(int presetNumber) {
try {
wled.sendString("{\"ps\":" + presetNumber + "}\n");
} catch (Exception e) {
System.err.println("Failed to set preset: " + e.getMessage());
}
}
public void setColor(int r, int g, int b) {
try {
String json = String.format(
"{\"seg\":[{\"col\":[[%d,%d,%d]]}]}\n", r, g, b);
wled.sendString(json);
} catch (Exception e) {
System.err.println("Failed to set color: " + e.getMessage());
}
}
public void setBrightness(int brightness) {
try {
wled.sendString("{\"bri\":" + brightness + "}\n");
} catch (Exception e) {
System.err.println("Failed to set brightness: " + e.getMessage());
}
}
@Override
public void periodic() {
try {
String response = wled.tryReadString();
if (response != null) {
// Process any responses from WLED
}
} catch (Exception e) {
// Silently ignore read errors in periodic
}
}
}
See Also
- WledPipe Interface - The interface SerialPipe implements
- NetworkPipe - For TCP/IP network connections
- DummyPipe - For testing and simulation
- Getting Started - Basic usage guide
- WLED JSON API - Official WLED documentation