RFID 實驗2-2 Node-RED , MQTT , ESP32 MFRC522 (Auto connected WIFI 更新)
Arduino程式
#include <WiFi.h>
#include <PubSubClient.h>
#include <SPI.h>
#include "MFRC522.h"
//=========================================
#include <AutoConnect.h>
#include <WebServer.h>
WebServer server;
AutoConnect Portal(server);
//=========================================
const int RST_PIN = 22; // Reset pin
const int SS_PIN = 21; // Slave select pin
//=========================================
//esp32 mfrc522
//21 SDA
//18 SCK
//23 MOSI
//21 MISO
//22 RST
//GND GND
//3.3v 3.3V
//==========================
#define BUILTIN_LED 2
// Update these with values suitable for your network.
#define MQTTid "" //id of this mqtt client
#define MQTTip "broker.mqtt-dashboard.com" //ip address or hostname of the mqtt broker
#define MQTTport 1883 //port of the mqtt broker
#define MQTTuser "alex9ufo" //username of this mqtt client
#define MQTTpsw "alex1234" //password of this mqtt client
//#define MQTTuser "your_username" //username of this mqtt client
//#define MQTTpsw "your_password" //password of this mqtt client
#define MQTTpubQos 2 //qos of publish (see README)
#define MQTTsubQos 1 //qos of subscribe
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
//Variables
long lastMsg = 0;
String IDNo_buf=""; //client.publish("alex9ufo/outTopic/RFID/json"
char jsonChar1[100];
String json = ""; //client.publish("alex9ufo/led/led_status",
char jsonChar2[100];
bool Flash = false; //true
bool Timer = false; //true
bool Send = false; //true
int Count= 0;
//=============================================================================
boolean pendingDisconnect = false;
void mqttConnectedCb(); // on connect callback
void mqttDisconnectedCb(); // on disconnect callback
void mqttDataCb(char* topic, byte* payload, unsigned int length); // on new message callback
WiFiClient wclient;
PubSubClient client(MQTTip, MQTTport, mqttDataCb, wclient);
//======================================================
void rootPage() {
char content[] = "Hello, world";
server.send(200, "text/plain", content);
}
//=======================================================
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 mqttConnectedCb() {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("alex9ufo/outTopic/RFID/json", jsonChar1, MQTTpubQos, true); // true means retain
// Once connected, publish an announcement...
client.publish("alex9ufo/led/led_status", jsonChar2, MQTTpubQos, true); // true means retain
// ... and resubscribe
client.subscribe("alex9ufo/inTopic/led/led_event", MQTTsubQos);
}
//=======================================================
void mqttDisconnectedCb() {
Serial.println("disconnected");
}
//=======================================================
void mqttDataCb(char* topic, byte* payload, unsigned int length) {
/*
you can convert payload to a C string appending a null terminator to it;
this is possible when the message (including protocol overhead) doesn't
exceeds the MQTT_MAX_PACKET_SIZE defined in the library header.
you can consider safe to do so when the length of topic plus the length of
message doesn't exceeds 115 characters
*/
char* message = (char *) payload;
message[length] = 0;
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
Serial.println(message);
String s = message;
s.trim();
// Switch on the LED if an 1 was received as first character
if (s == "OFF") {
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
Serial.println("Received OFF , Send LOW TO BuildIn_LED");
Flash = false;
Timer = false;
json ="OFF";
Send = true ;
} // if (s == "OFF")
if (s == "ON") {
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off (Note that HIGH is the voltage level
// but actually the LED is on; this is because
Serial.println("Received ON , Send HIGH TO BuildIn_LED");
Flash = false;
Timer = false;
json ="ON";
Send = true ;
} //if (s == "ON")
if (s == "TOGGLE") {
digitalWrite(BUILTIN_LED, !digitalRead(BUILTIN_LED)); // Turn the LED toggle
// but actually the LED is on; this is because
Serial.println("Received TOGGLE , Send Toggle(H->L , L->H) TO BuildIn_LED");
Flash = false;
Timer = false;
json ="TOGGLE";
Send = true ;
} //if (s == "TOGGLE")
if (s == "FLASH") {
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off (Note that HIGH is the voltage level
// but actually the LED is on; this is because
Serial.println("Received FLASH , Flashing BuildIn_LED ");
Flash = true;
Timer = false;
json ="FLASH";
Send = true ;
} //if (message[0] == 'FLASH')
if (s == "TIMER") {
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off (Note that HIGH is the voltage level
// but actually the LED is on; this is because
Serial.println("Received TIMER , BuildIn_LED ON 5 SEC");
Flash = false;
Timer = true;
Count= 10;
json ="TIMER";
Send = true ;
} //if (message[0] == 'TIMER')
}
//======================================================
void setup() {
Serial.begin(115200);
Serial.println("Configuring ESP32...");
pinMode(BUILTIN_LED, OUTPUT);
Serial.println(F("Booting...."));
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
//=================================================
Serial.println("Configuring your Mobile WiFi to esp32ap...");
Serial.println("Configuring another WiFi SSID,PWD...");
server.on("/", rootPage);
if (Portal.begin()) {
Serial.println("HTTP server:" + WiFi.localIP().toString());
}
//===================================================
Serial.println(F("Ready!"));
Serial.println(F("Control Build LED ON,OFF,FLASH,TOGGLE,TIMER...."));
Serial.println(F("======================================================"));
Serial.println(F("Scan for Card and print UID:"));
}
//======================================================
void process_mqtt() {
if (WiFi.status() == WL_CONNECTED) {
if (client.connected()) {
client.loop();
} else {
// client id, client username, client password, last will topic, last will qos, last will retain, last will message
if (client.connect(MQTTid, MQTTuser, MQTTpsw, MQTTid "/status", 2, true, "0")) {
pendingDisconnect = false;
mqttConnectedCb();
}
}
} else {
if (client.connected())
client.disconnect();
}
if (!client.connected() && !pendingDisconnect) {
pendingDisconnect = true;
mqttDisconnectedCb();
}
}
//======================================================
void loop() {
process_mqtt();
long now = millis();
if (Flash)
{
digitalWrite(BUILTIN_LED, !digitalRead(BUILTIN_LED));
delay(500);
}
if (Timer)
{
digitalWrite(BUILTIN_LED, HIGH);
delay(500);
Count=Count-1;
if (Count == 0 ){
Timer=false;
digitalWrite(BUILTIN_LED, LOW);
}
}
if (Send) {
// Convert JSON string to character array
json.toCharArray(jsonChar2, json.length()+1);
if (client.connected()) {
Serial.print("Publish message: ");
Serial.println(json);
// Publish JSON character array to MQTT topic
client.publish("alex9ufo/led/led_status",jsonChar2);
}
Send = false;
}
if (WiFi.status() == WL_CONNECTED) {
//========Auto Connect===============================
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) { // 如果出現新卡片就讀取卡片資料
delay(100);
String IDNo = printHex(mfrc522.uid.uidByte, mfrc522.uid.size);
// Show some details of the PICC (that is: the tag/card)
if ((IDNo != IDNo_buf) || (now - lastMsg > 3000)) { //不同卡片 或是 等3秒
lastMsg = now;
Serial.print(F("Card UID:"));
Serial.println(IDNo);
//Serial.println(IDNo_buf);
IDNo_buf="";
IDNo_buf=IDNo;
// Convert data to JSON string
String json1 =
"{\"data\":{"
"\"RFID_No\": \"" + IDNo + "\"}"
"}";
// Convert JSON string to character array
json1.toCharArray(jsonChar1, json1.length()+1);
if (client.connected()) {
Serial.print("Publish message: ");
Serial.println(json1);
// Publish JSON character array to MQTT topic
client.publish("alex9ufo/outTopic/RFID/json",jsonChar1);
}
} // if ((IDNo != IDNo_buf) || (now - lastMsg > 5000))
} // if (mfrc522.PICC_IsNewCardPresent()
} //========Auto Connect===============================
Portal.handleClient();
} //Loop
//=========================================================
Node-Red程式
[{"id":"7ca7ba65.e3eae4","type":"mqtt in","z":"aec35ebb.84d4a","name":"RFID MQTT","topic":"alex9ufo/outTopic/RFID/json","qos":"2","datatype":"auto","broker":"e4d9b72d.d14398","x":110,"y":60,"wires":[["ec1b040b.7b68f8","4d6d0edf.2de43","950f089e.98c2a8","674e8bc2.a736e4"]]},{"id":"950f089e.98c2a8","type":"debug","z":"aec35ebb.84d4a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":350,"y":60,"wires":[]},{"id":"ec1b040b.7b68f8","type":"function","z":"aec35ebb.84d4a","name":"json+時分秒","func":"var date = new Date();\nvar h = date.getHours();\nvar m = date.getMinutes();\nvar s = date.getSeconds();\nif(h<10){\n h = '0'+h;\n}\nif(m<10){\n m = '0' + m;\n}\nif(s<10){\n s = '0' + s;\n}\nmsg.payload = 'Time:(' + h + ':' + m + ':' + s + ')'+ msg.payload ;\nreturn msg;\n","outputs":1,"noerr":0,"x":350,"y":100,"wires":[["ce4bb36e.f0bae"]]},{"id":"ce4bb36e.f0bae","type":"ui_text","z":"aec35ebb.84d4a","group":"ac0f1141.eb50e","order":0,"width":0,"height":0,"name":"","label":"MQTT收到RFID訊息-->","format":"{{msg.payload}}","layout":"row-left","x":570,"y":100,"wires":[]},{"id":"4d6d0edf.2de43","type":"switch","z":"aec35ebb.84d4a","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"966aa5f4","vt":"str"},{"t":"cont","v":"7021ed10","vt":"str"},{"t":"cont","v":"96928cf4","vt":"str"},{"t":"cont","v":"b68b19f5","vt":"str"},{"t":"neq","v":"","vt":"str"}],"checkall":"true","repair":false,"outputs":5,"x":130,"y":220,"wires":[["7b51c354.54ebcc"],["6894eabe.fa65b4"],["c80c472d.a310a8"],["ffa9cb24.e8d968"],["a1102d13.69b74"]]},{"id":"7b51c354.54ebcc","type":"function","z":"aec35ebb.84d4a","name":"Alex9ufo卡片","func":"msg.payload = 'Alex9ufo Card'\nreturn msg;","outputs":1,"noerr":0,"x":310,"y":140,"wires":[["9ac29315.ee87f"]]},{"id":"e5092603.335138","type":"ui_button","z":"aec35ebb.84d4a","name":"","group":"ac0f1141.eb50e","order":0,"width":"3","height":"1","passthru":false,"label":"語音測試","tooltip":"","color":"white","bgcolor":"orange","icon":"fa-circle","payload":"語音測試","payloadType":"str","topic":"","x":500,"y":260,"wires":[["9a73ac08.bb98e"]]},{"id":"9a73ac08.bb98e","type":"ui_text_input","z":"aec35ebb.84d4a","name":"","label":"","tooltip":"","group":"ac0f1141.eb50e","order":9,"width":0,"height":0,"passthru":true,"mode":"text","delay":"0","topic":"","x":660,"y":280,"wires":[["173f1caf.f07fe3","dacf4f4b.91266"]]},{"id":"dacf4f4b.91266","type":"debug","z":"aec35ebb.84d4a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":890,"y":220,"wires":[]},{"id":"173f1caf.f07fe3","type":"ui_audio","z":"aec35ebb.84d4a","name":"播放聲音","group":"ac0f1141.eb50e","voice":"zh-TW","always":true,"x":880,"y":260,"wires":[]},{"id":"ee949fd5.b6d52","type":"ui_button","z":"aec35ebb.84d4a","name":"Alex9ufo","group":"ac0f1141.eb50e","order":2,"width":"3","height":"1","passthru":false,"label":"手動插入測試資料","tooltip":"","color":"","bgcolor":"","icon":"","payload":"{\"RFIDNo\":\"Alex9ufo測試\"}","payloadType":"json","topic":"","x":100,"y":540,"wires":[["cf201a65.cc7d68"]]},{"id":"cf201a65.cc7d68","type":"mqtt out","z":"aec35ebb.84d4a","name":"","topic":"alex9ufo/outTopic/RFID/json","qos":"","retain":"","broker":"e4d9b72d.d14398","x":340,"y":540,"wires":[]},{"id":"a672cacc.25a3a8","type":"inject","z":"aec35ebb.84d4a","name":"CREATE DB","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"CREATE TABLE RFIDtable( id INT PRIMARY KEY NOT NULL, currenttime TEXT , uidname TEXT)","payload":"","payloadType":"date","x":605,"y":436,"wires":[["766d807e.0f878"]]},{"id":"9cca2e6c.7b4d3","type":"ui_button","z":"aec35ebb.84d4a","name":"","group":"ac0f1141.eb50e","order":10,"width":"3","height":"1","passthru":false,"label":"檢視RFID資料庫","tooltip":"","color":"","bgcolor":" purple","icon":"","payload":"檢視資料庫","payloadType":"str","topic":"SELECT * FROM RFIDtable ORDER BY id DESC LIMIT 100;","x":605,"y":396,"wires":[["766d807e.0f878"]]},{"id":"e458cbbd.dc0bf8","type":"ui_button","z":"aec35ebb.84d4a","name":"","group":"ac0f1141.eb50e","order":11,"width":"3","height":"1","passthru":false,"label":"建立RFID資料庫(只能執行一次)","tooltip":"","color":"","bgcolor":" purple","icon":"","payload":"","payloadType":"str","topic":"CREATE TABLE RFIDtable( id INT PRIMARY KEY NOT NULL, currenttime TEXT , uidname TEXT)","x":645,"y":356,"wires":[["766d807e.0f878"]]},{"id":"cccd9168.f1c4","type":"inject","z":"aec35ebb.84d4a","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":340,"wires":[["a1102d13.69b74"]]},{"id":"a1102d13.69b74","type":"function","z":"aec35ebb.84d4a","name":"卡片判別","func":"var st1 = msg.payload.split('\": \"')[1].substr(0,8);\nvar st2='';\n switch (st1)\n {\n case '966aa5f4':\n st2 = 'Alex9ufo Car';\n break;\n case '7021ed10':\n st2 = 'RaspberryPi Card';\n break;\n case '96928cf4':\n st2 = 'Node-red Car';\n break;\n case 'b68b19f5':\n st2 = 'VIP Car';\n break;\n default:\n st2 = 'illegal Card';\n }\n msg.payload=st2;\nreturn msg;\n\n/***\nif ( st1 == '966aa5f4') \n{\n msg.payload = 'Alex9ufo Car';\n}\nelse if ( st1 == '7021ed10') \n{\n msg.payload = 'RaspberryPi Card';\n}\nelse if (st1 == '96928cf4') \n{\n msg.payload = 'Node-red Card';\n} \nelse if (st1 == 'b68b19f5') \n{\n msg.payload = 'VIP Card';\n} \nelse\n{\n msg.payload = 'illegal Card';\n} \nreturn msg;\n***/","outputs":1,"noerr":0,"initialize":"","finalize":"","x":320,"y":320,"wires":[["9a73ac08.bb98e","ce5c82e4.4fccd","50bcaa8e.9adb04"]]},{"id":"ffa9cb24.e8d968","type":"function","z":"aec35ebb.84d4a","name":"VIP卡片","func":"msg.payload = 'VIP Card'\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":300,"y":260,"wires":[["9ac29315.ee87f"]]},{"id":"c80c472d.a310a8","type":"function","z":"aec35ebb.84d4a","name":"Node-red卡片","func":"msg.payload = 'Node-red Card'\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":320,"y":220,"wires":[["9ac29315.ee87f"]]},{"id":"6894eabe.fa65b4","type":"function","z":"aec35ebb.84d4a","name":"RaspberryPi卡片","func":"msg.payload = 'RaspberryPi Card'\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":180,"wires":[["9ac29315.ee87f"]]},{"id":"ce5c82e4.4fccd","type":"json","z":"aec35ebb.84d4a","name":"","property":"payload","action":"str","pretty":false,"x":510,"y":320,"wires":[["1041aa16.a3cd16"]]},{"id":"1041aa16.a3cd16","type":"function","z":"aec35ebb.84d4a","name":"INSERT","func":"msg.topic = \"INSERT INTO RFIDtable (id,currenttime, uidname) VALUES (?,?,?)\";\n\nvar 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(); //秒\n\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 hms= yyyy + '/'+ MM + '/'+ dd + ' ' + h + ':' + m + ':' + s ;\nvar id= Date.now() ;\nmsg.payload = [id ,hms, msg.payload];\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":660,"y":320,"wires":[["54a1fb7.a966704","766d807e.0f878"]]},{"id":"54a1fb7.a966704","type":"debug","z":"aec35ebb.84d4a","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":950,"y":300,"wires":[]},{"id":"766d807e.0f878","type":"sqlite","z":"aec35ebb.84d4a","mydb":"61a261a8.68f6a","sqlquery":"msg.topic","sql":"","name":"RFID","x":845,"y":396,"wires":[["4cbbe12b.412e5"]]},{"id":"e328a81.0c5de58","type":"function","z":"aec35ebb.84d4a","name":"Format timestamp","func":"var date = new Date();\nvar h = date.getHours();\nvar m = date.getMinutes();\nvar s = date.getSeconds();\nif(h<10){\n h = '0'+h;\n}\nif(m<10){\n m = '0' + m;\n}\nif(s<10){\n s = '0' + s;\n}\nmsg.payload = msg.payload + ' --> Time:(' + h + ':' + m + ':' + s + ')' ;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":330,"y":480,"wires":[["bbb7ab40.23bf58"]]},{"id":"bbb7ab40.23bf58","type":"function","z":"aec35ebb.84d4a","name":"Set Line API ","func":"msg.headers = {'content-type':'application/x-www-form-urlencoded','Authorization':'Bearer cEI4hx24xyopKGAArgZcKJNHE1V7KeeIi4Lzny3dDNO'};\nmsg.payload = {\"message\":msg.payload};\nreturn msg;","outputs":1,"noerr":0,"x":530,"y":480,"wires":[["d02b70d3.6b314"]]},{"id":"d02b70d3.6b314","type":"http request","z":"aec35ebb.84d4a","name":"","method":"POST","ret":"txt","url":"https://notify-api.line.me/api/notify","tls":"","x":700,"y":480,"wires":[["7eb05ac.7a7f5a4"]]},{"id":"7eb05ac.7a7f5a4","type":"debug","z":"aec35ebb.84d4a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":870,"y":480,"wires":[]},{"id":"cdc537ef.7d1cd8","type":"link in","z":"aec35ebb.84d4a","name":"","links":["674e8bc2.a736e4"],"x":195,"y":480,"wires":[["e328a81.0c5de58"]]},{"id":"674e8bc2.a736e4","type":"link out","z":"aec35ebb.84d4a","name":"","links":["cdc537ef.7d1cd8"],"x":155,"y":140,"wires":[]},{"id":"50bcaa8e.9adb04","type":"debug","z":"aec35ebb.84d4a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":390,"y":400,"wires":[]},{"id":"9ac29315.ee87f","type":"debug","z":"aec35ebb.84d4a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":180,"wires":[]},{"id":"5fe4c31a.ee046c","type":"mqtt in","z":"aec35ebb.84d4a","name":"","topic":"alex9ufo/led/led_status","qos":"1","datatype":"auto","broker":"841df58d.ee5e98","x":120,"y":720,"wires":[["4fef5e0c.0c2f4","aefa83ca.a0a86","b7f0745f.84e068"]]},{"id":"1fdea3d9.07b8ec","type":"ui_text","z":"aec35ebb.84d4a","group":"ac0f1141.eb50e","order":0,"width":0,"height":0,"name":"","label":"ESP32發行到MQTT的資料-->","format":"{{msg.payload}}","layout":"row-left","x":530,"y":760,"wires":[]},{"id":"adf0dddb.e7492","type":"ui_button","z":"aec35ebb.84d4a","name":"","group":"ac0f1141.eb50e","order":0,"width":"2","height":"1","passthru":false,"label":"LED 開","tooltip":"","color":"white","bgcolor":" purple","icon":"fa-circle","payload":"ON","payloadType":"str","topic":"","x":80,"y":960,"wires":[["241a91d0.3f762e","b3d8f564.b52198","daeef46b.0e2ef8"]]},{"id":"6e5be5b9.7d291c","type":"ui_button","z":"aec35ebb.84d4a","name":"","group":"ac0f1141.eb50e","order":0,"width":"2","height":"1","passthru":false,"label":"LED 關","tooltip":"","color":"black","bgcolor":"","icon":"fa-circle-o","payload":"OFF","payloadType":"str","topic":"","x":80,"y":1000,"wires":[["241a91d0.3f762e","b3d8f564.b52198","daeef46b.0e2ef8"]]},{"id":"241a91d0.3f762e","type":"mqtt out","z":"aec35ebb.84d4a","name":"","topic":"alex9ufo/inTopic/led/led_event","qos":"1","retain":"true","broker":"841df58d.ee5e98","x":390,"y":1060,"wires":[]},{"id":"4fef5e0c.0c2f4","type":"function","z":"aec35ebb.84d4a","name":"Format timestamp","func":"var date = new Date();\nvar h = date.getHours()+8;\nvar m = date.getMinutes();\nvar s = date.getSeconds();\nif(h<10){\n h = '0'+h;\n}\nif(m<10){\n m = '0' + m;\n}\nif(s<10){\n s = '0' + s;\n}\nmsg.payload = msg.payload + ' --> Time:(' + h + ':' + m + ':' + s + ')' ;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":170,"y":840,"wires":[["a15bdd95.baf39"]]},{"id":"a15bdd95.baf39","type":"function","z":"aec35ebb.84d4a","name":"Set Line API ","func":"msg.headers = {'content-type':'application/x-www-form-urlencoded','Authorization':'Bearer A4wwPNh2WqB7dlfeQyyIAwtggn1kfZSI5LkkCdia1gB'};\nmsg.payload = {\"message\":msg.payload};\nreturn msg;\n\n//oR7KdXvK1eobRr2sRRgsl4PMq23DjDlhfUs96SyUBZu","outputs":1,"noerr":0,"x":130,"y":900,"wires":[["b2e2dcb8.f96dc"]]},{"id":"b2e2dcb8.f96dc","type":"http request","z":"aec35ebb.84d4a","name":"","method":"POST","ret":"txt","paytoqs":false,"url":"https://notify-api.line.me/api/notify","tls":"","persist":false,"proxy":"","authType":"","x":300,"y":900,"wires":[["f93fabd5.140668"]]},{"id":"f93fabd5.140668","type":"debug","z":"aec35ebb.84d4a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":450,"y":900,"wires":[]},{"id":"aefa83ca.a0a86","type":"debug","z":"aec35ebb.84d4a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":370,"y":720,"wires":[]},{"id":"700e1450.26211c","type":"ui_audio","z":"aec35ebb.84d4a","name":"","group":"ac0f1141.eb50e","voice":"zh-TW","always":true,"x":460,"y":800,"wires":[]},{"id":"e860446d.4118c8","type":"ui_button","z":"aec35ebb.84d4a","name":"","group":"ac0f1141.eb50e","order":0,"width":"3","height":"1","passthru":false,"label":"LED 開關反向","tooltip":"","color":"","bgcolor":" purple","icon":"fa-circle-o","payload":"TOGGLE","payloadType":"str","topic":"","x":100,"y":1040,"wires":[["241a91d0.3f762e","b3d8f564.b52198","daeef46b.0e2ef8"]]},{"id":"b7f0745f.84e068","type":"function","z":"aec35ebb.84d4a","name":"","func":"var st1;\nif (msg.payload === \"ON\") {\n st1=\"LED開\"; \n} \nelse if (msg.payload === \"OFF\") {\n st1=\"LED關\";\n}\nelse if (msg.payload === \"FLASH\") {\n st1=\"LED閃爍\";\n}\nelse if (msg.payload === \"TIMER\") {\n st1=\"LED開五秒鐘\";\n}\nelse if (msg.payload === \"TOGGLE\") {\n st1=\"LED ON OFF 交換\";\n}\n\nmsg.payload=st1;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":290,"y":800,"wires":[["700e1450.26211c","1fdea3d9.07b8ec","39349df9.ecc1c2"]]},{"id":"7256705c.1a88a","type":"ui_button","z":"aec35ebb.84d4a","name":"","group":"ac0f1141.eb50e","order":0,"width":"3","height":"1","passthru":false,"label":"LED 開5秒鐘","tooltip":"","color":"purple","bgcolor":"","icon":"fa-circle-o","payload":"TIMER","payloadType":"str","topic":"","x":90,"y":1120,"wires":[["241a91d0.3f762e","b3d8f564.b52198","daeef46b.0e2ef8"]]},{"id":"39349df9.ecc1c2","type":"function","z":"aec35ebb.84d4a","name":"INSERT","func":"msg.topic = \"INSERT INTO LED (time_led, led_status) VALUES (?,?)\";\n//msg.topic = \"INSERT INTO LED (id, time_led, led_status) VALUES (?,?,?)\";\n\n\nvar 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(); //秒\n\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}\n\nvar hms= yyyy + '/'+ MM + '/'+ dd + ' ' + h + ':' + m + ':' + s ;\n//var id= Date.now() ;\n//msg.payload = [id ,hms, msg.payload];\nmsg.payload = [hms, msg.payload];\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":460,"y":840,"wires":[["751c487d.87e478"]]},{"id":"751c487d.87e478","type":"sqlite","z":"aec35ebb.84d4a","mydb":"19f59ce9.3edc23","sqlquery":"msg.topic","sql":"","name":"LED Status","x":610,"y":840,"wires":[["38f762ea.862b5e"]]},{"id":"38f762ea.862b5e","type":"debug","z":"aec35ebb.84d4a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":790,"y":840,"wires":[]},{"id":"927aa8a.9026158","type":"comment","z":"aec35ebb.84d4a","name":"Node-Red publish 到 HiveMQ Broker , ESP32訂閱","info":"將 alex9ufo/inTopic 到(publish)HiveMQ Broker \n給 Arduino 訂閱(Subscribe)","x":210,"y":1160,"wires":[]},{"id":"5dae70d9.9f923","type":"comment","z":"aec35ebb.84d4a","name":"ESP32向 HiveMQ Broker發行 Node-Red 訂閱subscribe","info":"將 Arduino 發行到(publish)HiveMQ Broker alex9ufo/led/led_status \n給 Node-red 或 MQTTB-Box 訂閱(Subscribe)","x":220,"y":680,"wires":[]},{"id":"5c42f812.39cd28","type":"ui_button","z":"aec35ebb.84d4a","name":"","group":"ac0f1141.eb50e","order":0,"width":"2","height":"1","passthru":false,"label":"LED 閃爍","tooltip":"","color":"yellow","bgcolor":"","icon":"fa-circle-o","payload":"FLASH","payloadType":"str","topic":"","x":80,"y":1080,"wires":[["241a91d0.3f762e","b3d8f564.b52198","daeef46b.0e2ef8"]]},{"id":"b3d8f564.b52198","type":"ui_audio","z":"aec35ebb.84d4a","name":"","group":"ac0f1141.eb50e","voice":"zh-TW","always":true,"x":340,"y":1120,"wires":[]},{"id":"daeef46b.0e2ef8","type":"ui_text","z":"aec35ebb.84d4a","group":"ac0f1141.eb50e","order":0,"width":0,"height":0,"name":"","label":"Node-RED發行到MQTT的LED訊息-->","format":"{{msg.payload}}","layout":"row-left","x":410,"y":1000,"wires":[]},{"id":"435c7055.912f7","type":"function","z":"aec35ebb.84d4a","name":"產生資料庫TABLE","func":"msg.topic = ' CREATE TABLE \"LED\" (\t\"id\"\tINTEGER,\t\"time_led\"\tTEXT,\"led_status\"\tTEXT,PRIMARY KEY(\"id\" AUTOINCREMENT))';\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":910,"y":960,"wires":[["d2ce00e1.c6ee7"]]},{"id":"9272b017.ee7f1","type":"inject","z":"aec35ebb.84d4a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":650,"y":980,"wires":[["435c7055.912f7"]]},{"id":"30703fc3.8cbe2","type":"ui_button","z":"aec35ebb.84d4a","name":"建立LED資料庫(只能執行一次)","group":"ac0f1141.eb50e","order":66,"width":"3","height":"1","passthru":false,"label":"建立LED資料庫(只能執行一次)","tooltip":"","color":" purple","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"","x":670,"y":940,"wires":[["435c7055.912f7"]]},{"id":"d2ce00e1.c6ee7","type":"sqlite","z":"aec35ebb.84d4a","mydb":"19f59ce9.3edc23","sqlquery":"msg.topic","sql":"","name":"LED Status","x":850,"y":1040,"wires":[["5b025574.1d341c","abd50644.865938"]]},{"id":"5b025574.1d341c","type":"debug","z":"aec35ebb.84d4a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1030,"y":1040,"wires":[]},{"id":"abd50644.865938","type":"ui_template","z":"aec35ebb.84d4a","group":"ac0f1141.eb50e","name":"","order":0,"width":"12","height":"4","format":"<table style=\"width:100%\">\n <tr>\n <th>Index</th> \n <th>Time</th> \n <th>Status</th>\n </tr>\n <tr ng-repeat=\"x in msg.payload | limitTo:20\">\n <td>{{$index}}</td>\n <td>{{msg.payload[$index].time_led }}</td>\n <td>{{msg.payload[$index].led_status}}</td> \n </tr>\n</table>\n","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":false,"templateScope":"local","x":940,"y":1100,"wires":[[]]},{"id":"e434da29.e3add8","type":"ui_button","z":"aec35ebb.84d4a","name":"","group":"ac0f1141.eb50e","order":10,"width":"3","height":"1","passthru":false,"label":"檢視LED資料庫","tooltip":"","color":"","bgcolor":"","icon":"","payload":"檢視LED資料庫","payloadType":"str","topic":"SELECT * FROM LED ORDER BY id DESC LIMIT 100;","x":660,"y":1040,"wires":[["d2ce00e1.c6ee7"]]},{"id":"4cbbe12b.412e5","type":"ui_template","z":"aec35ebb.84d4a","group":"ac0f1141.eb50e","name":"","order":0,"width":"12","height":"4","format":"<table style=\"width:100%\">\n <tr>\n <th>Index</th> \n <th>Date</th> \n <th>RFID</th>\n </tr>\n <tr ng-repeat=\"x in msg.payload | limitTo:20\">\n <td>{{$index}}</td>\n <td>{{msg.payload[$index].currenttime}}</td>\n <td>{{msg.payload[$index].uidname}}</td> \n </tr>\n</table>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":false,"templateScope":"local","x":980,"y":420,"wires":[[]]},{"id":"e4d9b72d.d14398","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":"ac0f1141.eb50e","type":"ui_group","name":"LED讀取","tab":"e03ab9d0.1a8f08","order":1,"disp":true,"width":"15","collapse":false},{"id":"61a261a8.68f6a","type":"sqlitedb","db":"rfid.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":"19f59ce9.3edc23","type":"sqlitedb","db":"MQTT_LED.db","mode":"RWC"},{"id":"e03ab9d0.1a8f08","type":"ui_tab","name":"LED","icon":"dashboard","order":1,"disabled":false,"hidden":false}]
沒有留言:
張貼留言