2024年10月27日 星期日

OPEN DATA AQI 空氣品質監測網 + NODE-RED

OPEN DATA AQI 空氣品質監測網 + NODE-RED 


首頁 - 空氣品質監測網 https://airtw.moenv.gov.tw/

https://data.gov.tw/datasets/search?p=1&size=10&s=_score_desc&rft=AQI





https://data.gov.tw/dataset/40448



https://data.moenv.gov.tw/api/v2/aqx_p_432?api_key=e8dd42e6-9b8b-43f8-991e-b3dee723a52d&limit=1000&sort=ImportDate desc&format=CSV




https://data.moenv.gov.tw/api/v2/aqx_p_432?api_key=e8dd42e6-9b8b-43f8-991e-b3dee723a52d&limit=1000&sort=ImportDate desc&format=JSON





將pm2.5改成pm25   修改字體顏色 msg.color="red";

var a = context.global.aqi_records;

var site_name=global.get('site_Name');

var pm25;

a.forEach(function(e,i){

    if(e.sitename== site_name){

        pm25 = e['pm2.5'];

    }

});

msg.payload=pm25;

msg.color="red";

return msg;


修改字體顏色 msg.color="red";

<font color={{msg.color}}> <font SIZE=14> {{msg.payload}}


程式:

[{"id":"1a6d4e4dfeb2a89c","type":"inject","z":"e4a0cb7337ebc1ca","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":80,"wires":[["bdd41465f672e54b","05512d2d20bdc046"]]},{"id":"bdd41465f672e54b","type":"http request","z":"e4a0cb7337ebc1ca","name":"temp request","method":"GET","ret":"txt","paytoqs":"ignore","url":"https://data.moenv.gov.tw/api/v2/aqx_p_432?api_key=e8dd42e6-9b8b-43f8-991e-b3dee723a52d&limit=1000&sort=ImportDate desc&format=JSON","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":270,"y":80,"wires":[["50bf4d2a7f40701e","2e1625eebcfbadb8"]]},{"id":"2e1625eebcfbadb8","type":"json","z":"e4a0cb7337ebc1ca","name":"","property":"payload","action":"","pretty":false,"x":450,"y":80,"wires":[["a5028a1af5858c50","00028a2228d705ba"]]},{"id":"27bbad2d119d1e8b","type":"debug","z":"e4a0cb7337ebc1ca","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":810,"y":20,"wires":[]},{"id":"a5028a1af5858c50","type":"debug","z":"e4a0cb7337ebc1ca","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":590,"y":40,"wires":[]},{"id":"00028a2228d705ba","type":"function","z":"e4a0cb7337ebc1ca","name":"Get records function ","func":"var msg1=msg.payload.records;\nmsg.payload=msg1;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":640,"y":80,"wires":[["27bbad2d119d1e8b","27277e7be99c652f","7658e8cd086cfb2d"]]},{"id":"50bf4d2a7f40701e","type":"debug","z":"e4a0cb7337ebc1ca","name":"debug ","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":390,"y":40,"wires":[]},{"id":"7658e8cd086cfb2d","type":"ui_table","z":"e4a0cb7337ebc1ca","group":"e25f0f5df917313b","name":"","order":1,"width":30,"height":15,"columns":[],"outputs":0,"cts":false,"x":850,"y":80,"wires":[]},{"id":"05512d2d20bdc046","type":"ui_dropdown","z":"e4a0cb7337ebc1ca","name":"","label":"","tooltip":"","place":"請選擇","group":"644296dd0b088d4a","order":1,"width":0,"height":0,"passthru":true,"multiple":false,"options":[{"label":"基隆","value":"基隆","type":"str"},{"label":"汐止","value":"汐止","type":"str"},{"label":"萬里","value":"萬里","type":"str"},{"label":"新店","value":"新店","type":"str"},{"label":"土城","value":"土城","type":"str"},{"label":"板橋","value":"板橋","type":"str"},{"label":"新莊","value":"新莊","type":"str"},{"label":"菜寮","value":"菜寮","type":"str"},{"label":"林口","value":"林口","type":"str"},{"label":"淡水","value":"淡水","type":"str"},{"label":"士林","value":"士林","type":"str"},{"label":"中山","value":"中山","type":"str"},{"label":"萬華","value":"萬華","type":"str"},{"label":"古亭","value":"古亭","type":"str"},{"label":"松山","value":"松山","type":"str"},{"label":"大同","value":"大同","type":"str"},{"label":"桃園","value":"桃園","type":"str"},{"label":"大園","value":"大園","type":"str"},{"label":"觀音","value":"觀音","type":"str"},{"label":"平鎮","value":"平鎮","type":"str"},{"label":"龍潭","value":"龍潭","type":"str"},{"label":"湖口","value":"湖口","type":"str"},{"label":"竹東","value":"竹東","type":"str"},{"label":"新竹","value":"新竹","type":"str"},{"label":"頭份","value":"頭份","type":"str"},{"label":"苗栗","value":"苗栗","type":"str"},{"label":"三義","value":"三義","type":"str"},{"label":"豐原","value":"豐原","type":"str"},{"label":"沙鹿","value":"沙鹿","type":"str"},{"label":"大里","value":"大里","type":"str"},{"label":"忠明","value":"忠明","type":"str"},{"label":"西屯","value":"西屯","type":"str"},{"label":"彰化","value":"彰化","type":"str"},{"label":"線西","value":"線西","type":"str"},{"label":"二林","value":"二林","type":"str"},{"label":"南投","value":"南投","type":"str"},{"label":"斗六","value":"斗六","type":"str"},{"label":"崙背","value":"崙背","type":"str"},{"label":"新港","value":"新港","type":"str"},{"label":"朴子","value":"朴子","type":"str"},{"label":"臺西","value":"臺西","type":"str"},{"label":"嘉義","value":"嘉義","type":"str"},{"label":"新營","value":"新營","type":"str"},{"label":"善化","value":"善化","type":"str"},{"label":"安南","value":"安南","type":"str"},{"label":"臺南","value":"臺南","type":"str"},{"label":"美濃","value":"美濃","type":"str"},{"label":"橋頭","value":"橋頭","type":"str"},{"label":"仁武","value":"仁武","type":"str"},{"label":"鳳山","value":"鳳山","type":"str"},{"label":"大寮","value":"大寮","type":"str"},{"label":"林園","value":"林園","type":"str"},{"label":"楠梓","value":"楠梓","type":"str"},{"label":"左營","value":"左營","type":"str"},{"label":"前金","value":"前金","type":"str"},{"label":"前鎮","value":"前鎮","type":"str"},{"label":"小港","value":"小港","type":"str"},{"label":"屏東","value":"屏東","type":"str"},{"label":"潮州","value":"潮州","type":"str"},{"label":"恆春","value":"恆春","type":"str"},{"label":"臺東","value":"臺東","type":"str"},{"label":"花蓮","value":"花蓮","type":"str"},{"label":"陽明","value":"陽明","type":"str"},{"label":"宜蘭","value":"宜蘭","type":"str"},{"label":"冬山","value":"冬山","type":"str"},{"label":"三重","value":"三重","type":"str"},{"label":"中壢","value":"中壢","type":"str"},{"label":"竹山","value":"竹山","type":"str"},{"label":"永和","value":"永和","type":"str"},{"label":"復興","value":"復興(高雄)","type":"str"},{"label":"埔里","value":"埔里","type":"str"},{"label":"馬祖","value":"馬祖","type":"str"},{"label":"金門","value":"金門","type":"str"},{"label":"馬公","value":"馬公","type":"str"},{"label":"關山","value":"關山","type":"str"},{"label":"麥寮","value":"麥寮","type":"str"},{"label":"富貴角","value":"富貴角","type":"str"},{"label":"大城","value":"大城","type":"str"},{"label":"彰化(員林)","value":"彰化(員林)","type":"str"},{"label":"高雄(湖內)","value":"高雄(湖內)","type":"str"},{"label":"臺南(麻豆)","value":"臺南(麻豆)","type":"str"},{"label":"屏東(琉球)","value":"屏東(琉球)","type":"str"},{"label":"新北(樹林)","value":"新北(樹林)","type":"str"},{"label":"宜蘭(頭城)","value":"宜蘭(頭城)","type":"str"},{"label":"屏東(枋山)","value":"屏東(枋山)","type":"str"}],"payload":"","topic":"topic","topicType":"msg","className":"","x":120,"y":200,"wires":[["1fcb9a66b4cab783","a4250be6a72bb5cc","bdd41465f672e54b"]]},{"id":"406a82461ebb6127","type":"debug","z":"e4a0cb7337ebc1ca","name":"debug ","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":470,"y":160,"wires":[]},{"id":"1fcb9a66b4cab783","type":"debug","z":"e4a0cb7337ebc1ca","name":"debug ","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":290,"y":160,"wires":[]},{"id":"a4250be6a72bb5cc","type":"function","z":"e4a0cb7337ebc1ca","name":"save sitename ","func":"var count=flow.get('siteName') || \"\";\nvar siteName=msg.payload;\nglobal.set('site_Name',siteName);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":200,"wires":[["406a82461ebb6127"]]},{"id":"27277e7be99c652f","type":"function","z":"e4a0cb7337ebc1ca","name":"Set context.global.aqi_records","func":"context.global.aqi_records=msg.payload;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":890,"y":120,"wires":[["c860954eba3df369","05347c3ee3a8e576"]]},{"id":"c860954eba3df369","type":"function","z":"e4a0cb7337ebc1ca","name":"get  Sitename  all records","func":"var rawdata = context.global.aqi_records;\nvar site_number=rawdata.length;\n\nvar site_name=global.get('site_Name');\n\nvar site_array=[];\nvar i;\nvar j=0;\n\nfor (i=0; i<site_number;i++)\n{\n    if (site_name==rawdata[i].sitename)\n    {\n      site_array[j]=rawdata[i];\n      j=j+1;\n    }\n}\n\nmsg.payload=site_array;\n//msg.payload= site_name; for debug\n \nflow.set(\"aqi_getdata\",site_array);\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":870,"y":180,"wires":[["6517e50a61650d90","2db6c6e67d3f94ef","be07333b50c1cb52"]]},{"id":"6517e50a61650d90","type":"debug","z":"e4a0cb7337ebc1ca","name":"debug ","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1110,"y":160,"wires":[]},{"id":"05347c3ee3a8e576","type":"debug","z":"e4a0cb7337ebc1ca","name":"debug ","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1090,"y":80,"wires":[]},{"id":"b4ab61d11191a319","type":"ui_text","z":"e4a0cb7337ebc1ca","group":"644296dd0b088d4a","order":2,"width":0,"height":0,"name":"","label":"county:","format":"{{msg.payload}}","layout":"row-left","className":"","x":1000,"y":240,"wires":[]},{"id":"3d95c6cd343b76ee","type":"function","z":"e4a0cb7337ebc1ca","name":"get   county","func":"\nvar county= msg.payload[0].county;\nmsg.payload=county;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":830,"y":240,"wires":[["b4ab61d11191a319"]]},{"id":"3852fb5c8289ed21","type":"function","z":"e4a0cb7337ebc1ca","name":"get   aqi","func":"\nvar aqi= msg.payload[0].aqi;\nmsg.payload=aqi;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":810,"y":280,"wires":[["cba30b1a0bf5e4a2"]]},{"id":"cba30b1a0bf5e4a2","type":"ui_text","z":"e4a0cb7337ebc1ca","group":"644296dd0b088d4a","order":3,"width":0,"height":0,"name":"","label":"aqi   :","format":"{{msg.payload}}","layout":"row-left","className":"","x":990,"y":280,"wires":[]},{"id":"2db6c6e67d3f94ef","type":"link out","z":"e4a0cb7337ebc1ca","name":"link out 60","mode":"link","links":["aa70689dc1280f43"],"x":1045,"y":200,"wires":[]},{"id":"aa70689dc1280f43","type":"link in","z":"e4a0cb7337ebc1ca","name":"link in 58","links":["2db6c6e67d3f94ef"],"x":515,"y":420,"wires":[["3d95c6cd343b76ee","3852fb5c8289ed21","d017f0b0b70ef380","283db37d57500bcd","817b71e58522ab11","7718658e755291db","af256cb701db155d","ea3c8ea89d323a22","b304e296422ceb9f","03e94ca4c106cb7b","2ff4bf6824d17fc5","dc6bc4529078d47e","e9290affe8d87f7a"]]},{"id":"283db37d57500bcd","type":"function","z":"e4a0cb7337ebc1ca","name":"get  status","func":"\nvar status= msg.payload[0].status;\nmsg.payload=status;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":820,"y":360,"wires":[["79cfc87ecd3257ed"]]},{"id":"79cfc87ecd3257ed","type":"ui_text","z":"e4a0cb7337ebc1ca","group":"644296dd0b088d4a","order":5,"width":0,"height":0,"name":"","label":"status:","format":"{{msg.payload}}","layout":"row-left","className":"","x":990,"y":360,"wires":[]},{"id":"d017f0b0b70ef380","type":"function","z":"e4a0cb7337ebc1ca","name":"get  pollutan","func":"\nvar pollutan= msg.payload[0].pollutan;\nmsg.payload=pollutan;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":830,"y":320,"wires":[["a1d62d271bd99d2a"]]},{"id":"a1d62d271bd99d2a","type":"ui_text","z":"e4a0cb7337ebc1ca","group":"644296dd0b088d4a","order":4,"width":0,"height":0,"name":"","label":"pollutan:","format":"{{msg.payload}}","layout":"row-left","className":"","x":1000,"y":320,"wires":[]},{"id":"817b71e58522ab11","type":"function","z":"e4a0cb7337ebc1ca","name":"get  so2","func":"\nvar so2= msg.payload[0].so2;\nmsg.payload=so2;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":820,"y":400,"wires":[["686ede1bd8e554f0"]]},{"id":"686ede1bd8e554f0","type":"ui_text","z":"e4a0cb7337ebc1ca","group":"644296dd0b088d4a","order":6,"width":0,"height":0,"name":"","label":"so2:","format":"{{msg.payload}}","layout":"row-left","className":"","x":990,"y":400,"wires":[]},{"id":"7718658e755291db","type":"function","z":"e4a0cb7337ebc1ca","name":"get  nox","func":"\nvar nox= msg.payload[0].nox;\nmsg.payload=nox;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":820,"y":600,"wires":[["2aad8b7a709ac247"]]},{"id":"2aad8b7a709ac247","type":"ui_text","z":"e4a0cb7337ebc1ca","group":"644296dd0b088d4a","order":12,"width":0,"height":0,"name":"","label":"nox:","format":"{{msg.payload}}","layout":"row-left","className":"","x":990,"y":600,"wires":[]},{"id":"af256cb701db155d","type":"function","z":"e4a0cb7337ebc1ca","name":"get  co","func":"\nvar co= msg.payload[0].co;\nmsg.payload=co;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":810,"y":440,"wires":[["babf1fe30aa0fd05"]]},{"id":"babf1fe30aa0fd05","type":"ui_text","z":"e4a0cb7337ebc1ca","group":"644296dd0b088d4a","order":7,"width":0,"height":0,"name":"","label":"co:","format":"{{msg.payload}}","layout":"row-left","className":"","x":990,"y":440,"wires":[]},{"id":"ea3c8ea89d323a22","type":"function","z":"e4a0cb7337ebc1ca","name":"get  o3","func":"\nvar o3= msg.payload[0].o3;\nmsg.payload=o3;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":810,"y":480,"wires":[["faa2ddbc2970a3be"]]},{"id":"faa2ddbc2970a3be","type":"ui_text","z":"e4a0cb7337ebc1ca","group":"644296dd0b088d4a","order":8,"width":0,"height":0,"name":"","label":"o3:","format":"{{msg.payload}}","layout":"row-left","className":"","x":990,"y":480,"wires":[]},{"id":"b304e296422ceb9f","type":"function","z":"e4a0cb7337ebc1ca","name":"get  pm10","func":"\nvar pm10= msg.payload[0].pm10;\nmsg.payload=pm10;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":820,"y":520,"wires":[["7eba1341c7e63f50"]]},{"id":"7eba1341c7e63f50","type":"ui_text","z":"e4a0cb7337ebc1ca","group":"644296dd0b088d4a","order":9,"width":0,"height":0,"name":"","label":"pm10:","format":"{{msg.payload}}","layout":"row-left","className":"","x":990,"y":520,"wires":[]},{"id":"04052761df475c81","type":"ui_text","z":"e4a0cb7337ebc1ca","group":"644296dd0b088d4a","order":10,"width":0,"height":0,"name":"","label":"pm2.5:","format":"<font color={{msg.color}}> <font SIZE=14> {{msg.payload}}","layout":"row-left","className":"","x":1170,"y":300,"wires":[]},{"id":"03e94ca4c106cb7b","type":"function","z":"e4a0cb7337ebc1ca","name":"get  no2","func":"\nvar no2= msg.payload[0].no2;\nmsg.payload=no2;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":820,"y":560,"wires":[["df445568a6efc59b"]]},{"id":"df445568a6efc59b","type":"ui_text","z":"e4a0cb7337ebc1ca","group":"644296dd0b088d4a","order":11,"width":0,"height":0,"name":"","label":"no2","format":"{{msg.payload}}","layout":"row-left","className":"","x":990,"y":560,"wires":[]},{"id":"2ff4bf6824d17fc5","type":"function","z":"e4a0cb7337ebc1ca","name":"get  no","func":"\nvar no= msg.payload[0].no;\nmsg.payload=no;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":810,"y":640,"wires":[["c2e7c6f50b7364ad"]]},{"id":"c2e7c6f50b7364ad","type":"ui_text","z":"e4a0cb7337ebc1ca","group":"644296dd0b088d4a","order":12,"width":0,"height":0,"name":"","label":"no:","format":"{{msg.payload}}","layout":"row-left","className":"","x":990,"y":640,"wires":[]},{"id":"dc6bc4529078d47e","type":"function","z":"e4a0cb7337ebc1ca","name":"get wind_speed","func":"\nvar wind_speed= msg.payload[0].wind_speed;\nmsg.payload=wind_speed;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":840,"y":680,"wires":[["2b2355e086857a86"]]},{"id":"2b2355e086857a86","type":"ui_text","z":"e4a0cb7337ebc1ca","group":"644296dd0b088d4a","order":12,"width":0,"height":0,"name":"","label":"wind_speed:","format":"{{msg.payload}}","layout":"row-left","className":"","x":1010,"y":680,"wires":[]},{"id":"e9290affe8d87f7a","type":"function","z":"e4a0cb7337ebc1ca","name":"get wind_direc","func":"msg.color=\"red\";\nvar wind_direc= msg.payload[0].wind_direc;\nmsg.payload=wind_direc;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":840,"y":720,"wires":[["3511d61f45c7e808"]]},{"id":"3511d61f45c7e808","type":"ui_text","z":"e4a0cb7337ebc1ca","group":"644296dd0b088d4a","order":12,"width":0,"height":0,"name":"","label":"wind_direc:","format":"<font color={{msg.color}}> {{msg.payload}}","layout":"row-left","className":"","x":1010,"y":720,"wires":[]},{"id":"be07333b50c1cb52","type":"function","z":"e4a0cb7337ebc1ca","name":"function get pm2.5","func":"var a = context.global.aqi_records;\nvar site_name=global.get('site_Name');\nvar pm25;\na.forEach(function(e,i){\n    if(e.sitename== site_name){\n        pm25 = e['pm2.5'];\n    }\n});\nmsg.payload=pm25;\nmsg.color=\"red\";\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1170,"y":200,"wires":[["04052761df475c81","a7f7fa6e0afc7fba"]]},{"id":"a7f7fa6e0afc7fba","type":"debug","z":"e4a0cb7337ebc1ca","name":"debug ","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1330,"y":180,"wires":[]},{"id":"e25f0f5df917313b","type":"ui_group","name":"Default","tab":"846ca1f8a495a33e","order":2,"disp":true,"width":30,"collapse":false,"className":""},{"id":"644296dd0b088d4a","type":"ui_group","name":"SiteName","tab":"846ca1f8a495a33e","order":1,"disp":true,"width":"3","collapse":false,"className":""},{"id":"846ca1f8a495a33e","type":"ui_tab","name":"空氣品質指標","icon":"dashboard","disabled":false,"hidden":false}]




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}}


Messaging API作為替代方案

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