Skip to content

How To Read Data from LabJack into Machinechat JEDI using a Python Plugin

Overview

This tutorial demonstrates how to collect data from a LabJack device and seamlessly integrate it into Machinechat JEDI for visualization and monitoring. We'll use the mclabjackreader.py (Machinechat LabJack Reader) Python script as a custom plugin for JEDI, simplifying the data transfer process.

LabJack T4
LabJack T4

Prerequisites

  • LabJack T4 device
  • Machinechat JEDI installed and running
  • Python 3.4 or newer (https://www.python.org/downloads/)
  • Basic familiarity with Python (if you want to modify the plugin)

Step-by-Step Guide

1. Install Python (if you haven't already)

  • Windows:
  • macOS/Linux: Python often comes pre-installed. To check, open a Terminal and type python3 --version. If you see a version number (3.4 or above), Python is ready. Otherwise, download and install Python from https://www.python.org/downloads/.

2. Install the labjack-ljm Python Library

  • Open Your Terminal or Command Prompt:

    • Windows: Press the Windows key + R, type "cmd", and press Enter.
    • macOS: Press Command + Space, type "Terminal", and press Enter.
  • Install using pip: Type the following command and press Enter:

    pip install labjack-ljm
    

  • Troubleshooting (macOS/Linux): If you get a "Permission denied" error, try:

    sudo pip install labjack-ljm
    

3. Download and Configure Machinechat JEDI

Refer to JEDI documentation for detailed instructions:

4. Obtain the mclabjackreader.py Plugin

  • Right-click this link and download mclabjackreader.py.
  • Place the downloaded file into the folder named plugins within your JEDI installation directory.

5. Customize the mclabjackreader.py Plugin

To configure the LabJack registers you want to read:

  • Open the mclabjackreader.py file in a text editor or Python IDE.
  • Edit the registers_to_read List:
    • Edit the existing register names (e.g., "AIN0", "AIN1") to include the registers you want to read from your LabJack device.
    • Refer to the LabJack documentation for a list of valid register names.
  • Adjust the PRECISION Variable: Change the value to control the number of decimal places sent to JEDI (e.g., PRECISION = 2 for two decimal places).

Here's the mclabjackreader.py code for quick reference:

#!/usr/bin/env python3
# Machinechat JEDI LabJack Reader
# Copyright (c) 2024 Machinechat LLC. All rights reserved.
# Licensed under the MIT License.

import sys
import time
from labjack import ljm

"""
This script reads data from a LabJack device and sends it to 
Machinechat JEDI using its custom data collector feature.  

Usage:
1. Edit the `registers_to_read` list with the registers you want to read.
2. Set the `PRECISION` variable to the number of decimal places you want to send.
3. Set the execution permission on the script: `chmod +x mclabjackreader.py`
4. Run the script: `./mclabjackreader.py` and test that it prints the data you expect.
5. Configure a custom data collector in Machinechat's software and set the command to
    `mclabjackreader.py` to start sending data to Machinechat JEDI.
"""

# === USER CONFIGURABLE SETTINGS ===

# Edit this list with the registers you want to read from the LabJack
registers_to_read = ["AIN0", "AIN1", "FIO4", "TEMPERATURE_DEVICE_K"]

# Number of decimal places to send for floating point values
PRECISION = 3

# LabJack connection details (usually won't need changing)
device_type = "ANY"
connection_type = "USB"
identifier = "ANY"

# === DO NOT EDIT BELOW THIS LINE UNLESS YOU WANT TO MODIFY THE FUNCTIONALITY ===

# *** Functions ***
def connect_labjack():
    """Tries to connect to the LabJack, retries a few times if needed."""
    for attempt in range(10):
        try:
            return ljm.openS(device_type, connection_type, identifier)
        except ljm.LJMError as e:
            print(f"Error connecting to LabJack (attempt {attempt + 1}): {e}")
            time.sleep(1)
    print("Failed to connect to LabJack after multiple attempts. Exiting.")
    sys.exit(1)


def read_and_send_data(handle, register):
    """Reads data from a LabJack register and sends it to Machinechat JEDI."""
    result = ljm.eReadName(handle, register)
    dev_id = "LJ" + str(ljm.getHandleInfo(handle)[2])  # Device ID based on serial
    print(f"metric:id={dev_id},n={register},vd={result:.{PRECISION}f},u=value") 


def main():
    """Main program loop."""
    handle = connect_labjack()

    try:
        for register in registers_to_read:
            read_and_send_data(handle, register)
    finally:
        ljm.close(handle)


# *** Start the program ***
if __name__ == "__main__":
    main()

6. Configure JEDI's Custom Data Collector

For detailed instructions on configuring the custom data collector, please refer to the Machinechat JEDI documentation: Custom Data Collector

7. Connect Your LabJack

Connect the LabJack to your computer using a USB cable.

8. Verify Data in JEDI

Go to JEDI's Device Dashboard. You should see data with a device ID starting with "LJ" (e.g., LJ12345 based on the LabJack's serial number).

9. Build Your Dashboard

Refer to JEDI documentation for detailed information about creating dashboards, rules, alerts, and actions with LabJack data:

Need More Help?

Contact our support team: support@machinechat.io