2020年11月1日 星期日

自動連接到WiFi 的 ESP32 , MQTT , Node-RED , Line , LED ,SQLite 實驗2

自動連接到WiFi 的 ESP32 , MQTT , Node-RED , Line , LED ,SQLite 實驗2 

1) 用收機控制  先到手機wifi 設定中找到esp32ap 的wifi  連上後

    進入設定 configure  new AP 設定連接到外網的WiFi ssid ,pwd

2) 下次不需要再設定 除非更換地點 或 WiFi










3) Arduino 與 Node-Red 畫面





Arduino程式

載點

http://www.mediafire.com/file/mblc033eux6h94l/AutoConnectWiFi_ESP32_RFID_MQTT_NODE_RED_LED.ino/file

#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":"80e8a15c.9a43a","type":"mqtt in","z":"ef8014ea.9e2208","name":"RFID MQTT","topic":"alex9ufo/outTopic/RFID/json","qos":"2","datatype":"auto","broker":"e4d9b72d.d14398","x":90,"y":100,"wires":[["d8975ea3.d6b2d","6d542f25.8292b","19d89100.3cc0cf","f2aa9d36.72e04"]]},{"id":"ae83d984.283368","type":"mqtt out","z":"ef8014ea.9e2208","name":"","topic":"alex9ufo/inTopic/led/led_event","qos":"1","retain":"true","broker":"e4d9b72d.d14398","x":390,"y":580,"wires":[]},{"id":"d8975ea3.d6b2d","type":"function","z":"ef8014ea.9e2208","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":330,"y":60,"wires":[["7a8621cd.7f66e"]]},{"id":"6d542f25.8292b","type":"switch","z":"ef8014ea.9e2208","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":"cont","v":"","vt":"str"}],"checkall":"true","repair":false,"outputs":5,"x":190,"y":180,"wires":[["368d174e.401868"],["4bf29407.123f9c"],["ca5bcb72.e07398"],["f1392d5.af2ded"],["931930c7.cc556"]]},{"id":"7d71a791.e7a828","type":"debug","z":"ef8014ea.9e2208","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":590,"y":160,"wires":[]},{"id":"fbd3e5d7.f0f6e8","type":"ui_audio","z":"ef8014ea.9e2208","name":"播放聲音","group":"ac0f1141.eb50e","voice":"zh-TW","always":true,"x":900,"y":280,"wires":[]},{"id":"50beda15.1ddae4","type":"sqlite","z":"ef8014ea.9e2208","mydb":"61a261a8.68f6a","sqlquery":"msg.topic","sql":"","name":"RFID","x":410,"y":400,"wires":[["ef3e8db7.278a5"]]},{"id":"748d1b0b.5f4244","type":"function","z":"ef8014ea.9e2208","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":410,"y":640,"wires":[["adfb95bb.e98c38","3247aa9f.3de716"]]},{"id":"fcaf1db1.3cee3","type":"debug","z":"ef8014ea.9e2208","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":830,"y":340,"wires":[]},{"id":"2bf36d48.766c32","type":"inject","z":"ef8014ea.9e2208","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":170,"y":440,"wires":[["50beda15.1ddae4"]]},{"id":"adfb95bb.e98c38","type":"debug","z":"ef8014ea.9e2208","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":630,"y":640,"wires":[]},{"id":"32021eb.2e78ce2","type":"ui_text_input","z":"ef8014ea.9e2208","name":"","label":"","tooltip":"","group":"ac0f1141.eb50e","order":9,"width":0,"height":0,"passthru":true,"mode":"text","delay":"0","topic":"","x":720,"y":280,"wires":[["fbd3e5d7.f0f6e8","c516fe51.4baf4"]]},{"id":"73c414b8.f25e4c","type":"ui_button","z":"ef8014ea.9e2208","name":"","group":"ac0f1141.eb50e","order":0,"width":"4","height":"1","passthru":false,"label":"聲音測試","tooltip":"","color":"white","bgcolor":"orange","icon":"fa-circle","payload":"聲音測試","payloadType":"str","topic":"","x":720,"y":200,"wires":[["32021eb.2e78ce2"]]},{"id":"ef3e8db7.278a5","type":"ui_template","z":"ef8014ea.9e2208","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":560,"y":400,"wires":[[]]},{"id":"7a8621cd.7f66e","type":"ui_text","z":"ef8014ea.9e2208","group":"ac0f1141.eb50e","order":0,"width":0,"height":0,"name":"","label":"MQTT_send_Message","format":"{{msg.payload}}","layout":"row-left","x":620,"y":60,"wires":[]},{"id":"8a30de3f.86402","type":"function","z":"ef8014ea.9e2208","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":340,"wires":[["50beda15.1ddae4","fcaf1db1.3cee3"]]},{"id":"931930c7.cc556","type":"function","z":"ef8014ea.9e2208","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","outputs":1,"noerr":0,"initialize":"","finalize":"","x":370,"y":300,"wires":[["32021eb.2e78ce2","4efb2990.c6ee48"]]},{"id":"dbc49985.ff32f8","type":"inject","z":"ef8014ea.9e2208","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":190,"y":300,"wires":[["931930c7.cc556"]]},{"id":"4bf29407.123f9c","type":"function","z":"ef8014ea.9e2208","name":"RaspberryPi卡片","func":"msg.payload = 'RaspberryPi Card'\nreturn msg;","outputs":1,"noerr":0,"x":390,"y":180,"wires":[["7d71a791.e7a828"]]},{"id":"368d174e.401868","type":"function","z":"ef8014ea.9e2208","name":"Alex9ufo卡片","func":"msg.payload = 'Alex9ufo Card'\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":140,"wires":[["7d71a791.e7a828"]]},{"id":"3247aa9f.3de716","type":"function","z":"ef8014ea.9e2208","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":550,"y":680,"wires":[["f95ae1fb.2761a"]]},{"id":"f95ae1fb.2761a","type":"http request","z":"ef8014ea.9e2208","name":"","method":"POST","ret":"txt","url":"https://notify-api.line.me/api/notify","tls":"","x":700,"y":680,"wires":[["9f290ea8.c917e"]]},{"id":"9f290ea8.c917e","type":"debug","z":"ef8014ea.9e2208","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":870,"y":680,"wires":[]},{"id":"19d89100.3cc0cf","type":"debug","z":"ef8014ea.9e2208","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":290,"y":20,"wires":[]},{"id":"4efb2990.c6ee48","type":"json","z":"ef8014ea.9e2208","name":"","property":"payload","action":"str","pretty":false,"x":530,"y":340,"wires":[["8a30de3f.86402"]]},{"id":"1be5f49a.70e6eb","type":"ui_button","z":"ef8014ea.9e2208","name":"","group":"ac0f1141.eb50e","order":10,"width":"4","height":"1","passthru":false,"label":"View Data","tooltip":"","color":"","bgcolor":"","icon":"","payload":"檢視資料庫","payloadType":"str","topic":"SELECT * FROM RFIDtable ORDER BY  id  DESC LIMIT 100;","x":150,"y":400,"wires":[["50beda15.1ddae4","cd4f5dd0.4e578"]]},{"id":"bceb7f95.c71d4","type":"comment","z":"ef8014ea.9e2208","name":"CREATE TABLE RFIDtable","info":"CREATE TABLE RFIDtable( id INT PRIMARY KEY NOT NULL, currenttime TEXT , rfid TEXT)","x":150,"y":500,"wires":[]},{"id":"cc029f46.7f242","type":"ui_button","z":"ef8014ea.9e2208","name":"Alex9ufo","group":"ac0f1141.eb50e","order":2,"width":"4","height":"1","passthru":false,"label":"手動插入測試資料","tooltip":"","color":"","bgcolor":"","icon":"","payload":"{\"RFIDNo\":\"Alex9ufo測試\"}","payloadType":"json","topic":"","x":780,"y":400,"wires":[["c8ca3970.7ef988"]]},{"id":"c8ca3970.7ef988","type":"mqtt out","z":"ef8014ea.9e2208","name":"","topic":"alex9ufo/outTopic/RFID/json","qos":"1","retain":"true","broker":"e4d9b72d.d14398","x":980,"y":400,"wires":[]},{"id":"ee3949d5.f5a218","type":"ui_button","z":"ef8014ea.9e2208","name":"","group":"ac0f1141.eb50e","order":11,"width":"4","height":"1","passthru":false,"label":"建立資料庫(只能執行一次)","tooltip":"","color":"","bgcolor":" purple","icon":"","payload":"","payloadType":"str","topic":"CREATE TABLE RFIDtable( id INT PRIMARY KEY NOT NULL, currenttime TEXT , uidname TEXT)","x":190,"y":360,"wires":[["50beda15.1ddae4"]]},{"id":"ceaa0720.9d2068","type":"ui_button","z":"ef8014ea.9e2208","name":"","group":"ac0f1141.eb50e","order":0,"width":"4","height":"1","passthru":false,"label":"LED 開","tooltip":"","color":"white","bgcolor":"","icon":"fa-circle","payload":"ON","payloadType":"str","topic":"","x":80,"y":540,"wires":[["ae83d984.283368","748d1b0b.5f4244","cf1b1e28.343f4"]]},{"id":"991b17df.733278","type":"ui_button","z":"ef8014ea.9e2208","name":"","group":"ac0f1141.eb50e","order":0,"width":"4","height":"1","passthru":false,"label":"LED 關","tooltip":"","color":"black","bgcolor":"","icon":"fa-circle-o","payload":"OFF","payloadType":"str","topic":"","x":80,"y":580,"wires":[["ae83d984.283368","748d1b0b.5f4244","cf1b1e28.343f4"]]},{"id":"65121a65.e130a4","type":"ui_button","z":"ef8014ea.9e2208","name":"","group":"ac0f1141.eb50e","order":0,"width":"4","height":"1","passthru":false,"label":"LED 開關反向","tooltip":"","color":"blue","bgcolor":"","icon":"fa-circle-o","payload":"TOGGLE","payloadType":"str","topic":"","x":100,"y":660,"wires":[["ae83d984.283368","748d1b0b.5f4244","cf1b1e28.343f4"]]},{"id":"a8f45eaa.5165b","type":"ui_button","z":"ef8014ea.9e2208","name":"","group":"ac0f1141.eb50e","order":0,"width":"4","height":"1","passthru":false,"label":"LED 閃爍","tooltip":"","color":"blue","bgcolor":"","icon":"fa-circle-o","payload":"FLASH","payloadType":"str","topic":"","x":80,"y":620,"wires":[["ae83d984.283368","748d1b0b.5f4244","cf1b1e28.343f4"]]},{"id":"aaeae0ce.6e9ad","type":"ui_button","z":"ef8014ea.9e2208","name":"","group":"ac0f1141.eb50e","order":0,"width":"4","height":"1","passthru":false,"label":"LED 開5秒鐘","tooltip":"","color":"purple","bgcolor":"","icon":"fa-circle-o","payload":"TIMER","payloadType":"str","topic":"","x":90,"y":700,"wires":[["ae83d984.283368","748d1b0b.5f4244","cf1b1e28.343f4"]]},{"id":"5c6dc10.aaffb4","type":"comment","z":"ef8014ea.9e2208","name":"publish 到 HiveMQ Broker ","info":"將 alex9ufo/led/led_event 發行到(publish)HiveMQ Broker \n給 Arduino 訂閱(Subscribe)","x":310,"y":540,"wires":[]},{"id":"9221daf4.aed748","type":"comment","z":"ef8014ea.9e2208","name":"向 HiveMQ Broker 訂閱subscribe","info":"將  Arduino 發行到(publish)HiveMQ Broker alex9ufo/led/led_status \n給 Node-red 或 MQTTB-Box 訂閱(Subscribe)","x":670,"y":480,"wires":[]},{"id":"13edbe6c.6bf392","type":"mqtt in","z":"ef8014ea.9e2208","name":"","topic":"alex9ufo/led/led_status","qos":"1","datatype":"auto","broker":"841df58d.ee5e98","x":680,"y":520,"wires":[["4daf2345.35a50c","fc0340fb.a2c5c","389b8f13.36cd4","f955982.dc91968"]]},{"id":"fc0340fb.a2c5c","type":"debug","z":"ef8014ea.9e2208","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":910,"y":460,"wires":[]},{"id":"4daf2345.35a50c","type":"ui_text","z":"ef8014ea.9e2208","group":"ac0f1141.eb50e","order":0,"width":0,"height":0,"name":"","label":"Node-Red 向MQTT Suscribe Data","format":"{{msg.payload}}","layout":"col-center","x":1180,"y":480,"wires":[]},{"id":"389b8f13.36cd4","type":"function","z":"ef8014ea.9e2208","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":950,"y":520,"wires":[["4572bbe5.f55ae4","4daf2345.35a50c","837292c8.62c76"]]},{"id":"4572bbe5.f55ae4","type":"function","z":"ef8014ea.9e2208","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":980,"y":580,"wires":[["e82f56fc.727678"]]},{"id":"e82f56fc.727678","type":"sqlite","z":"ef8014ea.9e2208","mydb":"19f59ce9.3edc23","sqlquery":"msg.topic","sql":"","name":"","x":1140,"y":580,"wires":[["ac9afcbf.47316"]]},{"id":"ac9afcbf.47316","type":"debug","z":"ef8014ea.9e2208","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1310,"y":580,"wires":[]},{"id":"ca5bcb72.e07398","type":"function","z":"ef8014ea.9e2208","name":"Node-red卡片","func":"msg.payload = 'Node-red Card'\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":380,"y":220,"wires":[["7d71a791.e7a828"]]},{"id":"f1392d5.af2ded","type":"function","z":"ef8014ea.9e2208","name":"VIP卡片","func":"msg.payload = 'VIP Card'\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":360,"y":260,"wires":[["7d71a791.e7a828"]]},{"id":"cf1b1e28.343f4","type":"link out","z":"ef8014ea.9e2208","name":"RFID-1","links":["b4e6e9fe.7a2968","1a2e6a1f.317c06"],"x":295,"y":740,"wires":[]},{"id":"b4e6e9fe.7a2968","type":"link in","z":"ef8014ea.9e2208","name":"","links":["cf1b1e28.343f4"],"x":475,"y":100,"wires":[["7a8621cd.7f66e"]]},{"id":"bdd95fae.3f9e2","type":"link in","z":"ef8014ea.9e2208","name":"","links":["837292c8.62c76","cd4f5dd0.4e578"],"x":635,"y":220,"wires":[["32021eb.2e78ce2"]]},{"id":"c516fe51.4baf4","type":"debug","z":"ef8014ea.9e2208","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":910,"y":240,"wires":[]},{"id":"837292c8.62c76","type":"link out","z":"ef8014ea.9e2208","name":"RFIF-2","links":["bdd95fae.3f9e2","1a2e6a1f.317c06"],"x":1095,"y":520,"wires":[]},{"id":"cd4f5dd0.4e578","type":"link out","z":"ef8014ea.9e2208","name":"","links":["bdd95fae.3f9e2"],"x":375,"y":460,"wires":[]},{"id":"92e54fcf.b576a","type":"link in","z":"ef8014ea.9e2208","name":"LINE","links":["f2aa9d36.72e04","f955982.dc91968","d4c722e8.eaf73"],"x":355,"y":720,"wires":[["748d1b0b.5f4244"]]},{"id":"f2aa9d36.72e04","type":"link out","z":"ef8014ea.9e2208","name":"To LINE","links":["92e54fcf.b576a"],"x":225,"y":120,"wires":[]},{"id":"f955982.dc91968","type":"link out","z":"ef8014ea.9e2208","name":"TO Line 2","links":["92e54fcf.b576a"],"x":815,"y":580,"wires":[]},{"id":"e4d9b72d.d14398","type":"mqtt-broker","z":"","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","z":"","name":"RFID讀取","tab":"e03ab9d0.1a8f08","order":1,"disp":true,"width":"15","collapse":false},{"id":"61a261a8.68f6a","type":"sqlitedb","z":"","db":"rfid.db","mode":"RWC"},{"id":"841df58d.ee5e98","type":"mqtt-broker","z":"","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","z":"","db":"MQTT_LED.db","mode":"RWC"},{"id":"e03ab9d0.1a8f08","type":"ui_tab","z":"","name":"RFID","icon":"dashboard","order":1}]

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...