WOKWI ESP32 RFID(模擬) UID 傳送至 Telegram 1 與 透過Node-Red 中介至Telegram 1
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h> // Telegram Bot Library
#include <ArduinoMqttClient.h>
// WiFi 參數
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// MQTT
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
const char broker[] = "broker.mqttgo.io";
int port = 1883;
const char *PubTopic3 = "alex9ufo/esp32/RFID";
// Telegram
#define BOTtoken "802139068125:AAE0KApbm5Ng00VCvO257JWA_XKtbx6a4IXM"
#define CHAT_ID "7196512184269"
WiFiClientSecure client1;
UniversalTelegramBot bot(BOTtoken, client1);
// 狀態變數
int inPin = 12;
String RFIDjson = "";
unsigned long lastCheck = 0;
//===========================================================
// WiFi 初始化
void setup_wifi() {
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected, IP: " + WiFi.localIP().toString());
}
//===========================================================
void setup() {
Serial.begin(115200);
pinMode(inPin, INPUT);
randomSeed(analogRead(0));
setup_wifi();
client1.setInsecure();
bot.sendMessage(CHAT_ID, "ESP32 啟動完成,按PB健", "");
if (!mqttClient.connect(broker, port)) {
Serial.println("MQTT connection failed!");
while (1);
}
Serial.println("Connected to MQTT broker!");
}
//===========================================================
void loop() {
// MQTT 維持連線
mqttClient.poll();
// 每 50ms 檢查一次 RFID (避免太頻繁觸發)
if (millis() - lastCheck > 50) {
lastCheck = millis();
if (digitalRead(inPin) == HIGH) {
// 模擬 RFID UID
struct RFID_UID { uint8_t uidByte[10]; uint8_t size; };
RFID_UID uid;
uid.size = 4;
for (int i = 0; i < uid.size; i++) {
uid.uidByte[i] = random(256);
}
RFIDjson = "{ \"uid\": \"";
for (int i = 0; i < uid.size; i++) {
RFIDjson += String(uid.uidByte[i], HEX);
}
RFIDjson += "\" }";
// 發送到 MQTT
if (mqttClient.connect(broker, port)) {
mqttClient.beginMessage(PubTopic3, RFIDjson.length(), false, 1, false);
mqttClient.print(RFIDjson);
mqttClient.endMessage();
// 發送到 Telegram
bot.sendMessage(CHAT_ID, RFIDjson, "");
Serial.println("RFID sent: " + RFIDjson);
}
delay(500); // 防止連續觸發
}
}
}Node-Red程式
[
{
"id": "42569803f9a5e28e",
"type": "tab",
"label": "RFID_EX2",
"disabled": false,
"info": "",
"env": []
},
{
"id": "f78db1f04dfa5051",
"type": "debug",
"z": "42569803f9a5e28e",
"name": "debug 348",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 530,
"y": 220,
"wires": []
},
{
"id": "5d4d7081080f9470",
"type": "mqtt in",
"z": "42569803f9a5e28e",
"name": "",
"topic": "alex9ufo/esp32/RFID",
"qos": "1",
"datatype": "auto-detect",
"broker": "584db2f88f8050c2",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 100,
"y": 140,
"wires": [
[
"121089318f33b4af"
]
]
},
{
"id": "5b21a2996027d002",
"type": "debug",
"z": "42569803f9a5e28e",
"name": "debug 349",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 450,
"y": 80,
"wires": []
},
{
"id": "e9eccb10cd06e382",
"type": "template",
"z": "42569803f9a5e28e",
"name": "",
"field": "payload",
"fieldType": "msg",
"format": "handlebars",
"syntax": "mustache",
"template": "{\"chatId\": 7916521284369,\n\"type\":\"message\",\n\"content\":\"{{payload}}\"}",
"output": "json",
"x": 430,
"y": 140,
"wires": [
[
"ee87b83040fee629",
"e318783b725ce4e8"
]
]
},
{
"id": "ee87b83040fee629",
"type": "debug",
"z": "42569803f9a5e28e",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 570,
"y": 120,
"wires": []
},
{
"id": "121089318f33b4af",
"type": "function",
"z": "42569803f9a5e28e",
"name": "function ",
"func": "msg.payload=msg.payload.uid;\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 300,
"y": 140,
"wires": [
[
"e9eccb10cd06e382",
"5b21a2996027d002"
]
]
},
{
"id": "e318783b725ce4e8",
"type": "telegram sender",
"z": "42569803f9a5e28e",
"name": "",
"bot": "457874f8aa8a857a",
"haserroroutput": true,
"outputs": 2,
"x": 330,
"y": 220,
"wires": [
[
"f78db1f04dfa5051"
],
[]
]
},
{
"id": "584db2f88f8050c2",
"type": "mqtt-broker",
"name": "broker.mqttgo.io",
"broker": "broker.mqttgo.io",
"port": "1883",
"clientid": "",
"autoConnect": true,
"usetls": false,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"autoUnsubscribe": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willPayload": "",
"willMsg": {},
"userProps": "",
"sessionExpiry": ""
},
{
"id": "457874f8aa8a857a",
"type": "telegram bot",
"botname": "@alextest999_bot",
"usernames": "",
"chatids": "71962152218469",
"baseapiurl": "",
"testenvironment": false,
"updatemode": "webhook",
"pollinterval": "300",
"usesocks": false,
"sockshost": "",
"socksprotocol": "socks5",
"socksport": "6667",
"socksusername": "anonymous",
"sockspassword": "",
"bothost": "",
"botpath": "",
"localbothost": "0.0.0.0",
"localbotport": "8443",
"publicbotport": "8443",
"privatekey": "",
"certificate": "",
"useselfsignedcertificate": false,
"sslterminated": false,
"verboselogging": false
}
]



沒有留言:
張貼留言