<!-- Source: https://docs.captron.com/application-notes/automationdirect-smartled -->

# AutomationDirect (Productivity 2000) — smartLED Integration Guide

This guide walks through publishing CAPTRON LED strip commands from an **AutomationDirect Productivity 2000** PLC over MQTT. By the end you'll have the P2000 driving an SLS10 strip through an SEH201 controller, with the ability to switch between multiple LED payloads on the fly.

## Prerequisites

Before starting, make sure:

- The **SEH201** controller is powered, on the network, and connected to your MQTT broker. See the *smartLED Integration Trainer Guide* for SEH201 setup.
- The **industrial PC** (or another machine on the same subnet) is running Mosquitto as the broker.
- **Productivity Suite** is installed on a PC that can reach the P2000 CPU.
- You know the P2000's IP address, the broker's IP address, and the SEH201's unique device ID (printed on the controller label).

---

## 1. Create the MQTT_Client tag

Open Productivity Suite and connect to the P2000 project.

1. Navigate to **Tag Database → Add Tags**.
2. Name the tag **`MQTT_Client`**.
3. Enable **System Structure**.
4. Click **Add** and then **Close**.

![Productivity Suite tag database](./images/automationdirect-image1.png)

![Add Tag dialog with MQTT_Client structure](./images/automationdirect-image2.png)

> **Why a structure?** Enabling System Structure pulls in the prebuilt MQTT client struct (`Enable`, `Publisher1Enable`, etc.) so you don't have to define those fields yourself. You'll reference them later in ladder logic.

---

## 2. Add Topic and Payload string tags

Still in the Tag Database, add two **string** tags to hold the MQTT topic and payload:

<table>
  <thead>
    <tr>
      <th>Tag name</th>
      <th>Type</th>
      <th>Length</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code>Topic</code></td>
      <td>String</td>
      <td><strong>128 characters</strong></td>
    </tr>
    <tr>
      <td><code>Payload</code></td>
      <td>String</td>
      <td><strong>128 characters</strong></td>
    </tr>
  </tbody>
</table>

> **Important:** both strings must be **128 characters**. The default string length is shorter and will truncate the JSON payload mid-message, which silently breaks publishing.

Click **Add Tags** to save.

### Set initial values

In the **Init Value** column for each tag, paste the following:

**Payload tag — init value:**

```json
{"LED_STRIP_1":{"Active":true,"Segments":[{"StartLED":0,"StopLED":80,"Speed":190,"Effect":4,"Colors":[{"R":0,"G":0,"B":150}]}]}}
```

**Topic tag — init value:**

```
captron.com/SEH201/nd/<unique-id>/Set/Data/LedStrip
```

> Replace `<unique-id>` with your SEH201's device ID (on the label). The topic is case-sensitive and the SEH201 will silently ignore typo'd topics — copy carefully.

![Tag database with Topic and Payload populated](./images/automationdirect-image3.png)

---

## 3. Configure the MQTT Client (Hardware Config)

Navigate to **Setup → Hardware Config → MQTT → MQTT Client**.

![Setup menu path](./images/automationdirect-image4.png)

![Hardware Config window](./images/automationdirect-image5.png)

![MQTT section](./images/automationdirect-image6.png)

![MQTT Client configuration](./images/automationdirect-image7.png)

In the MQTT Client dialog:

1. Enter a **Client ID** (anything unique on the broker — e.g., `P2000_LineA`).
2. Enter the **IP Address** of the MQTT broker (the industrial PC).
3. Enable **Use Structure**.
4. Enter the **struct tag name** — `MQTT_Client` (the tag you created in step 1).

![MQTT Client dialog with Client ID, IP, and struct configured](./images/automationdirect-image8.png)

---

## 4. Add a Publisher

Inside the MQTT Client configuration:

1. Click the **plus sign** to add a publisher.
2. Add the **Topic** tag and the **Payload** tag to the publisher.
3. Confirm by clicking **OK**.

![Publisher added with Topic and Payload tags](./images/automationdirect-image9.png)

---

## 5. Write the publishing logic

Create a new **Task** for the MQTT logic.

Add the following rungs:

1. An **out coil** for `MQTT_CLIENT.Enable` — this enables MQTT publishing and subscribing on the PLC.
2. An **out coil** for `MQTT_CLIENT.Publisher1Enable` — when this bit goes high, the PLC publishes the payload you configured in step 4.

![Ladder rungs for Enable and Publisher1Enable](./images/automationdirect-image10.png)

---

## 6. Download and test

Download the project to the CPU. Once running, the SLS10 LED strip should display the effect defined in the payload (blue strobe, in this example).

If nothing happens, check:

- **Broker connectivity** — can the P2000 ping the industrial PC?
- **Topic spelling** — the `<unique-id>` segment must match the SEH201 label exactly.
- **JSON formatting** — straight quotes only. If you typed the payload in Word or Notepad with autocorrect on, smart quotes will break parsing.
- **Both string tags are 128 chars** — short strings truncate the JSON.

---

## 7. Dynamic payload switching (multiple LED states)

The P2000 only supports **3 publishers** per MQTT client. To switch between more than three LED states, use a single publisher with a **master payload tag** and copy the desired payload into it at runtime.

### Set up the master payload and state payloads

1. Create a string tag named **`LEDStripMasterPayload`** (128 characters). This is the tag the publisher will actually point to.
2. Create one string tag per LED state you want to switch between. Set each one's **Init Value** to the JSON payload for that state.

![Master payload and state payload tags](./images/automationdirect-image11.png)

In this example, three digital inputs select between three LED payloads.

![Digital inputs mapped to payloads](./images/automationdirect-image12.png)

### Copy the active payload into the master

Add **Copy Data** instructions that move the selected state payload into `LEDStripMasterPayload` when its digital input goes high.

![Copy Data rungs](./images/automationdirect-image13.png)

### Add a debounce counter and timer

To avoid republishing on every scan while an input is held, gate the publish with a simple counter and a per-input timer.

1. Create a counter tag in the Tag Database.

   ![Counter tag](./images/automationdirect-image14.png)

2. Add a **Simple Timer** in a sub-rung for each of the three inputs.

   ![Simple timer rung](./images/automationdirect-image15.png)

   ![Timer wired per input](./images/automationdirect-image16.png)

3. Set the timer's **preset value** to `100` (100 ms) in the init values.

   ![Timer preset = 100](./images/automationdirect-image17.png)

### Trigger the publish

Wire up the final publishing rung:

- A **normally open contact** for `Simple_Timer.Done`
- An **out coil** for `MQTT_CLIENT.Publisher1Enable`

![Publish trigger rung](./images/automationdirect-image18.png)

When a digital input goes high, the matching payload copies into `LEDStripMasterPayload`, the timer expires, and the publisher fires once — driving the SLS10 strip to the new state.

---

## Reference: example payloads to drop into state tags

Copy these directly into the **Init Value** of your state payload tags. They're already formatted with straight quotes.

**Solid red, LEDs 0–80:**

```json
{"LED_STRIP_1":{"Active":true,"Segments":[{"StartLED":0,"StopLED":80,"Speed":0,"Effect":0,"Colors":[{"R":255,"G":0,"B":0}]}]}}
```

**Solid green, LEDs 0–80:**

```json
{"LED_STRIP_1":{"Active":true,"Segments":[{"StartLED":0,"StopLED":80,"Speed":0,"Effect":0,"Colors":[{"R":0,"G":255,"B":0}]}]}}
```

**Blue strobe (Effect 4), LEDs 0–80:**

```json
{"LED_STRIP_1":{"Active":true,"Segments":[{"StartLED":0,"StopLED":80,"Speed":190,"Effect":4,"Colors":[{"R":0,"G":0,"B":150}]}]}}
```

See the *smartLED Integration Trainer Guide* (Example Topics and Payloads section) for the full list of effects, button payloads, and the SMC47 setup sequence.

---

## Download: example program

Download the ready-made AutomationDirect Productivity Suite AOI used in this guide:

<a href="/downloads/LEDSTRIPMQTTAOI.adpro" target="_blank">LEDSTRIPMQTTAOI.adpro</a>
