F1-MQTT-LVGL-Intelligent-House

This is a intelligent house system based on stm32f103zet6, integrate mqtt,lvgl into system,can use touchscreen to get data from some sensors and see data in server platform

MIT License

Stars
4

F1-Hal-MQTT-LVGL

1. Description

This is a simple intelligent sensor system,which integrate light weight Mqtt server lvgl and so on technologies. Its functions include detecting temperaturehumidity light intensity around the environment(can add more module by yourself),then show the data in touchscreen and you can get the datas by specified platform(here is mqttx server).

2. Platform

keil vscode stm32cubemx Squareline studio

3. Hardware

  • STM32F103ZET6
  • Espressif
  • ili9341(2.8 inch)
  • DTH11
  • Lightsensor
  • LED
  • Beep
  • MQ2
  • Motor(No add yet)

4. Build process

1. Create basic document

2. FSMC Configuration

3. LCD and LVGL

1. Refresh function

add a function in Gui.h as the lvgl refresh the screen

2. GUI buffer choose way

Find it in F1-Hal-MQTT-LVGL\LVGL\examples\porting\lv_port_disp.c

3. Touch function

more message see in F1-Hal-MQTT-LVGL\LVGL\examples\porting\lv_port_indev.c

4. MQTT Configuration

please follow Weidongshan video to set up

1. Running mosquitto

after download mosquitto , running it in windows command screen

cd "c:\Program Files\mosquitto"

.\mosquitto.exe -c mosquitto.conf -v
2. Running MQTTX

5.Lv event call back

more message in F1-Hal-MQTT-LVGL\LVGL\APP\UI_demo\ui_events.c

1. Synchronize Arc and Label
1. Create a label

Arc should be the labels father element

2. Add the event Callback
3. Encode your callback

Get the data from a global value,then put it into a static buffer by using sprintf,the follow the picture to encode.

In a similar way,control Led is same way,judge two state(checked or unchecked) to run led off or on.

5. Some Question need to attention

1. LCD initiation

  • @attention: refer to FSMC , so pay attention to pins which come to correct port,especially in T_Penlcd_bl .

2. lvgl transplant

1. Memory use

  • @attention : Knowing what size in your mcu resources ,for the stm32f103zet6,its ram is 64k and rom 512k,rom looks enough,but focus on ram.

2. keil configuration

  • @reason : because of lvgl has c++ element,dont open the MicroLiB
  • @add : if you want to use printf to debug visually ,it is important to close semi-hosting pattern.

    some codes help you close and add it in c document which everywhere.

    dont forget redirect the port

    (refer to F1-Hal-MQTT-LVGL\SYSTEM\Uart_Redefine.c)

#if 0
#pragma import(__use_no_semihosting) //  C 
void _sys_exit(int x)                //  _sys_exit() 
{
  x = x;
}
struct __FILE // 
{
  int handle;
};
/* FILE is typedef  d in stdio.h. */
FILE __stdout;

#endif

int fputc(int ch, FILE *f)
{
  HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
  return ch;
}

int fgetc(FILE *f)
{
  uint8_t ch = 0;
  HAL_UART_Receive(&huart1, &ch, 1, HAL_MAX_DELAY);
  return ch;
}

3. Set up the heartbeat

When you use the rtos created by hal bank,it is necessary to add heartbeat function in a important place

here is two way:

configUSE_TICK_HOOK need to set 1

but I find the second way easy to aggravate cpu burden

3. FreeRTOS Configuration

1. some functions to detect stack or heap

BaseType_t val;

/*  */		   
printf("FreeRTOS: %d \r\n",configTOTAL_HEAP_SIZE);//FreeRTOS
printf(": %d \r\n", xPortGetFreeHeapSize());//
printf(":%d \r\n\r\n",xPortGetMinimumEverFreeHeapSize());//

/*  */
val = uxTaskGetStackHighWaterMark(SPITask_Handler);//SPI
printf("SPI:%d \r\n",(int)val*4);                             
printf("MQTT stack is %d\r\n", (int *)uxTaskGetStackHighWaterMark(G_xQueuePlatform));

2. Time Delay ⭐

1. Error use
  • @attention : Dont use software delay time in RTOS like as follow:
/**
 * @brief  
 * @param  xus 0~233015
 * @retval 
 */
void Delay_us(uint32_t xus)
{
    SysTick->LOAD = 72 * xus;   // 
    SysTick->VAL = 0x00;        // 
    SysTick->CTRL = 0x00000005; // HCLK
    while (!(SysTick->CTRL & 0x00010000))
        ;                       // 
    SysTick->CTRL = 0x00000004; // 
}

/**
 * @brief  
 * @param  xms 0~4294967295
 * @retval 
 */
void Delay_ms(uint32_t xms)
{
    while (xms--)
    {
        Delay_us(1000);
    }
}

/**
 * @brief  
 * @param  xs 0~4294967295
 * @retval 
 */
void Delay_s(uint32_t xs)
{
    while (xs--)
    {
        Delay_ms(1000);
    }
}

It will cause the messy time for rtos get

such as

program stuck in

if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) 1 ) { taskYIELD(); }

it will never return the errQUEUE_EMPTY for stopping ,then enter prvildeTask

2. Solution

Using the basedtime and never to interrupt its running

void Delay_us(uint32_t xus)
{
    extern TIM_HandleTypeDef TIME_BASE_DELAY;
    TIM_HandleTypeDef *hHalTim = &TIME_BASE_DELAY;

    uint32_t ticks;
    uint32_t told, tnow, tcnt = 0;
    uint32_t reload = __HAL_TIM_GET_AUTORELOAD(hHalTim);

    ticks = xus * reload / (1000); /* reload1ms */
    told = __HAL_TIM_GET_COUNTER(hHalTim);
    while (1)
    {
        tnow = __HAL_TIM_GET_COUNTER(hHalTim);
        if (tnow != told)
        {
            if (tnow > told)
            {
                tcnt += tnow - told;
            }
            else
            {
                tcnt += reload - told + tnow;
            }
            told = tnow;
            if (tcnt >= ticks)
            {
                break;
            }
        }
    }
}
void Delay_ms(uint32_t xms)
{
    for (int i = 0; i < xms; i++)
        Delay_us(1000);
}

void Delay_s(uint32_t xs)
{
    while (xs--)
    {
        Delay_ms(1000);
    }
}

6. Rebuild Project

When you rebuild the project by stm32cubemx,some place need to change,avoiding the errors occur.

1. Add define

set INCLUDE_xTaskGetCurrentTaskHandle 1 in F1-Hal-MQTT-LVGL\Core\Inc\FreeRTOSConfig.h

2. Change define

avoid redefine about Systick_Handler

#define USE_CUSTOM_SYSTICK_HANDLER_IMPLEMENTATION 0

3. USART3_IRQHandler

USART3_IRQHandler need to be commented when use yourself interrupt

4. Add Headfile

put it in F1-Hal-MQTT-LVGL\Core\Inc\FreeRTOSConfig.h

#ifndef CMSIS device_ header
#define CMSIS device header "stm32f1xx.h"
#endif