Skip to content

Commit 85de8ea

Browse files
authored
Merge pull request #268 from Tinyu-Zhao/master
MQTT and Finger Example
2 parents ebccfce + 00567df commit 85de8ea

File tree

6 files changed

+197
-393
lines changed

6 files changed

+197
-393
lines changed

examples/Advanced/MQTT/MQTT.ino

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
*******************************************************************************
3+
* Copyright (c) 2021 by M5Stack
4+
* Equipped with M5Core sample source code
5+
* 配套 M5Core 示例源代码
6+
* Visit the website for more information:https://docs.m5stack.com/en/core/gray
7+
* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/gray
8+
*
9+
* describe:MQTT.
10+
* date:2021/11/5
11+
*******************************************************************************
12+
*/
13+
#include "M5Stack.h"
14+
#include <WiFi.h>
15+
#include <PubSubClient.h>
16+
17+
WiFiClient espClient;
18+
PubSubClient client(espClient);
19+
20+
// Configure the name and password of the connected wifi and your MQTT Serve host. 配置所连接wifi的名称、密码以及你MQTT服务器域名
21+
const char* ssid = "Explore-F";
22+
const char* password = "xingchentansuo123";
23+
const char* mqtt_server = "mqtt.m5stack.com";
24+
25+
unsigned long lastMsg = 0;
26+
#define MSG_BUFFER_SIZE (50)
27+
char msg[MSG_BUFFER_SIZE];
28+
int value = 0;
29+
30+
void setupWifi();
31+
void callback(char* topic, byte* payload, unsigned int length);
32+
void reConnect();
33+
34+
void setup() {
35+
M5.begin();
36+
M5.Power.begin();
37+
setupWifi();
38+
client.setServer(mqtt_server, 1883); //Sets the server details. 配置所连接的服务器
39+
client.setCallback(callback); //Sets the message callback function. 设置消息回调函数
40+
}
41+
42+
void loop() {
43+
if (!client.connected()) {
44+
reConnect();
45+
}
46+
client.loop(); //This function is called periodically to allow clients to process incoming messages and maintain connections to the server.
47+
//定期调用此函数,以允许主机处理传入消息并保持与服务器的连接
48+
49+
unsigned long now = millis(); //Obtain the host startup duration. 获取主机开机时长
50+
if (now - lastMsg > 2000) {
51+
lastMsg = now;
52+
++value;
53+
snprintf (msg, MSG_BUFFER_SIZE, "hello world #%ld", value); //Format to the specified string and store it in MSG. 格式化成指定字符串并存入msg中
54+
M5.Lcd.print("Publish message: ");
55+
M5.Lcd.println(msg);
56+
client.publish("M5Stack", msg); //Publishes a message to the specified topic. 发送一条消息至指定话题
57+
if(value%12==0){
58+
M5.Lcd.clear();
59+
M5.Lcd.setCursor(0,0);
60+
}
61+
}
62+
}
63+
64+
void setupWifi() {
65+
delay(10);
66+
M5.Lcd.printf("Connecting to %s",ssid);
67+
WiFi.mode(WIFI_STA); //Set the mode to WiFi station mode. 设置模式为WIFI站模式
68+
WiFi.begin(ssid, password); //Start Wifi connection. 开始wifi连接
69+
70+
while (WiFi.status() != WL_CONNECTED) {
71+
delay(500);
72+
M5.Lcd.print(".");
73+
}
74+
M5.Lcd.printf("\nSuccess\n");
75+
}
76+
77+
void callback(char* topic, byte* payload, unsigned int length) {
78+
M5.Lcd.print("Message arrived [");
79+
M5.Lcd.print(topic);
80+
M5.Lcd.print("] ");
81+
for (int i = 0; i < length; i++) {
82+
M5.Lcd.print((char)payload[i]);
83+
}
84+
M5.Lcd.println();
85+
}
86+
87+
void reConnect() {
88+
while (!client.connected()) {
89+
M5.Lcd.print("Attempting MQTT connection...");
90+
// Create a random client ID. 创建一个随机的客户端ID
91+
String clientId = "M5Stack-";
92+
clientId += String(random(0xffff), HEX);
93+
// Attempt to connect. 尝试重新连接
94+
if (client.connect(clientId.c_str())) {
95+
M5.Lcd.printf("\nSuccess\n");
96+
// Once connected, publish an announcement to the topic. 一旦连接,发送一条消息至指定话题
97+
client.publish("M5Stack", "hello world");
98+
// ... and resubscribe. 重新订阅话题
99+
client.subscribe("M5Stack");
100+
} else {
101+
M5.Lcd.print("failed, rc=");
102+
M5.Lcd.print(client.state());
103+
M5.Lcd.println("try again in 5 seconds");
104+
delay(5000);
105+
}
106+
}
107+
}
Lines changed: 86 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,99 @@
1+
/*
2+
*******************************************************************************
3+
* Copyright (c) 2021 by M5Stack
4+
* Equipped with M5Core sample source code
5+
* 配套 M5Core 示例源代码
6+
* Visit the website for more information:https://docs.m5stack.com/en/core/gray
7+
* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/gray
8+
*
9+
* describe:Finger Unit example
10+
* date:2021/10/28
11+
*******************************************************************************
12+
Description: FINGER UNIT use case: Press the left button to enter the fingerprint entry mode. Press the middle button to enter the fingerprint identification mode,Right click to delete all saved users
13+
FINGER UNIT 使用案例按左键进入指纹录入模式,短按中间键进入指纹识别模式,按下右键删除所有保存的用户
14+
*/
15+
116
#include <M5Stack.h>
2-
#include "finger.h"
17+
#include "M5_FPC1020A.h"
318

4-
uint8_t userNum; //User number
519
FingerPrint FP_M;
620

7-
void CleanScreen()
8-
{
9-
M5.Lcd.setTextColor(WHITE);
10-
M5.Lcd.fillRect(0,50,400,300,BLACK);
11-
M5.Lcd.setCursor(0, 50);
12-
M5.Lcd.setTextSize(2);
13-
userNum = FP_M.fpm_getUserNum();
14-
M5.Lcd.print("userNum:");
15-
M5.Lcd.println(userNum);
16-
}
17-
1821
void setup() {
19-
M5.begin();
20-
M5.Power.begin();
21-
Serial.begin(115200);
22-
Serial2.begin(19200, SERIAL_8N1, 16, 17);
23-
M5.Lcd.clear(BLACK);
24-
M5.Lcd.setTextColor(YELLOW);
25-
M5.Lcd.setTextFont(2);
26-
M5.Lcd.setTextSize(3);
27-
M5.Lcd.setCursor(20, 0);
28-
M5.Lcd.println("Finger example");
29-
Serial.printf("Finger example\n");
30-
M5.Lcd.setTextColor(WHITE);
31-
M5.Lcd.fillRect(0,50,400,300,BLACK);
32-
M5.Lcd.setCursor(0, 50);
33-
M5.Lcd.setTextSize(2);
34-
userNum = FP_M.fpm_getUserNum();
35-
M5.Lcd.print("userNum:");
36-
M5.Lcd.println(userNum);
22+
M5.begin();
23+
M5.Power.begin();
24+
M5.Lcd.setTextColor(GREEN);
25+
M5.Lcd.setTextSize(2);
26+
FP_M.begin();
27+
M5.Lcd.drawString("Add User", 10, 210);
28+
M5.Lcd.drawString("Verify", 125, 210);
29+
M5.Lcd.drawString("Del User", 220, 210);
30+
31+
M5.Lcd.setCursor(0, 20);
32+
M5.Lcd.println("Finger Unit TEST");
33+
M5.Lcd.println("1. delete all user");
34+
M5.Lcd.println("2. add a user fingerprint");
35+
M5.Lcd.println("3. verify user permission");
3736
}
3837

39-
//ButtonA: Add user
40-
//ButtonB: Matching
41-
//ButtonC: Delete All User
4238
void loop(){
43-
uint8_t res1;
44-
if(M5.BtnA.wasPressed()){
45-
CleanScreen();
46-
M5.Lcd.println("Fingerprint Typing");
47-
48-
res1 = FP_M.fpm_addUser(userNum,1);
49-
if(res1 == ACK_SUCCESS){
50-
M5.Lcd.println("Success");
51-
}
52-
else if(res1 == ACK_FAIL){
53-
M5.Lcd.println("Fail");
54-
}
55-
else if(res1 == ACK_FULL){
56-
M5.Lcd.println("Full");
57-
}
58-
else{
59-
M5.Lcd.println("Timeout");
60-
}
61-
userNum++;
62-
}
39+
uint8_t res1;
40+
//ButtonA: Add user. 添加用户
41+
if(M5.BtnA.wasPressed()){
42+
M5.Lcd.fillRect(0, 0, 320, 200, BLACK);
43+
Serial.println("Start Fingerprint Typing");
44+
Serial.println("Put Your Finger on the sensor");
45+
Serial.println("wating....");
46+
47+
M5.Lcd.println("Start Fingerprint Typing");
48+
M5.Lcd.println("Put Your Finger on the sensor");
49+
M5.Lcd.println("wating....");
6350

64-
if(M5.BtnB.wasPressed()){
65-
CleanScreen();
66-
M5.Lcd.println("Matching");
67-
68-
res1 = FP_M.fpm_compareFinger();
69-
if(res1 == ACK_SUCCESS){
70-
M5.Lcd.println("Success");
71-
}
72-
if(res1 == ACK_NOUSER){
73-
M5.Lcd.println("No Such User");
74-
}
75-
if(res1 == ACK_TIMEOUT){
76-
M5.Lcd.println("Timeout");
77-
}
51+
res1 = FP_M.fpm_addUser(22,1); //(user_num, userPermission)
52+
if(res1 == ACK_SUCCESS){
53+
M5.Lcd.println("Success");
54+
Serial.println("Success");
55+
}else{
56+
M5.Lcd.println("Fail");
57+
Serial.println("Fail");
7858
}
59+
}
60+
//ButtonB: Matching. 匹配指纹
61+
if(M5.BtnB.wasPressed()){
7962

80-
if(M5.BtnC.wasPressed()){
81-
res1 = FP_M.fpm_deleteAllUser();
82-
CleanScreen();
83-
84-
if(res1 == ACK_SUCCESS){
85-
M5.Lcd.println("Delete All User Successful");
86-
}
87-
else{
88-
M5.Lcd.println("Delete All User Failed");
89-
}
63+
M5.Lcd.fillRect(0, 0, 320, 100, BLACK);
64+
Serial.println("Start Verify Fingerprint");
65+
res1 = FP_M.fpm_compareFinger();
66+
if(res1 == ACK_SUCCESS){
67+
68+
Serial.println("Success");
69+
Serial.print("User ID: ");
70+
Serial.println(FP_M.fpm_getUserId());
71+
72+
M5.Lcd.println("Success");
73+
M5.Lcd.print("User ID: ");
74+
M5.Lcd.println(FP_M.fpm_getUserId());
75+
76+
}else{
77+
Serial.println("No Such User");
78+
79+
M5.Lcd.println("No Such User");
80+
}
81+
}
82+
//ButtonC: Delete All User. 删除所有用户
83+
if(M5.BtnC.wasPressed()){
84+
M5.Lcd.fillRect(0, 0, 320, 100, BLACK);
85+
Serial.println("Start Delete Fingerprint");
86+
M5.Lcd.println("Start Delete Fingerprint");
87+
res1 = FP_M.fpm_deleteAllUser();
88+
if(res1 == ACK_SUCCESS){
89+
Serial.println("Delete All User Successful");
90+
M5.Lcd.println("Delete All User Successful");
91+
}
92+
else{
93+
Serial.println("Delete All User Failed");
94+
M5.Lcd.println("Delete All User Failed");
9095
}
91-
M5.update();
96+
}
97+
M5.Lcd.setCursor(0, 20);
98+
M5.update();
9299
}

0 commit comments

Comments
 (0)