ESP32 MQTT – Publish DHT11/DHT22 Temperature and Humidity Readings (Arduino IDE)
Arduino程式
#include "DHT.h"
#include <WiFi.h>
extern "C" {
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"
}
#include <AsyncMQTT_ESP32.h>
//#define WIFI_SSID "REPLACE_WITH_YOUR_SSID"
//#define WIFI_PASSWORD "REPLACE_WITH_YOUR_PASSWORD"
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// Raspberry Pi Mosquitto MQTT Broker
//#define MQTT_HOST IPAddress(192, 168, 1, XXX)
// For a cloud MQTT broker, type the domain name
//#define MQTT_HOST "example.com"
#define MQTT_HOST "test.mosquitto.org" // Broker address
#define MQTT_PORT 1883
// Temperature MQTT Topics
#define MQTT_PUB_TEMP "alex9ufo/esp32/dht/temperature"
#define MQTT_PUB_HUM "alex9ufo/esp32/dht/humidity"
// Digital pin connected to the DHT sensor
#define DHTPIN 4
// Uncomment whatever DHT sensor type you're using
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE);
// Variables to hold sensor readings
float temp;
float hum;
AsyncMqttClient mqttClient;
TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer;
unsigned long previousMillis = 0; // Stores last time temperature was published
const long interval = 10000; // Interval at which to publish sensor readings
void connectToWifi() {
Serial.println("Connecting to Wi-Fi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}
void connectToMqtt() {
Serial.println("Connecting to MQTT...");
mqttClient.connect();
}
void WiFiEvent(WiFiEvent_t event) {
Serial.printf("[WiFi-event] event: %d\n", event);
switch(event) {
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
connectToMqtt();
break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
xTimerStart(wifiReconnectTimer, 0);
break;
}
}
void onMqttConnect(bool sessionPresent) {
Serial.println("Connected to MQTT.");
Serial.print("Session present: ");
Serial.println(sessionPresent);
}
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
Serial.println("Disconnected from MQTT.");
if (WiFi.isConnected()) {
xTimerStart(mqttReconnectTimer, 0);
}
}
/*void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
Serial.println("Subscribe acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
Serial.print(" qos: ");
Serial.println(qos);
}
void onMqttUnsubscribe(uint16_t packetId) {
Serial.println("Unsubscribe acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
}*/
void onMqttPublish(uint16_t packetId) {
Serial.print("Publish acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
}
void setup() {
Serial.begin(115200);
Serial.println();
dht.begin();
mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));
WiFi.onEvent(WiFiEvent);
mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
//mqttClient.onSubscribe(onMqttSubscribe);
//mqttClient.onUnsubscribe(onMqttUnsubscribe);
mqttClient.onPublish(onMqttPublish);
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
// If your broker requires authentication (username and password), set them below
//mqttClient.setCredentials("REPlACE_WITH_YOUR_USER", "REPLACE_WITH_YOUR_PASSWORD");
connectToWifi();
}
void loop() {
unsigned long currentMillis = millis();
// Every X number of seconds (interval = 10 seconds)
// it publishes a new MQTT message
if (currentMillis - previousMillis >= interval) {
// Save the last time a new reading was published
previousMillis = currentMillis;
// New DHT sensor readings
hum = dht.readHumidity();
// Read temperature as Celsius (the default)
temp = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
//temp = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(temp) || isnan(hum)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Publish an MQTT message on topic esp32/dht/temperature
uint16_t packetIdPub1 = mqttClient.publish(MQTT_PUB_TEMP, 1, true, String(temp).c_str());
Serial.printf("Publishing on topic %s at QoS 1, packetId: %i", MQTT_PUB_TEMP, packetIdPub1);
Serial.printf("Message: %.2f \n", temp);
// Publish an MQTT message on topic esp32/dht/humidity
uint16_t packetIdPub2 = mqttClient.publish(MQTT_PUB_HUM, 1, true, String(hum).c_str());
Serial.printf("Publishing on topic %s at QoS 1, packetId %i: ", MQTT_PUB_HUM, packetIdPub2);
Serial.printf("Message: %.2f \n", hum);
}
}
Node-Red程式
[
{
"id": "5640b148882b4cf5",
"type": "comment",
"z": "6fc7559b07049ecb",
"name": "資料表 :TABLE DHT22STATUS",
"info": "CREATE TABLE DHT22STATUS (\nid INTEGER,\nTemp TEXT,\nHumi TEXT,\nDate DATE,\nTime TIME,\nPRIMARY KEY (id)\n);\n\n",
"x": 290,
"y": 480,
"wires": []
},
{
"id": "1ded01ae7510fc0c",
"type": "ui_gauge",
"z": "6fc7559b07049ecb",
"name": "",
"group": "7aa2048fc0eab3a0",
"order": 10,
"width": 6,
"height": 3,
"gtype": "gage",
"title": "溫度",
"label": "°C",
"format": "{{value}}",
"min": "-40",
"max": "80",
"colors": [
"#00b500",
"#e6e600",
"#ca3838"
],
"seg1": "20",
"seg2": "40",
"className": "",
"x": 450,
"y": 140,
"wires": []
},
{
"id": "e02dae7010eb1b11",
"type": "mqtt in",
"z": "6fc7559b07049ecb",
"name": "DHT22 溫度 Subscribe",
"topic": "alex9ufo/esp32/dht/temperature",
"qos": "1",
"datatype": "auto-detect",
"broker": "21957383cfd8785a",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 120,
"y": 120,
"wires": [
[
"719f87e8967a2305"
]
]
},
{
"id": "9b99804f9ec58f33",
"type": "ui_gauge",
"z": "6fc7559b07049ecb",
"name": "",
"group": "7aa2048fc0eab3a0",
"order": 11,
"width": 6,
"height": 3,
"gtype": "gage",
"title": "濕度",
"label": "%",
"format": "{{value}}",
"min": 0,
"max": "100",
"colors": [
"#00b500",
"#e6e600",
"#ca3838"
],
"seg1": "40",
"seg2": "60",
"className": "",
"x": 450,
"y": 200,
"wires": []
},
{
"id": "461cfa80b0273d7e",
"type": "sqlite",
"z": "6fc7559b07049ecb",
"mydb": "647ea72ac9a75c97",
"sqlquery": "msg.topic",
"sql": "",
"name": "DHT22_STATUS",
"x": 770,
"y": 540,
"wires": [
[
"6d524339e6dff301"
]
]
},
{
"id": "268ba01972a277f5",
"type": "function",
"z": "6fc7559b07049ecb",
"name": "CREATE DATABASE",
"func": "//CREATE TABLE DHT22STATUS(\n// id INTEGER,\n// Temp TEXT,\n// Humi TEXT,\n// Date DATE,\n// Time TIME,\n// PRIMARY KEY(id)\n//);\n\n//CREATE TABLE DHT22STATUS (id INTEGER,Temp TEXT, Humi TEXT, Date DATE,Time TIME,PRIMARY KEY (id));\nmsg.topic = \"CREATE TABLE DHT22STATUS (id INTEGER,Temp TEXT, Humi TEXT,Date DATE,Time TIME,PRIMARY KEY (id))\";\nreturn msg;\n",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 460,
"y": 540,
"wires": [
[
"461cfa80b0273d7e"
]
]
},
{
"id": "42b83c22313a88aa",
"type": "ui_button",
"z": "6fc7559b07049ecb",
"name": "",
"group": "2fc188c006769769",
"order": 6,
"width": 2,
"height": 1,
"passthru": false,
"label": "建立資料庫",
"tooltip": "",
"color": "",
"bgcolor": "",
"className": "",
"icon": "",
"payload": "建立資料庫",
"payloadType": "str",
"topic": "topic",
"topicType": "msg",
"x": 230,
"y": 540,
"wires": [
[
"268ba01972a277f5",
"f7e5c311d42cc72b"
]
]
},
{
"id": "4707f2164188b011",
"type": "function",
"z": "6fc7559b07049ecb",
"name": "INSERT",
"func": "//var temp1=msg.payload[0];\n//var humi1=msg.payload[1];\n\nvar temp1 = flow.get('temp2');\nvar humi1 = flow.get('humi2');\n\nvar Today = new Date();\nvar yyyy = Today.getFullYear(); //年\nvar MM = Today.getMonth()+1; //月\nvar dd = Today.getDate(); //日\nvar h = Today.getHours(); //時\nvar m = Today.getMinutes(); //分\nvar s = Today.getSeconds(); //秒\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 DHT22STATUS (Temp,Humi , Date , Time ) VALUES ($temp1, $humi1, $var_date , $var_time ) \" ;\nmsg.payload = [temp1, humi1 , var_date , var_time ]\nreturn msg;\n\n\n//CREATE TABLE DHT22STATUS(\n// id INTEGER,\n// Temp TEXT,\n// Humi TEXT,\n// Date DATE,\n// Time TIME,\n// PRIMARY KEY(id)\n//);\n\n//CREATE TABLE DHT22STATUS (id INTEGER,Temp TEXT, Humi TEXT, Date DATE,Time TIME,PRIMARY KEY (id));",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 500,
"y": 420,
"wires": [
[
"91d1cdd83e777129"
]
]
},
{
"id": "91d1cdd83e777129",
"type": "sqlite",
"z": "6fc7559b07049ecb",
"mydb": "647ea72ac9a75c97",
"sqlquery": "msg.topic",
"sql": "",
"name": "DHT22_STATUS",
"x": 690,
"y": 420,
"wires": [
[
"d3aa708c457c7204",
"e1ecb014ec0a045e"
]
]
},
{
"id": "6d524339e6dff301",
"type": "debug",
"z": "6fc7559b07049ecb",
"name": "debug ",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 970,
"y": 540,
"wires": []
},
{
"id": "d3aa708c457c7204",
"type": "debug",
"z": "6fc7559b07049ecb",
"name": "debug ",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 950,
"y": 420,
"wires": []
},
{
"id": "79ed12d6d169a758",
"type": "ui_button",
"z": "6fc7559b07049ecb",
"name": "",
"group": "2fc188c006769769",
"order": 7,
"width": 6,
"height": 1,
"passthru": false,
"label": "檢視資料庫資料",
"tooltip": "",
"color": "",
"bgcolor": "",
"className": "",
"icon": "",
"payload": "檢視資料",
"payloadType": "str",
"topic": "topic",
"topicType": "msg",
"x": 240,
"y": 660,
"wires": [
[
"e1ecb014ec0a045e",
"f7e5c311d42cc72b"
]
]
},
{
"id": "e1ecb014ec0a045e",
"type": "function",
"z": "6fc7559b07049ecb",
"name": "檢視資料",
"func": "//CREATE TABLE DHT22STATUS(\n// id INTEGER,\n// Temp TEXT,\n// Humi TEXT,\n// Date DATE,\n// Time TIME,\n// PRIMARY KEY(id)\n//);\n\n//CREATE TABLE DHT22STATUS (id INTEGER,Temp TEXT, Humi TEXT, Date DATE,Time TIME,PRIMARY KEY (id));\n//SELECT * FROM DHT22STATUS ORDER BY id DESC LIMIT 50;\n\nmsg.topic = \"SELECT * FROM DHT22STATUS ORDER BY id DESC LIMIT 50\";\nreturn msg;",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 520,
"y": 660,
"wires": [
[
"196d0a848e9b51d8"
]
]
},
{
"id": "034d60ab31a25883",
"type": "ui_table",
"z": "6fc7559b07049ecb",
"group": "60bc276d0fdd5026",
"name": "",
"order": 1,
"width": 10,
"height": 14,
"columns": [],
"outputs": 0,
"cts": false,
"x": 1070,
"y": 660,
"wires": []
},
{
"id": "196d0a848e9b51d8",
"type": "sqlite",
"z": "6fc7559b07049ecb",
"mydb": "647ea72ac9a75c97",
"sqlquery": "msg.topic",
"sql": "",
"name": "DHT22_STATUS",
"x": 770,
"y": 660,
"wires": [
[
"034d60ab31a25883"
]
]
},
{
"id": "d183a13f88978c3a",
"type": "ui_button",
"z": "6fc7559b07049ecb",
"name": "",
"group": "2fc188c006769769",
"order": 3,
"width": 2,
"height": 1,
"passthru": false,
"label": "刪除資料庫 ",
"tooltip": "",
"color": "",
"bgcolor": "",
"className": "",
"icon": "",
"payload": "刪除資料庫 ",
"payloadType": "str",
"topic": "topic",
"topicType": "msg",
"x": 230,
"y": 920,
"wires": [
[
"537d0cedc28a1115",
"7de63fde2be756a0"
]
]
},
{
"id": "7d5838e569f8aad8",
"type": "function",
"z": "6fc7559b07049ecb",
"name": "DROP DATABASE",
"func": "\n//CREATE TABLE DHT22STATUS(\n// id INTEGER,\n// Temp TEXT,\n// Humi TEXT,\n// Date DATE,\n// Time TIME,\n// PRIMARY KEY(id)\n//);\n\n//CREATE TABLE DHT22STATUS (id INTEGER,Temp TEXT, Humi TEXT, Date DATE,Time TIME,PRIMARY KEY (id));\n//SELECT * FROM DHT22STATUS ORDER BY id DESC LIMIT 50;\n\nmsg.topic = \"DROP TABLE DHT22STATUS\";\nreturn msg;\n",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 900,
"y": 920,
"wires": [
[
"12154f6e2c3364fe"
]
]
},
{
"id": "12154f6e2c3364fe",
"type": "sqlite",
"z": "6fc7559b07049ecb",
"mydb": "647ea72ac9a75c97",
"sqlquery": "msg.topic",
"sql": "",
"name": "DHT22_STATUS",
"x": 1110,
"y": 900,
"wires": [
[
"63797f4590221f1d"
]
]
},
{
"id": "e40f2b7932674ebb",
"type": "ui_button",
"z": "6fc7559b07049ecb",
"name": "",
"group": "2fc188c006769769",
"order": 2,
"width": 2,
"height": 1,
"passthru": false,
"label": "刪除所有資料 ",
"tooltip": "",
"color": "",
"bgcolor": "",
"className": "",
"icon": "",
"payload": "刪除所有資料 ",
"payloadType": "str",
"topic": "topic",
"topicType": "msg",
"x": 240,
"y": 860,
"wires": [
[
"7de63fde2be756a0",
"066b3cc3b01fb17b"
]
]
},
{
"id": "f502278d96e3a9dd",
"type": "function",
"z": "6fc7559b07049ecb",
"name": "DELETE ALL DATA",
"func": "\n//CREATE TABLE DHT22STATUS(\n// id INTEGER,\n// Temp TEXT,\n// Humi TEXT,\n// Date DATE,\n// Time TIME,\n// PRIMARY KEY(id)\n//);\n\n//CREATE TABLE DHT22STATUS (id INTEGER,Temp TEXT, Humi TEXT, Date DATE,Time TIME,PRIMARY KEY (id));\n//SELECT * FROM DHT22STATUS ORDER BY id DESC LIMIT 50;\n\nmsg.topic = \"DELETE from DHT22STATUS\";\nreturn msg;\n",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 890,
"y": 860,
"wires": [
[
"12154f6e2c3364fe"
]
]
},
{
"id": "537d0cedc28a1115",
"type": "ui_toast",
"z": "6fc7559b07049ecb",
"position": "prompt",
"displayTime": "3",
"highlight": "",
"sendall": true,
"outputs": 1,
"ok": "OK",
"cancel": "Cancel",
"raw": true,
"className": "",
"topic": "",
"name": "",
"x": 490,
"y": 920,
"wires": [
[
"f27780b726f51f50"
]
]
},
{
"id": "f27780b726f51f50",
"type": "function",
"z": "6fc7559b07049ecb",
"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": 680,
"y": 920,
"wires": [
[
"7d5838e569f8aad8"
],
[]
]
},
{
"id": "7de63fde2be756a0",
"type": "ui_audio",
"z": "6fc7559b07049ecb",
"name": "",
"group": "2fc188c006769769",
"voice": "Microsoft Hanhan - Chinese (Traditional, Taiwan)",
"always": true,
"x": 375,
"y": 900,
"wires": [],
"l": false
},
{
"id": "066b3cc3b01fb17b",
"type": "ui_toast",
"z": "6fc7559b07049ecb",
"position": "prompt",
"displayTime": "3",
"highlight": "",
"sendall": true,
"outputs": 1,
"ok": "OK",
"cancel": "Cancel",
"raw": true,
"className": "",
"topic": "",
"name": "",
"x": 490,
"y": 860,
"wires": [
[
"474334f8e3deb630"
]
]
},
{
"id": "474334f8e3deb630",
"type": "function",
"z": "6fc7559b07049ecb",
"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": 680,
"y": 860,
"wires": [
[
"f502278d96e3a9dd"
],
[]
]
},
{
"id": "f7e5c311d42cc72b",
"type": "ui_audio",
"z": "6fc7559b07049ecb",
"name": "",
"group": "2fc188c006769769",
"voice": "Microsoft Hanhan - Chinese (Traditional, Taiwan)",
"always": true,
"x": 365,
"y": 600,
"wires": [],
"l": false
},
{
"id": "63797f4590221f1d",
"type": "link out",
"z": "6fc7559b07049ecb",
"name": "link out 65",
"mode": "link",
"links": [
"07427ec2928408b7"
],
"x": 1235,
"y": 860,
"wires": []
},
{
"id": "07427ec2928408b7",
"type": "link in",
"z": "6fc7559b07049ecb",
"name": "link in 63",
"links": [
"63797f4590221f1d"
],
"x": 435,
"y": 740,
"wires": [
[
"e1ecb014ec0a045e"
]
]
},
{
"id": "bcceb14254aac9d8",
"type": "ui_button",
"z": "6fc7559b07049ecb",
"name": "",
"group": "2fc188c006769769",
"order": 1,
"width": 2,
"height": 1,
"passthru": false,
"label": "查詢一筆資料",
"tooltip": "",
"color": "",
"bgcolor": "",
"className": "",
"icon": "",
"payload": "查詢一筆資料",
"payloadType": "str",
"topic": "topic",
"topicType": "msg",
"x": 240,
"y": 1000,
"wires": [
[
"7696fc84f97e7be5",
"d4d5dc7503fbe57e"
]
]
},
{
"id": "e077ddca23d5302e",
"type": "function",
"z": "6fc7559b07049ecb",
"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 DHT22STATUS\";\n temp=temp+\" WHERE id LIKE '\"+ id +\"'\";\n}\nmsg.topic=temp;\nglobal.set(\"SEL1\",0);\n\nreturn msg;\n\n//CREATE TABLE DHT22STATUS(\n// id INTEGER,\n// Temp TEXT,\n// Humi TEXT,\n// Date DATE,\n// Time TIME,\n// PRIMARY KEY(id)\n//);\n\n//CREATE TABLE DHT22STATUS (id INTEGER,Temp TEXT, Humi TEXT, Date DATE,Time TIME,PRIMARY KEY (id));\n//SELECT * FROM DHT22STATUS ORDER BY id DESC LIMIT 50;\n",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 780,
"y": 1000,
"wires": [
[
"d5c8b3eca9615bc2"
]
]
},
{
"id": "e6a06ec2490f8bf9",
"type": "ui_button",
"z": "6fc7559b07049ecb",
"name": "",
"group": "2fc188c006769769",
"order": 4,
"width": 2,
"height": 1,
"passthru": false,
"label": "刪除一筆資料",
"tooltip": "",
"color": "",
"bgcolor": "",
"className": "",
"icon": "",
"payload": "刪除一筆資料",
"payloadType": "str",
"topic": "topic",
"topicType": "msg",
"x": 240,
"y": 1060,
"wires": [
[
"d4d5dc7503fbe57e",
"4fb71e1248d7770c"
]
]
},
{
"id": "d4d5dc7503fbe57e",
"type": "ui_audio",
"z": "6fc7559b07049ecb",
"name": "",
"group": "2fc188c006769769",
"voice": "Microsoft Hanhan - Chinese (Traditional, Taiwan)",
"always": true,
"x": 375,
"y": 1100,
"wires": [],
"l": false
},
{
"id": "39b5b457d1d984e2",
"type": "ui_form",
"z": "6fc7559b07049ecb",
"name": "",
"label": "輸入id",
"group": "2fc188c006769769",
"order": 8,
"width": 0,
"height": 0,
"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": 630,
"y": 1060,
"wires": [
[
"e077ddca23d5302e",
"d561260c49287216",
"bb120f4b335c6e46"
]
]
},
{
"id": "d561260c49287216",
"type": "function",
"z": "6fc7559b07049ecb",
"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 DHT22STATUS\";\n temp=temp+\" WHERE id LIKE '\"+ id +\"'\";\n}\n\nmsg.topic=temp;\nglobal.set(\"SEL2\",0)\nreturn msg;\n\n//CREATE TABLE DHT22STATUS(\n// id INTEGER,\n// Temp TEXT,\n// Humi TEXT,\n// Date DATE,\n// Time TIME,\n// PRIMARY KEY(id)\n//);\n\n//CREATE TABLE DHT22STATUS (id INTEGER,Temp TEXT, Humi TEXT, Date DATE,Time TIME,PRIMARY KEY (id));\n//SELECT * FROM DHT22STATUS ORDER BY id DESC LIMIT 50;\n",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 800,
"y": 1060,
"wires": [
[
"1f60cb4d9347e867",
"cf26aff8968959b8"
]
]
},
{
"id": "1f60cb4d9347e867",
"type": "sqlite",
"z": "6fc7559b07049ecb",
"mydb": "647ea72ac9a75c97",
"sqlquery": "msg.topic",
"sql": "",
"name": "DHT22_STATUS",
"x": 1190,
"y": 1060,
"wires": [
[
"63797f4590221f1d"
]
]
},
{
"id": "3272a5b7c20bfa19",
"type": "ui_button",
"z": "6fc7559b07049ecb",
"name": "",
"group": "2fc188c006769769",
"order": 5,
"width": 2,
"height": 1,
"passthru": false,
"label": "更正一筆資料",
"tooltip": "",
"color": "",
"bgcolor": "",
"className": "",
"icon": "",
"payload": "更正一筆資料",
"payloadType": "str",
"topic": "topic",
"topicType": "msg",
"x": 240,
"y": 1140,
"wires": [
[
"d4d5dc7503fbe57e",
"a338d5a3635e077c"
]
]
},
{
"id": "8a1ebe7bba14a463",
"type": "comment",
"z": "6fc7559b07049ecb",
"name": "UPDATE查詢的WHERE",
"info": "UPDATE查詢的WHERE子句的基本語法如下:\n\nUPDATE table_name\nSET column1 = value1, column2 = value2...., columnN = valueN\nWHERE [condition];",
"x": 260,
"y": 1180,
"wires": []
},
{
"id": "26110da0b86c2667",
"type": "function",
"z": "6fc7559b07049ecb",
"name": "更正一筆資料",
"func": "//\nvar id = global.get(\"ID\");\nvar temp2 = msg.payload.Temp;\nvar humi2 = msg.payload.Humi;\nvar date = msg.payload.date;\nvar time = msg.payload.time;\n\nvar s=global.get(\"SEL3\")\nmsg.topic=\"\";\nvar temp=\"\";\n\nif (s==3)\n{\n temp =\"update DHT22STATUS set \";\n temp = temp +\" Temp= '\" + temp2 +\"'\";\n temp = temp +\" , Humi= '\" + humi2 +\"'\";\n temp=temp+\" , Date= '\" + date +\"'\";\n temp=temp+\" , Time= '\" + time +\"'\";\n temp=temp+\" WHERE id=\" + id;\n \n\n}\nmsg.topic=temp;\n\nreturn msg;\n\n//CREATE TABLE DHT22STATUS(\n// id INTEGER,\n// Temp TEXT,\n// Humi TEXT,\n// Date DATE,\n// Time TIME,\n// PRIMARY KEY(id)\n//);\n\n//CREATE TABLE DHT22STATUS (id INTEGER,Temp TEXT, Humi TEXT, Date DATE,Time TIME,PRIMARY KEY (id));\n//SELECT * FROM DHT22STATUS ORDER BY id DESC LIMIT 50;\n",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 900,
"y": 1180,
"wires": [
[
"1f60cb4d9347e867",
"8cd4f794290938a0"
]
]
},
{
"id": "7696fc84f97e7be5",
"type": "function",
"z": "6fc7559b07049ecb",
"name": "function flow set1",
"func": "var s1=1;\nglobal.set(\"SEL1\",s1);\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 450,
"y": 1000,
"wires": [
[
"39b5b457d1d984e2"
]
]
},
{
"id": "4fb71e1248d7770c",
"type": "function",
"z": "6fc7559b07049ecb",
"name": "function flow set2",
"func": "var s1=2;\nglobal.set(\"SEL2\",s1);\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 450,
"y": 1060,
"wires": [
[
"39b5b457d1d984e2"
]
]
},
{
"id": "a338d5a3635e077c",
"type": "function",
"z": "6fc7559b07049ecb",
"name": "function flow set3",
"func": "var s1=3;\nglobal.set(\"SEL3\",s1);\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 450,
"y": 1140,
"wires": [
[
"39b5b457d1d984e2"
]
]
},
{
"id": "9f645b00e8806dec",
"type": "ui_form",
"z": "6fc7559b07049ecb",
"name": "",
"label": "更正欄位",
"group": "2fc188c006769769",
"order": 9,
"width": 0,
"height": 0,
"options": [
{
"label": "Temperature",
"value": "Temp",
"type": "text",
"required": true,
"rows": null
},
{
"label": "Humidity",
"value": "Humi",
"type": "text",
"required": true,
"rows": null
},
{
"label": "DATE",
"value": "date",
"type": "text",
"required": true,
"rows": null
},
{
"label": "TIME",
"value": "time",
"type": "text",
"required": true,
"rows": null
}
],
"formValue": {
"Temp": "",
"Humi": "",
"date": "",
"time": ""
},
"payload": "",
"submit": "Submit",
"cancel": "Cancle",
"topic": "Form",
"topicType": "str",
"splitLayout": false,
"className": "",
"x": 720,
"y": 1180,
"wires": [
[
"26110da0b86c2667"
]
]
},
{
"id": "cf26aff8968959b8",
"type": "debug",
"z": "6fc7559b07049ecb",
"name": "debug 341",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "topic",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 990,
"y": 1040,
"wires": []
},
{
"id": "bb120f4b335c6e46",
"type": "function",
"z": "6fc7559b07049ecb",
"name": "Store ID資料",
"func": "//\nvar id = msg.payload.id;\nglobal.set(\"ID\",id)\nreturn msg;\n\n//CREATE TABLE DHT22STATUS(\n// id INTEGER,\n// Temp TEXT,\n// Humi TEXT,\n// Date DATE,\n// Time TIME,\n// PRIMARY KEY(id)\n//);\n\n//CREATE TABLE DHT22STATUS (id INTEGER,Temp TEXT, Humi TEXT, Date DATE,Time TIME,PRIMARY KEY (id));\n//SELECT * FROM DHT22STATUS ORDER BY id DESC LIMIT 50;\n",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 730,
"y": 1120,
"wires": [
[
"9f645b00e8806dec",
"96d76efbc888515b"
]
]
},
{
"id": "96d76efbc888515b",
"type": "function",
"z": "6fc7559b07049ecb",
"name": "查詢一筆資料",
"func": "//\nvar id = global.get(\"ID\");\nmsg.topic=\"\";\nvar temp=\"\";\ntemp =\"SELECT * FROM DHT22STATUS\";\ntemp=temp+\" WHERE id LIKE '\"+ id +\"'\";\n\nmsg.topic=temp;\n\nreturn msg;\n\n//CREATE TABLE DHT22STATUS(\n// id INTEGER,\n// Temp TEXT,\n// Humi TEXT,\n// Date DATE,\n// Time TIME,\n// PRIMARY KEY(id)\n//);\n\n//CREATE TABLE DHT22STATUS (id INTEGER,Temp TEXT, Humi TEXT, Date DATE,Time TIME,PRIMARY KEY (id));\n//SELECT * FROM DHT22STATUS ORDER BY id DESC LIMIT 50;\n",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 920,
"y": 1120,
"wires": [
[
"d5c8b3eca9615bc2"
]
]
},
{
"id": "8cd4f794290938a0",
"type": "debug",
"z": "6fc7559b07049ecb",
"name": "debug 342",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "topic",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 1090,
"y": 1180,
"wires": []
},
{
"id": "0ecb56d1c3d20ec5",
"type": "function",
"z": "6fc7559b07049ecb",
"name": "function ",
"func": "var temp1 = flow.get('temp2');\nvar humi1 = flow.get('humi2');\n\nmsg.payload=\"---ESP32回來資料---溫度\" +temp1+'℃ , 濕度' + humi1+'%';\nreturn msg;",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 400,
"y": 1300,
"wires": [
[
"fd26b6bf4d0cba30",
"1bc91052ac871f04"
]
]
},
{
"id": "fd26b6bf4d0cba30",
"type": "function",
"z": "6fc7559b07049ecb",
"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,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 590,
"y": 1300,
"wires": [
[
"09f4f949214d30ea"
]
]
},
{
"id": "09f4f949214d30ea",
"type": "http request",
"z": "6fc7559b07049ecb",
"name": "",
"method": "POST",
"ret": "txt",
"paytoqs": false,
"url": "https://notify-api.line.me/api/notify",
"tls": "",
"persist": false,
"proxy": "",
"authType": "",
"x": 760,
"y": 1300,
"wires": [
[
"cf0157a631e1cabb"
]
]
},
{
"id": "cf0157a631e1cabb",
"type": "debug",
"z": "6fc7559b07049ecb",
"name": "debug 343",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 930,
"y": 1300,
"wires": []
},
{
"id": "d7074c27b1464c96",
"type": "comment",
"z": "6fc7559b07049ecb",
"name": "Line Notify Message ",
"info": "",
"x": 610,
"y": 1260,
"wires": []
},
{
"id": "08a695bdc148df10",
"type": "ui_audio",
"z": "6fc7559b07049ecb",
"name": "",
"group": "2fc188c006769769",
"voice": "Microsoft Hanhan - Chinese (Traditional, Taiwan)",
"always": "",
"x": 760,
"y": 1340,
"wires": []
},
{
"id": "1bc91052ac871f04",
"type": "delay",
"z": "6fc7559b07049ecb",
"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": 580,
"y": 1340,
"wires": [
[
"08a695bdc148df10"
]
]
},
{
"id": "d5c8b3eca9615bc2",
"type": "sqlite",
"z": "6fc7559b07049ecb",
"mydb": "647ea72ac9a75c97",
"sqlquery": "msg.topic",
"sql": "",
"name": "DHT22_STATUS",
"x": 990,
"y": 1000,
"wires": [
[
"034d60ab31a25883"
]
]
},
{
"id": "fbc2953598cf4267",
"type": "debug",
"z": "6fc7559b07049ecb",
"name": "debug 344",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 470,
"y": 100,
"wires": []
},
{
"id": "719f87e8967a2305",
"type": "function",
"z": "6fc7559b07049ecb",
"name": "function 106",
"func": "var temp1=msg.payload;\nmsg.payload=temp1;\nflow.set('temp2', temp1)\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 310,
"y": 120,
"wires": [
[
"fbc2953598cf4267",
"1ded01ae7510fc0c"
]
]
},
{
"id": "45f509856efe690d",
"type": "function",
"z": "6fc7559b07049ecb",
"name": "function 107",
"func": "var humi1=msg.payload;\nmsg.payload=humi1;\nflow.set('humi2', humi1)\n\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 310,
"y": 240,
"wires": [
[
"9b6561f841fa0ee4",
"9b99804f9ec58f33"
]
]
},
{
"id": "9b6561f841fa0ee4",
"type": "delay",
"z": "6fc7559b07049ecb",
"name": "",
"pauseType": "delay",
"timeout": "20",
"timeoutUnits": "milliseconds",
"rate": "1",
"nbRateUnits": "1",
"rateUnits": "second",
"randomFirst": "1",
"randomLast": "5",
"randomUnits": "seconds",
"drop": false,
"allowrate": false,
"outputs": 1,
"x": 490,
"y": 240,
"wires": [
[
"4707f2164188b011"
]
]
},
{
"id": "2a93d3d48ca5bffc",
"type": "delay",
"z": "6fc7559b07049ecb",
"name": "",
"pauseType": "delay",
"timeout": "20",
"timeoutUnits": "milliseconds",
"rate": "1",
"nbRateUnits": "1",
"rateUnits": "second",
"randomFirst": "1",
"randomLast": "5",
"randomUnits": "seconds",
"drop": false,
"allowrate": false,
"outputs": 1,
"x": 510,
"y": 1480,
"wires": [
[
"0ecb56d1c3d20ec5"
]
]
},
{
"id": "c04a783269eaa952",
"type": "mqtt in",
"z": "6fc7559b07049ecb",
"name": "DHT22 濕度 Subscribe",
"topic": "alex9ufo/esp32/dht/humidity",
"qos": "1",
"datatype": "auto-detect",
"broker": "21957383cfd8785a",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 120,
"y": 240,
"wires": [
[
"45f509856efe690d"
]
]
},
{
"id": "fa060205c07034e2",
"type": "mqtt in",
"z": "6fc7559b07049ecb",
"name": "DHT22 溫度 Subscribe",
"topic": "alex9ufo/esp32/dht/temperature",
"qos": "1",
"datatype": "auto-detect",
"broker": "21957383cfd8785a",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 160,
"y": 1420,
"wires": [
[
"42c211f9dfe60fdf"
]
]
},
{
"id": "9414f8cf7ff7f750",
"type": "mqtt in",
"z": "6fc7559b07049ecb",
"name": "DHT22 濕度 Subscribe",
"topic": "alex9ufo/esp32/dht/humidity",
"qos": "1",
"datatype": "auto-detect",
"broker": "21957383cfd8785a",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 160,
"y": 1480,
"wires": [
[
"333eb7aab13c6ce0"
]
]
},
{
"id": "42c211f9dfe60fdf",
"type": "function",
"z": "6fc7559b07049ecb",
"name": "function 110",
"func": "var temp1=msg.payload;\nmsg.payload=temp1;\nflow.set('temp2', temp1)\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 350,
"y": 1420,
"wires": [
[]
]
},
{
"id": "333eb7aab13c6ce0",
"type": "function",
"z": "6fc7559b07049ecb",
"name": "function 111",
"func": "var humi1=msg.payload;\nmsg.payload=humi1;\nflow.set('humi2', humi1)\n\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 350,
"y": 1480,
"wires": [
[
"2a93d3d48ca5bffc"
]
]
},
{
"id": "7aa2048fc0eab3a0",
"type": "ui_group",
"name": "溫度濕度",
"tab": "fbd26925fd4f8d9b",
"order": 3,
"disp": true,
"width": "6",
"collapse": false,
"className": ""
},
{
"id": "21957383cfd8785a",
"type": "mqtt-broker",
"name": "test.mosquitto.org",
"broker": "test.mosquitto.org",
"port": "1883",
"clientid": "",
"autoConnect": true,
"usetls": false,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"autoUnsubscribe": true,
"birthTopic": "",
"birthQos": "0",
"birthRetain": "false",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closeRetain": "false",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willRetain": "false",
"willPayload": "",
"willMsg": {},
"userProps": "",
"sessionExpiry": ""
},
{
"id": "647ea72ac9a75c97",
"type": "sqlitedb",
"db": "2025DHT22.db",
"mode": "RWC"
},
{
"id": "2fc188c006769769",
"type": "ui_group",
"name": "DHT22 資料",
"tab": "fbd26925fd4f8d9b",
"order": 1,
"disp": true,
"width": "6",
"collapse": false,
"className": ""
},
{
"id": "60bc276d0fdd5026",
"type": "ui_group",
"name": "DHT22顯示區",
"tab": "fbd26925fd4f8d9b",
"order": 2,
"disp": true,
"width": "10",
"collapse": false,
"className": ""
},
{
"id": "fbd26925fd4f8d9b",
"type": "ui_tab",
"name": "DHT22 Database",
"icon": "dashboard",
"disabled": false,
"hidden": false
}
]
沒有留言:
張貼留言