diff --git a/examples/Advanced/MQTT/MQTT.ino b/examples/Advanced/MQTT/MQTT.ino new file mode 100644 index 00000000..061ebac4 --- /dev/null +++ b/examples/Advanced/MQTT/MQTT.ino @@ -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 +#include + +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); + } + } +} diff --git a/examples/Unit/FINGER_FPC1020A/FINGER_FPC1020A.ino b/examples/Unit/FINGER_FPC1020A/FINGER_FPC1020A.ino index 623e72e7..bd2e0dee 100644 --- a/examples/Unit/FINGER_FPC1020A/FINGER_FPC1020A.ino +++ b/examples/Unit/FINGER_FPC1020A/FINGER_FPC1020A.ino @@ -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 -#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(); } diff --git a/examples/Unit/FINGER_FPC1020A/finger.cpp b/examples/Unit/FINGER_FPC1020A/finger.cpp deleted file mode 100644 index 95ddce4c..00000000 --- a/examples/Unit/FINGER_FPC1020A/finger.cpp +++ /dev/null @@ -1,234 +0,0 @@ -/* - 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. -*/ - -#include -#include "finger.h" - - -FingerPrint::FingerPrint(void){ - -} - -FingerPrint FP; - -#define F_Time 10000 - -uint8_t FingerPrint::fpm_sendAndReceive(uint16_t timeout) -{ - uint8_t i, j; - uint8_t checkSum = 0; - - FP.RxCnt = 0; - FP.TxBuf[5] = 0; - - Serial2.write(CMD_HEAD); - for (i = 1; i < 6; i++) - { - Serial2.write(FP.TxBuf[i]); - checkSum ^= FP.TxBuf[i]; - } - Serial2.write(checkSum); - Serial2.write(CMD_TAIL); - - while ((!Serial2.available()) && timeout > 0) - { - delay(1); - timeout--; - } - - uint8_t ch; - for(i=0;i<8;i++) - { - if(Serial2.available()){ - ch = Serial2.read(); - FP.RxCnt++; - FP.RxBuf[i] = ch; - } - } - - if (FP.RxCnt != 8) {FP.RxCnt = 0;return ACK_TIMEOUT;} - if (FP.RxBuf[HEAD] != CMD_HEAD) return ACK_FAIL; - if (FP.RxBuf[TAIL] != CMD_TAIL) return ACK_FAIL; - if (FP.RxBuf[CMD] != (FP.TxBuf[CMD])) return ACK_FAIL; - - checkSum = 0; - for (j = 1; j < CHK; j++) { - checkSum ^= FP.RxBuf[j]; - } - if (checkSum != FP.RxBuf[CHK]) { - return ACK_FAIL; - } - return ACK_SUCCESS; -} - -uint8_t FingerPrint::fpm_sleep(void) -{ - uint8_t res; - - FP.TxBuf[CMD] = CMD_SLEEP_MODE; - FP.TxBuf[P1] = 0; - FP.TxBuf[P2] = 0; - FP.TxBuf[P3] = 0; - - res = fpm_sendAndReceive(500); - - if(res == ACK_SUCCESS) { - return ACK_SUCCESS; - } - else { - return ACK_FAIL; - } - -} - -uint8_t FingerPrint::fpm_setAddMode(uint8_t fpm_mode) -{ - uint8_t res; - - FP.TxBuf[CMD] = CMD_ADD_MODE; - FP.TxBuf[P1] = 0; - FP.TxBuf[P2] = fpm_mode; - FP.TxBuf[P3] = 0; - - res = fpm_sendAndReceive(200); - - if(res == ACK_SUCCESS && RxBuf[Q3] == ACK_SUCCESS) { - return ACK_SUCCESS; - } - else { - return ACK_FAIL; - } -} - -uint8_t FingerPrint::fpm_readAddMode(void) -{ - FP.TxBuf[CMD] = CMD_ADD_MODE; - FP.TxBuf[P1] = 0; - FP.TxBuf[P2] = 0; - FP.TxBuf[P3] = 0X01; - - fpm_sendAndReceive(200); - - return RxBuf[Q2]; -} - -uint16_t FingerPrint::fpm_getUserNum(void) -{ - uint8_t res; - - FP.TxBuf[CMD] = CMD_USER_CNT; - FP.TxBuf[P1] = 0; - FP.TxBuf[P2] = 0; - FP.TxBuf[P3] = 0; - - res = fpm_sendAndReceive(200); - - if(res == ACK_SUCCESS && FP.RxBuf[Q3] == ACK_SUCCESS) { - return FP.RxBuf[Q2]; - } - else { - return 0XFF; - } - -} - -uint8_t FingerPrint::fpm_deleteAllUser(void) -{ - uint8_t res; - - FP.TxBuf[CMD] = CMD_DEL_ALL; - FP.TxBuf[P1] = 0; - FP.TxBuf[P2] = 0; - FP.TxBuf[P3] = 0; - - res = fpm_sendAndReceive(200); - - if(res == ACK_SUCCESS && RxBuf[Q3] == ACK_SUCCESS) { - return ACK_SUCCESS; - } - else { - return ACK_FAIL; - } -} - -uint8_t FingerPrint::fpm_deleteUser(uint8_t userNum) -{ - uint8_t res; - - FP.TxBuf[CMD] = CMD_DEL; - FP.TxBuf[P1] = 0; - FP.TxBuf[P2] = userNum; - FP.TxBuf[P3] = 0; - - res = fpm_sendAndReceive(200); - - if(res == ACK_SUCCESS && RxBuf[Q3] == ACK_SUCCESS) { - return ACK_SUCCESS; - } - else { - return ACK_FAIL; - } -} - -uint8_t FingerPrint::fpm_addUser(uint8_t userNum, uint8_t userPermission) -{ - uint8_t res; - - FP.TxBuf[CMD] = CMD_ADD_1; - FP.TxBuf[P1] = 0; - FP.TxBuf[P2] = userNum; - FP.TxBuf[P3] = userPermission; - - res = fpm_sendAndReceive(F_Time); - - if(res == ACK_SUCCESS) { - if(FP.RxBuf[Q3] == ACK_SUCCESS) { - FP.TxBuf[CMD] = CMD_ADD_2; - - res = fpm_sendAndReceive(F_Time); - - if(res == ACK_SUCCESS) { - if(FP.RxBuf[Q3] == ACK_SUCCESS) { - FP.TxBuf[CMD] = CMD_ADD_3; - - res = fpm_sendAndReceive(F_Time); - - if(res == ACK_SUCCESS) { - return FP.RxBuf[Q3]; - } - } - } - } - } - return res; - -} - -uint8_t FingerPrint::fpm_compareFinger(void) -{ - uint8_t res; - - FP.TxBuf[CMD] = CMD_MATCH; - FP.TxBuf[P1] = 0; - FP.TxBuf[P2] = 0; - FP.TxBuf[P3] = 0; - - res = fpm_sendAndReceive(8000); - - if(res == ACK_SUCCESS) - { - if(FP.RxBuf[Q3] == ACK_NOUSER) { - return ACK_NOUSER; - } - if(FP.RxBuf[Q3] == ACK_TIMEOUT) { - return ACK_TIMEOUT; - } - if((FP.RxBuf[Q2] != 0) && (FP.RxBuf[Q3] == 1 || FP.RxBuf[Q3] == 2 || FP.RxBuf[Q3] == 3)) { - return ACK_SUCCESS; - } - } - return res; -} - - diff --git a/examples/Unit/FINGER_FPC1020A/finger.h b/examples/Unit/FINGER_FPC1020A/finger.h deleted file mode 100644 index dee89e75..00000000 --- a/examples/Unit/FINGER_FPC1020A/finger.h +++ /dev/null @@ -1,77 +0,0 @@ -#ifndef __TFS_M64_H -#define __TFS_M64_H - -#define TRUE 1 -#define FALSE 0 - -#define ACK_SUCCESS 0x00 -#define ACK_FAIL 0x01 -#define ACK_FULL 0x04 -#define ACK_NOUSER 0x05 -#define ACK_USER_EXIST 0x07 -#define ACK_TIMEOUT 0x08 - -#define ACK_GO_OUT 0x0F - -#define ACK_ALL_USER 0x00 -#define ACK_GUEST_USER 0x01 -#define ACK_NORMAL_USER 0x02 -#define ACK_MASTER_USER 0x03 - -#define USER_MAX_CNT 50 - -#define HEAD 0 -#define CMD 1 -#define CHK 6 -#define TAIL 7 - -#define P1 2 -#define P2 3 -#define P3 4 -#define Q1 2 -#define Q2 3 -#define Q3 4 - -#define CMD_HEAD 0xF5 -#define CMD_TAIL 0xF5 -#define CMD_ADD_1 0x01 -#define CMD_ADD_2 0x02 -#define CMD_ADD_3 0x03 -#define CMD_MATCH 0x0C -#define CMD_DEL 0x04 -#define CMD_DEL_ALL 0x05 -#define CMD_USER_CNT 0x09 -#define CMD_SLEEP_MODE 0x2C -#define CMD_ADD_MODE 0x2D - -#define CMD_FINGER_DETECTED 0x14 - - -class FingerPrint { - - public: - FingerPrint(void); - uint8_t fpm_sendAndReceive(uint16_t delayMs); - uint8_t fpm_sleep(void); - uint8_t fpm_setAddMode(uint8_t fpm_mode); - uint8_t fpm_readAddMode(void); - uint16_t fpm_getUserNum(void); - uint8_t fpm_deleteAllUser(void); - uint8_t fpm_deleteUser(uint8_t userNum); - uint8_t fpm_addUser(uint8_t userNum, uint8_t userPermission); - uint8_t fpm_compareFinger(void); - public: - uint8_t TxBuf[9]; - uint8_t RxBuf[9]; - uint8_t RxCnt; - private: - - private: - -}; - - -#endif /* __TFS-M64_H */ - - - diff --git a/library.json b/library.json index 3937508a..30d701e4 100644 --- a/library.json +++ b/library.json @@ -10,7 +10,7 @@ "type": "git", "url": "https://github.com/m5stack/m5stack.git" }, - "version": "0.3.6", + "version": "0.3.7", "framework": "arduino", "platforms": "espressif32" } diff --git a/library.properties b/library.properties index e3bb4164..f9418f6d 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=M5Stack -version=0.3.6 +version=0.3.7 author=M5Stack maintainer=M5Stack sentence=Library for M5Stack Core development kit @@ -8,4 +8,5 @@ category=Device Control url=https://github.com/m5stack/m5stack architectures=esp32 includes=M5Stack.h -depends=M5GFX,ESP32CAN,UNIT_ENV,UNIT_4RELAY,ADXL345,FastLED,MODULE_GRBL13.2,Adafruit MCP4725,Adafruit TCS34725,Adafruit NeoPixel,MAX30100lib,MFRC522_I2C,M5_BM8563,M5_ADS1100,M5_ADS1115,M5_FPC1020A,HX711 Arduino Library,PCA9554,TinyGPSPlus,Adafruit SGP30 Sensor,FFT,TFTTerminal,ClosedCube TCA9548A,M5GFX,ArduinoJson,M5_EzData \ No newline at end of file +depends=M5GFX,ESP32CAN,UNIT_ENV,UNIT_4RELAY,ADXL345,FastLED,MODULE_GRBL13.2,Adafruit MCP4725,Adafruit TCS34725,Adafruit NeoPixel,MAX30100lib,MFRC522_I2C,M5_BM8563,M5_ADS1100,M5_ADS1115,M5_FPC1020A,HX711 Arduino Library,PCA9554,TinyGPSPlus,Adafruit SGP30 Sensor,FFT,TFTTerminal,ClosedCube TCA9548A,M5GFX,ArduinoJson,M5_EzData,PubSubClient +