2024年5月22日 星期三

ESP32 — MQTT 基本範例 (Node-Red , MQTT, SQLite)---2

 ESP32 — MQTT 基本範例 (Node-Red , MQTT, SQLite)---2



https://www.youtube.com/watch?v=lecWlbk5TVU

https://www.youtube.com/watch?v=BOuHvjaQmac



ESP32 arduino程式 (WOKWI)

// Wifi 與 MQttClient 程式庫
#include <ArduinoMqttClient.h>
#include <WiFi.h>
     
int LED = 2;  //定義LED接腳

char ssid[]="Wokwi-GUEST";
char pass[]="";

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/led_event";
const char *PubTopic2 = "alex9ufo/led/led_status";

const char willTopic[] = "alex9ufo/led/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/led_event") {
  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("-----------------------");
  }  

}

//===========================================================
//副程式  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);

}
//===========================================================
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();

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

Node-Red程式

[{"id":"e4de61f840ff4186","type":"sqlite","z":"1cb8c7b06aa228ec","mydb":"f5c97c74cc496505","sqlquery":"msg.topic","sql":"","name":"LED_STATUS","x":540,"y":420,"wires":[["9b39e7f9d0de3c6c"]]},{"id":"7d81c14d45b924d9","type":"function","z":"1cb8c7b06aa228ec","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":320,"y":420,"wires":[["e4de61f840ff4186"]]},{"id":"43a9ffc15d03ddc0","type":"ui_button","z":"1cb8c7b06aa228ec","name":"","group":"adfc6534bf97e4f1","order":7,"width":3,"height":1,"passthru":false,"label":"建立LED資料庫","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"建立資料庫","payloadType":"str","topic":"topic","topicType":"msg","x":100,"y":420,"wires":[["7d81c14d45b924d9","3b2a733d94aa3174"]]},{"id":"28d0c6542865dcab","type":"ui_button","z":"1cb8c7b06aa228ec","name":"","group":"adfc6534bf97e4f1","order":1,"width":3,"height":1,"passthru":false,"label":"ON","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"on","payloadType":"str","topic":"topic","topicType":"msg","x":70,"y":60,"wires":[["b276c95f6102c9e6","62a3a53547369382","363407e738e7cff2"]]},{"id":"aad1c2096a9d30b3","type":"ui_button","z":"1cb8c7b06aa228ec","name":"","group":"adfc6534bf97e4f1","order":2,"width":3,"height":1,"passthru":false,"label":"OFF","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"off","payloadType":"str","topic":"topic","topicType":"msg","x":70,"y":100,"wires":[["b276c95f6102c9e6","62a3a53547369382","363407e738e7cff2"]]},{"id":"908599ef8c86ed18","type":"ui_button","z":"1cb8c7b06aa228ec","name":"","group":"adfc6534bf97e4f1","order":3,"width":3,"height":1,"passthru":false,"label":"TIMER","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"timer","payloadType":"str","topic":"topic","topicType":"msg","x":80,"y":140,"wires":[["b276c95f6102c9e6","62a3a53547369382","363407e738e7cff2"]]},{"id":"274736447d2b843d","type":"ui_button","z":"1cb8c7b06aa228ec","name":"","group":"adfc6534bf97e4f1","order":4,"width":3,"height":1,"passthru":false,"label":"FLASH","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"flash","payloadType":"str","topic":"topic","topicType":"msg","x":80,"y":180,"wires":[["b276c95f6102c9e6","62a3a53547369382","363407e738e7cff2"]]},{"id":"5e6bcf2aef18ddd3","type":"function","z":"1cb8c7b06aa228ec","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":400,"y":280,"wires":[["4ca8f98f0e9682d0"]]},{"id":"b276c95f6102c9e6","type":"ui_audio","z":"1cb8c7b06aa228ec","name":"","group":"adfc6534bf97e4f1","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":255,"y":120,"wires":[],"l":false},{"id":"4ca8f98f0e9682d0","type":"sqlite","z":"1cb8c7b06aa228ec","mydb":"f5c97c74cc496505","sqlquery":"msg.topic","sql":"","name":"LED_STATUS","x":560,"y":280,"wires":[["818d2b33739f7962","12249c85f15ce393"]]},{"id":"9b39e7f9d0de3c6c","type":"debug","z":"1cb8c7b06aa228ec","name":"debug ","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":690,"y":420,"wires":[]},{"id":"818d2b33739f7962","type":"debug","z":"1cb8c7b06aa228ec","name":"debug ","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":750,"y":280,"wires":[]},{"id":"43a95c8e62974938","type":"ui_button","z":"1cb8c7b06aa228ec","name":"","group":"adfc6534bf97e4f1","order":9,"width":6,"height":1,"passthru":false,"label":"檢視資料庫資料","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"檢視資料","payloadType":"str","topic":"topic","topicType":"msg","x":100,"y":360,"wires":[["12249c85f15ce393","3b2a733d94aa3174"]]},{"id":"12249c85f15ce393","type":"function","z":"1cb8c7b06aa228ec","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":400,"y":360,"wires":[["35735ca5dc3427a1"]]},{"id":"9220d9a167300332","type":"ui_table","z":"1cb8c7b06aa228ec","group":"e9069c1d660c717d","name":"","order":1,"width":8,"height":10,"columns":[],"outputs":0,"cts":false,"x":730,"y":360,"wires":[]},{"id":"35735ca5dc3427a1","type":"sqlite","z":"1cb8c7b06aa228ec","mydb":"f5c97c74cc496505","sqlquery":"msg.topic","sql":"","name":"LED_STATUS","x":580,"y":360,"wires":[["9220d9a167300332"]]},{"id":"a78e0cc08c28827b","type":"sqlite","z":"1cb8c7b06aa228ec","mydb":"f5c97c74cc496505","sqlquery":"msg.topic","sql":"","name":"LED_STATUS","x":860,"y":480,"wires":[["767ef5c9f2337832"]]},{"id":"be3ad5eddc38f6e5","type":"ui_button","z":"1cb8c7b06aa228ec","name":"","group":"adfc6534bf97e4f1","order":8,"width":3,"height":1,"passthru":false,"label":"刪除所有資料 ","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"刪除所有資料 ","payloadType":"str","topic":"topic","topicType":"msg","x":100,"y":480,"wires":[["921595beceb1bf10","386929a18f8c3baa"]]},{"id":"3d5c971ba2dd4094","type":"function","z":"1cb8c7b06aa228ec","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":670,"y":480,"wires":[["a78e0cc08c28827b"]]},{"id":"921595beceb1bf10","type":"ui_audio","z":"1cb8c7b06aa228ec","name":"","group":"adfc6534bf97e4f1","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":225,"y":540,"wires":[],"l":false},{"id":"386929a18f8c3baa","type":"ui_toast","z":"1cb8c7b06aa228ec","position":"prompt","displayTime":"3","highlight":"","sendall":true,"outputs":1,"ok":"OK","cancel":"Cancel","raw":true,"className":"","topic":"","name":"","x":290,"y":480,"wires":[["78a9770191c1eb0f"]]},{"id":"78a9770191c1eb0f","type":"function","z":"1cb8c7b06aa228ec","name":"OK or Cancel","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":460,"y":480,"wires":[["3d5c971ba2dd4094"],[]]},{"id":"3b2a733d94aa3174","type":"ui_audio","z":"1cb8c7b06aa228ec","name":"","group":"adfc6534bf97e4f1","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":245,"y":380,"wires":[],"l":false},{"id":"767ef5c9f2337832","type":"link out","z":"1cb8c7b06aa228ec","name":"link out 57","mode":"link","links":["f3248f140546ebdf"],"x":845,"y":420,"wires":[]},{"id":"f3248f140546ebdf","type":"link in","z":"1cb8c7b06aa228ec","name":"link in 55","links":["767ef5c9f2337832"],"x":915,"y":420,"wires":[["12249c85f15ce393"]]},{"id":"62a3a53547369382","type":"mqtt out","z":"1cb8c7b06aa228ec","name":"Control LED out","topic":"alex9ufo/led/led_event","qos":"1","retain":"true","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"603bb104.d6134","x":280,"y":60,"wires":[]},{"id":"dbbfb6c3eed987cd","type":"mqtt in","z":"1cb8c7b06aa228ec","name":"Control LED in","topic":"alex9ufo/led/led_status","qos":"1","datatype":"auto-detect","broker":"603bb104.d6134","nl":false,"rap":true,"rh":0,"inputs":0,"x":100,"y":280,"wires":[["5e6bcf2aef18ddd3","0187a4131d9cbc68"]]},{"id":"363407e738e7cff2","type":"debug","z":"1cb8c7b06aa228ec","name":"debug ","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":310,"y":180,"wires":[]},{"id":"0187a4131d9cbc68","type":"ui_text","z":"1cb8c7b06aa228ec","group":"adfc6534bf97e4f1","order":5,"width":6,"height":1,"name":"","label":"MQTT來的","format":"<font color = RED>{{msg.payload}}","layout":"row-left","className":"","x":450,"y":220,"wires":[]},{"id":"0c28f914af70b6c9","type":"mqtt in","z":"1cb8c7b06aa228ec","name":"Control LED in","topic":"alex9ufo/led/led_status","qos":"1","datatype":"auto-detect","broker":"603bb104.d6134","nl":false,"rap":true,"rh":0,"inputs":0,"x":100,"y":220,"wires":[["3a3953fad846f64b"]]},{"id":"3a3953fad846f64b","type":"function","z":"1cb8c7b06aa228ec","name":"判斷","func":"var check= flow.get('ledsimu');\nvar mqttin=msg.payload;\n\nif (check == true)\n{\n    msg.payload=mqttin;\n    return [msg,null];\n}\nif (check == false)\n{\n    msg.payload=mqttin;\n    return [null ,msg];\n}\n","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":220,"wires":[["0187a4131d9cbc68","5e6bcf2aef18ddd3"],[]]},{"id":"f5c97c74cc496505","type":"sqlitedb","db":"C:\\Users\\User\\.node-red\\2024EX_LED2.db","mode":"RWC"},{"id":"adfc6534bf97e4f1","type":"ui_group","name":"LED2_控制","tab":"2eed7cc3b24b8f69","order":2,"disp":true,"width":"6","collapse":false,"className":""},{"id":"e9069c1d660c717d","type":"ui_group","name":"顯示區","tab":"2eed7cc3b24b8f69","order":3,"disp":true,"width":8,"collapse":false,"className":""},{"id":"603bb104.d6134","type":"mqtt-broker","name":"","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"","autoConnect":true,"usetls":false,"compatmode":false,"protocolVersion":"4","keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""},{"id":"2eed7cc3b24b8f69","type":"ui_tab","name":"WOKWI_LED","icon":"dashboard","order":125,"disabled":false,"hidden":false}]



2024-05-27 更新  Node-Red 含 Line Notify






[{"id":"e4de61f840ff4186","type":"sqlite","z":"1cb8c7b06aa228ec","mydb":"f5c97c74cc496505","sqlquery":"msg.topic","sql":"","name":"LED_STATUS","x":540,"y":420,"wires":[["9b39e7f9d0de3c6c"]]},{"id":"7d81c14d45b924d9","type":"function","z":"1cb8c7b06aa228ec","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":320,"y":420,"wires":[["e4de61f840ff4186"]]},{"id":"43a9ffc15d03ddc0","type":"ui_button","z":"1cb8c7b06aa228ec","name":"","group":"adfc6534bf97e4f1","order":7,"width":3,"height":1,"passthru":false,"label":"建立LED資料庫","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"建立資料庫","payloadType":"str","topic":"topic","topicType":"msg","x":100,"y":420,"wires":[["7d81c14d45b924d9","3b2a733d94aa3174"]]},{"id":"28d0c6542865dcab","type":"ui_button","z":"1cb8c7b06aa228ec","name":"","group":"adfc6534bf97e4f1","order":1,"width":3,"height":1,"passthru":false,"label":"ON","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"on","payloadType":"str","topic":"topic","topicType":"msg","x":70,"y":60,"wires":[["b276c95f6102c9e6","62a3a53547369382","363407e738e7cff2"]]},{"id":"aad1c2096a9d30b3","type":"ui_button","z":"1cb8c7b06aa228ec","name":"","group":"adfc6534bf97e4f1","order":2,"width":3,"height":1,"passthru":false,"label":"OFF","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"off","payloadType":"str","topic":"topic","topicType":"msg","x":70,"y":100,"wires":[["b276c95f6102c9e6","62a3a53547369382","363407e738e7cff2"]]},{"id":"908599ef8c86ed18","type":"ui_button","z":"1cb8c7b06aa228ec","name":"","group":"adfc6534bf97e4f1","order":3,"width":3,"height":1,"passthru":false,"label":"TIMER","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"timer","payloadType":"str","topic":"topic","topicType":"msg","x":80,"y":140,"wires":[["b276c95f6102c9e6","62a3a53547369382","363407e738e7cff2"]]},{"id":"274736447d2b843d","type":"ui_button","z":"1cb8c7b06aa228ec","name":"","group":"adfc6534bf97e4f1","order":4,"width":3,"height":1,"passthru":false,"label":"FLASH","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"flash","payloadType":"str","topic":"topic","topicType":"msg","x":80,"y":180,"wires":[["b276c95f6102c9e6","62a3a53547369382","363407e738e7cff2"]]},{"id":"5e6bcf2aef18ddd3","type":"function","z":"1cb8c7b06aa228ec","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":400,"y":280,"wires":[["4ca8f98f0e9682d0"]]},{"id":"b276c95f6102c9e6","type":"ui_audio","z":"1cb8c7b06aa228ec","name":"","group":"adfc6534bf97e4f1","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":255,"y":120,"wires":[],"l":false},{"id":"4ca8f98f0e9682d0","type":"sqlite","z":"1cb8c7b06aa228ec","mydb":"f5c97c74cc496505","sqlquery":"msg.topic","sql":"","name":"LED_STATUS","x":560,"y":280,"wires":[["818d2b33739f7962","12249c85f15ce393"]]},{"id":"9b39e7f9d0de3c6c","type":"debug","z":"1cb8c7b06aa228ec","name":"debug ","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":690,"y":420,"wires":[]},{"id":"818d2b33739f7962","type":"debug","z":"1cb8c7b06aa228ec","name":"debug ","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":750,"y":280,"wires":[]},{"id":"43a95c8e62974938","type":"ui_button","z":"1cb8c7b06aa228ec","name":"","group":"adfc6534bf97e4f1","order":9,"width":6,"height":1,"passthru":false,"label":"檢視資料庫資料","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"檢視資料","payloadType":"str","topic":"topic","topicType":"msg","x":100,"y":360,"wires":[["12249c85f15ce393","3b2a733d94aa3174"]]},{"id":"12249c85f15ce393","type":"function","z":"1cb8c7b06aa228ec","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":400,"y":360,"wires":[["35735ca5dc3427a1"]]},{"id":"9220d9a167300332","type":"ui_table","z":"1cb8c7b06aa228ec","group":"e9069c1d660c717d","name":"","order":1,"width":8,"height":10,"columns":[],"outputs":0,"cts":false,"x":730,"y":360,"wires":[]},{"id":"35735ca5dc3427a1","type":"sqlite","z":"1cb8c7b06aa228ec","mydb":"f5c97c74cc496505","sqlquery":"msg.topic","sql":"","name":"LED_STATUS","x":580,"y":360,"wires":[["9220d9a167300332"]]},{"id":"a78e0cc08c28827b","type":"sqlite","z":"1cb8c7b06aa228ec","mydb":"f5c97c74cc496505","sqlquery":"msg.topic","sql":"","name":"LED_STATUS","x":860,"y":480,"wires":[["767ef5c9f2337832"]]},{"id":"be3ad5eddc38f6e5","type":"ui_button","z":"1cb8c7b06aa228ec","name":"","group":"adfc6534bf97e4f1","order":8,"width":3,"height":1,"passthru":false,"label":"刪除所有資料 ","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"刪除所有資料 ","payloadType":"str","topic":"topic","topicType":"msg","x":100,"y":480,"wires":[["921595beceb1bf10","386929a18f8c3baa"]]},{"id":"3d5c971ba2dd4094","type":"function","z":"1cb8c7b06aa228ec","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":670,"y":480,"wires":[["a78e0cc08c28827b"]]},{"id":"921595beceb1bf10","type":"ui_audio","z":"1cb8c7b06aa228ec","name":"","group":"adfc6534bf97e4f1","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":225,"y":540,"wires":[],"l":false},{"id":"386929a18f8c3baa","type":"ui_toast","z":"1cb8c7b06aa228ec","position":"prompt","displayTime":"3","highlight":"","sendall":true,"outputs":1,"ok":"OK","cancel":"Cancel","raw":true,"className":"","topic":"","name":"","x":290,"y":480,"wires":[["78a9770191c1eb0f"]]},{"id":"78a9770191c1eb0f","type":"function","z":"1cb8c7b06aa228ec","name":"OK or Cancel","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":460,"y":480,"wires":[["3d5c971ba2dd4094"],[]]},{"id":"3b2a733d94aa3174","type":"ui_audio","z":"1cb8c7b06aa228ec","name":"","group":"adfc6534bf97e4f1","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":245,"y":380,"wires":[],"l":false},{"id":"767ef5c9f2337832","type":"link out","z":"1cb8c7b06aa228ec","name":"link out 57","mode":"link","links":["f3248f140546ebdf"],"x":845,"y":420,"wires":[]},{"id":"f3248f140546ebdf","type":"link in","z":"1cb8c7b06aa228ec","name":"link in 55","links":["767ef5c9f2337832"],"x":915,"y":420,"wires":[["12249c85f15ce393"]]},{"id":"62a3a53547369382","type":"mqtt out","z":"1cb8c7b06aa228ec","name":"Control LED out","topic":"alex9ufo/led/led_event","qos":"1","retain":"true","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"603bb104.d6134","x":280,"y":60,"wires":[]},{"id":"dbbfb6c3eed987cd","type":"mqtt in","z":"1cb8c7b06aa228ec","name":"Control LED in","topic":"alex9ufo/led/led_status","qos":"1","datatype":"auto-detect","broker":"603bb104.d6134","nl":false,"rap":true,"rh":0,"inputs":0,"x":100,"y":280,"wires":[["5e6bcf2aef18ddd3","0187a4131d9cbc68"]]},{"id":"363407e738e7cff2","type":"debug","z":"1cb8c7b06aa228ec","name":"debug ","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":310,"y":180,"wires":[]},{"id":"0187a4131d9cbc68","type":"ui_text","z":"1cb8c7b06aa228ec","group":"adfc6534bf97e4f1","order":5,"width":6,"height":1,"name":"","label":"MQTT來的","format":"<font color = RED>{{msg.payload}}","layout":"row-left","className":"","x":450,"y":220,"wires":[]},{"id":"0c28f914af70b6c9","type":"mqtt in","z":"1cb8c7b06aa228ec","name":"Control LED in","topic":"alex9ufo/led/led_status","qos":"1","datatype":"auto-detect","broker":"603bb104.d6134","nl":false,"rap":true,"rh":0,"inputs":0,"x":100,"y":220,"wires":[["3a3953fad846f64b"]]},{"id":"3a3953fad846f64b","type":"function","z":"1cb8c7b06aa228ec","name":"判斷","func":"var check= flow.get('ledsimu');\nvar mqttin=msg.payload;\n\nif (check == true)\n{\n    msg.payload=mqttin;\n    return [msg,null];\n}\nif (check == false)\n{\n    msg.payload=mqttin;\n    return [null ,msg];\n}\n","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":220,"wires":[["0187a4131d9cbc68","5e6bcf2aef18ddd3"],[]]},{"id":"620c6304c6809821","type":"comment","z":"1cb8c7b06aa228ec","name":"Line API","info":"","x":80,"y":600,"wires":[]},{"id":"7139ab918dd842bf","type":"function","z":"1cb8c7b06aa228ec","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":310,"y":640,"wires":[["37578ace0bc47a29"]]},{"id":"37578ace0bc47a29","type":"function","z":"1cb8c7b06aa228ec","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,"initialize":"","finalize":"","libs":[],"x":490,"y":640,"wires":[["1a27b5a0ccdeb3f0"]]},{"id":"1a27b5a0ccdeb3f0","type":"http request","z":"1cb8c7b06aa228ec","name":"","method":"POST","ret":"txt","paytoqs":false,"url":"https://notify-api.line.me/api/notify","tls":"","persist":false,"proxy":"","authType":"","x":660,"y":640,"wires":[["0df091a0ac52c38c"]]},{"id":"0df091a0ac52c38c","type":"debug","z":"1cb8c7b06aa228ec","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":850,"y":640,"wires":[]},{"id":"ad5fdcf07bb10bdf","type":"mqtt in","z":"1cb8c7b06aa228ec","name":"Control LED in","topic":"alex9ufo/led/led_status","qos":"1","datatype":"auto-detect","broker":"603bb104.d6134","nl":false,"rap":true,"rh":0,"inputs":0,"x":100,"y":640,"wires":[["7139ab918dd842bf"]]},{"id":"f5c97c74cc496505","type":"sqlitedb","db":"C:\\Users\\User\\.node-red\\2024EX_LED2.db","mode":"RWC"},{"id":"adfc6534bf97e4f1","type":"ui_group","name":"LED2_控制","tab":"2eed7cc3b24b8f69","order":2,"disp":true,"width":"6","collapse":false,"className":""},{"id":"e9069c1d660c717d","type":"ui_group","name":"顯示區","tab":"2eed7cc3b24b8f69","order":3,"disp":true,"width":8,"collapse":false,"className":""},{"id":"603bb104.d6134","type":"mqtt-broker","name":"","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"","autoConnect":true,"usetls":false,"compatmode":false,"protocolVersion":"4","keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""},{"id":"2eed7cc3b24b8f69","type":"ui_tab","name":"WOKWI_LED","icon":"dashboard","order":125,"disabled":false,"hidden":false}]


設定 LINE Notify  https://md.webduino.io/s/LCGRt1Jve

LINE Notify」的服務,透過開通權杖 ( Token ),讓 Node-Red(LED) 主動傳訊息至 LINE 帳號中,可用來結合通知、警告等應用。

步驟:

  1. 準備好自己的 LINE 帳號密碼。

  2. 前往 LINE Notify 官方網站。( 請使用電腦開啟!)

  3. 登入帳號。

  4. 點擊右上角帳號,選擇選單中的「個人頁面」。

  5. 點擊「發行權杖」。

  6. 設定權杖,填寫權杖名稱、接收通知的聊天室。這裡以「透過1對1聊天接收LINE Notify的通知」為例。

    • 權杖名稱:發送訊息時,會顯示的名稱。
    • 接收通知的聊天室:要使用 Notify 的聊天室,或是個人訊息。

  7. 按下「發行」後,會出現發行的權杖如下圖,並將權杖複製。

    權杖只會顯示 1 次,每次使用程式積木都需要輸入,為避免忘記,建議先將權杖記錄下來。

  8. 設定完成後,就可以在「已連動的服務」中看到剛剛設定的權杖了。

  9. 打開 LINE 應用程式,可以看到名稱為「LINE Notify」的帳號發送訊息「已發行個人存取權杖。」。



沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

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