> For the complete documentation index, see [llms.txt](https://docs.aic-eec.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aic-eec.com/iot-development-with-infineon-psoc-tm-6-and-bdh-platform/iot-connectivity/controlling-psoc-tm-led-using-mqtt.md).

# Controlling PSoC™ LED using MQTT

## Lab Overview&#x20;

**Node-red** → Msg\[TURN ON/TURN OFF] →Publish → MQTT(QoS0)

**PSoC6** →MQTT(QoS0)→ Subscribe → control \[LED ON/OFF]

## **Let start**

1. Connect the board to your PC using the provided USB cable through the KitProg3 USB connector.
2. การตั้งค่าให้ใช้งาน Broker ของ mosquitto >> link <https://test.mosquitto.org/> แบบ ไม่ต้องเข้ารหัส (unencrypted) และ ไม่ต้องตรวจสอบตัวตน(unauthenticated)

![fig: how to setup server port of Mosquitto broker](https://paper-attachments.dropboxusercontent.com/s_DE46169648EC1184505F0FEE30B79C93229F6F5C4674567BCFF38BB678D2F8D7_1681979509506_Untitled.png)

* ไปที่ config >> mqtt\_client\_config.h
  * ตั้งค่าที่อยู่ broker และ port แบบ Non-secure
  * ตั้งค่าการเชื่อมต่อ MQTT เป็นแบบ Non-secure
  * ตั้งชื่อ mqtt Topic สำหรับใช้ในการสื่อสารระหว่าง publisher และ subscriber
  * ตั้งค่า QoS เป็น mode 0 ในการสื่อผ่าน MQTT แบบ Non-secure

```c
   // i. MQTT CLIENT CONNECTION CONFIGURATION MACROS
   #define MQTT_BROKER_ADDRESS               "test.mosquitto.org"
   #define MQTT_PORT                         1883
   
   // ii. MQTT NON-SECURE
   #define MQTT_SECURE_CONNECTION            ( 0 )
   
   /* iii. The MQTT topics to be used by the publisher and subscriber. */
   #define MQTT_PUB_TOPIC                    "PSoC6Status"
   #define MQTT_SUB_TOPIC                    "PSoC6Status"
   
   /* iv. Set the QoS that is associated with the MQTT publish, and subscribe message. */
   #define MQTT_MESSAGES_QOS                 ( 0 )

```

3. Build และ upload โปรแกรมลงบอร์ด
4. เขียนโปรแกรม Node-red ใช้ MQTT เป็น publisher ควบคุม LED บอร์ด PSoC6 <<<<<<< HEAD
   * import code โปรแกรม node-red และตั้งค่า node-red ตามขั้นตอน [setting Node-red](https://github.com/Advance-Innovation-Centre-AIC/Lab_NodeRed_MQTT_control_LED_Psoc6#setting-node-red) (<https://www.dropbox.com/scl/fi/u3asndwvf30y1gog2t6lp/BDH\\_mtb\\_example-node-red\\_control\\_leb\\_psoc6\\_via\\_mqtt.paper?dl=0\\&rlkey=vqr4wopa8oxgyw5ug59u624sp#:uid=879208286062881172009041\\&h2=Setting-Node-red>)
5. run โปรแกรม Node-red และเปิดเบราว์เซอร์ url: <http://127.0.0.1:1880/ui>
6. ทดลองกด ON/OFF บนเบราว์เซอร์ node-red สังเกตุ LED บอร์ด ON-OFF

<figure><img src="https://paper-attachments.dropboxusercontent.com/s_DE46169648EC1184505F0FEE30B79C93229F6F5C4674567BCFF38BB678D2F8D7_1681982082489_image.png" alt=""><figcaption><p>Fig: output ON/OFF control by Node-red</p></figcaption></figure>

### **Setting Node-red**

1. เริ่มใช้งาน node-red → [ติดตั้ง Node-red บน Windows](https://github.com/Advance-Innovation-Centre-AIC/EE_Curriculum/blob/main/term2_65_EMB62_IoT/LAB01/Get_started_Node-red.md#%E0%B8%95%E0%B8%B4%E0%B8%94%E0%B8%95%E0%B8%B1%E0%B9%89%E0%B8%87-node-red-%E0%B8%9A%E0%B8%99-windows)
2. import code Button control LED PSoC6
   1. คลิ๊กที่ ไฟล์ source >>[Button control LED PSoC6.json](https://github.com/Advance-Innovation-Centre-AIC/Lab_NodeRed_MQTT_control_LED_Psoc6/blob/2ced2d2bc014f3dbe35f54c52da94548c25be7fc/flow/ButtonControl_LED_PSoC6.json)
   2. copy code ไปที่ แถบขวามือ>>คลิ๊กที่แถบ ขีดสามขีด>> เลือก import
   3. วาง code ลงหน้า cilpboard >> เลือก new flow >> คลิ๊ก import

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2F7gWQlDRBrKl9GSWvgpWK%2Fimage.png?alt=media&amp;token=5bed61bf-bf09-4c25-bf5b-52ad34440ce3" alt=""><figcaption><p>import function: คลิ๊กที่แถบ ขีดสามขีด>> เลือก import</p></figcaption></figure>

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FSuYB8OfdIWweZeHAW2Bh%2Fimage.png?alt=media&amp;token=177a78f9-98d3-4edc-8a70-6a55c3678166" alt=""><figcaption><p>วาง code ลงหน้า cilpboard >> เลือก new flow >> คลิ๊ก import</p></figcaption></figure>

&#x20;                   d.   สังเกตที่แถบ flow จะมี flow ชื่อว่า “Button control LED PSoC6” ปรากฎ

&#x20;                         *หมายเหตุ:* สำหรับหรับ node-red ที่ติดตั้งใหม่ ให้ติดตั้ง module dashboard เพิ่ม

* &#x20;ไปที่ Manage palette >>Install>> search modules >> พิมพ์ “Dashboard >> เลือก node-red-dashboard >> install
* ดูตัวอย่างการใช้งาน dashboard เบื้องต้นได้จาก [LAB: Basic Node-red Dashboard](https://github.com/Advance-Innovation-Centre-AIC/EE_Curriculum/tree/main/term2_65_EMB64_Applied_ES/LAB11#lab11-basic-node-red-dashboard)

![](https://paper-attachments.dropboxusercontent.com/s_DE46169648EC1184505F0FEE30B79C93229F6F5C4674567BCFF38BB678D2F8D7_1683012990126_image.png)

3. ตรวจสอบการตั้งค่า ใช้ node-red เป็น Publish โดยดับเบิ้ลคลิ๊กที่ mqtt out node หรือ PSoC6Status node ที่โปรแกรม node-red และ แก้ไขการตั้งค่าข้อมูลช่องให้ตรงตามข้อมูลด้านล่าง ดังนี้
   1. Server >> แก้ไขเป็น “IP address ของ Broker” port: “1883”
   2. Topic >> ตั้งค่าเป็น PSoC6Status
   3. QoS >> ตั้งค่าเป็น “0”

![MQTT out node](https://paper-attachments.dropboxusercontent.com/s_DE46169648EC1184505F0FEE30B79C93229F6F5C4674567BCFF38BB678D2F8D7_1683013295944_image.png)

4. ตรวจสอบการตั้งค่า ใช้ node-red dashboard ปุ่ม button node ทั้ง 2 node สำหรับควบคุม On/Off LED
   * การตั้งค่าตรงดังภาพด้านล่างหรือไม่

![Button node for OFF LED](https://paper-attachments.dropboxusercontent.com/s_DE46169648EC1184505F0FEE30B79C93229F6F5C4674567BCFF38BB678D2F8D7_1683013661342_image.png)

![Button node for ON LED](https://paper-attachments.dropboxusercontent.com/s_DE46169648EC1184505F0FEE30B79C93229F6F5C4674567BCFF38BB678D2F8D7_1683013674573_image.png)

![Button node set to switch off](https://paper-attachments.dropboxusercontent.com/s_DE46169648EC1184505F0FEE30B79C93229F6F5C4674567BCFF38BB678D2F8D7_1681980738044_Untitled.png)

![Button node set to switch on](https://paper-attachments.dropboxusercontent.com/s_DE46169648EC1184505F0FEE30B79C93229F6F5C4674567BCFF38BB678D2F8D7_1681980818819_Untitled.png)

5. คลิ๊ก Deploy เพื่อให้ Node-red ทำงานตามโปรแกรมที่ได้ตั้งค่าไว้
6. เปิดหน้าเบราว์เซอร์ขึ้นมา ใส่ “your ip address:1883/ui” จะปรากฎหน้า dashboard ของ node-red

ตัวอย่าง

```
        http://127.0.0.1:1880/ui
```

![fig: node-red dashboard](https://paper-attachments.dropboxusercontent.com/s_DE46169648EC1184505F0FEE30B79C93229F6F5C4674567BCFF38BB678D2F8D7_1681981115605_image.png)

***

{% hint style="info" %}
**References:**

* <https://github.com/Infineon/mtb-example-wifi-mqtt-radar-presence>
* <https://github.com/Infineon/mtb-example-wifi-dual-core-virtual-mqtt-client>
* [https://www.infineon.com/dgdl/Infineon-ModusToolbox\_3.0\_Eclipse\_IDE\_User\_Guide-GettingStarted-v01\_00-EN.pdf](https://www.infineon.com/dgdl/Infineon-ModusToolbox_3.0_Eclipse_IDE_User_Guide-GettingStarted-v01_00-EN.pdf?fileId=8ac78c8c8386267f0183a8d7043b58ee\&utm_source=cypress\&utm_medium=referral\&utm_campaign=202110_globe_en_all_integration-files\&redirId=188241)
  {% endhint %}

***

*Date: 10 May 2023*

*Author: Soontree Jaikhong (AIC-Researcher)*
