In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
What is the design and implementation of "digital elf" based on RT-Thread? in order to solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
I. Overview
With the continuous growth of intelligent devices in recent years, tablet computers, intelligent digital audio and video players, mobile digital cameras and other digital products are becoming more and more popular among urban and rural residents. In particular, "Cloud Life" makes people have more demand for digital products, coupled with the emergence of Internet technology, 5G technology, intelligent new products, new phased consumption mode, etc., so that the upsurge of digital product consumption.
This design is a "digital elf" design based on RT-Thread, and the hardware is developed by Sparrow No.1 development board with BK7252 as the main control chip. BK7252, is a high-performance WiFi module, using highly integrated radio frequency chip, internal integration of advanced 2.4GHz Wi-Fi 1T1R technology, support for camera image output, with the best power performance, RF performance, stability, versatility and reliability, suitable for a variety of applications and different product requirements. The module has 512KB embedded RAM and 4Mbyte Flash space, and the CPU main frequency is as high as 180Mhz.
On this basis, this design mainly realizes the following functions, including the update and display of weather and epidemic data, MP3 music player and digital camera. The MP3 player has the functions of volume adjustment, playback / stop control and song switching; the digital camera displays the pictures on the LCD screen, and simultaneously has the functions of SD card storage and OneNet cloud platform storage. The product can act as a home digital assistant, so it is named "digital elf". At the same time, in the process of completing this work, I refer to some network materials and netizens' ideas. I would like to express my thanks here.
II. Overview of RT-Thread usage
This design is based on the Sparrow No.1 development board SDK, and its RT-Thread is version 3.1.0.
Figure 1 RT-Thread version
A list of devices registered on the RT-Thread system, which mainly uses devices such as rtc,sd0,w0,sound,uart1. The RT-Thread components used include FinSH console, virtual file system, and POSIX interface. Network tools and NetUtils,WebClient,cJSON,EasyFlash,rt_ota,TJpgDec,Player are involved in the software package.
Figure 2 device registration list
III. Hardware framework
The Sparrow 1 development board is rich in peripherals, but it is rich in resources, including WiFi, BLE, camera, audio speaker, MIC recording, TF card holder, five-way button, and a 1.44-inch LCD screen. It uses the common TypeC interface as the power supply and debugging serial port, and reserves to support the lithium battery power supply interface.
The human-computer interaction part of this design mainly uses the five-way buttons and LCD display screen of the development board, in which the keys are used for function selection, data refresh and music playback control functions.
In normal mode: "←": music playback, "⭕": weather and epidemic data refresh, "→": camera photo
In music playback mode: "↑": volume increase, "↓": volume decrease, "←": next song, "⭕": stop playback
The equipment is connected by WIFI, and the power-on is automatically connected to the network. The data is displayed by LCD, and the device is connected with SD to store the camera. In addition, photos can also be uploaded to OneNet cloud platform through WIFI and viewed by remote web pages or smart terminals such as mobile phones.
IV. Software framework description
The system software flow chart is shown in the following figure. After the device is powered on, it starts the RT-Thread operating system, and initializes the relevant hardware devices at the same time, such as LCD display screen, audio speaker, and hanging the SD card to the file system, and then carries on the network connection of the device. After the network connection, enter the application startup command to start the program, LCD displays the welcome interface, and then the user can choose through the five-way button to enter the function, mainly the weather epidemic data update display, MP3 player function and digital camera function. After taking pictures, the camera will store local SD card data as well as OneNet cloud storage.
Fifth, software module description
1. Equipment networking
The device networking mainly uses the wlan driver in the RT-Thread component. After the power-on initialization is completed, query the wlan wireless device in the main program, and connect the WIFI network according to the SSID and PASSWORD configured by the user. The specific code implementation of this section is as follows:
1static int iot_station_connect (char * ssid, char * passwd) {
2rt_err_t result = RT_EOK
3struct rt_wlan_info info
4struct rt_wlan_device * wlan
5rt_tick_t tick = 0
6wlan = (struct rt_wlan_device *) rt_device_find (WIFI_DEVICE_STA
7_NAME)
8if (! wlan)
9 {
10rt_kprintf ("no wlan:%s device\ n", WIFI_DEVICE_STA_NAME)
11return-1;}
12result = rt_wlan_init (wlan, WIFI_STATION)
13rt_wlan_register_event_handler (wlan, WIFI_EVT_STA_CONNECTED, I
14ot_wlan_sta_connected_event)
15rt_wlan_register_event_handler (wlan, WIFI_EVT_STA_DISCONNECTED
16, iot_wlan_sta_disconnected_event)
17rt_wlan_info_init (& info, WIFI_STATION, SECURITY_WPA2_AES_PSK
18ssid)
19result = rt_wlan_connect (wlan, & info, passwd)
20rt_wlan_info_deinit & info)
21return result
22}
2. NTP network time synchronization
The main idea of network time synchronization is to call the ntp synchronization function in the network toolkit and to achieve the purpose of timing synchronization of network time by creating independent time synchronization threads. When using this section, you need to enable the RTC feature in RT-Thread. The related codes are as follows: 1 {
2time_t cur_time = ntp_sync_to_rtc ()
3if (cur_time)
4 {
5rt_kprintf ("Get local time from NTP server:% s", ctime ((const t)
6imeteort *) & cur_time))
7rt_kprintf ("The system time is updated. Timezone is% d.\ n", NTP
8_TIMEZONE)
9}}
10static rt_thread_t tid1 = RT_NULL
11static void ntcthread1_entry (void * parameter) {
12while (1)
13 {
14time_t cur_time = ntp_sync_to_rtc ()
15if (cur_time)
16 {
17rt_kprintf ("Get local time from NTP server:% s", ctime ((const tim)
18e_t *) & cur_time))
19rt_kprintf ("The system time is updated. Timezone is% d.\ n", NTP_T
20IMEZONE)
21 break;}
22else
23 {
24rt_thread_mdelay (1000)
25}}}
26void NTCThreadInit (void) {
27if (tid1! = RT_NULL)
28 {
29rt_kprintf ("ntc thread still run\ n")
30 years;}
31rt_kprintf ("NTC thread init\ n")
32tid1 = rt_thread_create ("NTC"
33ntcthread1_entry, RT_NULL
34THREAD_STACK_SIZE
35THREAD_PRIORITY, THREAD_TIMESLICE)
36if (tid1! = RT_NULL)
37rt_thread_startup (tid1)
38}
3. Update of weather epidemic data
This part mainly uses the function of the webclient toolkit to obtain the relevant Json data by calling the API interface of weather and epidemic data, and uses the CJson toolkit to analyze the returned Json data. Finally, the data is displayed by LCD. The related code is as follows:
1#define GET_URI "http://www.weather.com.cn/data/sk/%s.html" / /
2 get the API of the weather
3#define GET_FY2020_URI "http://www.dzyong.top:3005/yiqing/total" / / epidemic situation
4 data API
5void get_weather (int argc, char * * argv) {
6rt_uint8_t * buffer = RT_NULL
7int resp_status
8struct webclient_session * session = RT_NULL
9char * weather_url = RT_NULL
10int content_length =-1, bytes_read = 0
11int content_pos = 0
12char * city_name = rt_calloc (1255)
13max * allocate space for weather_url * /
14weather_url = rt_calloc (1, GET_URL_LEN_MAX)
15if (weather_url = = RT_NULL)
16 {
17rt_kprintf ("No memory for weather_url!\ n")
18goto _ _ exit
19}
20if (argc = = 1) {
21strcpy (city_name, AREA_ID)
22}
23else if (argc = = 2) {
24strcpy (city_name, argv [1])
25}
26Universe * splice GET URL * /
27rt_snprintf (weather_url, GET_URL_LEN_MAX, GET_URI, city_name)
Create a session and set the size of the response * /
29session = webclient_session_create (GET_HEADER_BUFSZ)
30if (session = = RT_NULL)
31 {
32rt_kprintf ("No memory for get header!\ n")
33goto _ _ exit
34}
35max * send GET request using default header * /
36if ((resp_status = webclient_get (session, weather_url)! = 200) {
37rt_kprintf ("webclient GET request failed, response (% d) error.\ n", resp_
38status)
39goto _ _ exit
40}
41ax * allocates buffers for storing received data * /
42buffer = rt_calloc (1, GET_RESP_BUFSZ)
43if (buffer = = RT_NULL)
44 {
45rt_kprintf ("No memory for data receive buffer!\ n")
46goto _ _ exit
47}
48content_length = webclient_content_length_get (session)
49if (content_length
< 0) { 50/* 返回的数据是分块传输的. */ 51do 52{ 53bytes_read = webclient_read(session, buffer, GET_RESP_BUFSZ); 54if (bytes_read GET_RESP 62_BUFSZ ? 63GET_RESP_BUFSZ : content_length - conte 64nt_pos); 65if (bytes_read valuestring); 97lcd_clear(BLACK); 98item = cJSON_GetObjectItem(object, "temp"); 99rt_kprintf("\ntemp :%s ", item->Valuestring)
100rt_sprintf (temp, "temperature:% s", item- > valuestring)
101lcd_disp_str_en_ch (0mem20 tempmaBLACKWITE)
102item = cJSON_GetObjectItem (object, "WD")
103rt_kprintf ("\ nwd:% s", item- > valuestring)
104item = cJSON_GetObjectItem (object, "WS")
105rt_kprintf ("\ nws:% s", item- > valuestring)
106item = cJSON_GetObjectItem (object, "SD")
107rt_kprintf ("\ nsd:% s", item- > valuestring)
108rt_sprintf (temp, "humidity:% s", item- > valuestring)
109lcd_disp_str_en_ch (0Magne40 tempmaBLACKWITE)
110item = cJSON_GetObjectItem (object, "time")
111rt_kprintf ("\ ntime:% s\ n", item- > valuestring)
112item = cJSON_GetObjectItem (object, "AP")
113rt_kprintf ("\ nap:% s", item- > valuestring)
114rt_sprintf (temp, "Air pressure:% s", item- > valuestring)
115lcd_disp_str_en_ch (0Penh 60th tempmage BLACKWITE)
116item = cJSON_GetObjectItem (object, "WSE")
117rt_kprintf ("\ nwse:% s", item- > valuestring)
118rt_sprintf (temp, "Wind:% s", item- > valuestring)
119lcd_disp_str_en_ch (0Magne80 tempMagneBLACKWITE)
120if (root! = RT_NULL)
121cJSON_Delete (root)
122}
123Universe * epidemic data analysis * /
124void fy2020_data_parse (rt_uint8_t * data) {
125uint8_t temp [100]
126cJSON * root = RT_NULL, * object = RT_NULL, * item = RT_NULL
127root = cJSON_Parse ((const char *) data)
128if (! root)
129 {
130rt_kprintf ("No memory for cJSON root!\ n")
131 years;}
1324. Music playback module
133 the function of this part is mainly realized by using the interface functions of the player software package, including music broadcasting
134 play, stop, song switching and volume control and other functions. Part of the code for this section is shown in the following figure:
135cJSON * dataArray = cJSON_GetObjectItem (root, "data"); / / fetch array
136int arraySize = cJSON_GetArraySize (dataArray); / / fetch array size
137cJSON * dataList = dataArray- > child
138while (dataList! = RT_NULL)
139 {
140rt_kprintf ("\ ndiagnosed:% d\ n", cJSON_GetObjectItem (dataList, "diagn)
141osed ")-> valueint)
142rt_kprintf ("\ ndeath:% d\ n", cJSON_GetObjectItem (dataList, "death")->
143valueint)
144rt_kprintf ("\ ncured:% d\ n", cJSON_GetObjectItem (dataList, "cured")->
145valueint)
146rt_kprintf ("\ ndate:% s\ n", cJSON_GetObjectItem (dataList, "date")-> va
147luestring)
148//LCD screen print information
149rt_sprintf (temp, "cumulative confirmation"
% d ", cJSON_GetObjectItem (dataList," diagnosed ")-> valueint)
151lcd_disp_str_en_ch (0min120 tempmaBLACKWITE)
152rt_sprintf (temp, "cumulative death"
Death:% d ", cJSON_GetObjectItem (dataList," death ")-> valueint)
154lcd_disp_str_en_ch (0meme140tempjenceBLACKMagneWite)
155rt_sprintf (temp, "cumulative treatment"
More than:% d ", cJSON_GetObjectItem (dataList," cured ")-> valueint)
157lcd_disp_str_en_ch (0mem160tempjenceBLACKWITE)
158rt_sprintf (temp, "when updated
159s ", cJSON_GetObjectItem (dataList," date ")-> valuestring)
160lcd_disp_str_en_ch (00.180 tempmaBLACKWITE)
161dataList = dataList- > next
162}
4. Music playback module
The function of this part is mainly realized by using the interface function of player software package, including music playing, stopping, song switching, volume control and so on. Part of the code for this section is shown in the following figure: 1LCDShowMusic ()
2rt_sprintf (music, "/ sd/music/%d.mp3", count)
3rt_kprintf ("key left is press...\ r\ n")
4rt_kprintf ("/ / player_play\ n")
5player_stop ()
6player_set_uri (music)
7player_play ()
8player_status = 1
9 countbacks +
10if (count > = 8)
11count = 0
12rt_kprintf ("/ / player_play end\ n")
5. Camera function realization
This part mainly acquires the image data collected by the camera through the IPC event, and displays the image data in LCD after decoding the image data by the TJpgDec software package. At the same time, the camera data is stored in the SD card by writing a file, and pushed to the OneNet cloud platform through the HTTP protocol.
1void take_photo (void) / / Manual Photo
2 {
3Universe / create an event in which the camera receives a picture
4session.event = rt_event_create ("vt_event", RT_IPC_FLAG_FIFO)
5camera_start (); / / enable the camera to transmit photos
6tvideo_capture (1)
7rt_event_recv (session.event, SEND_FRAME_EVENT, RT_EVENT_FLAG_OR | RT_EVE
8NT_FLAG_CLEAR, RT_WAITING_FOREVER, RT_NULL)
9int fd, res
10time_t cur_time
11struct tm * cur_tm
12char time_now [50]; / / Save the file to the sd card specified path
13max * output current time * /
14cur_time = time (RT_NULL)
15cur_tm = localtime (& cur_time)
16rt_sprintf (time_now, "d-d-d-d-d-d", cur_tm- > tm_year + 19
1700, cur_tm- > tm_mon + 1, cur_tm- > tm_mday, cur_tm- > tm_hour, cur_tm- > tm_min, cur
18cm-> tm_sec)
19rt_sprintf (file_name, "/ sd/images/%s.jpg", time_now)
20rt_kprintf ("name =% s\ n", file_name)
21fd = open (file_name, O_WRONLY | O_CREAT)
22if (fd > = 0) {
23write (fd, session.buf, session.total_len)
24close (fd)
25rt_kprintf ("save% s okstores!\ n", file_name)
26res = Decode_Jpg (file_name)
27rt_kprintf ("res =% d\ n", res)
28}
29else
30 {
31rt_kprintf ("save pic failed requests!\ n")
32}
33pm / Photo data upload
34webclient_post_pic (session.buf, session.total_len)
35tvideo_capture (0)
36}
37MSH_CMD_EXPORT (take_photo,take_photo); this is the answer to the question about the design and implementation of "digital elf" based on RT-Thread. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.