-
-
Save sriengchhunchheang/9193dfe2735fb15ae25e7815fb97a2c3 to your computer and use it in GitHub Desktop.
publiser task file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************************************************************** | |
* File Name: publisher_task.c | |
* | |
* Description: This file contains the task that sets up the user button GPIO | |
* for the publisher and publishes MQTT messages on the topic | |
* 'MQTT_PUB_TOPIC' to control a device that is actuated by the | |
* subscriber task. The file also contains the ISR that notifies | |
* the publisher task about the new device state to be published. | |
* | |
* Related Document: See README.md | |
* | |
* | |
******************************************************************************* | |
* Copyright 2020-2023, Cypress Semiconductor Corporation (an Infineon company) or | |
* an affiliate of Cypress Semiconductor Corporation. All rights reserved. | |
* | |
* This software, including source code, documentation and related | |
* materials ("Software") is owned by Cypress Semiconductor Corporation | |
* or one of its affiliates ("Cypress") and is protected by and subject to | |
* worldwide patent protection (United States and foreign), | |
* United States copyright laws and international treaty provisions. | |
* Therefore, you may use this Software only as provided in the license | |
* agreement accompanying the software package from which you | |
* obtained this Software ("EULA"). | |
* If no EULA applies, Cypress hereby grants you a personal, non-exclusive, | |
* non-transferable license to copy, modify, and compile the Software | |
* source code solely for use in connection with Cypress's | |
* integrated circuit products. Any reproduction, modification, translation, | |
* compilation, or representation of this Software except as specified | |
* above is prohibited without the express written permission of Cypress. | |
* | |
* Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO WARRANTY OF ANY KIND, | |
* EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED | |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress | |
* reserves the right to make changes to the Software without notice. Cypress | |
* does not assume any liability arising out of the application or use of the | |
* Software or any product or circuit described in the Software. Cypress does | |
* not authorize its products for use in any products where a malfunction or | |
* failure of the Cypress product may reasonably be expected to result in | |
* significant property damage, injury or death ("High Risk Product"). By | |
* including Cypress's product in a High Risk Product, the manufacturer | |
* of such system or application assumes all risk of such use and in doing | |
* so agrees to indemnify Cypress against all liability. | |
*******************************************************************************/ | |
#include "cyhal.h" | |
#include "cybsp.h" | |
#include "FreeRTOS.h" | |
/* Task header files */ | |
#include "publisher_task.h" | |
#include "mqtt_task.h" | |
#include "subscriber_task.h" | |
/* Configuration file for MQTT client */ | |
#include "mqtt_client_config.h" | |
/* Middleware libraries */ | |
#include "cy_mqtt_api.h" | |
#include "cy_retarget_io.h" | |
#include "xensiv_dps3xx_mtb.h" | |
// i2c pin | |
#define DPS_I2C_SDA (P6_1) // Define me | |
#define DPS_I2C_SCL (P6_0) // Define me | |
/****************************************************************************** | |
* Macros | |
******************************************************************************/ | |
/* Interrupt priority for User Button Input. */ | |
#define USER_BTN_INTR_PRIORITY (3) | |
/* The maximum number of times each PUBLISH in this example will be retried. */ | |
#define PUBLISH_RETRY_LIMIT (10) | |
/* A PUBLISH message is retried if no response is received within this | |
* time (in milliseconds). | |
*/ | |
#define PUBLISH_RETRY_MS (1000) | |
/* Queue length of a message queue that is used to communicate with the | |
* publisher task. | |
*/ | |
#define PUBLISHER_TASK_QUEUE_LENGTH (3u) | |
/****************************************************************************** | |
* Function Prototypes | |
*******************************************************************************/ | |
static void publisher_init(void); | |
static void publisher_deinit(void); | |
static void isr_button_press(void *callback_arg, cyhal_gpio_event_t event); | |
void print_heap_usage(char *msg); | |
static void xensiv_dps3xx_init(void); | |
/****************************************************************************** | |
* Global Variables | |
*******************************************************************************/ | |
/* FreeRTOS task handle for this task. */ | |
TaskHandle_t publisher_task_handle; | |
/* Handle of the queue holding the commands for the publisher task */ | |
QueueHandle_t publisher_task_q; | |
/* Structure to store publish message information. */ | |
cy_mqtt_publish_info_t publish_info = | |
{ | |
.qos = (cy_mqtt_qos_t) MQTT_MESSAGES_QOS, | |
.topic = MQTT_PUB_TOPIC, | |
.topic_len = (sizeof(MQTT_PUB_TOPIC) - 1), | |
.retain = false, | |
.dup = false | |
}; | |
/* Structure of pressure_sensor. */ | |
xensiv_dps3xx_t pressure_sensor; | |
cyhal_i2c_t i2c; | |
cyhal_i2c_cfg_t i2c_cfg = { | |
.is_slave = false, | |
.address = 0, | |
.frequencyhal_hz = 400000 | |
}; | |
/* Structure that stores the callback data for the GPIO interrupt event. */ | |
cyhal_gpio_callback_data_t cb_data = | |
{ | |
.callback = isr_button_press, | |
.callback_arg = NULL | |
}; | |
/****************************************************************************** | |
* Function Name: publisher_task | |
****************************************************************************** | |
* Summary: | |
* Task that sets up the user button GPIO for the publisher and publishes | |
* MQTT messages to the broker. The user button init and deinit operations, | |
* and the MQTT publish operation is performed based on commands sent by other | |
* tasks and callbacks over a message queue. | |
* | |
* Parameters: | |
* void *pvParameters : Task parameter defined during task creation (unused) | |
* | |
* Return: | |
* void | |
* | |
******************************************************************************/ | |
void publisher_task(void *pvParameters) | |
{ | |
/* Status variable */ | |
cy_rslt_t result; | |
publisher_data_t publisher_q_data; | |
/* Command to the MQTT client task */ | |
mqtt_task_cmd_t mqtt_task_cmd; | |
/* To avoid compiler warnings */ | |
(void) pvParameters; | |
/* Initialize and set-up the user button GPIO. */ | |
// publisher_init(); | |
/* Create a message queue to communicate with other tasks and callbacks. */ | |
publisher_task_q = xQueueCreate(PUBLISHER_TASK_QUEUE_LENGTH, sizeof(publisher_data_t)); | |
{ | |
if (pdTRUE == xQueueReceive(publisher_task_q, &publisher_q_data, portMAX_DELAY)) | |
{ | |
/******************************************************************************************* | |
* Example code: read sensor to MQTT Pub | |
****************************************************************************************** | |
PSoc6 : | |
1. ทำหน้าที่อ่านค่า temperature,pressure sensor | |
2. ตั้งค่า เป็น Publisher ส่งข้อมูล sensor ไปยัง MQTT topic name: PSoC6Status/temperature,pressure | |
Credit Author : Soontree Jaikhong (AIC-Reseacher) -2023 | |
*******************************************************************************************/ | |
/* get function for Initialize pressure sensor */ | |
xensiv_dps3xx_init(); | |
for (;;) | |
{ | |
/* Get the pressure and temperature data and print the results to the UART */ | |
float pressure, temperature; | |
xensiv_dps3xx_read(&pressure_sensor, &pressure, &temperature); | |
// printf("Pressure : %8f\r\n", pressure); | |
char temp[20]; | |
char ps[20]; | |
char data_sensor[100]; | |
/* make the number into string using sprintf function */ | |
sprintf(temp, "%4.2f", temperature); | |
sprintf(ps, "%2.2f", pressure); | |
strcpy(data_sensor, temp); // Copy temp into data_sensor | |
strcat(data_sensor, ","); // Add a space to data_sensor | |
strcat(data_sensor, ps); // Concatenate ps to data_sensor | |
printf("*********TEST function sending sensor value: %s \r\n",data_sensor); | |
cyhal_system_delay_ms(1000); | |
publish_info.payload = data_sensor; | |
publish_info.payload_len = strlen(publish_info.payload); | |
printf(" Publisher: Publishing '%s' on the topic '%s'\n\n", | |
(char *) publish_info.payload, publish_info.topic); | |
result = cy_mqtt_publish(mqtt_connection, &publish_info); | |
if (result != CY_RSLT_SUCCESS) | |
{ | |
printf(" Publisher: MQTT Publish failed with error 0x%0X.\n\n", (int)result); | |
/* Communicate the publish failure with the the MQTT | |
* client task. | |
*/ | |
mqtt_task_cmd = HANDLE_MQTT_PUBLISH_FAILURE; | |
xQueueSend(mqtt_task_q, &mqtt_task_cmd, portMAX_DELAY); | |
} | |
print_heap_usage("publisher_task: After publishing an MQTT message"); | |
} | |
} | |
} | |
} | |
/****************************************************************************** | |
* Function Name: publisher_init | |
****************************************************************************** | |
* Summary: | |
* Function that initializes and sets-up the user button GPIO pin along with | |
* its interrupt. | |
* | |
* Parameters: | |
* void | |
* | |
* Return: | |
* void | |
* | |
******************************************************************************/ | |
static void publisher_init(void) | |
{ | |
/* Initialize the user button GPIO and register interrupt on falling edge. */ | |
cyhal_gpio_init(CYBSP_USER_BTN, CYHAL_GPIO_DIR_INPUT, | |
CYHAL_GPIO_DRIVE_PULLUP, CYBSP_BTN_OFF); | |
cyhal_gpio_register_callback(CYBSP_USER_BTN, &cb_data); | |
cyhal_gpio_enable_event(CYBSP_USER_BTN, CYHAL_GPIO_IRQ_FALL, | |
USER_BTN_INTR_PRIORITY, true); | |
printf("Press the user button (SW2) to publish \"%s\"/\"%s\" on the topic '%s'...\n\n", | |
MQTT_DEVICE_ON_MESSAGE, MQTT_DEVICE_OFF_MESSAGE, publish_info.topic); | |
} | |
/****************************************************************************** | |
* Function Name: publisher_deinit | |
****************************************************************************** | |
* Summary: | |
* Cleanup function for the publisher task that disables the user button | |
* interrupt and deinits the user button GPIO pin. | |
* | |
* Parameters: | |
* void | |
* | |
* Return: | |
* void | |
* | |
******************************************************************************/ | |
static void publisher_deinit(void) | |
{ | |
/* Deregister the ISR and disable the interrupt on the user button. */ | |
cyhal_gpio_register_callback(CYBSP_USER_BTN, &cb_data); | |
cyhal_gpio_enable_event(CYBSP_USER_BTN, CYHAL_GPIO_IRQ_FALL, | |
USER_BTN_INTR_PRIORITY, false); | |
cyhal_gpio_free(CYBSP_USER_BTN); | |
} | |
/****************************************************************************** | |
* Function Name: isr_button_press | |
****************************************************************************** | |
* Summary: | |
* GPIO interrupt service routine. This function detects button | |
* presses and sends the publish command along with the data to be published | |
* to the publisher task over a message queue. Based on the current device | |
* state, the publish data is set so that the device state gets toggled. | |
* | |
* Parameters: | |
* void *callback_arg : pointer to variable passed to the ISR (unused) | |
* cyhal_gpio_event_t event : GPIO event type (unused) | |
* | |
* Return: | |
* void | |
* | |
******************************************************************************/ | |
static void isr_button_press(void *callback_arg, cyhal_gpio_event_t event) | |
{ | |
BaseType_t xHigherPriorityTaskWoken = pdFALSE; | |
publisher_data_t publisher_q_data; | |
/* To avoid compiler warnings */ | |
(void) callback_arg; | |
(void) event; | |
/* Assign the publish command to be sent to the publisher task. */ | |
publisher_q_data.cmd = PUBLISH_MQTT_MSG; | |
/* Assign the publish message payload so that the device state toggles. */ | |
if (current_device_state == DEVICE_ON_STATE) | |
{ | |
publisher_q_data.data = (char *)MQTT_DEVICE_OFF_MESSAGE; | |
} | |
else | |
{ | |
publisher_q_data.data = (char *)MQTT_DEVICE_ON_MESSAGE; | |
} | |
/* Send the command and data to publisher task over the queue */ | |
xQueueSendFromISR(publisher_task_q, &publisher_q_data, &xHigherPriorityTaskWoken); | |
portYIELD_FROM_ISR(xHigherPriorityTaskWoken); | |
} | |
static void xensiv_dps3xx_init(void){ | |
/* Initialize i2c for pressure sensor */ | |
cyhal_i2c_init(&i2c, DPS_I2C_SDA, DPS_I2C_SCL, NULL); | |
cyhal_i2c_configure(&i2c, &i2c_cfg); | |
/* Initialize pressure sensor */ | |
xensiv_dps3xx_mtb_init_i2c(&pressure_sensor,&i2c,XENSIV_DPS3XX_I2C_ADDR_DEFAULT); | |
} | |
/* [] END OF FILE */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment