2024年10月23日 星期三

WOKWI ESP32 LED Control , Node-Red MQTT SQLITE  

WOKWI ESP32 LED Control ,  Node-Red  MQTT SQLITE  





const char broker[] = "test.mosquitto.org";
//const char broker[] = "broker.mqtt-dashboard.com";
int        port     = 1883;

const char *SubTopic1 = "alex9ufo/LED_control";
const char *PubTopic2 = "alex9ufo/LED_status";

Wokwi硬體


Wokwi 程式庫

從程式 #include <ArduinoMqttClient.h> copy ArduinoMqttClient


Wokwi程式  (控制led on ,off , timer ,flash)

// Wifi 與 MQttClient 程式庫
#include <ArduinoMqttClient.h>
#include <WiFi.h>
//#include "arduino_secrets1.h"


//#define LED 13           //定義LED接腳
int LED = 13;
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
//char ssid[] = "TOTOLINK_A3002MU";    // your network SSID (name)
//char pass[] = "24063173";    // your network password (use for WPA, or use as key for WEP)
// WiFi SSID password , SSID 和密碼進行Wi-Fi 設定

///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] =  "Wokwi-GUEST"; // your network SSID (name)
char pass[] =  "" ;           // your network password (use for WPA, or use as key for WEP)


//char ssid[] = "dlink-103A";    // your network SSID (name)
//char pass[] = "bdcce12882";    // your network password (use for WPA, or use as key for WEP)

WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);

const char broker[] = "test.mosquitto.org";
//const char broker[] = "broker.mqtt-dashboard.com";
int        port     = 1883;
String json = "";

const char *SubTopic1 = "alex9ufo/LED_control";
const char *PubTopic2 = "alex9ufo/LED_status";
//const char *PubTopic3 = "alex9ufo/2024/RFID/RFID_UID";
//const char *PubTopic4 = "alex9ufo/2024/RFID/RFID_PICC";

const char willTopic[] = "alex9ufo/2024/RFID/Starting";

//===========================================================
//布林代數 LED狀態 是否連上網路ESP32 ready ?
bool ledState = false;
bool atwork = false;
bool Send = false;  //true
String LEDjson = "";
int Count= 0;
bool Flash = false;  //true
bool Timer = false;  //true

//===========================================================
void onMqttMessage(int messageSize) {
  // we received a message, print out the topic and contents
  Serial.print("Received a message with topic '");
  Serial.print(mqttClient.messageTopic());
  String Topic= mqttClient.messageTopic();
  Serial.print("', duplicate = ");
  Serial.print(mqttClient.messageDup() ? "true" : "false");
  Serial.print(", QoS = ");
  Serial.print(mqttClient.messageQoS());
  Serial.print(", retained = ");
  Serial.print(mqttClient.messageRetain() ? "true" : "false");
  Serial.print("', length ");
  Serial.print(messageSize);
  Serial.println(" bytes:");
  String message="";
  // use the Stream interface to print the contents
  while (mqttClient.available()) {
    //Serial.print((char)mqttClient.read());
    message += (char)mqttClient.read();
  }

  Serial.println(message);
  message.trim();
  Topic.trim();

  if (Topic=="alex9ufo/LED_control") {
  if (message == "on") {
    digitalWrite(LED, LOW);  // Turn on the LED
    //ledState = true;  //ledState = ture HIGH
    //設定 各個 旗號
    LEDjson ="ON";
    Send = true ;
    Serial.print("LED =");
    Serial.println(LEDjson);
  }

  if (message == "off" ) {
    digitalWrite(LED, HIGH); // Turn off the LED
    //ledState = false; //ledState = false LOW
    LEDjson ="OFF";
    Send = true ;
    Serial.print("LED =");
    Serial.println(LEDjson);
  }
 
  if (message == "flash" ) {
    digitalWrite(LED, HIGH); // Turn off the LED
    Flash = true;
    Timer = false;
    LEDjson ="FLASH";
    Send = true ;  
    Serial.print("LED =");
    Serial.println(LEDjson);      
  }

  if (message == "timer" ) {
    digitalWrite(LED, LOW); // Turn off the LED
    Flash = false;
    Timer = true;
    LEDjson ="TIMER";
    Send = true ;
    Count= 11;
    Serial.print("LED =");
    Serial.println(LEDjson);  
  }
 
    Serial.println();
    Serial.println("-----------------------");
  }  

}

//===========================================================
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);
        id +=" ";
      }
      return id;
}
//===========================================================
//副程式  setup wifi
void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);     //print ssid
  WiFi.begin(ssid, pass);  //初始化WiFi 函式庫並回傳目前的網路狀態
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }   //假設 wifi 未連接 show ………

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}  
//===========================================================
//判斷 旗號Flash , Timer 是否為真
void LED_Message() {
  //判斷 旗號 Flash / timer  是否為真 ? 閃爍 定時
 
  if (Flash){
    digitalWrite(LED, !digitalRead(LED));
    delay(500);
    if (digitalRead(LED))
      ledState = true;
    else
      ledState = false;

  } //(Flash)

  if (Timer) {
    digitalWrite(LED, LOW);
    delay(500);
    if (digitalRead(LED))
      ledState = true;
    else
      ledState = false;

  Count=Count-1;
  if (Count == 0 ){
    Timer=false;
    digitalWrite(LED, HIGH);
    ledState = false;
    }
  } //(Timer)
 
 
  ////判斷 旗號 Send 是否為真 回傳MQTT訊息到MQTT Broker 
  if (Send) {
    // Convert JSON string to character array
    Serial.print("Publish message: ");
    Serial.println(LEDjson);
    LEDjson.trim();

    bool retained = false;
    int qos = 1;
    bool dup = false;
   
    // Publish JSON character array to MQTT topic
    mqttClient.beginMessage(PubTopic2,  LEDjson.length(), retained, qos, dup);  //LED Status
    mqttClient.print(LEDjson);
    mqttClient.endMessage();
    Send = false;    //處理過後 旗號 Send為假
  }

}
//===========================================================
void setup() {
  pinMode(LED, OUTPUT);
  digitalWrite(LED, HIGH);  // Turn off the LED initially
  //Initialize serial and wait for port to open:
  Serial.begin(115200);   // Initialize serial communications with the PC
  while (!Serial);    // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
 
  setup_wifi();
  Serial.println("You're connected to the network");
  Serial.println();
 
  String willPayload = "ESP32 Start working....!";
  bool willRetain = true;
  int willQos = 1;

  mqttClient.beginWill(willTopic, willPayload.length(), willRetain, willQos);
  mqttClient.print(willPayload);
  mqttClient.endWill();

  Serial.print("Attempting to connect to the MQTT broker: ");
  Serial.println(broker);

  if (!mqttClient.connect(broker, port)) {
    Serial.print("MQTT connection failed! Error code = ");
    Serial.println(mqttClient.connectError());

    while (1);
  }

  Serial.println("You're connected to the MQTT broker!");
  Serial.println();

  // set the message receive callback
  mqttClient.onMessage(onMqttMessage);
  Serial.print("Subscribing to topic: ");
  Serial.println(SubTopic1);
  // subscribe to a topic
  // the second parameter sets the QoS of the subscription,
  // the the library supports subscribing at QoS 0, 1, or 2
  int subscribeQos = 1;
  mqttClient.subscribe(SubTopic1, subscribeQos);

  Serial.println();
}
//===========================================================
void loop() {
  // call poll() regularly to allow the library to receive MQTT messages and
  // send MQTT keep alives which avoids being disconnected by the broker
  mqttClient.poll();
  LED_Message();
  Serial.println();

}

////=========================================================== 


Node-Red程式及設定畫面

需要的節點有 dashboard , sqilte , mqtt in , mqtt out ,node-red-node-ui-table






匯入程式 暨 節點安裝


SQLite DB Browser :  https://sqlitebrowser.org/dl/

Downloads

(Please consider sponsoring us on Patreon 😄)

Windows

Our latest release (3.13.1) for Windows:

Free code signing provided by SignPath.io, certificate by SignPath Foundation.


資料庫位置 C:\Users\User\.node-red\202409-LED_STATUS.db


用dbbrower 開啟檔案 
C:\Users\User\.node-red\202409-LED_STATUS.db





取得 Line Notify 的 發行權杖 使得LEN ON , LED OFF 狀態可以隨時由手機APP LINE取得資訊

LINE結果 畫面




進入 發行權杖

指定 名稱 與 要Line通知的人 或 群組


將發行權杖 複製

檢視一下 發行的權杖是否active 









手機Line Notify 會送出 HEllo! LED Change 與一個貼圖 



7) LINE 通知 的方法2


設定2個function



發行權杖 放入 Authorization':'Bearer 後面
A4wwPNh2WqB723dlfeQyyIAwtggn1kfZSI5LkkCdia1gB'};


msg.headers = {'content-type':'application/x-www-form-urlencoded','Authorization':'Bearer A4wwPNh2WqB723dlfeQyyIAwtggn1kfZSI5LkkCdia1gB'};
msg.payload = {"message":msg.payload};
return msg;

//oR7KdXvK1eob33Rr2sRRgsl4PMq23DjDlhfUs96SyUBZu


Bearer  A4wwPN  
有空白不可以連接再一起


設定Htpp Request

Request 方法 :POST
URL :  https://notify-api.line.me/api/notify



Node-Red程式

[{"id":"f3d8f922069f4408","type":"inject","z":"7a42b2d144311ab4","name":"CREATE","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"CREATE TABLE LEDSTATUS (id INTEGER,STATUS TEXT,Date DATE,Time TIME,PRIMARY KEY (id));","payload":"","payloadType":"date","x":140,"y":80,"wires":[["92e892ad91a0f0b7"]]},{"id":"8e470c2298da2da2","type":"inject","z":"7a42b2d144311ab4","name":"SELECT","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"SELECT * FROM LEDSTATUS","payload":"","payloadType":"date","x":140,"y":160,"wires":[["92e892ad91a0f0b7"]]},{"id":"a0d5135eea3dba60","type":"inject","z":"7a42b2d144311ab4","name":"INSERT","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"INSERT INTO LEDSTATUS (STATUS ,  Date , Time )values(\"on\", \"11/01\" , \"21:05\") ","payload":"","payloadType":"date","x":140,"y":120,"wires":[["92e892ad91a0f0b7"]]},{"id":"9f0e5a660afbe0e7","type":"inject","z":"7a42b2d144311ab4","name":"DELETE","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"DELETE from LEDSTATUS","payload":"","payloadType":"date","x":140,"y":200,"wires":[["92e892ad91a0f0b7"]]},{"id":"4a8be28292787044","type":"inject","z":"7a42b2d144311ab4","name":"DROP TABLE","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"DROP TABLE LEDSTATUS","payload":"","payloadType":"date","x":150,"y":240,"wires":[["92e892ad91a0f0b7"]]},{"id":"92e892ad91a0f0b7","type":"sqlite","z":"7a42b2d144311ab4","mydb":"4e71ffcc.32ba8","sqlquery":"msg.topic","sql":"","name":"LED_STATUS","x":400,"y":160,"wires":[["a19491318b4b4af6"]]},{"id":"a19491318b4b4af6","type":"debug","z":"7a42b2d144311ab4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":570,"y":160,"wires":[]},{"id":"849a2c25980790ee","type":"comment","z":"7a42b2d144311ab4","name":"TABLE LEDSTATUS","info":"CREATE TABLE LEDSTATUS (\nid INTEGER,\nSTATUS TEXT,\nDate DATE,\nTime TIME,\nPRIMARY KEY (id)\n);","x":150,"y":40,"wires":[]},{"id":"eda02cbd637989cf","type":"sqlite","z":"7a42b2d144311ab4","mydb":"4e71ffcc.32ba8","sqlquery":"msg.topic","sql":"","name":"LED_STATUS","x":540,"y":320,"wires":[["82f62a184eca343d"]]},{"id":"44a5f20a5011e139","type":"function","z":"7a42b2d144311ab4","name":"CREATE DATABASE","func":"//CREATE TABLE LEDSTATUS (\n//id INTEGER,\n//STATUS TEXT,\n//Date DATE,\n//Time TIME,\n//PRIMARY KEY (id)\n//);\n//CREATE TABLE LEDSTATUS (id INTEGER,STATUS TEXT,Date DATE,Time TIME,PRIMARY KEY (id));\nmsg.topic = \"CREATE TABLE LEDSTATUS (id INTEGER,STATUS TEXT,Date DATE,Time TIME,PRIMARY KEY (id))\";\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":320,"wires":[["eda02cbd637989cf"]]},{"id":"edd0dac671e252f4","type":"ui_button","z":"7a42b2d144311ab4","name":"","group":"900c57566dc0cb2b","order":7,"width":4,"height":1,"passthru":false,"label":"建立資料庫","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"建立資料庫","payloadType":"str","topic":"topic","topicType":"msg","x":130,"y":320,"wires":[["44a5f20a5011e139","4e69062f7f288e2c"]]},{"id":"4e69062f7f288e2c","type":"ui_audio","z":"7a42b2d144311ab4","name":"","group":"900c57566dc0cb2b","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":255,"y":280,"wires":[],"l":false},{"id":"3537ab66e1e4856c","type":"ui_button","z":"7a42b2d144311ab4","name":"","group":"4392f6e50bbac53e","order":1,"width":0,"height":0,"passthru":false,"label":"ON","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"on","payloadType":"str","topic":"topic","topicType":"msg","x":110,"y":380,"wires":[["a305ef32c88bd32e","fea0b02e79ebe16b","f2bed117bd62aa5b","b11d966a7d530363"]]},{"id":"0da310b0f7f543e5","type":"ui_button","z":"7a42b2d144311ab4","name":"","group":"4392f6e50bbac53e","order":2,"width":0,"height":0,"passthru":false,"label":"OFF","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"off","payloadType":"str","topic":"topic","topicType":"msg","x":110,"y":420,"wires":[["a305ef32c88bd32e","fea0b02e79ebe16b","f2bed117bd62aa5b","b11d966a7d530363"]]},{"id":"0d2601d82f0d1e17","type":"ui_button","z":"7a42b2d144311ab4","name":"","group":"4392f6e50bbac53e","order":3,"width":0,"height":0,"passthru":false,"label":"TIMER","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"timer","payloadType":"str","topic":"topic","topicType":"msg","x":120,"y":460,"wires":[["a305ef32c88bd32e","fea0b02e79ebe16b","f2bed117bd62aa5b","b11d966a7d530363"]]},{"id":"cba6f4c72133f5e9","type":"ui_button","z":"7a42b2d144311ab4","name":"","group":"4392f6e50bbac53e","order":4,"width":0,"height":0,"passthru":false,"label":"FLASH","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"flash","payloadType":"str","topic":"topic","topicType":"msg","x":120,"y":500,"wires":[["fea0b02e79ebe16b","a305ef32c88bd32e","f2bed117bd62aa5b","b11d966a7d530363"]]},{"id":"fea0b02e79ebe16b","type":"function","z":"7a42b2d144311ab4","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 myLED = msg.payload;\n\n\nmsg.topic = \"INSERT INTO LEDSTATUS ( STATUS , Date , Time ) VALUES ($myLED,  $var_date ,  $var_time ) \" ;\nmsg.payload = [myLED, var_date , var_time ]\nreturn msg;\n\n\n//INSERT INTO LEDSTATUS (\n//id INTEGER,\n//STATUS TEXT,\n//Date DATE,\n//Time TIME,\n//PRIMARY KEY (id)\n//);\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":460,"wires":[["11c73175cb709168"]]},{"id":"a305ef32c88bd32e","type":"ui_audio","z":"7a42b2d144311ab4","name":"","group":"900c57566dc0cb2b","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":275,"y":420,"wires":[],"l":false},{"id":"11c73175cb709168","type":"sqlite","z":"7a42b2d144311ab4","mydb":"4e71ffcc.32ba8","sqlquery":"msg.topic","sql":"","name":"LED_STATUS","x":500,"y":460,"wires":[["666065a9ad32682f","c9bc6ca8e7fac1bf"]]},{"id":"82f62a184eca343d","type":"debug","z":"7a42b2d144311ab4","name":"debug ","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":710,"y":320,"wires":[]},{"id":"666065a9ad32682f","type":"debug","z":"7a42b2d144311ab4","name":"debug ","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":710,"y":460,"wires":[]},{"id":"bff256484354ece8","type":"ui_button","z":"7a42b2d144311ab4","name":"","group":"900c57566dc0cb2b","order":1,"width":4,"height":1,"passthru":false,"label":"檢視資料庫資料","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"檢視資料","payloadType":"str","topic":"topic","topicType":"msg","x":140,"y":620,"wires":[["c9bc6ca8e7fac1bf","b883a444212c9b7c"]]},{"id":"c9bc6ca8e7fac1bf","type":"function","z":"7a42b2d144311ab4","name":"檢視資料","func":"//INSERT INTO LEDSTATUS (\n//id INTEGER,\n//STATUS TEXT,\n//Date DATE,\n//Time TIME,\n//PRIMARY KEY (id)\n//);\n\n//SELECT * FROM LEDSTATUS ORDER BY  id DESC LIMIT 50;\n\nmsg.topic = \"SELECT * FROM LEDSTATUS ORDER BY id DESC LIMIT 50\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":620,"wires":[["04e438e24a693b65"]]},{"id":"7f6d56c2b2c877cb","type":"ui_table","z":"7a42b2d144311ab4","group":"900c57566dc0cb2b","name":"","order":4,"width":10,"height":11,"columns":[],"outputs":0,"cts":false,"x":1050,"y":620,"wires":[]},{"id":"04e438e24a693b65","type":"sqlite","z":"7a42b2d144311ab4","mydb":"4e71ffcc.32ba8","sqlquery":"msg.topic","sql":"","name":"LED_STATUS","x":540,"y":620,"wires":[["7f6d56c2b2c877cb"]]},{"id":"0d1b7e61c79c13b0","type":"ui_button","z":"7a42b2d144311ab4","name":"","group":"900c57566dc0cb2b","order":5,"width":3,"height":1,"passthru":false,"label":"刪除資料庫 ","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"刪除資料庫 ","payloadType":"str","topic":"topic","topicType":"msg","x":130,"y":860,"wires":[["68803ef562c28f2f","7aaea1c4e6a0e43a"]]},{"id":"9fa92029663a332f","type":"function","z":"7a42b2d144311ab4","name":"DROP DATABASE","func":"//CREATE TABLE LEDSTATUS (\n//id INTEGER,\n//STATUS TEXT,\n//Date DATE,\n//Time TIME,\n//PRIMARY KEY (id)\n//);\n//CREATE TABLE LEDSTATUS (id INTEGER,STATUS TEXT,Date DATE,Time TIME,PRIMARY KEY (id));\nmsg.topic = \"DROP TABLE LEDSTATUS\";\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":640,"y":800,"wires":[["f4c08b3f23c89c7d"]]},{"id":"f4c08b3f23c89c7d","type":"sqlite","z":"7a42b2d144311ab4","mydb":"4e71ffcc.32ba8","sqlquery":"msg.topic","sql":"","name":"LED_STATUS","x":780,"y":720,"wires":[["7dc3fdbb4e07f377"]]},{"id":"580d43a83df62593","type":"ui_button","z":"7a42b2d144311ab4","name":"","group":"900c57566dc0cb2b","order":6,"width":3,"height":1,"passthru":false,"label":"刪除所有資料 ","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"刪除所有資料 ","payloadType":"str","topic":"topic","topicType":"msg","x":140,"y":780,"wires":[["7aaea1c4e6a0e43a","10d510946b24f25e"]]},{"id":"817dd666deaa6bd5","type":"function","z":"7a42b2d144311ab4","name":"DELETE ALL DATA","func":"//CREATE TABLE LEDSTATUS (\n//id INTEGER,\n//STATUS TEXT,\n//Date DATE,\n//Time TIME,\n//PRIMARY KEY (id)\n//);\n//CREATE TABLE LEDSTATUS (id INTEGER,STATUS TEXT,Date DATE,Time TIME,PRIMARY KEY (id));\nmsg.topic = \"DELETE from LEDSTATUS\";\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":720,"wires":[["f4c08b3f23c89c7d"]]},{"id":"68803ef562c28f2f","type":"ui_toast","z":"7a42b2d144311ab4","position":"prompt","displayTime":"3","highlight":"","sendall":true,"outputs":1,"ok":"OK","cancel":"Cancel","raw":true,"className":"","topic":"","name":"","x":310,"y":860,"wires":[["752604303b88509b"]]},{"id":"752604303b88509b","type":"function","z":"7a42b2d144311ab4","name":"function 99","func":"var topic=msg.payload;\nif (topic==\"\"){\n    return [msg,null];\n    \n}\nif (topic==\"Cancel\"){\n    return [null,msg];\n    \n}\nreturn msg;","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":860,"wires":[["9fa92029663a332f"],[]]},{"id":"7aaea1c4e6a0e43a","type":"ui_audio","z":"7a42b2d144311ab4","name":"","group":"4392f6e50bbac53e","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":315,"y":820,"wires":[],"l":false},{"id":"10d510946b24f25e","type":"ui_toast","z":"7a42b2d144311ab4","position":"prompt","displayTime":"3","highlight":"","sendall":true,"outputs":1,"ok":"OK","cancel":"Cancel","raw":true,"className":"","topic":"","name":"","x":330,"y":780,"wires":[["d0589f0b5f2e64b1"]]},{"id":"d0589f0b5f2e64b1","type":"function","z":"7a42b2d144311ab4","name":"function 100","func":"var topic=msg.payload;\nif (topic==\"\"){\n    return [msg,null];\n    \n}\nif (topic==\"Cancel\"){\n    return [null,msg];\n    \n}\nreturn msg;","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":720,"wires":[["817dd666deaa6bd5"],[]]},{"id":"b883a444212c9b7c","type":"ui_audio","z":"7a42b2d144311ab4","name":"","group":"4392f6e50bbac53e","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":255,"y":560,"wires":[],"l":false},{"id":"17f886975252f763","type":"ui_button","z":"7a42b2d144311ab4","name":"","group":"900c57566dc0cb2b","order":2,"width":3,"height":1,"passthru":false,"label":"查詢一筆資料","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"查詢一筆資料","payloadType":"str","topic":"topic","topicType":"msg","x":140,"y":920,"wires":[["e7f4c1dc10affdab","c0665136d5b8982f"]]},{"id":"1cab01ed201f670c","type":"function","z":"7a42b2d144311ab4","name":"查詢一筆資料","func":"//\nvar id = msg.payload.id;\nvar s=global.get(\"SEL1\")\nmsg.topic=\"\";\nvar temp=\"\";\n\nif (s==1)\n{\n    temp =\"SELECT * FROM LEDSTATUS\";\n    temp=temp+\" WHERE id LIKE '\"+ id +\"'\";\n}\nmsg.topic=temp;\nglobal.set(\"SEL1\",0);\n\nreturn msg;\n\n//SELECT * FROM COMPANY WHERE AGE  LIKE 'XXX%';\n//INSERT INTO LEDSTATUS (\n//id INTEGER,\n//STATUS TEXT,\n//Date DATE,\n//Time TIME,\n//PRIMARY KEY (id)\n//);\n\n//SELECT * FROM LEDSTATUS ORDER BY  id DESC LIMIT 50;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":880,"wires":[["1828dcd738c3f733"]]},{"id":"1828dcd738c3f733","type":"sqlite","z":"7a42b2d144311ab4","mydb":"4e71ffcc.32ba8","sqlquery":"msg.topic","sql":"","name":"LED_STATUS","x":900,"y":840,"wires":[["7f6d56c2b2c877cb"]]},{"id":"6da0e65117795f3e","type":"ui_button","z":"7a42b2d144311ab4","name":"","group":"900c57566dc0cb2b","order":3,"width":3,"height":1,"passthru":false,"label":"刪除一筆資料","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"刪除一筆資料","payloadType":"str","topic":"topic","topicType":"msg","x":140,"y":1020,"wires":[["c0665136d5b8982f","a0d254a9a1dcb131"]]},{"id":"c0665136d5b8982f","type":"ui_audio","z":"7a42b2d144311ab4","name":"","group":"4392f6e50bbac53e","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":285,"y":980,"wires":[],"l":false},{"id":"c75075c0eca80bcb","type":"ui_form","z":"7a42b2d144311ab4","name":"","label":"輸入id","group":"900c57566dc0cb2b","order":8,"width":10,"height":1,"options":[{"label":"ID","value":"id","type":"number","required":true,"rows":null}],"formValue":{"id":""},"payload":"","submit":"Submit","cancel":"Cancle","topic":"Form","topicType":"str","splitLayout":false,"className":"","x":530,"y":940,"wires":[["1cab01ed201f670c","eb4d3cb1349c4729","0dd5d8a8b2647f20"]]},{"id":"eb4d3cb1349c4729","type":"function","z":"7a42b2d144311ab4","name":"刪除一筆資料","func":"//\nvar id = msg.payload.id;\nvar s=global.get(\"SEL2\")\nmsg.topic=\"\";\nvar temp=\"\";\n\nif (s==2)\n{\n    temp =\"DELETE FROM LEDSTATUS\";\n    temp=temp+\" WHERE id LIKE '\"+ id +\"'\";\n}\n\nmsg.topic=temp;\nglobal.set(\"SEL2\",0)\nreturn msg;\n\n//SELECT * FROM COMPANY WHERE AGE  LIKE 'XXX%';\n//INSERT INTO LEDSTATUS (\n//id INTEGER,\n//STATUS TEXT,\n//Date DATE,\n//Time TIME,\n//PRIMARY KEY (id)\n//);\n\n//DELETE FROM COMPANY WHERE ID = 7;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":940,"wires":[["67156d3971c67d99"]]},{"id":"7dc3fdbb4e07f377","type":"link out","z":"7a42b2d144311ab4","name":"link out 59","mode":"link","links":["4dc7bf3c32365bcb"],"x":1085,"y":720,"wires":[]},{"id":"4dc7bf3c32365bcb","type":"link in","z":"7a42b2d144311ab4","name":"link in 57","links":["7dc3fdbb4e07f377"],"x":275,"y":660,"wires":[["c9bc6ca8e7fac1bf"]]},{"id":"67156d3971c67d99","type":"sqlite","z":"7a42b2d144311ab4","mydb":"4e71ffcc.32ba8","sqlquery":"msg.topic","sql":"","name":"LED_STATUS","x":920,"y":940,"wires":[["7dc3fdbb4e07f377"]]},{"id":"e7f4c1dc10affdab","type":"function","z":"7a42b2d144311ab4","name":"function flow set1","func":"var s1=1;\nglobal.set(\"SEL1\",s1);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":920,"wires":[["c75075c0eca80bcb"]]},{"id":"a0d254a9a1dcb131","type":"function","z":"7a42b2d144311ab4","name":"function flow set2","func":"var s1=2;\nglobal.set(\"SEL2\",s1);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":370,"y":1020,"wires":[["c75075c0eca80bcb"]]},{"id":"0dd5d8a8b2647f20","type":"function","z":"7a42b2d144311ab4","name":"Store ID資料","func":"//\nvar id = msg.payload.id;\nglobal.set(\"ID\",id)\nreturn msg;\n\n//SELECT * FROM COMPANY WHERE AGE  LIKE 'XXX%';\n//INSERT INTO LEDSTATUS (\n//id INTEGER,\n//STATUS TEXT,\n//Date DATE,\n//Time TIME,\n//PRIMARY KEY (id)\n//);\n\n//DELETE FROM COMPANY WHERE ID = 7;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":710,"y":1000,"wires":[["83108e96521122ae"]]},{"id":"83108e96521122ae","type":"function","z":"7a42b2d144311ab4","name":"查詢一筆資料","func":"//\nvar id = global.get(\"ID\");\nmsg.topic=\"\";\nvar temp=\"\";\ntemp =\"SELECT * FROM LEDSTATUS\";\ntemp=temp+\" WHERE id LIKE '\"+ id +\"'\";\n\nmsg.topic=temp;\n\nreturn msg;\n\n//SELECT * FROM COMPANY WHERE AGE  LIKE 'XXX%';\n//INSERT INTO LEDSTATUS (\n//id INTEGER,\n//STATUS TEXT,\n//Date DATE,\n//Time TIME,\n//PRIMARY KEY (id)\n//);\n\n//SELECT * FROM LEDSTATUS ORDER BY  id DESC LIMIT 50;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":920,"y":1000,"wires":[["1828dcd738c3f733"]]},{"id":"4ccd2116b579ee87","type":"mqtt in","z":"7a42b2d144311ab4","name":"LED status ","topic":"alex9ufo/LED_status","qos":"2","datatype":"utf8","broker":"70940176.2b2d3","nl":false,"rap":true,"rh":0,"inputs":0,"x":120,"y":1220,"wires":[["6ab01204c21e6eb3"]]},{"id":"6ab01204c21e6eb3","type":"function","z":"7a42b2d144311ab4","name":"function ","func":"msg.payload=\" ---ESP32回來資料---\" +msg.payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":1220,"wires":[["a64ed28999f8aacd","3e647b5a88e634e1","653f7bd1cc3a49c0"]]},{"id":"a64ed28999f8aacd","type":"function","z":"7a42b2d144311ab4","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":490,"y":1220,"wires":[["879661d7ce01f783"]]},{"id":"879661d7ce01f783","type":"http request","z":"7a42b2d144311ab4","name":"","method":"POST","ret":"txt","paytoqs":false,"url":"https://notify-api.line.me/api/notify","tls":"","persist":false,"proxy":"","authType":"","x":660,"y":1220,"wires":[["f595b0b9ab9804f6"]]},{"id":"f595b0b9ab9804f6","type":"debug","z":"7a42b2d144311ab4","name":"debug 307","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":830,"y":1220,"wires":[]},{"id":"65c9def0a13993e8","type":"comment","z":"7a42b2d144311ab4","name":"Line Notify Message ","info":"","x":150,"y":1180,"wires":[]},{"id":"f2bed117bd62aa5b","type":"mqtt out","z":"7a42b2d144311ab4","name":"Control LED","topic":"alex9ufo/LED_control","qos":"1","retain":"true","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"70940176.2b2d3","x":330,"y":380,"wires":[]},{"id":"fae61926f8142b84","type":"comment","z":"7a42b2d144311ab4","name":"alex9ufo/LED_control","info":"","x":520,"y":420,"wires":[]},{"id":"b11d966a7d530363","type":"ui_text","z":"7a42b2d144311ab4","group":"4392f6e50bbac53e","order":5,"width":6,"height":1,"name":"","label":"Node-RED發行到MQTT的資料 : ","format":"{{msg.payload}}","layout":"row-left","className":"","x":390,"y":500,"wires":[]},{"id":"653f7bd1cc3a49c0","type":"ui_text","z":"7a42b2d144311ab4","group":"4392f6e50bbac53e","order":6,"width":6,"height":1,"name":"","label":"Node-RED 訂閱MQTT的資料 : ","format":"{{msg.payload}}","layout":"row-left","className":"","x":550,"y":1300,"wires":[]},{"id":"e83725a0ea50b538","type":"ui_audio","z":"7a42b2d144311ab4","name":"","group":"4392f6e50bbac53e","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":"","x":660,"y":1260,"wires":[]},{"id":"3e647b5a88e634e1","type":"delay","z":"7a42b2d144311ab4","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":480,"y":1260,"wires":[["e83725a0ea50b538"]]},{"id":"2a251dbb282f44ef","type":"comment","z":"7a42b2d144311ab4","name":"資料庫位置 C:\\Users\\User\\.node-red\\202409-LED_STATUS.db","info":"","x":540,"y":120,"wires":[]},{"id":"d30a3f467a03e8a4","type":"comment","z":"7a42b2d144311ab4","name":"資料庫位置 C:\\Users\\User\\.node-red\\202409-LED_STATUS.db","info":"","x":520,"y":280,"wires":[]},{"id":"4e71ffcc.32ba8","type":"sqlitedb","db":"C:\\Users\\User\\.node-red\\202409_LED_STATUS.db","mode":"RWC"},{"id":"900c57566dc0cb2b","type":"ui_group","name":"資料庫","tab":"6e71507755b5c249","order":2,"disp":true,"width":10,"collapse":false,"className":""},{"id":"4392f6e50bbac53e","type":"ui_group","name":"命令","tab":"6e71507755b5c249","order":4,"disp":true,"width":"6","collapse":false,"className":""},{"id":"70940176.2b2d3","type":"mqtt-broker","name":"","broker":"test.mosquitto.org","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""},{"id":"6e71507755b5c249","type":"ui_tab","name":"2024-09","icon":"dashboard","disabled":false,"hidden":false}]


資料庫系統 建立資料庫(Create) 新增一筆(insert) 檢視資料庫(Select) 刪除所有資料(Delete) 刪除資料庫(Drop )




資料庫位置

C:\Users\User\.node-red\202409-LED_STATUS.db

建立資料庫(Create)

CREATE TABLE LEDSTATUS (id INTEGER,STATUS TEXT,Date DATE,Time TIME,PRIMARY KEY (id));

新增一筆(insert)

INSERT INTO LEDSTATUS (STATUS ,  Date , Time )values("on", "11/01" , "21:05")

檢視資料庫(Select)

SELECT * FROM LEDSTATUS

刪除所有資料(Delete)

DELETE from LEDSTATUS

刪除資料庫(Drop table )

DROP TABLE LEDSTATUS

 

LED控制命令



MQTT out

Broker : test.mosquitto.org :1883

Topic : alex9ufo/LED_control

ON

Payload : on

OFF

Payload : off

TIMER

Payload : timer

FLASH

Payload : flash

SQLite



Dashboard Text



 

資料庫



LED_STATUS

SQLite

</> SQL Query : Via msg.topic

建立資料庫

//CREATE TABLE LEDSTATUS (

//id INTEGER,

//STATUS TEXT,

//Date DATE,

//Time TIME,

//PRIMARY KEY (id)

//);

//CREATE TABLE LEDSTATUS (id INTEGER,STATUS TEXT,Date DATE,Time TIME,PRIMARY KEY (id));

msg.topic = "CREATE TABLE LEDSTATUS (id INTEGER,STATUS TEXT,Date DATE,Time TIME,PRIMARY KEY (id))";

return msg;

檢視資料庫資料

//INSERT INTO LEDSTATUS (

//id INTEGER,

//STATUS TEXT,

//Date DATE,

//Time TIME,

//PRIMARY KEY (id)

//);

 

//SELECT * FROM LEDSTATUS ORDER BY  id DESC LIMIT 50;

 

msg.topic = "SELECT * FROM LEDSTATUS ORDER BY id DESC LIMIT 50";

return msg;

刪除所有資料

//CREATE TABLE LEDSTATUS (

//id INTEGER,

//STATUS TEXT,

//Date DATE,

//Time TIME,

//PRIMARY KEY (id)

//);

//CREATE TABLE LEDSTATUS (id INTEGER,STATUS TEXT,Date DATE,Time TIME,PRIMARY KEY (id));

msg.topic = "DELETE from LEDSTATUS";

return msg;

Function 100

 

&

 

Function 99

 

確認

Yes , Cancel

var topic=msg.payload;

if (topic==""){

    return [msg,null];

   

}

if (topic=="Cancel"){

    return [null,msg];

   

}

return msg;

刪除資料庫

DROP Database

//CREATE TABLE LEDSTATUS (

//id INTEGER,

//STATUS TEXT,

//Date DATE,

//Time TIME,

//PRIMARY KEY (id)

//);

//CREATE TABLE LEDSTATUS (id INTEGER,STATUS TEXT,Date DATE,Time TIME,PRIMARY KEY (id));

msg.topic = "DROP TABLE LEDSTATUS";

return msg;

查詢一筆資料

//

var id = msg.payload.id;

var s=global.get("SEL1")

msg.topic="";

var temp="";

 

if (s==1)

{

    temp ="SELECT * FROM LEDSTATUS";

    temp=temp+" WHERE id LIKE '"+ id +"'";

}

msg.topic=temp;

global.set("SEL1",0);

 

return msg;

輸入id



刪除一筆資料

//

var id = msg.payload.id;

var s=global.get("SEL2")

msg.topic="";

var temp="";

 

if (s==2)

{

    temp ="DELETE FROM LEDSTATUS";

    temp=temp+" WHERE id LIKE '"+ id +"'";

}

 

msg.topic=temp;

global.set("SEL2",0)

return msg;

Store ID資料 function

//

var id = msg.payload.id;

global.set("ID",id)

return msg;

查詢一筆資料 function

//

var id = global.get("ID");

msg.topic="";

var temp="";

temp ="SELECT * FROM LEDSTATUS";

temp=temp+" WHERE id LIKE '"+ id +"'";

 

msg.topic=temp;

 

return msg;

 






Line Notify的發行



MQTT in



Function

msg.payload=" ---ESP32回來資料---" +msg.payload;

return msg;

Set Line API

msg.headers = {'content-type':'application/x-www-form-urlencoded','Authorization':'Bearer A4wwPNh12WqB7dlfeQyyIAwtggn1kfZSI5LkkCdia1gB'};

msg.payload = {"message":msg.payload};

return msg;

 

//oR7KdXvK1eobRr2sRRgsl4PMq23DjDlhfUs96SyUBZu



http 請求

POST

https://notify-api.line.me/api/notify



Dashboard Text

{{msg.payload}}


沒有留言:

張貼留言

113 學年度第 1 學期 RFID應用課程 Arduino程式

113 學年度第 1 學期 RFID應用課程 Arduino程式 https://www.mediafire.com/file/zr0h0p3iosq12jw/MFRC522+(2).7z/file 內含修改過後的 MFRC522 程式庫 (原程式有錯誤) //定義MFRC522...