MartinFowler

[Training Camp_Advanced Class] Internet of Things Desk Lamp

 
Overview

Functional Requirements:

Because I'm lazy and don't want to get out of bed to turn on the lights manually every time, I add the idle desk lamp at home to the IoT module to transform it into a smart remote control desk lamp. On the basis of the original button control, the following functions have been added to it: remote switching on and off lights, timed switching on and off lights, people coming to turn on the lights and people leaving to turn off the lights, voice control, temperature and humidity measurement and display, etc. (Some functions can be found in the APP configuration on or off). In addition, I have recently become obsessed with a series of smart home products from Xiaomi. They have a complete range of products, complete ecology, good quality and appearance. The most important thing is that the price is very affordable, which is suitable for people like me who are poor and love to experience. After experiencing the smart home, I felt itchy looking at the clumsy old equipment at home. I wanted to replace it but felt it was not cost-effective. I just happened to take advantage of this EDA summer training camp.Issue couponsOpportunities for training, DIY design, and empowering old equipment.

Circuit introduction and device selection:

According to the above functional requirements, the entire circuit design is divided into seven modules, including the main control module, WIFI module, power module, relay module, external interface module, memory module and auxiliary module. After clarifying the structure of the entire circuit system, design and select components for each part in turn. First of all, the main control module uses STM32F103C8T6. The main reason for choosing this chip is to consider the convenience of program development, cost and inventory. Since I started, I have basically been developing based on ST's MCU. I am quite familiar with the relevant development process, and I have also saved some typical peripheral driver routines. In fact, I use more F4 for daily work, but F1 is enough so there is no additional cost. Finally, I still have some C8T6 in stock. To sum up. The WIFI module selected is the ESP-12F (ESP8266MOD) developed by Essence based on Espressif's ESP8266EX chip. The reason is that we are familiar with this module. Jialichuang has stock and can place orders directly, which is convenient. WIFI and MCU communicate through UART2. During the design, two pins were placed on TX and RX to facilitate debugging. After debugging, just use a jumper cap to connect them. A button is designed to reset the WIFI module. In the power module, the interface uses the commonly used Android micro USB. The voltage stabilizing module design is completely copied from Zero Demon’s plan. There is also a CH340E module for USB to TTL conversion, which is connected to UART1, which is convenient for printing debugging information and finding bugs during the code debugging stage. The selection of the relay module is the same as that of Zero Demon. The difference is the quantity, because my desk lamp has four mechanical buttons (switch, low brightness, medium brightness, high brightness). In order to adapt, four-way relays are placed. The external interface module is used to connect other functional modules. The external modules include: temperature and humidity sensor SHT30 (same model for Xiaomi, not disassembled, see what is said online), 0.96-inch OLED display, human body infrared sensor module, Bluetooth module, for debugging ESP8266 power supply, SWD programming interface. Among them, the SHT30 and the OLED display use the same I2C interface, and a one-to-two line will be used at that time; the infrared sensor outputs a 0/1 signal according to whether it senses a person, and uses the GPIO external interrupt function; the voice control module depends on whether it senses a person or not. Sensing the sound output 0/1 signal also uses the external interrupt function. The photoresistor uses GL7537, which uses the ADC sampling voltage to reflect the resistance of the photoresistor and thus the current ambient brightness value. The Bluetooth module is connected to UART3 (considering that there is a guarantee when the WIFI function cannot be adjusted). When designing, we considered that some functional modules were not welded to the board but connected through interface connections. This is because there will be strong electricity on the circuit board and will be placed in the casing, and some sensors must be exposed. . Except for debugging, the interfaces all use 1.25mm connectors, which are 10,000 times more reliable than a single DuPont cable connection. When the time comes, the module is connected to the motherboard through wiring, and the module is fixed on the case through mounting holes or hot melt glue. The memory module is used to save configuration information and can quickly enter the configured functional state after restarting. The 16MB FLASH chip W25Q128 from Winbond is used. In fact, W25Q32 is enough. The main reason for choosing Q128 is that I have this chip on hand and the program is ready-made, which is convenient. The LED lights used in the main debugging stage of the auxiliary module include the power indicator light, the program running status indicator light, and 4 LED lights. The four LED lights were originally designed to display the status of the four relays. Now think about it, in theory, there are only 4 states during normal operation, lights off/low brightness/medium brightness/high brightness, just Two LED lights can display its status, and the remaining two are used as backup according to the actual situation encountered during the program adjustment stage (such as indicating the WIFI networking status).

software design:

Software design mainly includes microcontroller control program design and control terminal (mobile phone, computer, etc.) interface design. The cloud server selected for this project is mobile onenet, and the device and cloud communication protocol adopts the EDP protocol. The reason for selection is that my wifi module uses ESP8266-12F. After searching on the Internet, I found a relatively reliable article with the keyword STM32 ESP8266 onenet, so I chose onenet. (Actually, when I first participated in the training class, I wanted to implement the more common MQTT protocol, but the time was too tight, and the protocol is just a tool. For now, I will do this and learn more later.)         Microcontroller control program design also includes Hardware driver design and application code design. The program is developed on MDK based on st library functions. The hardware driver part includes GPIO configuration (LED light relay), external interrupt (human body infrared and acoustic sensor), ADC (photoresistor), I2C (SHT30, OLED display), UART (WIFI and serial port). This part prepares you for writing applications later. The application code design is based on functional requirements and preset logic or system operation ideas, calling the previously written driver API interface to string together various parts, which mainly includes the implementation of system function logic and the WIFI module to connect to the cloud. The mobile phone interface design is based on the onenet access manual and can be built step by step. It mainly builds products, creates equipment, creates data flows, and finally creates a control terminal interface through the fool-like operation of dragging modules, which can connect to onenet and then communicate with the equipment to realize the issuance of control instructions and the distribution and display of data. Attached is a complete set of source programs uploaded with a readme attached.

Debugging process

Overall debugging ideas

First is the hardware. The boards made at Jiali Chuang are empty boards (five boards for five yuan are shipped in 24 hours, which is really cool). The main reason is that there are not many components, and the empty boards are quick to make and easy to debug. When I first learned how to draw a board, I was always very confident and felt that everything would run away as soon as I soldered it. However, there were basically a lot of problems and it was not easy to find them. Therefore, I developed a habit later on, dividing the designed board into multiple modules according to functions, welding them one by one and debugging them one by one, and then moved on to the next part after they were ok. This project divides the board into the following modules, and the debugging sequence is: power supply, MCU, WIFI, relays, and other external sensors and modules. The former is the basis for the latter, that is, only when the power supply is normal can other modules work. Only after the MCU, as the system brain, works normally can the remaining modules be connected together to play a role. WIFI and relays, as the core parts of this project, must be ok under the premise that everything above is ok. Debugging, while other modules such as sound, light, temperature and humidity OLED will be debugged later as increments. Next is software debugging. The software idea is similar to that of hardware. According to the system operation logic and difficult and easy procedures, the order is: bottom driver, device cloud communication and upper-layer application. When I write code, I generally outline a framework, and then when it comes to specific implementation, If I have done something related before, I will just copy it over. If I haven’t, I will search around CSDN and find something suitable and then copy it over.                    image.png

Problems encountered and solutions

Question 1. After welding the power supply, MCU module and auxiliary circuits such as LED for indication, we found that LED_RUN is always on. What I designed is to control the high and low levels through GPIO when the program is running normally, and then control the LED_RUN 1s periodic flashing.

Solution steps: First of all, I wondered whether the corresponding pin of the MCU was not soldered properly and was short-circuited to VCC. I found that there was no short-circuit through the electric meter test, but I found that the voltage was always around 2.5V. So I suspected whether there was a problem with the buck module or the MCU hardware, but I checked that the LDO output was normal 3.3V, which ruled out the power supply problem. I suspected that the MCU was broken, but it would be troublesome to disassemble and resolder it. I thought it happened to have a serial port and other LED lights. So I printed the debugging characters through the serial port and turned on all the other LED lights. I found that the serial port was OK, but three of the other four LED lights were on and one was not. I also suspected that there was a problem with some pins of the MCU. When I was about to replace the MCU, I suddenly thought that I could first check whether the LED light that was not on was soldered backwards. It turned out that it was soldered backwards, which reduced the risk to the MCU. doubts, and then start checking from the software. Finally, I found that the GPIO port PA15 I used is a special JTAG port. If I want to use it as a GPIO port, I need to reuse it first and add the corresponding code. For specific implementation, please see the previous CSDN article (thanks)   https://blog.csdn.net/qhw5279/article/details/72630637

Question 2. The relay does not respond.

Solution steps: At the beginning, it was also suspected that the hardware was not soldered properly or there was a problem with the chip. Re-soldered a relay driver chip. The test still doesn't work. After checking the program, there is no problem. The IO port has both high and low levels. Finally found out that it was a problem with the schematic diagram. . . At that time, Song Gong actually provided a complete circuit, and there was also an application circuit in the NUD3105 chip manual, but he was blindly confident and not familiar with the mos tube driver, so he drew it wrong. Correct and incorrect examples are given below.                                                 23.jpg

When encountering this kind of hardware principle error, I can only re-draw the diagram and make the board, but the time is too tight. I thought that if I wait for the new board to arrive before debugging, I will definitely not be able to keep up with the progress, and I am worried that there will be another communication problem. So after throwing the board out, I operated on the old board and saved one relay first by cutting wires and flying wires, and then continued to debug other functions. Below is a diagram of the flying wires. I was worried that dust or something would fall into the flying wires and cause a short circuit and burn the USB port of my computer, so I used hot melt glue to paste it.

Flying line chart front.jpg

Flying line diagram back side.jpg

Question 3: When debugging the ESP8266, when using the USB to TTL module to connect to the ESP8266 alone, the AT commands do not respond. It worked after reburning the firmware, but then it didn't work again.

Solution steps: By consulting the relevant documents of ESP8266, it should be caused by the fact that I did not reset when I powered on. I added the corresponding IO configuration and gave the RESET pin of ESP8266 a low level for 100ms and then pulled it high.

Question 4: I have no idea about the connection between ESP8266 and cloud server and mobile APP design, and I don’t know where to start.

Solution steps: After searching CSDN with the keyword stm32f103 ESP8266 mqtt, I found that there is a Jingwei Electronics program, but it uses SIM module communication. I tried to modify it to WIFI module communication, but failed. I continued to search for information and found that several articles about using China Mobile Onenet as a cloud server gave relatively detailed information. After roughly understanding the onenet access tutorial, I followed other people’s experience posts and official tutorials to operate. I found that the program only needs to change the device ID KEY value and WIFI account password, and the registration and creation of onenet is also very simple, and then the communication starts like magic. After taking the step from 0 to 1, the rest is simple. It is nothing more than adding modules, changing the interface, and debugging functions. Reference information is also given below (thanks): https://www.cnblogs.com/luxiaoguogege/p/10136996.html

Question 5: There is no inching button in the onenet application design. There is only one button, and the left is on and the right is off. It is easy to press the wrong button and the logic is unclear or the operation is complicated. In addition, there is no control that can intuitively indicate the current status of the desk lamp. .

Solution steps: Divide the desk lamp switch button into 4. Regardless of whether you click on or off of a single button, the instructions sent to the device are the same, so that the function of each button is independent (the previous design was to let the switch button is on, and then select brightness (this is how the mechanical button of the desk lamp itself is implemented)). In addition, a data stream is added to display the current status of the light.

Question 6: When SHT30 read the temperature and humidity, it was found that the temperature was normal but the humidity was always over 99.9.

Solution steps: Look carefully at the datasheet and find that it not only has a single read mode, but also reads multiple times after sending a command. The frequency of multiple reads is relatively low, more than ten or twenty times per second, so I thought it was It's not that after I sent the read command, the sensor was read before it was converted to me, so I added a 5ms delay between sending the read command and reading the register value, and the problem was solved.

Question 7: The output value of the PIR human body sensing module keeps jumping from 0/1.

Solution steps: Add a software anti-shake processing and it will be OK.




Results description and display

Most of the functions have been implemented currently. Including light control and status feedback, temperature and humidity measurement and display, and smart switch functions, for example, the following two scenarios: 1 When the light is originally on, but detects no one and no sound, it automatically turns off. 2 When the light itself is off, it will turn on when weak light, people or sounds are detected. app.jpg

Later work plan (8.16)

within the project cycle

Next, we need to debug the OLED display screen, fix the casing, integrate the entire system, shoot some function demonstration videos, organize and upload the engineering materials.

After the project ends

To continue completing the above unfinished work, I also want to add a learning-assisted function, similar to Pomodoro. By sending an instruction and timing, after reaching the specified time interval, a prompt will be generated: through flashing lights, or using backup Bluetooth. One port in the interface is multiplexed as an IO port to drive an external buzzer. In addition, learn to use cloud servers (BAT) from several other companies. After all, these three should have a wider scope of use and stronger functions. In addition, be sure to study the MQTT protocol in depth, and later learn about the NB-IOT protocol and the use of corresponding modules. Try to get started with IoT design as soon as possible.

8.24

On the last day, the main functions were integrated and debugged. See the attached video for the debugging process. Physical demonstration 1.jpgPhysical demonstration 2.jpg

Gains from participating in the event (experience):

1 The expected purpose of transforming old products has been achieved. Now you can finally turn off the lights without getting out of bed. 2. Learned the basic process and key technical points of the implementation of IoT devices. For example, IoT devices are mainly developed on the operating side-cloud-device side. 3 Listening to the teacher's lectures and watching the students' works and discussion issues made up for my previous blind spots in knowledge. For example, capacitor filtering must pass through capacitors, as well as the driving of relays and some places in PCB design that cannot be laid with copper, etc. . 4. Driven by the project, you can actively look for solutions when encountering problems and solve them one by one. This kind of project experience and the experience of finding bugs and solving bugs when encountering problems are very valuable. 5 Thanks to Lichuang Thanks to Lichuang EDA team, Lichuang EDA is really easy to use and saves a lot of time in building a library. Lichuang Mall also has good materials at reasonable prices, which avoids the embarrassment of buying questionable materials on Taobao. Jialichuang's board making is also very nice.

Attachment description

The entire source program is uploaded in the attachment. The video of the demonstration includes a project introduction, preliminary functional demonstration and final functional demonstration. I don’t know why the final function demonstration cannot be displayed in the project introduction preview, but it can be played normally after downloading for personal testing. ~~~~

参考设计图片
×
 
 
Search Datasheet?

Supported by EEWorld Datasheet

Forum More
Update:2025-05-15 12:19:14

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
community

Robot
development
community

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号