2023年7月28日 星期五

實驗 ESP32 + MFRC522 RFID + SQLite + Node-RED 新增模式 自動比對模式

實驗 ESP32 + MFRC522 RFID + SQLite + Node-RED 新增模式 自動比對模式





 




#include <WiFi.h>
#include <PubSubClient.h>
#include <SPI.h>
#include <MFRC522.h>
// GPIO 2 D1
#define LED 2
/* Wiring RFID RC522 module
=============================================================================
GND     = GND   3.3V    = 3.3V
The following table shows the typical pin layout used:
 *             MFRC522      ESP32     Arduino       Arduino   Arduino    Arduino          Arduino
 *             Reader/PCD             Uno/101       Mega      Nano v3    Leonardo/Micro   Pro Micro
 * Signal      Pin          Pin       Pin           Pin       Pin        Pin              Pin
 * -----------------------------------------------------------------------------------------
 * RST/Reset   RST          GPIO27    9             5         D9         RESET/ICSP-5     RST
 * SPI SS      SDA(SS)      GPIO5     10            53        D10        10               10
 * SPI MOSI    MOSI         GPIO23    11 / ICSP-4   51        D11        ICSP-4           16
 * SPI MISO    MISO         GPIO19    12 / ICSP-1   50        D12        ICSP-1           14
 * SPI SCK     SCK          GPIO18    13 / ICSP-3   52        D13        ICSP-3           15
 *
[1] (1, 2) Configurable, typically defined as RST_PIN in sketch/program.
[2] (1, 2) Configurable, typically defined as SS_PIN in sketch/program.
[3] The SDA pin might be labeled SS on some/older MFRC522 boards
=============================================================================
*/

#define SS_PIN  5  // ESP32 pin GIOP5
#define RST_PIN 27 // ESP32 pin GIOP27


// WiFi
const char *ssid = "alex9ufo"; // Enter your Wi-Fi name
const char *password = "alex9981";  // Enter Wi-Fi password

// MQTT Broker
const char *mqtt_broker = "broker.mqtt-dashboard.com";
const char *topic1 = "alex9ufo/esp32/led";
const char *topic2 = "alex9ufo/esp32/RFID";
const char *topic = "alex9ufo/esp32/Starting";
const char *topic3 = "alex9ufo/esp32/led_status";



const char *mqtt_username = "alex9ufo";
const char *mqtt_password = "public";
const int mqtt_port = 1883;

bool ledState = false;
bool atwork = false;

WiFiClient espClient;
PubSubClient client(espClient);
MFRC522 rfid(SS_PIN, RST_PIN); // Create MFRC522 instance

long lastMsg = 0;
long lastMsg1= 0;

char msg[50];
String json = "";
bool Flash = false;  //true
bool Timer = false;  //true
bool Send = false;  //true
int Count= 0;

char jsonChar1[100];  //client.publish("alex9ufo/Esp32/RFID"
//===========================================================
String printHex(byte *buffer, byte bufferSize) {
      String id = "";
      for (byte i = 0; i < bufferSize; i++) {
        id += buffer[i] < 0x10 ? "0" : "";
        id += String(buffer[i], HEX);
      }
      return id;
    }
//===========================================================
void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}    
//===========================================================
void setup() {
    // Set software serial baud to 115200;
    Serial.begin(115200);
    delay(1000); // Delay for stability
    //======================================================
    // Connecting to a WiFi network
    setup_wifi();
    // Setting LED pin as output
    pinMode(LED, OUTPUT);
    digitalWrite(LED, LOW);  // Turn off the LED initially
    //======================================================
    SPI.begin();           // Init SPI bus
    rfid.PCD_Init();    // Init MFRC522

    Serial.println(F("Ready!"));
    Serial.println(F("======================================================"));
    Serial.println("Tap an RFID/NFC tag on the RFID-RC522 reader");


    // Connecting to an MQTT broker
    client.setServer(mqtt_broker, mqtt_port);
    client.setCallback(callback);
    while (!client.connected()) {
        String client_id = "esp32-client-";
        client_id += String(WiFi.macAddress());
        Serial.printf("The client %s connects to the public MQTT broker\n", client_id.c_str());
        if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
            Serial.println("Public HiveMQ MQTT broker (broker.mqtt-dashboard.com) connected");
        } else {
            Serial.print("Failed with state ");
            Serial.print(client.state());
            delay(2000);
        }
    }

    // Publish and subscribe
    client.subscribe(topic1);
    client.publish(topic,"ESP32 at work");
}
//===========================================================
void callback(char *topic, byte *payload, unsigned int length) {
    Serial.print("Message arrived in topic: ");
    Serial.println(topic);
    Serial.print("Message: ");
    String message;
    for (int i = 0; i < length; i++) {
        message += (char) payload[i];  // Convert *byte to string
    }
    Serial.print(message);
    if (message == "on" && !ledState) {
        digitalWrite(LED, HIGH);  // Turn on the LED
        ledState = true;  //ledState = ture HIGH
        Flash = false;
        Timer = false;
        json ="ON";
        Send = true ;
    }
    if (message == "off" && ledState) {
        digitalWrite(LED, LOW); // Turn off the LED
        ledState = false; //ledState = false LOW
        Flash = false;
        Timer = false;
        json ="OFF";
        Send = true ;

    }
    if (message == "flash" ) {
        digitalWrite(LED, LOW); // Turn off the LED
        Flash = true;
        Timer = false;
        json ="FLASH";
        Send = true ;        

    }
    if (message == "timer" ) {
        digitalWrite(LED, LOW); // Turn off the LED
        Flash = false;
        Timer = true;
        json ="TIMER";
        Send = true ;
        Count= 11;
    }

    if (message == "toggle" ) {
        digitalWrite(LED, !digitalRead(LED));   // Turn the LED toggle
        if (digitalRead(LED))
            ledState = true;
        else
            ledState = false;
       
        Flash = false;
        Timer = false;
        json ="TOGGLE";
        Send = true ;        
    }

    Serial.println();
    Serial.println("-----------------------");
}
//===========================================================
void loop() {
    client.loop();
    delay(100); // Delay for a short period in each loop iteration
    if (Flash){
        digitalWrite(LED, !digitalRead(LED));
        delay(500);
        if (digitalRead(LED))
            ledState = true;
        else
            ledState = false;
    } //(Flash)
       
    if (Timer) {
        digitalWrite(LED, HIGH);
        delay(500);
        if (digitalRead(LED))
            ledState = true;
        else
            ledState = false;

        Count=Count-1;
        if (Count == 0 ){
            Timer=false;
            digitalWrite(LED, LOW);
            ledState = false;
        }
    } //(Timer)
   
    if (client.connected()) {        
        if (Send) {
          // Convert JSON string to character array
          json.toCharArray(jsonChar1, json.length()+1);
          Serial.print("Publish message: ");
          Serial.println(json);
          // Publish JSON character array to MQTT topic
          client.publish(topic3,jsonChar1);
        }
        Send = false;    
    }

    long now = millis();
    long now1 = millis();
    if (now1 - lastMsg1 > 80000) {
        lastMsg1 = now1;
        if (atwork) {
            client.publish(topic,"ESP32 at work");
            atwork=false;
        } else {
            client.publish(topic,"             ");
            atwork=true;
        }
     }
     
    if (now - lastMsg > 5000) {
      Serial.println("Reading RFIF card");
      lastMsg = now;
      if (rfid.PICC_IsNewCardPresent()) { // new tag is available
        if (rfid.PICC_ReadCardSerial()) { // NUID has been readed
          MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
          Serial.print("RFID/NFC Tag Type: ");
          Serial.println(rfid.PICC_GetTypeName(piccType));

          // print UID in Serial Monitor in the hex format
          Serial.print("UID:");
          for (int i = 0; i < rfid.uid.size; i++) {
            Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
            Serial.print(rfid.uid.uidByte[i], HEX);
          }
          Serial.println();

          String json = printHex(rfid.uid.uidByte, rfid.uid.size);
          // Convert JSON string to character array
          json.toCharArray(jsonChar1, json.length()+1);
   
          if  (client.connected()) {
            Serial.print("Publish message: ");
            Serial.println(json);
            // Publish JSON character array to MQTT topic
            client.publish(topic2,jsonChar1);
          }    
          rfid.PICC_HaltA(); // halt PICC
          rfid.PCD_StopCrypto1(); // stop encryption on PCD
         
         
        }
      }
       
  }

}
//===========================================================

[{"id":"706b6e9b757aebb8","type":"ui_button","z":"e00ab2f59ea60e48","name":"","group":"85b1efd8d6d7c0dd","order":27,"width":3,"height":1,"passthru":false,"label":"建立資料庫","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"建立資料庫","payloadType":"str","topic":"topic","topicType":"msg","x":90,"y":420,"wires":[["67f9addd869291c7","2b2cdf27a461f498"]]},{"id":"2103b06fa10ff814","type":"ui_button","z":"e00ab2f59ea60e48","name":"","group":"85b1efd8d6d7c0dd","order":30,"width":5,"height":2,"passthru":false,"label":"檢視資料庫資料","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"檢視資料","payloadType":"str","topic":"topic","topicType":"msg","x":100,"y":740,"wires":[["476e761995ad05a1","46fe8b21023dbd8f"]]},{"id":"57e3c58826303f74","type":"ui_button","z":"e00ab2f59ea60e48","name":"","group":"85b1efd8d6d7c0dd","order":25,"width":3,"height":1,"passthru":false,"label":"刪除所有資料","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"刪除所有資料","payloadType":"str","topic":"topic","topicType":"msg","x":100,"y":980,"wires":[["9dc987951e71577d","4886988bd55a3ed0"]]},{"id":"68645501e5220a45","type":"sqlite","z":"e00ab2f59ea60e48","mydb":"dd7f8134e015a1cb","sqlquery":"msg.topic","sql":"","name":"My_RFID_database","x":540,"y":420,"wires":[["bf962fae1f621539","476e761995ad05a1"]]},{"id":"67f9addd869291c7","type":"function","z":"e00ab2f59ea60e48","name":"CREATE DATABASE","func":"\n\n//CREATE TABLE \"RFIDtable\" (\n//\t\"id\"\tINT NOT NULL,\n//  \"uidname\"  TEXT,\n//  \"currentdate\" DATE, \n//  \"currenttime\" TIME\n//\tPRIMARY KEY(\"id\")\n//);\nmsg.topic = \"CREATE TABLE RFIDtable(id INTEGER PRIMARY KEY AUTOINCREMENT, uidname TEXT, currentdate DATE, currenttime TIME)\";\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":420,"wires":[["68645501e5220a45"]]},{"id":"bf962fae1f621539","type":"debug","z":"e00ab2f59ea60e48","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":790,"y":420,"wires":[]},{"id":"ec1d54d7d41770f1","type":"function","z":"e00ab2f59ea60e48","name":"INSERT","func":"var Today = new Date();\nvar yyyy = Today.getFullYear(); //年\nvar MM = Today.getMonth()+1;    //月\nvar dd = Today.getDate();       //日\nvar h = Today.getHours();       //時\nvar m = Today.getMinutes();     //分\nvar s = Today.getSeconds();     //秒\nif(MM<10)\n{\n   MM = '0'+MM;\n}\n\nif(dd<10)\n{\n   dd = '0'+dd;\n}\n\nif(h<10)\n{\n   h = '0'+h;\n}\n\nif(m<10)\n{\n  m = '0' + m;\n}\n\nif(s<10)\n{\n  s = '0' + s;\n}\nvar var_date = yyyy+'/'+MM+'/'+dd;\nvar var_time = h+':'+m+':'+s;\n\nvar myRFID = flow.get('uid_temp');\n\n\nmsg.topic = \"INSERT INTO RFIDtable ( uidname , currentdate, currenttime ) VALUES ($myRFID,  $var_date ,  $var_time ) \" ;\nmsg.payload = [myRFID, var_date , var_time ]\nreturn msg;\n\n//CREATE TABLE \"RFIDtable\" (\n//\t\"id\"\tINT NOT NULL,\n//  \"uidname\"  TEXT,\n//  \"currentdate\" DATE, \n//  \"currenttime\" TIME\n//\tPRIMARY KEY(\"id\")\n//);","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":460,"wires":[["9f604fc4cad58ee9","68645501e5220a45"]]},{"id":"9dc987951e71577d","type":"function","z":"e00ab2f59ea60e48","name":"刪除所有資料","func":"//DELETE from RFIDtable\nmsg.topic = \"DELETE from RFIDtable\";\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":980,"wires":[["11c361088aa44a65"]]},{"id":"11c361088aa44a65","type":"sqlite","z":"e00ab2f59ea60e48","mydb":"dd7f8134e015a1cb","sqlquery":"msg.topic","sql":"","name":"My_RFID_database","x":540,"y":980,"wires":[["1420491c3111a4b9"]]},{"id":"5d4b1f1cbc6fce11","type":"ui_button","z":"e00ab2f59ea60e48","name":"","group":"85b1efd8d6d7c0dd","order":23,"width":3,"height":1,"passthru":false,"label":"刪除資料庫","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"刪除資料庫","payloadType":"str","topic":"topic","topicType":"msg","x":90,"y":1060,"wires":[["0015e46940ed050b","e2d7e93ee5805748"]]},{"id":"0015e46940ed050b","type":"function","z":"e00ab2f59ea60e48","name":"刪除資料庫","func":"//DROP TABLE RFIDtable\nmsg.topic = \"DROP TABLE RFIDtable\";\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":1060,"wires":[["11c361088aa44a65"]]},{"id":"1d9ee8b4e6190689","type":"ui_template","z":"e00ab2f59ea60e48","group":"85b1efd8d6d7c0dd","name":"","order":13,"width":12,"height":8,"format":"<table style=\"width:100%\">\n  <tr>\n    <th>idex</th> \n    <th>database_id</th>\n    <th>UID </th>\n    <th>Date</th>\n    <th>Times</th>\n  </tr>\n  \n  <tr ng-repeat=\"x in msg.payload | limitTo:50\">\n    <td>{{$index}}</td>\n    <td>{{msg.payload[$index].id}}</td>\n    <td>{{msg.payload[$index].uidname}}</td>\n    <td>{{msg.payload[$index].currentdate}}</td>\n    <td>{{msg.payload[$index].currenttime}}</td>\n  </tr>\n</table>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"local","className":"","x":960,"y":1140,"wires":[[]]},{"id":"1420491c3111a4b9","type":"debug","z":"e00ab2f59ea60e48","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":730,"y":980,"wires":[]},{"id":"476e761995ad05a1","type":"function","z":"e00ab2f59ea60e48","name":"檢視資料","func":"\n//CREATE TABLE \"RFIDtable\" (\n//\t\"id\"\tINT NOT NULL,\n//  \"uidname\"  TEXT,\n//  \"currentdate\" DATE, \n//  \"currenttime\" TIME\n//\tPRIMARY KEY(\"id\")\n//);\n\n//SELECT * FROM RFIDtable ORDER BY  id DESC LIMIT 50;\n\nmsg.topic = \"SELECT * FROM RFIDtable ORDER BY id DESC LIMIT 50\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":740,"wires":[["ee4f554f4ec77f7e"]]},{"id":"ee4f554f4ec77f7e","type":"sqlite","z":"e00ab2f59ea60e48","mydb":"dd7f8134e015a1cb","sqlquery":"msg.topic","sql":"","name":"My_RFID_database","x":780.9005737304688,"y":873.5454711914062,"wires":[["1d9ee8b4e6190689"]]},{"id":"3a85e89d3ef728a4","type":"function","z":"e00ab2f59ea60e48","name":"SELECT ALL","func":"var del_idtemp=msg.payload;\nflow.set(\"idtemp\", del_idtemp);\n\nmsg.topic = \"SELECT * FROM RFIDtable \";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":900,"wires":[["11c361088aa44a65","6f162682a89dbd5d"]]},{"id":"3c17f4cb6a2d344c","type":"function","z":"e00ab2f59ea60e48","name":"確認 刪除","func":"var del_id = flow.get(\"idtemp\");\n\n\nmsg.topic = \"DELETE FROM RFIDtable WHERE id= ($del_id) \" ;\nmsg.payload = [del_id]\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":800,"wires":[["caaf5693e66f95e0"]]},{"id":"2fde96e5cbd18edf","type":"ui_numeric","z":"e00ab2f59ea60e48","name":"","label":"刪除的database_id","tooltip":"","group":"85b1efd8d6d7c0dd","order":26,"width":3,"height":1,"wrap":false,"passthru":true,"topic":"topic","topicType":"msg","format":"{{value}}","min":"1","max":"100","step":1,"className":"","x":110,"y":900,"wires":[["3a85e89d3ef728a4","4886988bd55a3ed0"]]},{"id":"caaf5693e66f95e0","type":"sqlite","z":"e00ab2f59ea60e48","mydb":"dd7f8134e015a1cb","sqlquery":"msg.topic","sql":"DELETE FROM dhtreadings WHERE id =  VALUES ($theid)","name":"My_RFID_database","x":500,"y":800,"wires":[["476e761995ad05a1"]]},{"id":"152668e1be54de6a","type":"ui_button","z":"e00ab2f59ea60e48","name":"","group":"85b1efd8d6d7c0dd","order":24,"width":3,"height":1,"passthru":false,"label":"刪除一筆資料","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"刪除一筆資料","payloadType":"str","topic":"topic","topicType":"msg","x":100,"y":800,"wires":[["3c17f4cb6a2d344c","46fe8b21023dbd8f"]]},{"id":"6f162682a89dbd5d","type":"debug","z":"e00ab2f59ea60e48","name":"debug 122","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":900,"wires":[]},{"id":"03942cf08ba0659d","type":"mqtt in","z":"e00ab2f59ea60e48","name":"新增 RFID","topic":"alex9ufo/esp32/RFID","qos":"2","datatype":"auto-detect","broker":"841df58d.ee5e98","nl":false,"rap":true,"rh":0,"inputs":0,"x":80,"y":460,"wires":[["80332a8adf5e0a72"]]},{"id":"9f604fc4cad58ee9","type":"debug","z":"e00ab2f59ea60e48","name":"debug 123","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":460,"wires":[]},{"id":"d874433685ecdedf","type":"ui_text_input","z":"e00ab2f59ea60e48","name":"","label":"新增一筆資料","tooltip":"","group":"85b1efd8d6d7c0dd","order":28,"width":3,"height":1,"passthru":true,"mode":"text","delay":300,"topic":"topic","sendOnBlur":true,"className":"","topicType":"msg","x":320,"y":540,"wires":[[]]},{"id":"d8cb65223db72c0f","type":"ui_audio","z":"e00ab2f59ea60e48","name":"","group":"85b1efd8d6d7c0dd","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":500,"y":620,"wires":[]},{"id":"e3c8ef8b8272c8a1","type":"function","z":"e00ab2f59ea60e48","name":"function ","func":"var temp= msg.payload;\nmsg.payload= \"新增一筆資料\" + temp;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":580,"wires":[["d8cb65223db72c0f","d41ee8d192e9b212"]]},{"id":"46fe8b21023dbd8f","type":"ui_audio","z":"e00ab2f59ea60e48","name":"","group":"85b1efd8d6d7c0dd","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":300,"y":760,"wires":[]},{"id":"2b2cdf27a461f498","type":"ui_audio","z":"e00ab2f59ea60e48","name":"","group":"85b1efd8d6d7c0dd","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":260,"y":380,"wires":[]},{"id":"fe992f6b585971a6","type":"function","z":"e00ab2f59ea60e48","name":"function 43","func":"//SELECT trackid,name FROM \ttracks WHERE name LIKE 'Wild%'\n//Query\n//CREATE TABLE \"RFIDtable\" (\n//\t\"id\"\tINT NOT NULL,\n//  \"uidname\"  TEXT,\n//  \"currentdate\" DATE, \n//  \"currenttime\" TIME\n//\tPRIMARY KEY(\"id\")\n//);\n//msg.topic = \"DELETE FROM RFIDtable WHERE id= ($del_id) \" ;\n\nvar query_uid = flow.get(\"uidtemp\");\nmsg.topic = \"SELECT id,uidname , currentdate,currenttime FROM RFIDtable WHERE uidname LIKE ($query_uid) \";\nmsg.payload = [query_uid]\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":330,"y":1140,"wires":[["5d39ee95b604a2ab","8b19fdfc7527fb1c"]]},{"id":"9dd72eea835f79da","type":"ui_button","z":"e00ab2f59ea60e48","name":"","group":"85b1efd8d6d7c0dd","order":17,"width":3,"height":1,"passthru":false,"label":"比對資料庫","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"比對資料庫","payloadType":"str","topic":"topic","topicType":"msg","x":90,"y":1140,"wires":[["fe992f6b585971a6","e2d7e93ee5805748","68f3d055ed3502fc"]]},{"id":"5d39ee95b604a2ab","type":"sqlite","z":"e00ab2f59ea60e48","mydb":"dd7f8134e015a1cb","sqlquery":"msg.topic","sql":"","name":"My_RFID_database","x":720,"y":1140,"wires":[["1d9ee8b4e6190689"]]},{"id":"6acf78a7006769bf","type":"function","z":"e00ab2f59ea60e48","name":"SELECT ALL","func":"var query_uidtemp=msg.payload;\nflow.set(\"uidtemp\", query_uidtemp);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":490,"y":680,"wires":[["41bf094d339beef6","2eb3dcef5af1360a","9c0209342f6a354d"]]},{"id":"f9ba799cb07e77a8","type":"ui_text_input","z":"e00ab2f59ea60e48","name":"","label":"手動查詢資料的uidname","tooltip":"","group":"85b1efd8d6d7c0dd","order":19,"width":3,"height":1,"passthru":true,"mode":"text","delay":300,"topic":"topic","sendOnBlur":true,"className":"","topicType":"msg","x":130,"y":680,"wires":[["6acf78a7006769bf"]]},{"id":"9463c7ddf98042e2","type":"function","z":"e00ab2f59ea60e48","name":"function ","func":"var query=msg.payload;\n\nif (query===true)\n    msg.payload='Query';\nelse\n    msg.payload='NotQuery';\n\nflow.set(\"query_temp\", msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":1300,"wires":[["e31e80a35e801f90","96c8124f4b81f99b"]]},{"id":"4ce345d0b714527d","type":"ui_switch","z":"e00ab2f59ea60e48","name":"","label":"新增模式  /自動比對模式 ","tooltip":"","group":"85b1efd8d6d7c0dd","order":16,"width":"6","height":1,"passthru":true,"decouple":"false","topic":"topic","topicType":"msg","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"className":"","x":310,"y":1300,"wires":[["9463c7ddf98042e2"]]},{"id":"e31e80a35e801f90","type":"debug","z":"e00ab2f59ea60e48","name":"debug 124","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":1260,"wires":[]},{"id":"0f4d4ba0dfe9870a","type":"switch","z":"e00ab2f59ea60e48","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Create","vt":"str"},{"t":"eq","v":"Query","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":110,"y":600,"wires":[["ec1d54d7d41770f1","d874433685ecdedf","e3c8ef8b8272c8a1","45d2e33fcc4622ee"],["bbdf90e0d7d6353e","d874433685ecdedf","97a43889c254084c"]]},{"id":"80332a8adf5e0a72","type":"function","z":"e00ab2f59ea60e48","name":"function ","func":"var query = flow.get(\"query_temp\");\n\nflow.set(\"uid_temp\", msg.payload);\n\nif (query==='NotQuery')\n    msg.payload='Create'\nelse\n    msg.payload='Query'\n    \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":100,"y":520,"wires":[["0f4d4ba0dfe9870a"]]},{"id":"45d2e33fcc4622ee","type":"debug","z":"e00ab2f59ea60e48","name":"debug 125","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":310,"y":500,"wires":[]},{"id":"96c8124f4b81f99b","type":"ui_audio","z":"e00ab2f59ea60e48","name":"","group":"fe68da70e8d7a3c8","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":680,"y":1300,"wires":[]},{"id":"8b19fdfc7527fb1c","type":"debug","z":"e00ab2f59ea60e48","name":"debug ","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":450,"y":1180,"wires":[]},{"id":"41bf094d339beef6","type":"debug","z":"e00ab2f59ea60e48","name":"debug 127","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":660,"wires":[]},{"id":"bbdf90e0d7d6353e","type":"function","z":"e00ab2f59ea60e48","name":"function","func":"\nvar a= flow.get(\"uid_temp\");\nmsg.payload=a;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":620,"wires":[["6acf78a7006769bf","d8cb65223db72c0f"]]},{"id":"2eb3dcef5af1360a","type":"ui_text","z":"e00ab2f59ea60e48","group":"85b1efd8d6d7c0dd","order":31,"width":6,"height":1,"name":"","label":"自動感應RFID查詢的uid","format":"{{msg.payload}}","layout":"row-left","className":"","x":750,"y":580,"wires":[]},{"id":"9c0209342f6a354d","type":"link out","z":"e00ab2f59ea60e48","name":"link out 10","mode":"link","links":[],"x":675,"y":700,"wires":[]},{"id":"02b54d1571610eec","type":"link in","z":"e00ab2f59ea60e48","name":"link in 12","links":["97a43889c254084c","f316a7347a7ff85a"],"x":215,"y":1180,"wires":[["fe992f6b585971a6"]]},{"id":"d2c8ad4c6ece28af","type":"function","z":"e00ab2f59ea60e48","name":"function ","func":"//CREATE TABLE \"RFIDtable\" (\n//\t\"id\"\tINT NOT NULL,\n//  \"uidname\"  TEXT,\n//  \"currentdate\" DATE, \n//  \"currenttime\" TIME\n//\tPRIMARY KEY(\"id\")\n//);\n\n//msg.topic = \"INSERT INTO RFIDtable ( uidname , currentdate, currenttime ) VALUES ($myRFID,  $var_date ,  $var_time ) \" ;\n//msg.payload = [myRFID, var_date , var_time ]\n//return msg;\n\n\nvar tmp=msg.payload;\nmsg.topic = \"select count( * ) as 總共有幾筆資料 from RFIDtable where uidname=($tmp)\";\nmsg.payload=[tmp];\nreturn msg;\n\n// select count( * ) as 總共有幾筆資料 from Customers where address='Japan'","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":1400,"wires":[["c56638cf7f3edd8f","654f3988cdcc3be6"]]},{"id":"c56638cf7f3edd8f","type":"sqlite","z":"e00ab2f59ea60e48","mydb":"dd7f8134e015a1cb","sqlquery":"msg.topic","sql":"","name":"My_RFID_database","x":640,"y":1400,"wires":[["7c76c34681ca43c6"]]},{"id":"7c76c34681ca43c6","type":"function","z":"e00ab2f59ea60e48","name":"function ","func":"var num=msg.payload[0].總共有幾筆資料;\nmsg.payload=num;\nreturn msg; \n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":760,"y":1440,"wires":[["4666a044c6ade98a","7b7d10207fb5be1c","5d88cef1f333a38a"]]},{"id":"8d339037aa892ef5","type":"ui_text","z":"e00ab2f59ea60e48","group":"85b1efd8d6d7c0dd","order":21,"width":4,"height":1,"name":"","label":"查詢結果","format":"<font face='arial'><font size=3><font color={{fcolor}}>{{msg.payload}}","layout":"row-left","className":"","x":1120,"y":1460,"wires":[]},{"id":"5bde43f098b82330","type":"ui_text","z":"e00ab2f59ea60e48","group":"85b1efd8d6d7c0dd","order":20,"width":4,"height":1,"name":"","label":"查詢結果:筆數","format":"<font face='arial'><font size=6><font color={{fcolor}}>{{msg.payload}}","layout":"row-left","className":"","x":1140,"y":1420,"wires":[]},{"id":"cbc9d4f59cf85636","type":"mqtt in","z":"e00ab2f59ea60e48","name":"","topic":"alex9ufo/esp32/Starting","qos":"2","datatype":"auto-detect","broker":"841df58d.ee5e98","nl":false,"rap":true,"rh":0,"inputs":0,"x":120,"y":1540,"wires":[["e395422ed7b58f1f","b58074554cf5347b","21d604f1ead7e925"]]},{"id":"e395422ed7b58f1f","type":"ui_audio","z":"e00ab2f59ea60e48","name":"","group":"85b1efd8d6d7c0dd","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":360,"y":1580,"wires":[]},{"id":"b58074554cf5347b","type":"debug","z":"e00ab2f59ea60e48","name":"debug 128","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":370,"y":1540,"wires":[]},{"id":"21d604f1ead7e925","type":"change","z":"e00ab2f59ea60e48","name":"","rules":[{"t":"set","p":"fcolor","pt":"msg","to":"red","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":1500,"wires":[["39b3d03a5d07d92b"]]},{"id":"39b3d03a5d07d92b","type":"ui_text","z":"e00ab2f59ea60e48","group":"85b1efd8d6d7c0dd","order":22,"width":4,"height":1,"name":"","label":"","format":"<font face='arial'><font size=4><font color={{fcolor}}>{{msg.payload}}","layout":"row-left","className":"","x":550,"y":1500,"wires":[]},{"id":"654f3988cdcc3be6","type":"debug","z":"e00ab2f59ea60e48","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":590,"y":1360,"wires":[]},{"id":"4666a044c6ade98a","type":"change","z":"e00ab2f59ea60e48","name":"","rules":[{"t":"set","p":"fcolor","pt":"msg","to":"red","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":960,"y":1420,"wires":[["5bde43f098b82330","c87837829cdb2764"]]},{"id":"3966baf94ebdfbe9","type":"change","z":"e00ab2f59ea60e48","name":"","rules":[{"t":"set","p":"fcolor","pt":"msg","to":"red","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":960,"y":1460,"wires":[["8d339037aa892ef5"]]},{"id":"68f3d055ed3502fc","type":"function","z":"e00ab2f59ea60e48","name":"function","func":"\nvar a= flow.get(\"uidtemp\");\nmsg.payload=a;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":280,"y":1400,"wires":[["d2c8ad4c6ece28af"]]},{"id":"97a43889c254084c","type":"link out","z":"e00ab2f59ea60e48","name":"link out 11","mode":"link","links":["fcae8d564a286774","02b54d1571610eec"],"x":195,"y":640,"wires":[]},{"id":"fcae8d564a286774","type":"link in","z":"e00ab2f59ea60e48","name":"link in 13","links":["97a43889c254084c","f316a7347a7ff85a"],"x":155,"y":1360,"wires":[["68f3d055ed3502fc"]]},{"id":"d7c26606738a42a8","type":"inject","z":"e00ab2f59ea60e48","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"2","topic":"false","payload":"false","payloadType":"str","x":110,"y":1300,"wires":[["4ce345d0b714527d"]]},{"id":"a3e482a6dab94145","type":"ui_audio","z":"e00ab2f59ea60e48","name":"","group":"85b1efd8d6d7c0dd","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":1120,"y":1500,"wires":[]},{"id":"c87837829cdb2764","type":"debug","z":"e00ab2f59ea60e48","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1110,"y":1380,"wires":[]},{"id":"0af0e51043722d4d","type":"delay","z":"e00ab2f59ea60e48","name":"","pauseType":"delay","timeout":"3","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":940,"y":1500,"wires":[["a3e482a6dab94145"]]},{"id":"7b7d10207fb5be1c","type":"function","z":"e00ab2f59ea60e48","name":"function ","func":"var n=msg.payload;\nif (n>0)\n    msg.payload='RFID符合';\nelse\n    msg.payload='RFID錯誤';\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":760,"y":1500,"wires":[["0af0e51043722d4d","3966baf94ebdfbe9"]]},{"id":"408e1cdc787670bc","type":"ui_button","z":"e00ab2f59ea60e48","name":"","group":"85b1efd8d6d7c0dd","order":6,"width":2,"height":1,"passthru":false,"label":"LED 開","tooltip":"","color":"white","bgcolor":"","className":"","icon":"fa-circle","payload":"on","payloadType":"str","topic":"","topicType":"str","x":80,"y":60,"wires":[["12e722168f45dfbd","5ea3c8758d0c7e22","462f5e9a0ccb3cc4"]]},{"id":"2f29aeee52ae7b68","type":"ui_button","z":"e00ab2f59ea60e48","name":"","group":"85b1efd8d6d7c0dd","order":7,"width":2,"height":1,"passthru":false,"label":"LED 關","tooltip":"","color":"black","bgcolor":"","className":"","icon":"fa-circle-o","payload":"off","payloadType":"str","topic":"","topicType":"str","x":80,"y":100,"wires":[["12e722168f45dfbd","5ea3c8758d0c7e22","462f5e9a0ccb3cc4"]]},{"id":"12e722168f45dfbd","type":"mqtt out","z":"e00ab2f59ea60e48","name":"Control LED","topic":"alex9ufo/esp32/led","qos":"1","retain":"true","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"70940176.2b2d3","x":330,"y":60,"wires":[]},{"id":"fdffa055cd552c2e","type":"comment","z":"e00ab2f59ea60e48","name":"Node-Red publish 到 HiveMQ Broker  , ESP32訂閱","info":"將 alex9ufo/inTopic 到(publish)HiveMQ Broker \n給 Arduino 訂閱(Subscribe)","x":210,"y":20,"wires":[]},{"id":"5ea3c8758d0c7e22","type":"ui_audio","z":"e00ab2f59ea60e48","name":"","group":"85b1efd8d6d7c0dd","voice":"zh-TW","always":true,"x":320,"y":200,"wires":[]},{"id":"462f5e9a0ccb3cc4","type":"ui_text","z":"e00ab2f59ea60e48","group":"85b1efd8d6d7c0dd","order":14,"width":0,"height":0,"name":"","label":"Node-RED發行到MQTT的資料","format":"{{msg.payload}}","layout":"row-left","className":"","x":390,"y":140,"wires":[]},{"id":"1af992d593a7d9b3","type":"ui_audio","z":"e00ab2f59ea60e48","name":"","group":"85b1efd8d6d7c0dd","voice":"en-US","always":"","x":560,"y":300,"wires":[]},{"id":"681c30b4a71aab24","type":"mqtt in","z":"e00ab2f59ea60e48","name":"LED status ","topic":"alex9ufo/esp32/led_status","qos":"2","datatype":"utf8","broker":"841df58d.ee5e98","nl":false,"rap":true,"rh":0,"inputs":0,"x":80,"y":300,"wires":[["84006085b93c7fa5"]]},{"id":"0a435887a60c37d1","type":"comment","z":"e00ab2f59ea60e48","name":"Node-Red  subscribe HiveMQ Broker  , ESP32發行","info":"將 alex9ufo/inTopic 到(publish)HiveMQ Broker \n給 Arduino 訂閱(Subscribe)","x":200,"y":260,"wires":[]},{"id":"211c5c17a18eeca3","type":"ui_text","z":"e00ab2f59ea60e48","group":"85b1efd8d6d7c0dd","order":15,"width":0,"height":0,"name":"","label":"Node-RED 訂閱MQTT的資料","format":"{{msg.payload}}","layout":"row-left","className":"","x":480,"y":340,"wires":[]},{"id":"b941d45014c54970","type":"ui_button","z":"e00ab2f59ea60e48","name":"","group":"85b1efd8d6d7c0dd","order":8,"width":3,"height":1,"passthru":false,"label":"LED 閃爍","tooltip":"","color":"black","bgcolor":"","className":"","icon":"fa-circle-o","payload":"flash","payloadType":"str","topic":"","topicType":"str","x":80,"y":140,"wires":[["12e722168f45dfbd","462f5e9a0ccb3cc4","5ea3c8758d0c7e22"]]},{"id":"ca019f3e91991a28","type":"ui_button","z":"e00ab2f59ea60e48","name":"","group":"85b1efd8d6d7c0dd","order":9,"width":2,"height":1,"passthru":false,"label":"LED反向","tooltip":"","color":"black","bgcolor":"","className":"","icon":"fa-circle-o","payload":"toggle","payloadType":"str","topic":"","topicType":"str","x":80,"y":180,"wires":[["12e722168f45dfbd","462f5e9a0ccb3cc4","5ea3c8758d0c7e22"]]},{"id":"2a9ec4a94bc9a22d","type":"ui_button","z":"e00ab2f59ea60e48","name":"","group":"85b1efd8d6d7c0dd","order":10,"width":3,"height":1,"passthru":false,"label":"LED 定時5秒","tooltip":"","color":"black","bgcolor":"","className":"","icon":"fa-circle-o","payload":"timer","payloadType":"str","topic":"","topicType":"str","x":90,"y":220,"wires":[["12e722168f45dfbd","462f5e9a0ccb3cc4","5ea3c8758d0c7e22"]]},{"id":"84006085b93c7fa5","type":"function","z":"e00ab2f59ea60e48","name":"function 48","func":"msg.payload=\" ---ESP32回來資料---\" +msg.payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":300,"wires":[["211c5c17a18eeca3","30e1509a90e5f6ea"]]},{"id":"30e1509a90e5f6ea","type":"delay","z":"e00ab2f59ea60e48","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":420,"y":300,"wires":[["1af992d593a7d9b3"]]},{"id":"d41ee8d192e9b212","type":"function","z":"e00ab2f59ea60e48","name":"function 49","func":"var myRFID = flow.get('uid_temp');\nmsg.payload=myRFID;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":580,"wires":[["2eb3dcef5af1360a"]]},{"id":"e2d7e93ee5805748","type":"ui_audio","z":"e00ab2f59ea60e48","name":"","group":"85b1efd8d6d7c0dd","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":300,"y":1100,"wires":[]},{"id":"4886988bd55a3ed0","type":"ui_audio","z":"e00ab2f59ea60e48","name":"","group":"85b1efd8d6d7c0dd","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":300,"y":940,"wires":[]},{"id":"5d88cef1f333a38a","type":"debug","z":"e00ab2f59ea60e48","name":"debug 129","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":890,"y":1320,"wires":[]},{"id":"85b1efd8d6d7c0dd","type":"ui_group","name":"ESP32","tab":"eeb8e179.a47a4","order":2,"disp":true,"width":"13","collapse":false,"className":""},{"id":"dd7f8134e015a1cb","type":"sqlitedb","db":"C:\\Users\\User\\.node-red\\my_2023rfid.db","mode":"RWC"},{"id":"841df58d.ee5e98","type":"mqtt-broker","name":"","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"fe68da70e8d7a3c8","type":"ui_group","name":"2023 RFID Home","tab":"9c33f04edb058032","order":3,"disp":true,"width":"14","collapse":false,"className":""},{"id":"70940176.2b2d3","type":"mqtt-broker","name":"","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"eeb8e179.a47a4","type":"ui_tab","name":"2023 RFID Database","icon":"dashboard","disabled":false,"hidden":false},{"id":"9c33f04edb058032","type":"ui_tab","name":"2023-07 Home","icon":"dashboard","disabled":false,"hidden":false}]

沒有留言:

張貼留言

Messaging API作為替代方案

  LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案? LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。 //CHANNEL_ACCESS_TOKEN = 'Messaging ...