Controlling PSoC™ LED using MQTT

Lab Overview

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)

  • ไปที่ config >> mqtt_client_config.h

    • ตั้งค่าที่อยู่ broker และ port แบบ Non-secure

    • ตั้งค่าการเชื่อมต่อ MQTT เป็นแบบ Non-secure

    • ตั้งชื่อ mqtt Topic สำหรับใช้ในการสื่อสารระหว่าง publisher และ subscriber

    • ตั้งค่า QoS เป็น mode 0 ในการสื่อผ่าน MQTT แบบ Non-secure

   // 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 )
  1. Build และ upload โปรแกรมลงบอร์ด

  2. เขียนโปรแกรม Node-red ใช้ MQTT เป็น publisher ควบคุม LED บอร์ด PSoC6 <<<<<<< HEAD

    • import code โปรแกรม node-red และตั้งค่า node-red ตามขั้นตอน 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)

  3. run โปรแกรม Node-red และเปิดเบราว์เซอร์ url: http://127.0.0.1:1880/ui

  4. ทดลองกด ON/OFF บนเบราว์เซอร์ node-red สังเกตุ LED บอร์ด ON-OFF

Setting Node-red

  1. เริ่มใช้งาน node-red → ติดตั้ง Node-red บน Windows

  2. import code Button control LED PSoC6

    1. คลิ๊กที่ ไฟล์ source >>Button control LED PSoC6.json

    2. copy code ไปที่ แถบขวามือ>>คลิ๊กที่แถบ ขีดสามขีด>> เลือก import

fig: import function

c. วาง code ลงหน้า cilpboard >> เลือก new flow >> คลิ๊ก import

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

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

  • ไปที่ Manage palette >>Install>> search modules >> พิมพ์ “Dashboard >> เลือก node-red-dashboard >> install

  • ดูตัวอย่างการใช้งาน dashboard เบื้องต้นได้จาก LAB: Basic Node-red Dashboard

  1. ตรวจสอบการตั้งค่า ใช้ node-red เป็น Publish โดยดับเบิ้ลคลิ๊กที่ mqtt out node หรือ PSoC6Status node ที่โปรแกรม node-red และ แก้ไขการตั้งค่าข้อมูลช่องให้ตรงตามข้อมูลด้านล่าง ดังนี้

    1. Server >> แก้ไขเป็น “IP address ของ Broker” port: “1883”

    2. Topic >> ตั้งค่าเป็น PSoC6Status

    3. QoS >> ตั้งค่าเป็น “0”

  1. ตรวจสอบการตั้งค่า ใช้ node-red dashboard ปุ่ม button node ทั้ง 2 node สำหรับควบคุม On/Off LED

    • การตั้งค่าตรงดังภาพด้านล่างหรือไม่

  1. คลิ๊ก Deploy เพื่อให้ Node-red ทำงานตามโปรแกรมที่ได้ตั้งค่าไว้

  2. เปิดหน้าเบราว์เซอร์ขึ้นมา ใส่ “your ip address:1883/ui” จะปรากฎหน้า dashboard ของ node-red

ตัวอย่าง

        http://127.0.0.1:1880/ui


Date: 10 May 2023

Author: Soontree Jaikhong (AIC-Researcher)

Last updated

Assoc. Prof. Wiroon Sriborrirux, Founder of Advance Innovation Center (AIC) and Bangsaen Design House (BDH), Electrical Engineering Department, Faculty of Engineering, Burapha University