-
Notifications
You must be signed in to change notification settings - Fork 437
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #268 from Tinyu-Zhao/master
MQTT and Finger Example
- Loading branch information
Showing
6 changed files
with
197 additions
and
393 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
******************************************************************************* | ||
* Copyright (c) 2021 by M5Stack | ||
* Equipped with M5Core sample source code | ||
* 配套 M5Core 示例源代码 | ||
* Visit the website for more information:https://docs.m5stack.com/en/core/gray | ||
* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/gray | ||
* | ||
* describe:MQTT. | ||
* date:2021/11/5 | ||
******************************************************************************* | ||
*/ | ||
#include "M5Stack.h" | ||
#include <WiFi.h> | ||
#include <PubSubClient.h> | ||
|
||
WiFiClient espClient; | ||
PubSubClient client(espClient); | ||
|
||
// Configure the name and password of the connected wifi and your MQTT Serve host. 配置所连接wifi的名称、密码以及你MQTT服务器域名 | ||
const char* ssid = "Explore-F"; | ||
const char* password = "xingchentansuo123"; | ||
const char* mqtt_server = "mqtt.m5stack.com"; | ||
|
||
unsigned long lastMsg = 0; | ||
#define MSG_BUFFER_SIZE (50) | ||
char msg[MSG_BUFFER_SIZE]; | ||
int value = 0; | ||
|
||
void setupWifi(); | ||
void callback(char* topic, byte* payload, unsigned int length); | ||
void reConnect(); | ||
|
||
void setup() { | ||
M5.begin(); | ||
M5.Power.begin(); | ||
setupWifi(); | ||
client.setServer(mqtt_server, 1883); //Sets the server details. 配置所连接的服务器 | ||
client.setCallback(callback); //Sets the message callback function. 设置消息回调函数 | ||
} | ||
|
||
void loop() { | ||
if (!client.connected()) { | ||
reConnect(); | ||
} | ||
client.loop(); //This function is called periodically to allow clients to process incoming messages and maintain connections to the server. | ||
//定期调用此函数,以允许主机处理传入消息并保持与服务器的连接 | ||
|
||
unsigned long now = millis(); //Obtain the host startup duration. 获取主机开机时长 | ||
if (now - lastMsg > 2000) { | ||
lastMsg = now; | ||
++value; | ||
snprintf (msg, MSG_BUFFER_SIZE, "hello world #%ld", value); //Format to the specified string and store it in MSG. 格式化成指定字符串并存入msg中 | ||
M5.Lcd.print("Publish message: "); | ||
M5.Lcd.println(msg); | ||
client.publish("M5Stack", msg); //Publishes a message to the specified topic. 发送一条消息至指定话题 | ||
if(value%12==0){ | ||
M5.Lcd.clear(); | ||
M5.Lcd.setCursor(0,0); | ||
} | ||
} | ||
} | ||
|
||
void setupWifi() { | ||
delay(10); | ||
M5.Lcd.printf("Connecting to %s",ssid); | ||
WiFi.mode(WIFI_STA); //Set the mode to WiFi station mode. 设置模式为WIFI站模式 | ||
WiFi.begin(ssid, password); //Start Wifi connection. 开始wifi连接 | ||
|
||
while (WiFi.status() != WL_CONNECTED) { | ||
delay(500); | ||
M5.Lcd.print("."); | ||
} | ||
M5.Lcd.printf("\nSuccess\n"); | ||
} | ||
|
||
void callback(char* topic, byte* payload, unsigned int length) { | ||
M5.Lcd.print("Message arrived ["); | ||
M5.Lcd.print(topic); | ||
M5.Lcd.print("] "); | ||
for (int i = 0; i < length; i++) { | ||
M5.Lcd.print((char)payload[i]); | ||
} | ||
M5.Lcd.println(); | ||
} | ||
|
||
void reConnect() { | ||
while (!client.connected()) { | ||
M5.Lcd.print("Attempting MQTT connection..."); | ||
// Create a random client ID. 创建一个随机的客户端ID | ||
String clientId = "M5Stack-"; | ||
clientId += String(random(0xffff), HEX); | ||
// Attempt to connect. 尝试重新连接 | ||
if (client.connect(clientId.c_str())) { | ||
M5.Lcd.printf("\nSuccess\n"); | ||
// Once connected, publish an announcement to the topic. 一旦连接,发送一条消息至指定话题 | ||
client.publish("M5Stack", "hello world"); | ||
// ... and resubscribe. 重新订阅话题 | ||
client.subscribe("M5Stack"); | ||
} else { | ||
M5.Lcd.print("failed, rc="); | ||
M5.Lcd.print(client.state()); | ||
M5.Lcd.println("try again in 5 seconds"); | ||
delay(5000); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,99 @@ | ||
/* | ||
******************************************************************************* | ||
* Copyright (c) 2021 by M5Stack | ||
* Equipped with M5Core sample source code | ||
* 配套 M5Core 示例源代码 | ||
* Visit the website for more information:https://docs.m5stack.com/en/core/gray | ||
* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/gray | ||
* | ||
* describe:Finger Unit example | ||
* date:2021/10/28 | ||
******************************************************************************* | ||
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 | ||
FINGER UNIT 使用案例按左键进入指纹录入模式,短按中间键进入指纹识别模式,按下右键删除所有保存的用户 | ||
*/ | ||
|
||
#include <M5Stack.h> | ||
#include "finger.h" | ||
#include "M5_FPC1020A.h" | ||
|
||
uint8_t userNum; //User number | ||
FingerPrint FP_M; | ||
|
||
void CleanScreen() | ||
{ | ||
M5.Lcd.setTextColor(WHITE); | ||
M5.Lcd.fillRect(0,50,400,300,BLACK); | ||
M5.Lcd.setCursor(0, 50); | ||
M5.Lcd.setTextSize(2); | ||
userNum = FP_M.fpm_getUserNum(); | ||
M5.Lcd.print("userNum:"); | ||
M5.Lcd.println(userNum); | ||
} | ||
|
||
void setup() { | ||
M5.begin(); | ||
M5.Power.begin(); | ||
Serial.begin(115200); | ||
Serial2.begin(19200, SERIAL_8N1, 16, 17); | ||
M5.Lcd.clear(BLACK); | ||
M5.Lcd.setTextColor(YELLOW); | ||
M5.Lcd.setTextFont(2); | ||
M5.Lcd.setTextSize(3); | ||
M5.Lcd.setCursor(20, 0); | ||
M5.Lcd.println("Finger example"); | ||
Serial.printf("Finger example\n"); | ||
M5.Lcd.setTextColor(WHITE); | ||
M5.Lcd.fillRect(0,50,400,300,BLACK); | ||
M5.Lcd.setCursor(0, 50); | ||
M5.Lcd.setTextSize(2); | ||
userNum = FP_M.fpm_getUserNum(); | ||
M5.Lcd.print("userNum:"); | ||
M5.Lcd.println(userNum); | ||
M5.begin(); | ||
M5.Power.begin(); | ||
M5.Lcd.setTextColor(GREEN); | ||
M5.Lcd.setTextSize(2); | ||
FP_M.begin(); | ||
M5.Lcd.drawString("Add User", 10, 210); | ||
M5.Lcd.drawString("Verify", 125, 210); | ||
M5.Lcd.drawString("Del User", 220, 210); | ||
|
||
M5.Lcd.setCursor(0, 20); | ||
M5.Lcd.println("Finger Unit TEST"); | ||
M5.Lcd.println("1. delete all user"); | ||
M5.Lcd.println("2. add a user fingerprint"); | ||
M5.Lcd.println("3. verify user permission"); | ||
} | ||
|
||
//ButtonA: Add user | ||
//ButtonB: Matching | ||
//ButtonC: Delete All User | ||
void loop(){ | ||
uint8_t res1; | ||
if(M5.BtnA.wasPressed()){ | ||
CleanScreen(); | ||
M5.Lcd.println("Fingerprint Typing"); | ||
|
||
res1 = FP_M.fpm_addUser(userNum,1); | ||
if(res1 == ACK_SUCCESS){ | ||
M5.Lcd.println("Success"); | ||
} | ||
else if(res1 == ACK_FAIL){ | ||
M5.Lcd.println("Fail"); | ||
} | ||
else if(res1 == ACK_FULL){ | ||
M5.Lcd.println("Full"); | ||
} | ||
else{ | ||
M5.Lcd.println("Timeout"); | ||
} | ||
userNum++; | ||
} | ||
uint8_t res1; | ||
//ButtonA: Add user. 添加用户 | ||
if(M5.BtnA.wasPressed()){ | ||
M5.Lcd.fillRect(0, 0, 320, 200, BLACK); | ||
Serial.println("Start Fingerprint Typing"); | ||
Serial.println("Put Your Finger on the sensor"); | ||
Serial.println("wating...."); | ||
|
||
M5.Lcd.println("Start Fingerprint Typing"); | ||
M5.Lcd.println("Put Your Finger on the sensor"); | ||
M5.Lcd.println("wating...."); | ||
|
||
if(M5.BtnB.wasPressed()){ | ||
CleanScreen(); | ||
M5.Lcd.println("Matching"); | ||
|
||
res1 = FP_M.fpm_compareFinger(); | ||
if(res1 == ACK_SUCCESS){ | ||
M5.Lcd.println("Success"); | ||
} | ||
if(res1 == ACK_NOUSER){ | ||
M5.Lcd.println("No Such User"); | ||
} | ||
if(res1 == ACK_TIMEOUT){ | ||
M5.Lcd.println("Timeout"); | ||
} | ||
res1 = FP_M.fpm_addUser(22,1); //(user_num, userPermission) | ||
if(res1 == ACK_SUCCESS){ | ||
M5.Lcd.println("Success"); | ||
Serial.println("Success"); | ||
}else{ | ||
M5.Lcd.println("Fail"); | ||
Serial.println("Fail"); | ||
} | ||
} | ||
//ButtonB: Matching. 匹配指纹 | ||
if(M5.BtnB.wasPressed()){ | ||
|
||
if(M5.BtnC.wasPressed()){ | ||
res1 = FP_M.fpm_deleteAllUser(); | ||
CleanScreen(); | ||
|
||
if(res1 == ACK_SUCCESS){ | ||
M5.Lcd.println("Delete All User Successful"); | ||
} | ||
else{ | ||
M5.Lcd.println("Delete All User Failed"); | ||
} | ||
M5.Lcd.fillRect(0, 0, 320, 100, BLACK); | ||
Serial.println("Start Verify Fingerprint"); | ||
res1 = FP_M.fpm_compareFinger(); | ||
if(res1 == ACK_SUCCESS){ | ||
|
||
Serial.println("Success"); | ||
Serial.print("User ID: "); | ||
Serial.println(FP_M.fpm_getUserId()); | ||
|
||
M5.Lcd.println("Success"); | ||
M5.Lcd.print("User ID: "); | ||
M5.Lcd.println(FP_M.fpm_getUserId()); | ||
|
||
}else{ | ||
Serial.println("No Such User"); | ||
|
||
M5.Lcd.println("No Such User"); | ||
} | ||
} | ||
//ButtonC: Delete All User. 删除所有用户 | ||
if(M5.BtnC.wasPressed()){ | ||
M5.Lcd.fillRect(0, 0, 320, 100, BLACK); | ||
Serial.println("Start Delete Fingerprint"); | ||
M5.Lcd.println("Start Delete Fingerprint"); | ||
res1 = FP_M.fpm_deleteAllUser(); | ||
if(res1 == ACK_SUCCESS){ | ||
Serial.println("Delete All User Successful"); | ||
M5.Lcd.println("Delete All User Successful"); | ||
} | ||
else{ | ||
Serial.println("Delete All User Failed"); | ||
M5.Lcd.println("Delete All User Failed"); | ||
} | ||
M5.update(); | ||
} | ||
M5.Lcd.setCursor(0, 20); | ||
M5.update(); | ||
} |
Oops, something went wrong.