Click the blue text to follow us
Hello everyone, I am Hello Alpha. Recently, I introduced a small smart home project I completed, which gained a lot of attention from friends. As a blogger who loves to share technology, of course, I have to open source it!

Project Overview
The project originated from the ElfBoard co-creation recruitment event held by Baoding Feiling Embedded Technology Co., Ltd. I was fortunate to become a co-creator, and the official provided an ELF 1 development board. For a detailed introduction to the ELF 1 development board, you can read “ELF 1 Unboxing Experience.” Watch the ELF 1 unboxing video:
As a co-creator, I used the ELF 1 to complete a small smart home project. For a detailed introduction to the small project, you can read “Smart Home System Based on Linux.” Watch the project introduction and demonstration video:
Open Source Repository
The repository has open-sourced the project source code for IMX6U, which is based on the LVGL Linux porting project lvgl_demo. The structure of the project source code is as follows:
lvgl_demo
├── app # Application source code
| ├── cJSON # cJSON parser
| ├── protocol # Custom communication protocol
| ├── socket # Socket source code
| ├── application.c # Main application
| ├── button.c # Button application
| ├── def.h # Some definitions
| └── weather.c # Weather application
├── lv_drivers # LVGL driver source code
├── lvgl # LVGL source code
├── ui # UI design source code
├── lv_conf.h # LVGL library configuration file
├── lv_drv_conf.h # LVGL driver configuration file
├── main.c # main.c file
├── Makefile # Makefile
└── README.md # README file
For information on LVGL’s Linux porting, you can refer to “LVGL Porting on Embedded Linux.” For UI design, you can refer to “Quick UI Design Using SquareLine Studio Based on LVGL.”
Data Interaction
The data interaction between IMX6U and ELF 1 is implemented through a socket interface based on the TCP protocol. IMX6U creates a server and accepts the ELF 1 client connection as shown below:
// Create server
server_fd = server_init_socket();
// Accept client connection
client_fd = server_accept_client(server_fd);
The relevant source code is implemented in the socket folder. You can refer to “Introduction to Socket Programming in Linux,” “Implementing TCP Server Using Socket in Linux,” and “Implementing TCP Client Using Socket in Linux.”
Communication Protocol
The communication protocol uses a simple and practical byte-based custom compatibility communication protocol. The open-source repository address is: https://github.com/alphazcc/protocol
For example, to control an LED with a button, the data packaging and sending function is implemented as follows:
static void led_ryg_turn(int cmd, int code) {
msg_frame_t led_msg_frame;
led_msg_frame.type = LED_RYG;
led_msg_frame.cmd = cmd;
led_msg_frame.code = code;
led_msg_frame.datalen = 0;
msg_buf_t *_msg_buf = pkg_frame(&led_msg_frame);
server_send_data(client_fd, (char *)_msg_buf->buf_ptr, _msg_buf->buf_size);
}
For receiving indoor air quality data (ADC data), the data reception and parsing functions are implemented as follows:
static int ctrl_dev_adc(msg_frame_t *_msg_pack) {
// Get ADC raw data
uint16_t adc_raw = (uint16_t)(_msg_pack->data[0] << 8) + _msg_pack->data[1];
// Update display, etc...
return 0;
}
static int ctrl_cmd_func(const msg_pkg_t *_msg_pkg) {
switch(_msg_pkg->pkg->type) {
case ADC: ctrl_dev_adc(_msg_pkg->pkg); break;
// Other devices, etc...
}
return 0;
}
static void *recv_thread(void *arg) {
while(1) {
if(server_receive_data(client_fd, (char *)recv_msg_buf->buf_ptr,
(ssize_t *)&recv_msg_buf->buf_size) == 0) {
msg_pkg_t *_msg_pkg =
unpkg_frame(recv_msg_buf->buf_ptr, recv_msg_buf->buf_size);
ctrl_cmd_func(_msg_pkg);
}
}
return NULL;
}
/* Data reception thread creation */
pthread_create(&recv_thread_id, NULL, recv_thread, NULL);
Weather Retrieval
The weather data is provided by Xinzhi Weather, which offers standardized data access through a standard Restful API interface, making it easy for apps, smart hardware, or enterprise systems to access refined weather data.
Register and log in to Xinzhi Weather, apply for the free version to meet usage requirements, and obtain the API KEY. Xinzhi Weather official website: https://www.seniverse.com
By creating a local client and sending a request packet to the Xinzhi Weather server, you can obtain weather data. The implementation process for obtaining, parsing, and updating the UI with weather data is as follows:
int weather_init(void)
{
/* Create client */
client_fd = client_init_socket();
/* Combine request packet */
sprintf(buffer, REQUEST_PKG, DAILY, API_KEY, WUHAN);
/* Send request to Xinzhi Weather server */
client_send_data(client_fd, buffer, strlen(buffer));
/* Clear cache to prepare for receiving weather data */
memset(buffer, 0, sizeof(buffer));
/* Receive weather data in JSON format */
client_receive_data(client_fd, buffer, &json_size);
/* Parse weather data into weather_daily */
cJSON_Parse_Weather_Daily(buffer, &weather_daily);
/* Print weather information */
weather_show_data(&weather_daily);
/* Update weather display */
weather_update_ui(&weather_daily);
/* Close client */
client_close_socket(client_fd);
return 0;
}
For parsing JSON format weather data, a lightweight JSON parser written in C, cJSON, is used. The open-source repository address is: https://github.com/DaveGamble/cJSON
Conclusion
The source code in this article has been formatted and simplified for readability, aiming to illustrate the implementation logic and methods. It is recommended to download the complete source code from the repository for analysis and reading.
The data collection on the ELF 1 development board is implemented using file operations. The implementation of client creation, data packaging, sending, receiving, parsing, and other functions is similar to those related to IMX6U, so it is not open-sourced. Friends can design it themselves.
This project is not difficult; the content is more inclined towards Linux application development. Friends are welcome to replicate it! Open source repository link: https://github.com/alphazcc/mini-project
More Content
- CSDN Blog: @Hello Alpha
- Bilibili: @Hello Alpha
- Zhihu: @Hello Alpha
The End
Scan to add me as a friend,
Join a high-quality group~
All electronic enthusiasts come here
Share, collect, view, and like!