MQTT协议的智能家居之指纹锁

Posted TOPthemaster

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MQTT协议的智能家居之指纹锁相关的知识,希望对你有一定的参考价值。

基于之前的智能家居项目,这次添加了指纹锁设备的设计,硬件端主要使用0.96oled显示屏,ESP8266-NODEMCU,BYJ48步进电机以及配套驱动板,AS608指纹模块。
整个项目除了硬件端,还接入了我自己的服务器,可通过APP进行云端远程操控,对指纹锁进行模式设置和指纹设置。
装置器件图放在下面,以供参考:
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
附上演示视频:

智能家居-基于mqtt协议的APP控制指纹锁装置

代码通过arduino编写,也一并贴在下面

/**
 * 本项目用于AS608指纹模块使用,使用到GPIO5(D1)引脚作为模拟输出TX引脚
 */
#include <SoftwareSerial.h>
#include <FS.h>
#include <U8g2lib.h>                       //U8g2lib库
#include <Wire.h>                          //Wire库
#include <Ticker.h>                        //Ticker库
#include <ESP8266WebServer.h>
#include <WiFiManager.h> 
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#define PRODUCT_ID    "TOP" //服务器用户名
#define API_KEY    "tx6WM==zmW21Z2pt4susBRlHMuY="//用户密码
#define DEVICE_ID "door_device"//设备名
#define TOPIC     "door_control"//订阅主题
//
bool door_clock=false;
//
WiFiClient wifiClient;
int count = 0; //ticker1控制 数据上传下发的间隔时间(s)
PubSubClient mqttClient(wifiClient);
const char* mqttServer = "***********;//服务器地址
const uint16_t mqttPort =****;//mqtt接口端口
char msgJson[75];//存json下发信息数据
char msg_buf[200];//存json上传数据及标识位
SoftwareSerial  mySerial(D3,D4);
const unsigned char welcome[] = {0X30,0X01,0X00,0X30,0X00,0X40,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X7C,0X00,0X00,0XC0,0X9F,0X1F,
0XFF,0X01,0X00,0XFC,0XC0,0X30,0X82,0X00,0X00,0X80,0X60,0X20,0XC2,0X00,0X00,0XC0,
0X20,0X20,0X62,0X00,0X00,0XC0,0X20,0X30,0X3A,0X00,0X00,0X40,0X30,0X10,0X0F,0X00,
0X00,0X40,0X10,0X18,0X03,0X00,0X00,0X40,0X20,0X0C,0X03,0X00,0X00,0X40,0XE0,0X07,
0X03,0X00,0X00,0X00,0XC0,0X01,0X02,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XE0,0X00,0X00,0X00,
0X00,0X00,0XF8,0X7F,0X00,0X00,0X00,0X00,0XFC,0X7F,0X00,0X00,0X00,0X00,0XFE,0XFF,
0X00,0X00,0X00,0X00,0XFF,0XFF,0X01,0X00,0X00,0X00,0XFF,0XFF,0X01,0X00,0X00,0X80,
0XFF,0XFC,0X03,0X00,0X00,0X80,0X1F,0XF0,0X03,0X00,0X00,0XC0,0X1F,0XE0,0X07,0X00,
0X00,0XC0,0X0F,0XC0,0X07,0X00,0X00,0XC0,0X0F,0XC0,0X07,0X00,0X00,0XC0,0X03,0X00,
0X07,0X00,0X00,0XC0,0X01,0X20,0X07,0X00,0X00,0XC0,0X7D,0XFC,0X02,0X00,0X00,0X80,
0X00,0X00,0X02,0X00,0X00,0X00,0X00,0X7A,0X03,0X00,0X00,0X00,0X18,0X10,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X80,0X02,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XE0,0X0F,0X00,0X00,
0X00,0X00,0X82,0X44,0X00,0X00,0X00,0XE0,0X07,0X60,0X00,0X00,0X00,0XF8,0X0F,0XF0,
0X00,0X00,0X00,0XFC,0X37,0XF0,0X03,0X00,0X00,0XFE,0XC7,0XFF,0X07,0X00,0X80,0XFF,
0X07,0XFF,0X1F,0X00,0XC0,0XFD,0X07,0X70,0X3F,0X00,0XF8,0XFF,0X03,0X70,0XFF,0X01,
0XFE,0XFF,0X07,0X70,0XFE,0X0F,0XFF,0XFF,0X09,0X78,0XFE,0X1F,0XFF,0XFF,0X03,0X7C,
0XFC,0X3F,0XFF,0XFF,0X23,0X7E,0XFC,0X3F,0XFF,0XFF,0XC3,0X3F,0XF8,0X7F,0XFF,0XFF,
0X87,0X3F,0XF8,0XFF,0XFF,0XFF,0X07,0X18,0XF8,0XFF,0XFF,0XFF,0X07,0X00,0XF8,0XFF,
0XFF,0XFF,0X07,0X00,0XF8,0XFF,0XFF,0XFF,0X0F,0X00,0XF8,0XFF,0XFF,0XFF,0X3F,0X00,
0XF8,0XFF,0XFF,0XFF,0XDF,0X00,0XF8,0XFF,0XFF,0XFF,0X1F,0X00,0XF8,0XFF,0XFF,0XFF,
0X1F,0X00,0XF0,0XFF,0XFF,0XFF,0X1F,0X00,0XF0,0XFF,0XFF,0XFF,0X1F,0X00,0XF0,0XFF,};
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, SCL, SDA, U8X8_PIN_NONE);//D7 GPIO13 D8 GPIO15// D1 D2
byte buf_getfinger[20]={0xEF,0x01,0xFF,0xFF,0xFF,0xFF,0x01,0x00,0x03,0x01,0x00,0x05};//录一个指纹
byte buf_clear[20]={0xEF,0x01,0xFF,0xFF,0xFF,0xFF,0x01,0x00,0x03,0x0d,0x00,0x11};//清空指纹信息
byte buf_delete[20]={0xEF,0x01,0xFF,0xFF,0xFF,0xFF,0x01,0x00,0x07,0x0c,0x00,0x01,0x11};//删除指纹信息 本条只是模板需要更改
byte buf_finger_char[20]={0xEF,0x01,0xFF,0xFF,0xFF,0xFF,0x01,0x00,0X04,0x02,0x01,0x00,0x08};//生成指纹特征
byte buf_identity[20]={0xEF,0x01,0xFF,0xFF,0xFF,0xFF,0x01,0x00,0X03,0x11,0x00,0x15};//工作模式,读取指纹数据进行比对  返回ID
byte buf_getlist[20]={0xEF,0x01,0xFF,0xFF,0xFF,0xFF,0x01,0x00,0x03,0x1f,0x00,0x23};//获取指纹目录,数据是001010101格式 不是很好用,所以采用了新方法使用(在录入指纹时编写记事本文件存储)
byte buf_add[20]={0xEF,0x01,0xFF,0xFF,0xFF,0xFF,0x01,0x00,0X03,0x10,0x00,0x14};//注册添加指纹 返回ID
byte buf_logo[6]={0xEF,0x01,0xFF,0xFF,0xFF,0xFF};
byte buf[32];
String file_name = "/AS608/users.txt"; //被读取的文件位置和名称
String file_list="";
int work_index=0;
void setup() {
    Serial.begin(57600);
    mySerial.begin(57600);
    //不采用外部按钮控制,不够灵活且占用引脚,改为软件控制,空出的引脚可作为门锁开关电机控制
    /*pinMode(D5,INPUT_PULLUP);//正常工作模式
    pinMode(D6,INPUT_PULLUP);//添加模式
    pinMode(D7,INPUT_PULLUP);//删除模式
    pinMode(D8,INPUT_PULLUP);//查询指纹列表
    digitalWrite(D6,HIGH);
    digitalWrite(D7,HIGH);
    digitalWrite(D5,HIGH);
    digitalWrite(D8,HIGH);*/
    //控制电机
    pinMode(D5,OUTPUT);
    pinMode(D6,OUTPUT);
    pinMode(D7,OUTPUT);
    pinMode(D8,OUTPUT);
     pinMode(D0,OUTPUT);//
    //pinMode(10,OUTPUT);
    //pinMode(9,OUTPUT);
    // WiFi.mode(WIFI_STA);
    WiFiManager wifiManager;
  // 自动连接WiFi。以下语句的参数是连接ESP8266时的WiFi名称
    wifiManager.autoConnect("门锁WiFi设置");
     mqttClient.setServer(mqttServer, mqttPort);
  // 设置MQTT订阅回调函数
  mqttClient.setCallback(receiveCallback);
  connectMQTTServer();
    if(SPIFFS.begin()){ // 启动SPIFFS
    Serial.println("SPIFFS Started.");
  } else {
    Serial.println("SPIFFS Failed to Start.");
  }
   u8g2.begin();
  //允许显示UTF8字符
  u8g2.enableUTF8Print();
  //文本的参考点为左上角
  u8g2.setFontPosTop();
  u8g2.clearBuffer();
  u8g2.drawXBMP(40, 0, 48, 64, welcome);
  u8g2.sendBuffer();
  delay(1000);
}
void connectMQTTServer() {
  String clientId = DEVICE_ID;
  String productId = PRODUCT_ID;
  String apiKey = API_KEY;
  // 连接MQTT服务器
  if (mqttClient.connect(clientId.c_str(), productId.c_str(), apiKey.c_str())) {
    Serial.println("MQTT Server Connected.");
    Serial.println("Server Address: ");
    Serial.println(mqttServer);
    Serial.println("ClientId:");
    Serial.println(clientId);
    subscribeTopic(); // 订阅指定主题
  } else {
    Serial.print("MQTT Server Connect Failed. Client State:");
    Serial.println(mqttClient.state());
    delay(1000);
  }
}
// 订阅指定主题
void subscribeTopic() {
  // 这么做是为确保不同设备使用同一个MQTT服务器测试消息订阅时,所订阅的主题名称不同
  String topicString = TOPIC;
  char subTopic[topicString.length() + 1];
  strcpy(subTopic, topicString.c_str());

  // 通过串口监视器输出是否成功订阅主题以及订阅的主题名称
  if (mqttClient.subscribe(subTopic)) {
    Serial.println("Subscrib Topic:");
    Serial.println(subTopic);
  } else {
    Serial.print("Subscribe Fail...");
  }
}
//获取下发指令topic 指定主题 payload 下发信息,以字节存储 length 下发信息长度
void receiveCallback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message Received [");
  Serial.print(topic);
  Serial.print("] ");
  String receiveMessage;
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
    receiveMessage += (char)payload[i];
  }
  Serial.MQTT协议的智能家居之窗帘机器人与空调

MQTT协议的智能家居之窗帘机器人与空调

海鑫智圣:物联网漫谈之MQTT协议

基于(springboot+vue+mqtt协议)的智能家居系统

基于mqtt通信协议的智能家居控制(更新)

重点springboot + rabbitmq + mqtt协议 做智能家居