Skip to content

Your First Script

This walkthrough guides you through writing a simple automation script that reads a device, logs data, and reacts to changes.

Prerequisites

  • Muxit server running (node start.js server --gui)
  • At least one connector configured (the built-in TestDevice works perfectly)

Step 1: Create the Script File

In the Explorer sidebar, click New Script or create workspace/scripts/hello.js manually.

javascript
log.info("Hello from Muxit!");

Click Run in the editor toolbar. You should see the message in the Output panel.

Step 2: Read a Device Property

The TestDevice connector is always available. Let's read from it:

javascript
const test = connector("test-device");

const voltage = await test.voltage();
log.info(`Voltage: ${voltage}V`);

connector("name") returns a proxy object. Calling a property name with no arguments reads it.

Step 3: Write to a Device

Pass an argument to set a property:

javascript
const test = connector("test-device");

await test.voltage(12);
log.info(`Set voltage to 12V`);

const readback = await test.voltage();
log.info(`Readback: ${readback}V`);

Step 4: Add a Loop

Most useful scripts run continuously. Use script.running for graceful shutdown:

javascript
const test = connector("test-device");

while (script.running) {
  const v = await test.voltage();
  const i = await test.current();
  log.info(`V=${v}V, I=${i}A, P=${(v * i).toFixed(3)}W`);
  await delay(1000);
}

log.info("Script stopped cleanly");

Click Stop in the editor toolbar to end the loop. The script exits gracefully because script.running becomes false.

Step 5: Emit Events

Other scripts and dashboard widgets can react to events you emit:

javascript
const test = connector("test-device");

while (script.running) {
  const v = await test.voltage();
  const i = await test.current();

  emit("reading", { voltage: v, current: i, power: v * i });
  log.info(`Emitted reading: V=${v}, I=${i}`);

  await delay(1000);
}

Step 6: Push Data to a Dashboard

Create a virtual store connector to bridge your script to dashboard widgets. First, create workspace/connectors/my-store.js:

javascript
export default {
  driver: "GUI",
  config: {
    widgets: [
      { name: "power", type: "gauge", label: "Power", min: 0, max: 100, unit: "W" },
      { name: "status", type: "text", label: "Status", value: "Idle" },
    ],
  },
};

Then update your script to write to the store:

javascript
const test = connector("test-device");
const store = connector("my-store");

await store.status("Running");

while (script.running) {
  const v = await test.voltage();
  const i = await test.current();
  await store.power(v * i);
  await delay(1000);
}

await store.status("Stopped");

Create a dashboard with a gauge widget bound to my-store.power to see it update live.

Step 7: Add Error Handling

Wrap device calls in try/catch for robustness:

javascript
const test = connector("test-device");

while (script.running) {
  try {
    const v = await test.voltage();
    log.info(`Voltage: ${v}V`);
  } catch (err) {
    log.error(`Failed to read: ${err.message}`);
  }
  await delay(1000);
}

Complete Example

Here's a polished version combining everything:

javascript
const test = connector("test-device");

// Setup
log.info("Starting voltage monitor");
await test.voltage(5);

// Main loop
while (script.running) {
  try {
    const v = await test.measured_voltage();
    const i = await test.measured_current();
    const power = v * i;

    log.info(`V=${v.toFixed(2)}V  I=${i.toFixed(3)}A  P=${power.toFixed(3)}W`);
    emit("reading", { voltage: v, current: i, power });

    if (power > 50) {
      log.warn("Power exceeding 50W!");
    }
  } catch (err) {
    log.error(`Read error: ${err.message}`);
  }

  await delay(500);
}

// Cleanup
await test.voltage(0);
log.info("Monitor stopped");

Next Steps

Muxit — Hardware Orchestration Platform