# Sending PSoC6’s sensor to MQTT (node-red)

## L*ab Objective:*

&#x20;   Building upon the foundational knowledge acquired from the previous lab on MQTT communication via the Mosquitto broker and public key cryptography, participants will now explore the integration of the PSoC 6 microcontroller's sensor data with Node-RED. Through this lab, participants will learn how to set up and configure Node-RED flows to receive, process, and visualize sensor data from the PSoC 6, transmitted via MQTT. The end goal is to enhance participants' proficiency in creating IoT workflows, harnessing the power of MQTT for real-time data communication, and visualizing sensor outputs in an interactive environment using Node-RED.

## 🔥 Requirements

<table><thead><tr><th width="429">Resources</th><th>Links</th></tr></thead><tbody><tr><td>Computer</td><td>💻</td></tr><tr><td>ModusToolbox™ software v3.0 or later</td><td><a href="https://www.infineon.com/modustoolbox">Link</a></td></tr><tr><td>CY8CKIT-062S2-43012 Infineon Board</td><td><a href="https://github.com/Advance-Innovation-Centre-AIC/BIIL_MTB-100_Hello_World_and_LED_Blinking_Programming_Template/assets/88732241/0215501d-b774-4045-8e64-ef49e28d8404">Link</a></td></tr><tr><td>Technical Report</td><td><a href="https://www.dropbox.com/scl/fi/amaxc94pte0ut2i1r5ewx/Technical-Report-Lab00.paper?rlkey=b3xm3vrerz9xgv1glb30cvy9z&#x26;dl=0">dropbox</a></td></tr></tbody></table>

## 🚩 Let start

### Use the previous lab

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FK8v6R1KyN2UwbdFHImby%2Fimage.png?alt=media&#x26;token=42a139df-12fa-485a-8f56-3ca215ef1ea0" alt=""><figcaption><p>Create Application</p></figcaption></figure>

### Config mqtt\_client\_config.h:

* Open config >> mqtt\_client\_config.h.\\

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FToJQwNXsi8zPNTLkpeAb%2Fimage.png?alt=media&#x26;token=86403514-ea6c-44ec-bbd7-e90c5cbcde8d" alt="" width="279"><figcaption></figcaption></figure>

{% code title="mqtt\_client\_config.h" %}

```cpp
#define MQTT_PUB_TOPIC                    "PSoC6Status/temperature,pressure"
#define MQTT_SUB_TOPIC                    "PSoC6Status/led"
```

{% endcode %}

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FMoWMshz2ZkFvrKt11OHq%2Fimage.png?alt=media&#x26;token=3525f99f-e52a-4d4a-bc20-52005274d22f" alt=""><figcaption></figcaption></figure>

* Open publisher\_task.c in source.
* Copy code from [p](https://gist.github.com/sriengchhunchheang/9193dfe2735fb15ae25e7815fb97a2c3)[ubliser\_task.c](https://gist.github.com/sriengchhunchheang/9193dfe2735fb15ae25e7815fb97a2c3) and past in publisher\_task.c  &#x20;

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FgkN0TP3ofoGQop76lVan%2Fimage.png?alt=media&#x26;token=788c19b7-5fb9-49bc-a91b-63041f9a6430" alt="" width="274"><figcaption></figcaption></figure>

### Add sensor-xensiv-dps3xx library

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FVRy7lZLyBZ3sDqCcm4Lq%2Fimage.png?alt=media&#x26;token=15f8a50f-5231-4a4e-9087-e7f105829a72" alt=""><figcaption><p>add sensor-xensiv-dps3xx library</p></figcaption></figure>

#### Save the file, Build and Launch the Application

### Download and install Nodejs

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FEif1UdCMtwWWwgEXoh2c%2Fimage.png?alt=media&#x26;token=972ecb6b-6018-4ee1-a746-d1b1e3583597" alt="" width="375"><figcaption><p><a href="https://nodejs.org/en/">Nodejs link</a></p></figcaption></figure>

### Set-up node-red

1. Check node-red version

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FFUBoHFr1GfkjDf1BtWtE%2Fimage.png?alt=media&#x26;token=5f8ba31f-c826-45a5-9afa-8b3b62d10629" alt="" width="398"><figcaption><p>Check node-red version</p></figcaption></figure>

2. Run Node-Red by using node-red start

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FKLgjLE3sQCMgIHLr60YM%2Fimage.png?alt=media&#x26;token=4efa9ccc-6b31-4527-b076-eced42e27b19" alt="" width="458"><figcaption></figcaption></figure>

3\. Open the Node-Red server: go to  <http://127.0.0.1:1880/>

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FY8PreKRWnvQvufh3314r%2Fimage.png?alt=media&#x26;token=8329c287-b8a3-4024-b39a-243609de5a9f" alt="" width="563"><figcaption><p>Node-Red</p></figcaption></figure>

4. Import Button control LED PSoC6 nodes:

   1. Go to Import
   2. Copy and Past this [json](https://gist.github.com/sriengchhunchheang/5ee9cb42096e31c1c7ba4e8559ec13ea)[ ](https://gist.github.com/sriengchhunchheang/5ee9cb42096e31c1c7ba4e8559ec13ea)code to create the nodes

   <figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FOhYs7cTWt1FxLd8VK981%2Fimage.png?alt=media&#x26;token=a437e3fd-4278-478f-b6d7-c45b039e692c" alt="" width="429"><figcaption><p>Go to 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%2Fr6DaUfbQ8nVcOko1byv4%2Fimage.png?alt=media&#x26;token=9ee98de0-623d-42bd-a093-c2f97547d69a" alt="" width="563"><figcaption><p>Copy and Past this <a href="https://gist.github.com/sriengchhunchheang/5ee9cb42096e31c1c7ba4e8559ec13ea">json</a><a href="https://gist.github.com/sriengchhunchheang/5ee9cb42096e31c1c7ba4e8559ec13ea"> </a>code to create the nodes</p></figcaption></figure>

5. You will get this flow of nodes for Button control LED on PSoC6

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FpckMJ4u8P1eg4ex7IA6N%2Fimage.png?alt=media&#x26;token=7eae66b1-2e19-4be4-8c51-312b3d697a94" alt=""><figcaption></figcaption></figure>

7. Open the MQTT Subscriber node and set it up as:![](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2Ftp6xlf7Q2Ek9VxtG4Wkp%2Fimage.png?alt=media\&token=31078219-06d5-4787-b8d0-8d5dc9b57427)
   1. Go to Server and click edit
   2. Upload your client.crt&#x20;
   3. Upload your client.key&#x20;
   4. Upload your mosquito.org.crt

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FfM2Y2nBR9D2NhYlW8e6X%2Fimage.png?alt=media&#x26;token=c0a9e277-7739-47d3-8702-f95970cb3b2b" alt="" width="224"><figcaption><p>Go to Server and click edit</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%2FNPpJB80WWqxpWTuynKSP%2Fimage.png?alt=media&#x26;token=0f511220-a352-43e2-9cf8-0e5eca194601" alt="" width="352"><figcaption><p>Upload files</p></figcaption></figure>

8. Open the MQTT Subscriber node and set it up as: ![](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FLIvTX8SQ8HlvquxQ8d51%2Fimage.png?alt=media\&token=03e26e5a-7014-4c96-92d7-34a4eaacac72)

   1. Topic >> set up as PSoC6Status/temperature,pressure&#x20;
   2. QoS >> set up as “1”

   <figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2F3gh3bpVqFhTFW5xXs4X4%2Fimage.png?alt=media&#x26;token=3387e0b7-5e9f-4d32-8a1a-b43d1f15cba7" alt="" width="563"><figcaption></figcaption></figure>

### Result&#x20;

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FBZPNnT7LCO7RN8cGZVQS%2Fimage.png?alt=media&#x26;token=10d73dd2-b2f2-4e4c-b17a-eef7ce1b3f2e" alt=""><figcaption></figcaption></figure>

> #### 🎉 <mark style="color:blue;">Congratulations! You can now complete this lab</mark>

## Supported toolchains (make variable 'TOOLCHAIN')

* GNU Arm® embedded compiler v10.3.1 (`GCC_ARM`) - Default value of `TOOLCHAIN`
* Arm® compiler v6.16 (`ARM`)
* IAR C/C++ compiler v9.30.1 (`IAR`)

## Supported kits (make variable 'TARGET')

* [PSoC™ 62S2 Wi-Fi Bluetooth® pioneer kit](https://www.infineon.com/CY8CKIT-062S2-43012) (`CY8CKIT-062S2-43012`)
* [PSoC™ 62S1 Wi-Fi Bluetooth® pioneer kit](https://www.infineon.com/CYW9P62S1-43438EVB-01) (`CYW9P62S1-43438EVB-01`)
* [PSoC™ 62S1 Wi-Fi Bluetooth® pioneer kit](https://www.infineon.com/CYW9P62S1-43012EVB-01) (`CYW9P62S1-43012EVB-01`)
* [PSoC™ 62S3 Wi-Fi Bluetooth® prototyping kit](https://www.infineon.com/CY8CPROTO-062S3-4343W) (`CY8CPROTO-062S3-4343W`)

## Related resources

| Resources                       | Links                                                                                                                                                      |
| ------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| ModusToolbox™ Software Training | [link](https://www.dropbox.com/sh/waj898o4o8eccx0/AAB3hBBaIQo2OvJ5-fubGJIha/training-modustoolbox-level1-getting-started-master/Manual/Ch2-Tools.pdf?dl=0) |

## Other resources

Infineon provides a wealth of data at [www.infineon.com](http://www.infineon.com) to help you select the right device, and quickly and effectively integrate it into your design.

## Document history

<table><thead><tr><th width="149">Version</th><th>Description of change</th></tr></thead><tbody><tr><td>1.0.0</td><td>Node-Red Read Sensor PSoC6 vai MQTT</td></tr></tbody></table>

## Authors:

* *Assoc. Prof. Wiroon Sriborrirux*
* *Mr. Sriengchhun Chheang*
* *Mr. Sabol Socare*<br>

<br>

***

© BDH Corporation, 2022-2023
