This article is shared from the HUAWEI CLOUD community " STM32 + HUAWEI CLOUD IOT Design Dynamic Combination Lock ", author: DS Xiaolong.
1 Introduction
With the improvement of people's living standards and the development of science and technology, the protection of personal information is very important. We designed an intelligent electronic password lock for the Internet of Things. It is composed of other modules, with functions such as remote control and random password generation. After software and hardware testing, the system has fast response, high sensitivity and good real-time performance. The system recognition accuracy rate is as high as 99%. The system runs stably, safely and reliably, with low power consumption and good scalability.
Currently supported unlocking methods:
(1) Support mobile phone APP remote unlocking. Through the HUAWEI CLOUD IoT platform to remotely send commands to unlock, the ESP8266 on the device is connected to the home router and connected to the HUAWEI CLOUD IoT platform. The RTC time of the device can be calibrated on the mobile APP, the unique ID of the device is obtained, and a random unlock is generated. Password, you can click the unlock button on the APP, and send instructions to the STM32 device through the API provided by the IoT platform to complete the unlocking.
(2) Unlock with random password. Both the mobile APP and the local device use the time as the algorithm seed, and use the algorithm to generate the unlock password. The valid time of each string of passwords is one minute. After checking the password displayed on the mobile APP, enter the completed password on the local device to compare and unlock.
2. Mobile APP design
2.1 Introduction to the development environment
The host computer software is designed using the Qt framework, which is a cross-platform C++ graphical user interface application framework. Qt is a cross-platform C++ graphical user interface application development framework developed by The Qt Company in 1991. It can develop GUI programs as well as non-GUI programs such as console tools and servers. To put it simply, QT can easily help you make software with an interface, and you don't even need to invest a lot of effort.
QT official website: https://www.qt.io/
2.2 Learning Tutorial
QT entry practice column: https://blog.csdn.net/xiaolong1126626497/category_11400392.html
QT5 environment installation tutorial: https://xiaolong.blog.csdn.net/article/details/120654599
Download QT5.12.6 download address:
https://download.qt.io/archive/qt/5.12/5.12.6/
After opening the link select:
qt-opensource-windows-x86-5.12.6.exe 13-Nov-2019 07:28 3.7G Details
When the software is installed, the network is disconnected, otherwise it will be prompted to enter an account.
When installing, check a mingw 32 compiler.
2.3 Realize the effect
3. Create a cloud device
3.1 Create device
Log in to the official website: https://www.huaweicloud.com/
Search the Internet of Things directly to open the page.
https://www.huaweicloud.com/product/iothub.html
Get the product ID, save the ID, and click to view the details:
产品ID为:61b9ba3a2b2aa20288c1e7f1.
![](https://img-blog.csdnimg.cn/img_convert/7993fcfa3b983437b820a0f54d82064a.png)
![](https://img-blog.csdnimg.cn/img_convert/ecbcc1365dc078fd97e8df6785f09a9f.png)
![](https://img-blog.csdnimg.cn/img_convert/f28e79c6da10afda2da02dc4cb89de68.png)
![](https://img-blog.csdnimg.cn/img_convert/28e99b17de10d8c4d32dedd0e873ba23.png)
![](https://img-blog.csdnimg.cn/img_convert/eb9a5e3c35cccc6a5a8766112f4f4b59.png)
3.2 Create MQTT login account and key
After the device is created, the MQTT login account and key will be generated to facilitate the device to log in to the cloud platform.
Official website tool address: https://iot-tool.obs-website.cn-north-4.myhuaweicloud.com/
![](https://img-blog.csdnimg.cn/img_convert/94d970fe47287026ad6df38236902120.png)
4. STM32 device side code design
4.1 Hardware related schematic
![](https://img-blog.csdnimg.cn/img_convert/289e3dc188452f44c05067176cb35c4c.png)
4.2 Hardware Wiring
1. 板载ESP8266串口WIFI模块与STM32的串口3相连接。
PB10--RXD 模块接收脚
PB11--TXD 模块发送脚
PB8---CH-PD---悬空
PB9---RST---悬空
GND---GND 地
VCC---VCC 电源(3.3V~5.0V)
2. 触摸按键使用TTP229型号的驱动芯片
SCL接PC11
SDA-OUT接PC10
电源接VCC-3.3
GND接GND
3. ULN2003控制28BYJ-48步进电机接线:
ULN2003接线:
IN4: PC9 d
IN3: PC8 c
IN2: PC7 b
IN1: PC6 a
+ : 5V
- : GND
4. OLED显示屏
D0----SCK-----PB14
D1----MOSI----PB13
RES—复位(低电平有效)—PB12
DC---数据和命令控制管脚—PB1
CS---片选引脚-----PA7
5. 板载按键
KEY1---PA0
KEY2---PC13
6.板载LED灯
LED1---PB5
LED2---PB0
LED3---PB1
7. 板载蜂鸣器
BEEP---PA8
4.3 Server connection core code
//华为物联网服务器的设备信息
#define MQTT_ClientID "61b9ba3a2b2aa20288c1e7f1_QQ1126626497_0_0_2021121510"
#define MQTT_UserName "61b9ba3a2b2aa20288c1e7f1_QQ1126626497"
#define MQTT_PassWord "385ce91dfe7da5b7431868d5d87e7998163c493344040935d5a00024d6324242"
//订阅与发布的主题
#define SET_TOPIC "$oc/devices/61b9ba3a2b2aa20288c1e7f1_QQ1126626497_0_0_2021121510/sys/messages/down" //订阅
#define POST_TOPIC "$oc/devices/61b9ba3a2b2aa20288c1e7f1_QQ1126626497_0_0_2021121510/sys/properties/report" //发布
char mqtt_message[200];//上报数据缓存区
int main()
{
u32 time_cnt=0;
u32 i;
u8 key;
LED_Init();
BEEP_Init();
KEY_Init();
USART1_Init(115200);
TIMER1_Init(72,20000); //超时时间20ms
USART2_Init(9600);//串口-蓝牙
TIMER2_Init(72,20000); //超时时间20ms
USART3_Init(115200);//串口-WIFI
TIMER3_Init(72,20000); //超时时间20ms
USART1_Printf("正在初始化WIFI请稍等.\n");
if(ESP8266_Init())
{
USART1_Printf("ESP8266硬件检测错误.\n");
}
else
{
//非加密端口
USART1_Printf("WIFI:%d\n",ESP8266_STA_TCP_Client_Mode("CMCC-Cqvn","99pu58cb","121.36.42.100",1883,1));
}
//2. MQTT协议初始化
MQTT_Init();
//3. 连接华为服务器
while(MQTT_Connect(MQTT_ClientID,MQTT_UserName,MQTT_PassWord))
{
USART1_Printf("服务器连接失败,正在重试...\n");
delay_ms(500);
}
USART1_Printf("服务器连接成功.\n");
//3. 订阅主题
if(MQTT_SubscribeTopic(SET_TOPIC,0,1))
{
USART1_Printf("主题订阅失败.\n");
}
else
{
USART1_Printf("主题订阅成功.\n");
}
..................
..................
...................
}
Click Follow to learn about HUAWEI CLOUD's new technologies for the first time~