Skip to content

Commit f9562c7

Browse files
authored
Merge pull request #149 from Tinyu-Zhao/master
MQTT and Finger unit
2 parents 62e997e + 5cdeeb0 commit f9562c7

File tree

4 files changed

+170
-3
lines changed

4 files changed

+170
-3
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 M5StickC sample source code
5+
* 配套 M5StickC 示例源代码
6+
* Visit the website for more information:https://docs.m5stack.com/en/core/m5stickc
7+
* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/m5stickc
8+
*
9+
* describe:MQTT.
10+
* date:2021/11/5
11+
*******************************************************************************
12+
*/
13+
#include "M5StickC.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.Lcd.setRotation(3);
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%4==0){
58+
M5.Lcd.fillScreen(BLACK);
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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
*******************************************************************************
3+
* Copyright (c) 2021 by M5Stack
4+
* Equipped with M5StickC sample source code
5+
* 配套 M5StickC 示例源代码
6+
* Visit the website for more information:https://docs.m5stack.com/en/core/m5stickc
7+
* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/m5stickc
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
13+
FINGER UNIT 使用案例按左键进入指纹录入模式,短按中间键进入指纹识别模式
14+
*/
15+
16+
#include <M5StickC.h>
17+
#include "M5_FPC1020A.h"
18+
19+
FingerPrint FP_M;
20+
21+
void setup() {
22+
M5.begin(); //Init M5StickC. 初始化M5StickC
23+
M5.Lcd.setRotation(3); //Rotate the screen. 旋转屏幕
24+
M5.Lcd.setTextColor(GREEN);
25+
M5.Lcd.println("Finger Unit TEST");
26+
M5.Lcd.println("Btn.A add a user fingerprint");
27+
M5.Lcd.println("Btn.B delete all user");
28+
FP_M.begin(&Serial2,32,33);
29+
}
30+
31+
void loop(){
32+
M5.update();
33+
uint8_t res1;
34+
//ButtonA: Add user. 添加用户
35+
if(M5.BtnA.wasPressed()){
36+
M5.Lcd.fillRect(0, 8, 160, 80, BLACK);
37+
M5.Lcd.setCursor(0,20);
38+
M5.Lcd.println("Start Fingerprint Typing");
39+
M5.Lcd.println("Put Your Finger on the sensor");
40+
M5.Lcd.println("wating....");
41+
42+
res1 = FP_M.fpm_addUser(22,1); //(user_num, userPermission)
43+
if(res1 == ACK_SUCCESS) M5.Lcd.println("Success");
44+
else M5.Lcd.println("Fail");
45+
}
46+
//ButtonB: Matching. 匹配指纹
47+
if(M5.BtnB.wasPressed()){
48+
M5.Lcd.fillRect(0, 8, 160, 80, BLACK);
49+
M5.Lcd.setCursor(0,20);
50+
M5.Lcd.println("Start Verify Fingerprint");
51+
res1 = FP_M.fpm_compareFinger();
52+
if(res1 == ACK_SUCCESS){
53+
M5.Lcd.println("Success");
54+
M5.Lcd.print("User ID: ");
55+
M5.Lcd.println(FP_M.fpm_getUserId());
56+
}else{
57+
M5.Lcd.println("No Such User");
58+
}
59+
}
60+
}

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"type": "git",
1111
"url": "https://github.com/m5stack/M5StickC.git"
1212
},
13-
"version": "0.2.4",
13+
"version": "0.2.5",
1414
"framework": "arduino",
1515
"platforms": "espressif32"
1616
}

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=M5StickC
2-
version=0.2.4
2+
version=0.2.5
33
author=M5StickC
44
maintainer=M5Stack
55
sentence=Library for M5StickC Core development kit
@@ -8,4 +8,4 @@ category=Device Control
88
url=https://github.com/m5stack/M5StickC.git
99
architectures=esp32
1010
includes=M5StickC.h
11-
depends=UNIT_ENV,Adafruit MCP4725,Adafruit TCS34725,Adafruit NeoPixel,MAX30100lib,FastLED,MFRC522_I2C,M5GFX,M5_BM8563,M5_ADS1100,M5_ADS1115,M5_FPC1020A,HX711 Arduino Library,PCA9554,TinyGPSPlus,Adafruit SGP30 Sensor,FFT,TFTTerminal,ClosedCube TCA9548A,M5GFX,M5_EzData,ArduinoJson
11+
depends=UNIT_ENV,Adafruit MCP4725,Adafruit TCS34725,Adafruit NeoPixel,MAX30100lib,FastLED,MFRC522_I2C,M5GFX,M5_BM8563,M5_ADS1100,M5_ADS1115,M5_FPC1020A,HX711 Arduino Library,PCA9554,TinyGPSPlus,Adafruit SGP30 Sensor,FFT,TFTTerminal,ClosedCube TCA9548A,M5GFX,M5_EzData,ArduinoJson,PubSubClient

0 commit comments

Comments
 (0)