#include <WiFi.h>
#include <MQTTPubSubClient.h>
#include "DHTesp.h"
extern "C" {
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"
}
#define Relay1 33 //D33 LED-RED
#define Relay2 25 //D25 LED-Blue
#define Relay3 26 //D26 LED-Blue
#define Relay4 27 //D27 LED-Orangs
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
//#define MQTT_HOST "test.mosquitto.org"
#define MQTT_HOST "broker.mqttgo.io"
#define MQTT_PORT 1883
const char *SubTopic1 = "alex9ufo/telegram/ledenv";
const char *PubTopic2 = "alex9ufo/telegram/ledStatus";
const char *PubTopic3 = "alex9ufo/telegram/temphumiIn";
#define DHT_PIN 32
// DHT parameters
DHTesp dhtSensor;
WiFiClient client;
MQTTPubSubClient mqtt;
int count;
float temp, hum;
bool Send= false , THSend= false;
String json="";
//=============================================================================
void connect() {
connect_to_wifi:
Serial.print("connecting to wifi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.println(" connected!");
connect_to_host:
Serial.print("connecting to host...");
client.stop();
while (!client.connect(MQTT_HOST, 1883)) {
Serial.print(".");
delay(1000);
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi disconnected");
goto connect_to_wifi;
}
}
Serial.println(" connected!");
Serial.print("connecting to mqtt broker...");
mqtt.disconnect();
while (!mqtt.connect("arduino", "", "")) {
Serial.print(".");
delay(1000);
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi disconnected");
goto connect_to_wifi;
}
if (client.connected() != 1) {
Serial.println("WiFiClient disconnected");
goto connect_to_host;
}
}
Serial.println(" connected!");
}
//=============================================================================
void Publish_message() {
if (Send){
mqtt.publish("alex9ufo/telegram/ledStatus", json);
Serial.println();
Serial.print("publish message to MQTT ---");
Serial.println(json);
Send= false;
json="";
}
}
//=============================================================================
void setup() {
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(Relay3, OUTPUT);
pinMode(Relay4, OUTPUT);
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD, 6);
mqtt.begin(client);
connect();
mqtt.subscribe([](const String& topic, const String& payload, const size_t size) {
Serial.println("mqtt received: " + topic + " - " + payload);
});
mqtt.subscribe("alex9ufo/telegram/ledenv", [] (const String& payload, const size_t size) {
Serial.print("alex9ufo/telegram/ledenv");
Serial.println(payload);
String message=payload;
message.trim();
Serial.print(message);
if ( message == "Gled" ) {
digitalWrite(Relay1, HIGH); // Turn on the LED
Send = true;
json="Green Relay on";
}
if ( message == "Rled" ) {
digitalWrite(Relay2, HIGH); // Turn on the LED
Send = true;
json="Red Relay on";
}
if ( message == "Bled" ) {
digitalWrite(Relay3, HIGH); // Turn on the LED
Send = true;
json="Blue Relay on";
}
if ( message == "Yled" ) {
digitalWrite(Relay4, HIGH); // Turn on the LED
Send = true;
json="Yellow Relay on";
}
if ( message == "ledOff" ) {
digitalWrite(Relay1, LOW); // Turn off the LED
digitalWrite(Relay2, LOW); // Turn off the LED
digitalWrite(Relay3, LOW); // Turn off the LED
digitalWrite(Relay4, LOW); // Turn off the LED
Send = true;
json="all led off";
}
if ( message == "Env" ) {
THSend = true;
Serial.println("Send temperature & humidity" );
}
});
}
//=============================================================================
void loop() {
mqtt.update();
Publish_message();
if (!mqtt.isConnected()) {
connect();
}
// publish message
if (THSend) {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
String temp= ("Temp: " + String(data.temperature, 2) + "°C");
String hum=("Humidity: " + String(data.humidity, 1) + "%");
String msgStr = String(temp) + "," + String(hum) ;
Serial.println( msgStr);
THSend=false;
delay(100);
mqtt.publish("alex9ufo/telegram/temphumiIn", msgStr);
}
}
Node-Red 程式
請修改 BOTtoken 與 CHAT_ID 為自己的設定值
[
{
"id": "ea63aa67.c972f",
"type": "template",
"z": "eb8f9c0d054be30c",
"name": "",
"field": "payload",
"fieldType": "msg",
"format": "handlebars",
"syntax": "mustache",
"template": "{\"chatId\": 7965218469,\n\"type\":\"message\",\n\"content\":\"{{payload}}\"}\n",
"output": "json",
"x": 870,
"y": 460,
"wires": [
[
"9e00d0a7.d5ccf",
"600063bd96d765e6"
]
]
},
{
"id": "9e00d0a7.d5ccf",
"type": "debug",
"z": "eb8f9c0d054be30c",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 1030,
"y": 460,
"wires": []
},
{
"id": "600063bd96d765e6",
"type": "telegram sender",
"z": "eb8f9c0d054be30c",
"name": "roboerto_bot",
"bot": "ae1a60539b8e5308",
"haserroroutput": true,
"outputs": 2,
"x": 1210,
"y": 460,
"wires": [
[],
[]
]
},
{
"id": "e4b8f2dd860a7973",
"type": "telegram receiver",
"z": "eb8f9c0d054be30c",
"name": "LED&DHT22",
"bot": "ae1a60539b8e5308",
"saveDataDir": "",
"filterCommands": false,
"x": 210,
"y": 100,
"wires": [
[
"ea58984a24ea493e",
"62827af676c70d73"
],
[]
]
},
{
"id": "ea58984a24ea493e",
"type": "switch",
"z": "eb8f9c0d054be30c",
"name": "",
"property": "payload.content",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "Green",
"vt": "str"
},
{
"t": "eq",
"v": "Red",
"vt": "str"
},
{
"t": "eq",
"v": "Blue",
"vt": "str"
},
{
"t": "eq",
"v": "Yellow",
"vt": "str"
},
{
"t": "eq",
"v": "Off",
"vt": "str"
},
{
"t": "eq",
"v": "Env",
"vt": "str"
}
],
"checkall": "true",
"repair": false,
"outputs": 6,
"x": 370,
"y": 120,
"wires": [
[
"b410ed94340bb528"
],
[
"13aa73e421c429c7"
],
[
"db816a1f10392614"
],
[
"67c5294a430a6393"
],
[
"4ed36e19be451327"
],
[
"ee0e9d6a1c10141e"
]
]
},
{
"id": "13aa73e421c429c7",
"type": "function",
"z": "eb8f9c0d054be30c",
"name": "Red Led",
"func": "\nmsg.payload = \"Rled\"\nreturn msg;\n\n\n",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 620,
"y": 180,
"wires": [
[
"0d73aa1efdf96848",
"1f1457a5daaf649f"
]
]
},
{
"id": "b410ed94340bb528",
"type": "function",
"z": "eb8f9c0d054be30c",
"name": "Green Led",
"func": "\nmsg.payload = \"Gled\"\nreturn msg;\n\n\n",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 630,
"y": 120,
"wires": [
[
"c173db3cc3868fe8",
"1f1457a5daaf649f"
]
]
},
{
"id": "4ed36e19be451327",
"type": "function",
"z": "eb8f9c0d054be30c",
"name": "Off",
"func": "\nmsg.payload = \"ledOff\"\nreturn msg;\n\n\n",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 610,
"y": 360,
"wires": [
[
"02e43d4dd4523371",
"1f1457a5daaf649f"
]
]
},
{
"id": "db816a1f10392614",
"type": "function",
"z": "eb8f9c0d054be30c",
"name": "Blue Led",
"func": "\nmsg.payload = \"Bled\"\nreturn msg;\n\n\n",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 620,
"y": 240,
"wires": [
[
"15dcd2ff9b951c47",
"1f1457a5daaf649f"
]
]
},
{
"id": "c173db3cc3868fe8",
"type": "template",
"z": "eb8f9c0d054be30c",
"name": "",
"field": "payload",
"fieldType": "msg",
"format": "handlebars",
"syntax": "mustache",
"template": "{\"chatId\": 7965218469,\n\"type\":\"message\",\n\"content\":\"Green LED Triggered\"}",
"output": "json",
"x": 870,
"y": 120,
"wires": [
[
"62f3360544d9ca11",
"4abe16519bcdcaca"
]
]
},
{
"id": "62f3360544d9ca11",
"type": "debug",
"z": "eb8f9c0d054be30c",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 1010,
"y": 120,
"wires": []
},
{
"id": "4abe16519bcdcaca",
"type": "telegram sender",
"z": "eb8f9c0d054be30c",
"name": "LED&DHT22",
"bot": "ae1a60539b8e5308",
"haserroroutput": true,
"outputs": 2,
"x": 1210,
"y": 120,
"wires": [
[],
[]
]
},
{
"id": "0d73aa1efdf96848",
"type": "template",
"z": "eb8f9c0d054be30c",
"name": "",
"field": "payload",
"fieldType": "msg",
"format": "handlebars",
"syntax": "mustache",
"template": "{\"chatId\": 7965218469,\n\"type\":\"message\",\n\"content\":\"Red LED Triggered\"}",
"output": "json",
"x": 870,
"y": 180,
"wires": [
[
"4795fb81ce5c47a6",
"12c1a78b02583d94"
]
]
},
{
"id": "4795fb81ce5c47a6",
"type": "debug",
"z": "eb8f9c0d054be30c",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 1030,
"y": 180,
"wires": []
},
{
"id": "12c1a78b02583d94",
"type": "telegram sender",
"z": "eb8f9c0d054be30c",
"name": "LED&DHT22",
"bot": "ae1a60539b8e5308",
"haserroroutput": true,
"outputs": 2,
"x": 1210,
"y": 180,
"wires": [
[],
[]
]
},
{
"id": "15dcd2ff9b951c47",
"type": "template",
"z": "eb8f9c0d054be30c",
"name": "",
"field": "payload",
"fieldType": "msg",
"format": "handlebars",
"syntax": "mustache",
"template": "{\"chatId\": 7965218469,\n\"type\":\"message\",\n\"content\":\"Blue LED Triggered\"}",
"output": "json",
"x": 870,
"y": 240,
"wires": [
[
"751fda5416547778",
"46ea1f39f11d1b65"
]
]
},
{
"id": "751fda5416547778",
"type": "debug",
"z": "eb8f9c0d054be30c",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 1030,
"y": 240,
"wires": []
},
{
"id": "46ea1f39f11d1b65",
"type": "telegram sender",
"z": "eb8f9c0d054be30c",
"name": "LED&DHT22",
"bot": "ae1a60539b8e5308",
"haserroroutput": true,
"outputs": 2,
"x": 1210,
"y": 240,
"wires": [
[],
[]
]
},
{
"id": "02e43d4dd4523371",
"type": "template",
"z": "eb8f9c0d054be30c",
"name": "",
"field": "payload",
"fieldType": "msg",
"format": "handlebars",
"syntax": "mustache",
"template": "{\"chatId\": 7965218469,\n\"type\":\"message\",\n\"content\":\"All LED Off\"}",
"output": "json",
"x": 870,
"y": 360,
"wires": [
[
"e9aaa495e9c42799",
"0730f1a37c585783"
]
]
},
{
"id": "e9aaa495e9c42799",
"type": "debug",
"z": "eb8f9c0d054be30c",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 1030,
"y": 360,
"wires": []
},
{
"id": "0730f1a37c585783",
"type": "telegram sender",
"z": "eb8f9c0d054be30c",
"name": "LED&DHT22",
"bot": "ae1a60539b8e5308",
"haserroroutput": true,
"outputs": 2,
"x": 1210,
"y": 360,
"wires": [
[],
[]
]
},
{
"id": "e30abb1d7e9afcb0",
"type": "inject",
"z": "eb8f9c0d054be30c",
"name": "Green",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 310,
"y": 180,
"wires": [
[
"b410ed94340bb528"
]
]
},
{
"id": "4a2a01d9556ad50f",
"type": "inject",
"z": "eb8f9c0d054be30c",
"name": "Blue",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 310,
"y": 260,
"wires": [
[
"db816a1f10392614"
]
]
},
{
"id": "3f3ce5ea96251201",
"type": "inject",
"z": "eb8f9c0d054be30c",
"name": "Red",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 310,
"y": 220,
"wires": [
[
"13aa73e421c429c7"
]
]
},
{
"id": "3f2c1cebdde09f03",
"type": "inject",
"z": "eb8f9c0d054be30c",
"name": "Off",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 310,
"y": 360,
"wires": [
[
"4ed36e19be451327"
]
]
},
{
"id": "26b87a7af5f875cc",
"type": "inject",
"z": "eb8f9c0d054be30c",
"name": "Env",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 310,
"y": 400,
"wires": [
[
"ee0e9d6a1c10141e"
]
]
},
{
"id": "1f1457a5daaf649f",
"type": "mqtt out",
"z": "eb8f9c0d054be30c",
"name": "LED&ENV",
"topic": "alex9ufo/telegram/ledenv",
"qos": "1",
"retain": "true",
"respTopic": "",
"contentType": "",
"userProps": "",
"correl": "",
"expiry": "",
"broker": "70940176.2b2d3",
"x": 830,
"y": 80,
"wires": []
},
{
"id": "e64478c042173d38",
"type": "mqtt in",
"z": "eb8f9c0d054be30c",
"name": "溫溼度",
"topic": "alex9ufo/telegram/temphumiIn",
"qos": "1",
"datatype": "auto-detect",
"broker": "70940176.2b2d3",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 310,
"y": 460,
"wires": [
[
"b396489ab68ee6f7"
]
]
},
{
"id": "b396489ab68ee6f7",
"type": "function",
"z": "eb8f9c0d054be30c",
"name": "溫溼度",
"func": "\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 610,
"y": 460,
"wires": [
[
"ea63aa67.c972f"
]
]
},
{
"id": "caebacbb90e0cd8f",
"type": "inject",
"z": "eb8f9c0d054be30c",
"name": "Yellow",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 310,
"y": 300,
"wires": [
[
"67c5294a430a6393"
]
]
},
{
"id": "67c5294a430a6393",
"type": "function",
"z": "eb8f9c0d054be30c",
"name": "Yellow Led",
"func": "\nmsg.payload = \"Yled\"\nreturn msg;\n\n\n",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 610,
"y": 300,
"wires": [
[
"96e1a413db03c30f",
"1f1457a5daaf649f"
]
]
},
{
"id": "96e1a413db03c30f",
"type": "template",
"z": "eb8f9c0d054be30c",
"name": "",
"field": "payload",
"fieldType": "msg",
"format": "handlebars",
"syntax": "mustache",
"template": "{\"chatId\": 7965218469,\n\"type\":\"message\",\n\"content\":\"Yellow LED Triggered\"}",
"output": "json",
"x": 870,
"y": 300,
"wires": [
[
"ddad59eda8eb459f",
"1b50a5477af7388e"
]
]
},
{
"id": "ddad59eda8eb459f",
"type": "debug",
"z": "eb8f9c0d054be30c",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 1030,
"y": 300,
"wires": []
},
{
"id": "1b50a5477af7388e",
"type": "telegram sender",
"z": "eb8f9c0d054be30c",
"name": "LED&DHT22",
"bot": "ae1a60539b8e5308",
"haserroroutput": true,
"outputs": 2,
"x": 1210,
"y": 300,
"wires": [
[],
[]
]
},
{
"id": "62827af676c70d73",
"type": "debug",
"z": "eb8f9c0d054be30c",
"name": "debug 372",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 410,
"y": 40,
"wires": []
},
{
"id": "36876fbd49a1e2cc",
"type": "mqtt in",
"z": "eb8f9c0d054be30c",
"name": "LED",
"topic": "alex9ufo/telegram/ledStatus",
"qos": "1",
"datatype": "auto-detect",
"broker": "70940176.2b2d3",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 610,
"y": 40,
"wires": [
[
"9a3cfc3341f011ad"
]
]
},
{
"id": "9a3cfc3341f011ad",
"type": "debug",
"z": "eb8f9c0d054be30c",
"name": "debug 373",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 750,
"y": 40,
"wires": []
},
{
"id": "ee0e9d6a1c10141e",
"type": "function",
"z": "eb8f9c0d054be30c",
"name": "Env",
"func": "\nmsg.payload = \"Env\"\nreturn msg;\n\n\n",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 610,
"y": 400,
"wires": [
[
"1f1457a5daaf649f",
"43bddf665cf1bb74"
]
]
},
{
"id": "9b8c9418831af290",
"type": "debug",
"z": "eb8f9c0d054be30c",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 1030,
"y": 400,
"wires": []
},
{
"id": "43bddf665cf1bb74",
"type": "template",
"z": "eb8f9c0d054be30c",
"name": "",
"field": "payload",
"fieldType": "msg",
"format": "handlebars",
"syntax": "mustache",
"template": "{\"chatId\": 7965218469,\n\"type\":\"message\",\n\"content\":\"temperature & humidity\"}",
"output": "json",
"x": 870,
"y": 400,
"wires": [
[
"9b8c9418831af290",
"d93ccbaad6a230aa"
]
]
},
{
"id": "d93ccbaad6a230aa",
"type": "telegram sender",
"z": "eb8f9c0d054be30c",
"name": "LED&DHT22",
"bot": "ae1a60539b8e5308",
"haserroroutput": true,
"outputs": 2,
"x": 1210,
"y": 400,
"wires": [
[],
[]
]
},
{
"id": "ae1a60539b8e5308",
"type": "telegram bot",
"botname": "ncutedu_leddht22_bot",
"usernames": "",
"chatids": "",
"baseapiurl": "",
"testenvironment": false,
"updatemode": "polling",
"pollinterval": "300",
"usesocks": false,
"sockshost": "",
"socksprotocol": "socks5",
"socksport": "6667",
"socksusername": "anonymous",
"sockspassword": "",
"bothost": "",
"botpath": "",
"localbothost": "",
"localbotport": "8443",
"publicbotport": "8443",
"privatekey": "",
"certificate": "",
"useselfsignedcertificate": false,
"sslterminated": false,
"verboselogging": false
},
{
"id": "70940176.2b2d3",
"type": "mqtt-broker",
"name": "",
"broker": "broker.mqttgo.io",
"port": "1883",
"clientid": "",
"autoConnect": true,
"usetls": false,
"protocolVersion": "4",
"keepalive": "15",
"cleansession": true,
"autoUnsubscribe": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willPayload": "",
"willMsg": {},
"sessionExpiry": ""
}
]
沒有留言:
張貼留言