# Problem Statement
Modern Industrial IoT systems require embedded edge nodes capable of sensing environmental parameters, publishing data using lightweight IoT protocols, and communicating with nearby systems through serial interfaces.
This project implements a Smart Sensor Data Publishing Node using the STM32L475 IoT Node. The system reads onboard sensor data and publishes it to an MQTT broker while simultaneously transmitting another sensor value through UART communication for local monitoring.
The system demonstrates practical implementation of embedded IoT concepts including sensor interfacing, MQTT communication, UART transmission, and real-time monitoring.
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Smart Sensor Data Publishing Node
******************************************************************************
* This project:
* 1. Reads HTS221 temperature sensor data
* 2. Publishes temperature data using MQTT
* 3. Reads LPS22HB pressure sensor data
* 4. Sends pressure data over UART
******************************************************************************
*/
/* USER CODE END Header */
#include "main.h"
#include "stdio.h"
#include "string.h"
/* BSP Sensor Drivers */
#include "hts221.h"
#include "lps22hb.h"
/* UART Handle */
UART_HandleTypeDef huart2;
/* I2C Handle */
I2C_HandleTypeDef hi2c1;
/* Variables */
float temperature = 0.0;
float humidity = 0.0;
float pressure = 0.0;
char uartBuffer[100];
char mqttPayload[100];
/* Function Prototypes */
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_I2C1_Init(void);
/* USER CODE BEGIN 0 */
/* Dummy MQTT Publish Function */
void MQTT_Publish(char *topic, char *payload)
{
/*
Replace this with actual MQTT middleware publish API.
Example:
mqtt_publish(topic, payload);
For demo:
sending publish message to UART.
*/
char mqttMsg[200];
sprintf(mqttMsg,
"MQTT Topic: %s\r\nPayload: %s\r\n\r\n",
topic,
payload);
HAL_UART_Transmit(&huart2,
(uint8_t*)mqttMsg,
strlen(mqttMsg),
HAL_MAX_DELAY);
}
/* USER CODE END 0 */
int main(void)
{
/* MCU Configuration */
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
/* Sensor Initialization */
BSP_HTS221_Init();
BSP_LPS22HB_Init();
sprintf(uartBuffer,
"Smart Sensor Node Started\r\n\r\n");
HAL_UART_Transmit(&huart2,
(uint8_t*)uartBuffer,
strlen(uartBuffer),
HAL_MAX_DELAY);
/* USER CODE END 2 */
/* Infinite loop */
while (1)
{
/* USER CODE BEGIN WHILE */
/* Read HTS221 Sensor */
BSP_HTS221_GetTemperature(&temperature);
BSP_HTS221_GetHumidity(&humidity);
/* MQTT Payload */
sprintf(mqttPayload,
"{ \"temperature\": %.2f, \"humidity\": %.2f }",
temperature,
humidity);
/* Publish MQTT Data */
MQTT_Publish("stm32l475/sensor/data",
mqttPayload);
/* Read Pressure Sensor */
BSP_LPS22HB_GetPressure(&pressure);
/* UART Output */
sprintf(uartBuffer,
"Pressure: %.2f hPa\r\n",
pressure);
HAL_UART_Transmit(&huart2,
(uint8_t*)uartBuffer,
strlen(uartBuffer),
HAL_MAX_DELAY);
HAL_Delay(2000);
/* USER CODE END WHILE */
}
}
/**
* @brief I2C1 Initialization Function
*/
static void MX_I2C1_Init(void)
{
hi2c1.Instance = I2C1;
hi2c1.Init.Timing = 0x10909CEC;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief USART2 Initialization Function
*/
static void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief GPIO Initialization Function
*/
static void MX_GPIO_Init(void)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
}
/**
* @brief System Clock Configuration
*/
void SystemClock_Config(void)
{
/* Auto-generated by CubeMX */
}
/**
* @brief Error Handler
*/
void Error_Handler(void)
{
while(1)
{
}
}






Comments