- 安裝 InfluxDB 資料庫
- 比較 InfluxDB 與關聯式資庫的不同
- 如何把ESP32送到 MQTT 資料寫入InfluxDB 資料庫
- 讀取InfluxDB 資料庫, 再把這些資料在資訊看板上顯示出來
Format code for writing InfluxDB
// Create the object key to match DB filed name from topic name(studio/temp1) msg.payload["temp"] = msg.payload["studio/temp1"]; // Delete the unwanted studio/temp1 delete msg.payload["studio/temp1"]; // Update temp from string to float msg.payload["temp"] = parseFloat(msg.payload["temp"]); msg.payload["humd"] = msg.payload["studio/humd1"]; delete msg.payload["studio/humd1"]; msg.payload["humd"] = parseFloat(msg.payload["humd"]); //remove unused topic delete msg.topic; //Create a temporary array var data = []; data.push(msg.payload); //Add object to DB Tag(index field) data.push({"location":"studio"}); //msg.payload.push({"location":"studio"}); //Copy temporary array to msg.payload msg.payload = data; return msg;
Format code for reading historical InfluxDB data
//Source from: https://discourse.nodered.org/t/display-2-extracts-from-influxdb-in-an-ui-chart/29665 var series = ["Temperature °C", "Humdity %"]; var labels = ["temp", "Humdity"]; var data0 = "["; var thetime0; for (var i=0; i < msg.payload.results[0].series[0].values.length; i++) { thetime0 = (msg.payload.results[0].series[0].values[i][0]); // Some manipulation of the time may be required thetime0 = Date.parse(thetime0); data0 += '{ "x":' + thetime0 + ', "y":' + (msg.payload.results[0].series[0].values[i][1]) + '}'; if (i < (msg.payload.results[0].series[0].values.length - 1)) { data0 += "," } else { data0 += "]" } } var data1 = "["; var thetime1; for (var j=0; j < msg.payload.results[0].series[0].values.length; j++) { thetime1 = (msg.payload.results[0].series[0].values[j][0]); // Some manipulation of the time may be required thetime1 = Date.parse(thetime1); data1 += '{ "x":' + thetime1 + ', "y":' + (msg.payload.results[0].series[0].values[j][2]) + '}'; if (j < (msg.payload.results[0].series[0].values.length - 1)) { data1 += "," } else { data1 += "]" } } var data = [data0, data1]; var jsondata = [JSON.parse(data0), JSON.parse(data1)]; msg.payload = [{"series": series, "data": jsondata, "labels": labels}]; msg.dataPoints = data; return msg;
請在此下載本篇教學的 NodeRead 程式: #6 監測資料寫入InfluxDB資料庫, 長期保存並在NodeRED資訊看板裡顯示
定義 InfluxDB Server:
Stonez56 一起來學樹莓派系列影片:
- #1 無鍵盤 無滑鼠 快速上手
https://youtu.be/BCkdSQNtVB0 - #2 改用 USB3/SSD 來開機, 畫面好流暢!
https://youtu.be/uI01GFmfH1k - #3 一行指令安裝 Node RED & 外加密碼保護
https://youtu.be/Mk8-xn_fHOA - #4 安裝 Mosquitto broker (MQTT) & 並在Node-RED & Google MQTTLens 進行測試 https://youtu.be/cK_LJPgR4mQ
- #5 溫溼度感測器DHT11由 ESP32 經 MQTT 送到 Node-RED
https://youtu.be/Vv1JRNYIZKc - #6 監測資料寫入InfluxDB資料庫, 長期保存並在NodeRED資訊看板裡顯示 https://youtu.be/UOoDolPVge0
- #7 2021 一起來學樹莓派 - 動態查詢InfluxDB 資料庫 & MQTT來遠端開/關繼電器 https://youtu.be/EpY_jl4wXtM