# การเชื่อมต่อด้วย BDH's MQTT

ใน Lab นี้ ผู้เข้าร่วมจะได้เรียนรู้เกี่ยวกับการสื่อสาร MQTT ผ่าน Mosquitto broker โดยเริ่มจากความเข้าใจพื้นฐานเกี่ยวกับการเข้ารหัสแบบ public key cryptography ผู้เข้าร่วมจะได้เรียนรู้วิธีการสร้าง key pairs และ certificate signing requests หลังจากนั้นจะศึกษาทำความเข้าใจโค้ดที่ใช้ในการสร้างการเชื่อมต่อ MQTT เป้าหมายสุดท้ายคือการทำให้ผู้เข้าร่วมมีความเข้าใจอย่างครอบคลุมเกี่ยวกับการสื่อสาร MQTT อย่างปลอดภัย และความสำคัญของโครงสร้างพื้นฐานกุญแจสาธารณะในการรักษาความสมบูรณ์และความปลอดภัยของข้อมูลในแอปพลิเคชัน IoT

## 1. Register Device ID (Topic ID) on BDH IoT

### 1.1 การเข้าใช้งาน KYD’s Platform:

* การเข้าใช้งาน KYD’s Platform:
* การใช้งานหน้าข้อมูลผู้แลระบบสามารถเข้าไปที่:  <https://app.kin-yoo-dee.com/auth/login>
* ชื่อผู้ใช้งาน: admin\@biil
* รหัสผ่าน: biil2023

<figure><img src="/files/MWRj2xbPpqHKdQGvQJDc" alt=""><figcaption><p>Login to BDH Platform</p></figcaption></figure>

<figure><img src="/files/CGdosmzt6W0NAWzW6toW" alt=""><figcaption></figcaption></figure>

### 1.2 **Register PSoC-6**

ทำการลงทะเบียนอุปกรณ์

* ประเภทอุปกรณ์: “CY8CKIT-62S2-40312”
* โมเดล: “CY8CKIT-62S2-40312”

<figure><img src="/files/4tu1b5Pjhr8dv95BoePY" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/tYoMumyme1wQNgyG3Yjz" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/AB5J4qwGfrZRiWAbnzh5" alt=""><figcaption></figcaption></figure>

### 1.3 **Register Sensor** shield

* ประเภทอุปกรณ์:  “CY8CKIT-028-SENSE”
* โมเดล: “CY8CKIT-028-SENSE”

<figure><img src="/files/2OnTlMxUgPUaOkA1fHMq" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/0NOdnDq5YBdLs0RDG4O9" alt=""><figcaption></figcaption></figure>

#### ข้อแนะนำในการลงทะเบียน&#x20;

1. ต้องใส่ข้อมูลให้ครบในช่องที่มี \* สีแดง
2. เลือกประเภทอุปกรณ์ให้ถูกต้อง&#x20;
3. ใส่โมเดลให้ตรงตาม Guidline&#x20;
4. &#x20;รหัส (ID/MAC) ควรตั้งชื่อให้เป็น pattren ที่สอดคล้องกับอุปกรณ์ที่อยู่ใน list อุปกรณ์ หรือถ้ามี uniqe ID อย่างเช่น MacAddress ก็สามารถใช้แทนได้เลย&#x20;
5. หมายเลข (S/N) โดยปกติจะเป็นตัวเลข Serial Number ของอุปกรณ์นั้นๆ ซึ่งถ้าไม่มีก็สามารถใส่เหมือนกับ รหัส (ID/MAC) ได้
6. ชื่ออุปกรณ์จะตั้งเป็นอะไรก็ได้ แต่แนะนำให้สื่อความหมายว่าเป็นอุปกรณ์ของใคร หรือโมเดลอะไร หรือใช้งานที่ไหนเช่น C40312\_NIPON\_LIVINGROOM&#x20;
7. ถ้ามีการส่งข้อมูลเข้า KYD’s Platform ให้ใส่ รหัสผู้ใช้งานและรหัสผ่าน ในส่วนของ MQTT ด้วย&#x20;

## 2. Config MQTT login on PSoC6’s config.h

### 2.1 **สร้างแอปพลิเคชัน Create Application**

<figure><img src="/files/C2a2e51YMJPspCe3WxIN" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/Xc2tNLE6biSC2Z4Zr24z" alt=""><figcaption></figcaption></figure>

### 2.2 ตั้งค่า wifi\_config.h

ใช้ฮอตสปอตมือถือของคุณเป็นจุดเชื่อมต่อ แก้ไขไฟล์ "wifi\_config.h" ให้ตรงกับการตั้งค่า SSID ของมือถือคุณ:

* [#define](https://www.dropbox.com/?q=%23define) WIFI\_SSID “mobile\_ssid”
* [#define](https://www.dropbox.com/?q=%23define) WIFI\_PASSWORD “password”

<figure><img src="/files/aM08Adl02wEHPuV3LLtF" alt=""><figcaption></figcaption></figure>

### 2.3 ตั้งค่า MQTT for PSoC-6-Board

<figure><img src="/files/w8mq1AJ7dBdESPisuW6E" alt=""><figcaption><p>ID: TEST0001, Password: 12341234</p></figcaption></figure>

<figure><img src="/files/VutrCnlFFtvx5zKCPku6" alt=""><figcaption></figcaption></figure>

### 2.4  ตั้งค่า MQTT for PSoC-6-Shell

<figure><img src="/files/9VKUzPmbrx0BMMhqoniY" alt=""><figcaption><p>Shell ID: TEST001, Password: 12341234</p></figcaption></figure>

<figure><img src="/files/YckT2cKG8TQtDbG4oDcX" alt=""><figcaption></figcaption></figure>

### **2.5 การตั้งค่า Chip ID ของ BMI160**

**ขั้นตอนที่ 1: ไปยังโฟลเดอร์ mtb\_shared**

* ไปยังโฟลเดอร์ mtb\_shared&#x20;

**ขั้นตอนที่ 2: เข้าสู่โฟลเดอร์ BMI160\_driver**

* ภายในโฟลเดอร์ mtb\_shared ให้หาตำแหน่งโฟลเดอร์ BMI160\_driver
* ดับเบิลคลิกเพื่อเข้าสู่โฟลเดอร์ BMI160\_driver

**ขั้นตอนที่ 3: แก้ไขไฟล์ bmi160\_defs.sh**

* ค้นหาไฟล์ที่ชื่อ bmi160\_defs.sh ภายในโฟลเดอร์ BMI160\_driver
* เปิดไฟล์ bmi160\_defs.sh เพื่อแก้ไข

**ขั้นตอนที่ 4: ค้นหาและเปลี่ยนค่า BMI160\_CHIP\_ID**

* ภายในไฟล์ bmi160\_defs.sh ค้นหาบรรทัดที่มีการประกาศ #define BMI160\_CHIP\_ID UINT8\_C(0xD1)
* เปลี่ยนค่า UINT8\_C(0xD1) ให้เป็น UINT8\_C(0xD8)

<figure><img src="/files/GLNOjckHDzWole9FzXF6" alt=""><figcaption></figcaption></figure>

**บันทึกไฟล์ และ รันโปรแกรม**

<figure><img src="/files/PfKXuanyjiKiuqsDki8J" alt=""><figcaption></figcaption></figure>

### 2.6 **ผลลัพธ์**

**การตั้งค่า PuTTY:**

**ขั้นตอนที่ 1: เปิด Device Manager เพื่อตรวจสอบพอร์ต**

1. ค้นหา  ***Device Manager***
2. ในหน้าต่าง ***Device Manager*** ให้ขยายหมวด ***Ports (COM & LPT)***
3. สังเกตพอร์ตที่เชื่อมต่ออยู่ ***(ตัวอย่างเช่น : COM13)***

<figure><img src="/files/oVZZLRNOxknBf0SekpKA" alt=""><figcaption><p>ค้นหา  <em><strong>Device Manager</strong></em></p></figcaption></figure>

<figure><img src="/files/3pqDAOnuHZdWRkRBpXs8" alt=""><figcaption><p><em><strong>ตัวอย่างเช่น : COM13</strong></em></p></figcaption></figure>

**ขั้นตอนที่ 2: เปิดโปรแกรม PuTTY**

* เปิดโปรแกรม  บนคอมพิวเตอร์ของคุณ

**ขั้นตอนที่ 3: ตั้งค่า PuTTY สำหรับการเชื่อมต่อ Serial**

1. ในหน้าต่าง PuTTY ที่เปิดขึ้นมา:
   * เลือกตัวเลือก **Serial** ในหมวด **Connection type**&#x20;
2. ตั้งค่า:
   * Serial line →  **COM13**  (หรือพอร์ตที่คุณตรวจสอบได้จาก Device Manager)
   * Speed → **115200**

3\. คลิกที่  **Open** เพื่อเริ่มการเชื่อมต่อ

<figure><img src="/files/dtdY13vYwnYu8h60G8lP" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/xT9opiuwWeclPODWTRl5" alt=""><figcaption></figcaption></figure>

## 3. Basic Data Log Monitoring on BDH IoT

After PSoC-6 connected with WiFi and MQTT, you can check the BDH Platform on Register device. You can see the device ID that you at to the code will be change color from red (not connect) to green (connected) than you can display the graph and this case the dataset will send to BDH cloud

<figure><img src="/files/5fitgn6Pk9xNIbjHTwJt" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/AnwmIALvGTuCFQb6NGOa" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/gZGFojZIoNjYx4BzRmFd" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/kZGZc8nigVQHMZeBePyZ" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/9Zlxwe7MCbFfWXbrZVAa" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/AJAmFWphANTd2GfVwoHe" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.aic-eec.com/iot-development-with-infineon-psoc-tm-6-and-bdh-platform/bdh-iot-connectivity/bdhs-mqtt.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
