Smart cup holder
Author: sipower
1. Brief introduction of the work (100-200 words)
(Design name, work photo, function introduction, etc.)
This work designs a smart cup holder that can detect the use status of the cup and remind users to drink water in time. By connecting to the Alibaba Cloud platform, the clock can be automatically calibrated. You can also set parameters for the cup holder in the cloud terminal. The main work of this work is to design a beautiful interface that is pleasing to the eye. The other is to connect to Alibaba Cloud to achieve cloud sharing and intelligent connection.
The picture below is a photo of the final physical work.
Figure 1. Photo of the work
2. System Block Diagram (Combined with Pictures and Text)
(System Software and Hardware Implementation Block Diagram)
The hardware uses the STM32L496G-DISCO evaluation board as the main control, and uses a reflective photoelectric sensor RPR-220 to detect whether the cup is on the cup holder. The ESP8266 module is used to connect to Alibaba Cloud, obtain network time, synchronize the cup status, set parameters, and other operations. The hardware block diagram is as follows.
Figure 2. Hardware block diagram
The software uses FreeRTOS system as the basic software platform and uses TouchGFX to design the interface. The system state machine is shown in the figure below.
Figure 3. State machine
In standby mode, an analog clock is displayed. If the user does not pick up the cup within a certain period of time, the next reminder state will be entered. If the user drinks water before the timeout, the encouragement state will be entered.
In the reminder state, the text "It's time to drink water!" is displayed. If the user picks up the water cup, the encouragement state is entered.
In the encouragement state, the text "Great!" is displayed. If the user puts down the cup, the system enters the standby state. If the user does not put down the cup within a certain period of time, the system enters the water receiving state.
In the water receiving state, the text "Have you gone to get water?" is displayed. If the user puts down the water cup, it enters the standby state.
3. Functional description of each part (combined with pictures and text)
(The functional description and explanation of each part are presented in a combination of pictures and text.)
1. UI interface implementation
First, let me introduce the development environment I use:
TouchGFX software version: 4.20.0
IAR EWARM software version: 9.30.1
STM32CubeMX software version: 6.6.1
STM32CubeProgrammer software version: 2.11.0
Development board: STM32L496G-DISCO
Desktop system: Window11
The following describes how to design the interface using TouchGFX 4.20.0 Designer. First, create an empty project based on STM32L496G-DISCO, as shown below.
Figure 4. Create a project
Add background, controls, etc. The following figure shows adding a simulated clock.
Figure 5. Adding controls
After some trial and error, I finally configured the interface as shown below. Due to my lack of experience, I also added a screen to test the usage of each control.
Figure 6: Interface design
I first tried out the various elements used on the test screen and confirmed that the code could be implemented before porting it to the official main screen. The following figure shows what the test screen looks like.
Figure 7. Test screen
Here is a special note. In order to simulate the picking up and putting down of a water cup, I set two transparent buttons on the main interface to realize the state change. The following figure is the description of one of the buttons. In order to make it easier to press, I use the upper half of the screen as the touchable area, and then put the button on the top layer and set it to transparent. Add interaction on the far right and call a virtual function.
Figure 8. Transparent button
The following figure is a detailed introduction to the implementation of the transparent button interaction virtual function. Note that some codes are automatically generated and some are added by me.
Figure 9. Virtual function implementation
Then add code to the <MainView.cpp> file to implement the state machine I designed. See the attachment for the specific code.
By using the simulation function of TouchGFX to test and debug, once you are sure there is no problem, you can download it to the development board with one click and test it directly on the hardware. See the video for the debugged interface.
Above, the basic interface is designed. It may look simple, but for those who are learning TouchGFX from scratch, they still need to refer to multiple examples, think about which functions can be used, how to configure them, and in what form to display them. But once you get started, it will be easy to use later.
2. Cup position detection solution
I use a reflective photoelectric sensor RPR-220 to detect whether the cup is on the cup holder. The sensor principle and the actual object are shown in the figure below.
Figure 10: Photoelectric sensor
To detect whether the cup is in place, you can actually use a mechanical pressure touch switch, which is simpler to use. The reason why the photoelectric type is finally used is that the photoelectric sensor does not contact, has a simple structure and high reliability. The circuit design of this sensor is shown in the figure below.
Figure 11. Sensor circuit diagram
This kind of sensor is mostly used to detect black marks with clear boundaries. I used it to detect water cups. Affected by the surface colors of different water cups, the output voltage signal was not consistent. Therefore, I designed a dynamic detection method, which is to detect the output voltage of the receiving tube when the light-emitting tube is turned on and off, and then make a difference. If there is no cup, the difference will be very small, otherwise it will be much larger, so the detection result will be more accurate.
The connection between the sensor and the STM32L496G-DISCO development board uses the reserved Arduino uno interface, using an ADC port to collect voltage and an IO port to control the light-emitting diode switch. The specific resources used are shown in the figure below.
Figure 12: Arduino uno interface
I found a bare PCB and soldered the photoelectric sensor with flying wires, as shown in the figure below.
Figure 13: Flying wire board
The final effect of connecting to the STM32L496G-DISCO development board is as shown in the figure below.
Figure 14: Connect the circuit board
The expected usage scenario is as shown below.
Figure 15: Usage scenario
After connecting the hardware circuit, the next step is to debug the program. First, configure the ADC and IO port output in STM32CubeMX. The figure below shows the ADC configuration. Using DMA to transfer sampled data, the CPU does not need to intervene in any operation, which is more efficient.
Figure 16. ADC configuration
The following figure shows the IO configuration, with the default high level, pull-up, and medium speed.
Figure 17. IO configuration
Here I would like to emphasize that you should not forget to configure the reference source. I forgot to configure it, but STM32CubeMX did not prompt me, which took me a long time to find the reason. As shown in the figure below.
Figure 18. Reference source configuration
Next, generate the code and add the specific implementation in the editor. The following code implements the cup detection and sends a message to the UI. The hardware obtains the parameters and passes them to the UI according to the MVP structure specified by TouchGFX. The specific transmission process is shown in the figure below.
Figure 19: Parameter passing
After all files are modified, compile, download, and run, you can see the final running results. The use process of the actual work is shown in the video.
- Connect to Alibaba Cloud to synchronize time and data
The development board connects to Alibaba Cloud through the ESP8266 module, and obtains network time, synchronizes water cup status, sets parameters, etc. The ESP8266 module I use is the ESP-12S produced by Anxinke, as shown below.
Figure 20: WiFi module
Since I bought it a long time ago, the firmware inside does not support the MQTT protocol, so the first thing to do is to upgrade the WiFi firmware. The link below is the official page of Anxinke, and all the software packages you need can be found on this page.
https://docs.ai-thinker.com/esp8266
There are many specific upgrade tutorials on the Internet, so I will not go into details. I will just list one here.
https://blog.csdn.net/jdhuzb/article/details/119678009
The hardware connection between the development board and the WiFi module, I use the UART1 port, corresponding to the PMOD#2 and PMOD#3 pins of CN3 on the board, as shown in the figure below.
Figure 21, Serial port interface
The CN3 interface can directly provide 3.3V power supply, which can be led out to the WiFi module with 4 wires. The connected experimental board is as shown in the figure below.
Figure 22: Connected experimental board
After the hardware is connected, start designing the software. First, use STM32CubeMX to configure the serial port, as shown below. Since you need to receive string frame data of indefinite length, you use the serial port idle interrupt + serial port DMA interrupt to achieve it, so you need to set the DMA channel.
Figure 23, Configuring the serial port
STM32CubeMX will automatically generate the basic configuration of the serial port. What is more important is that you need to write your own idle interrupt callback function, which is not provided by the official HAL library. I wrote the callback function for UART1 and UART2 transparent transmission, which basically realizes DMA fully automatic transmission and reception, and only occupies very few CPU resources.
There are two points to note: First, the DMA buffer must be long enough, at least larger than the maximum message length sent back by Alibaba Cloud. Second, the ORE flag must be cleared before the next interrupt is enabled to prevent data from coming in during the interrupt processing to disable DMA, causing an ORE error.
The next step is to build products and devices on the Alibaba Cloud platform. There are too many online tutorials for this, so I won’t go into detail here. I’ll just post the following link for your reference.
https://blog.csdn.net/u014421313/article/details/125412417
The following figure shows a device I created on Alibaba Cloud, with three attributes: water cup status, water cup at location timeout, and water cup out of location timeout.
Figure 24. Devices created on Alibaba Cloud
The next step is to write the networking program for the MCU. There are many routines on the Internet. Most of them are complicated and difficult to understand, probably because of various abnormal situations. I use the simplest and crudest way to connect to Alibaba Cloud. The code structure is also very simple, which is suitable for beginners to practice. I first defined the networking information in the header file "esp_mqtt.h". Considering that this is just a demonstration project and does not require flexible configuration, I did not split it into flexible ones.
Then in "esp_mqtt.c", send frames directly through the serial port, leaving a little delay between each frame, and wait for Alibaba Cloud to return the data.
Here I will focus on the data parsing callback function. This is divided into two parts. The first part is to extract the three parameters of the hour, minute, and second in the received time string and send them to the analog clock for time calibration. This is relatively simple. Just find the string position and use the atoi() function to convert the string into an int number. The second part is to parse the setting message received from Alibaba Cloud. Most of the routines on the Internet use string functions to extract this kind of json string with few parameters and simple content bit by bit. I think this is more troublesome, so here I directly call the cJSON library for parsing, which is refreshing! Finally, the results are printed out from the debugging serial port. After repeated debugging, Alibaba Cloud data reporting and subscription were finally realized. See the video for specific demonstration.
Then, the updated time data and the subscribed setting data are synchronized to the TouchGFX interface. Finally, a web application is made to implement the cloud settings.
When writing this part of the program, I initially planned to use osMessageCreate to create two message queues pointing to structures, and pass the time data and setting data obtained from Alibaba Cloud to the TouchGFX code. However, after repeated research, I found that due to the re-encapsulation of STM32CubeMX, the default osMessageCreate can only create queues pointing to int32 types. If it points to a pointer, memory space must be allocated separately, which is quite troublesome. Here I am lazy and only create a message queue to pass the flag to indicate what information is currently updated, and set the structures corresponding to the time data and setting data as global variables, which are directly called in the TouchGFX code. Since the flag bit transfer follows the MVP method, there is no problem in actual use.
Finally, a WEB application was designed to indicate and set the parameters of the water cup holder. The specific application interface is shown in the figure below.
Figure 25: WEB application
4. Source Code
The code is in the following link:
https://download.eeworld.com.cn/detail/sipower/625171
5. Demonstration video of the work’s functions
(Video introduction + link)
The video is available at the following link:
- Design UI and implement state machine.
https://www.bilibili.com/video/BV1WD4y1y7nF?share_source=copy_web
2. The software and hardware debugging of the smart cup holder was successful.
https://www.bilibili.com/video/BV1kg411Y7ts?share_source=copy_web
3. The smart cup holder is connected to Alibaba Cloud.
https://www.bilibili.com/video/BV1wD4y1C727?share_source=copy_web
4. Alibaba Cloud display and settings for smart water cup holder.
https://www.bilibili.com/video/BV1QD4y1k72e?share_source=copy_web
6. Project Summary
(Project text summary + post sharing link summary)
Through this work design, I have basically mastered the development process of TouchGFX and became familiar with the operation of integrating TouchGFX and hardware interfaces on the FreeRTOS system. By adding a WiFi module, communication with Alibaba Cloud and IoT operations were achieved. This has added new skills for future work.
In short, I would like to thank Digi-Key and EEWORLD for providing this creative opportunity. You have provided financial and technical support for the learning and research of engineers, which has a positive impact on the entire industry. I hope that both companies will do better and better.
The post link is as follows:
1. Unboxing sharing:
https://en.eeworld.com/bbs/thread-1206983-1-1.html
2. Construction of TouchGFX development environment based on STM32L496G-DISCO
https://en.eeworld.com/bbs/thread-1220158-1-1.html
3. UI design based on TouchGFX
https://en.eeworld.com/bbs/thread-1220165-1-1.html
4. Add hardware support for TouchGFX interface
https://en.eeworld.com/bbs/thread-1220212-1-1.html
5. Connect to Alibaba Cloud to synchronize time and data
https://en.eeworld.com/bbs/thread-1220875-1-1.html
VII. Others
None.
智能水杯托_作品提交.doc
(6.83 MB, downloads: 3)
|