Telegram: Control ESP32 4 Relays + DHT22
請修改 BOTtoken 與 CHAT_ID 為自己的設定值
#define BOTtoken "8023906815:AAE01KApbm5Ng00VC1vO57JWA_XKtbx6a4IXM" // your Bot Token (Get from Botfather)
// Use @myidbot to find out the chat ID of an individual or a group
// Also note that you need to click "start" on a bot before it can
// message you
#define CHAT_ID "796152118469"
WOKWI程式
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h> // Universal Telegram Bot Library written by Brian Lough: https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot
#include <ArduinoJson.h>
#include "DHTesp.h"
DHTesp dhtSensor;
// Replace with your network credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Initialize Telegram BOT
#define BOTtoken "8023906815:AAE01KApbm5Ng00VC1vO57JWA_XKtbx6a4IXM" // your Bot Token (Get from Botfather)
// Use @myidbot to find out the chat ID of an individual or a group
// Also note that you need to click "start" on a bot before it can
// message you
//請修改 BOTtoken 與 CHAT_ID 為自己的設定值
#define CHAT_ID "796152118469"
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
// Checks for new messages every 1 second.
int botRequestDelay = 1000;
unsigned long lastTimeBotRan;
#define PIN_RELAY_1 27 // The ESP32 pin GPIO27 connected to the IN1 pin of relay module
#define PIN_RELAY_2 26 // The ESP32 pin GPIO26 connected to the IN2 pin of relay module
#define PIN_RELAY_3 25 // The ESP32 pin GPIO25 connected to the IN3 pin of relay module
#define PIN_RELAY_4 33 // The ESP32 pin GPIO33 connected to the IN4 pin of relay module
bool ledState1 = LOW;
bool ledState2 = LOW;
bool ledState3 = LOW;
bool ledState4 = LOW;
const int DHT_PIN = 32;
// Handle what happens when you receive new messages
void handleNewMessages(int numNewMessages) {
Serial.println("handleNewMessages");
Serial.println(String(numNewMessages));
for (int i=0; i<numNewMessages; i++) {
// Chat id of the requester
String chat_id = String(bot.messages[i].chat_id);
if (chat_id != CHAT_ID){
bot.sendMessage(chat_id, "Unauthorized user", "");
continue;
}
// Print the received message
String text = bot.messages[i].text;
Serial.println(text);
String from_name = bot.messages[i].from_name;
if (text == "/start") {
String welcome = "Welcome, " + from_name + ".\n";
welcome += "Use the following commands to control your outputs.\n\n";
welcome += "/led1_on to turn Relay1 ON \n";
welcome += "/led1_off to turn Relay1 OFF \n";
welcome += "/led2_on to turn Relay2 ON \n";
welcome += "/led2_off to turn Relay2 OFF \n";
welcome += "/led3_on to turn Relay3 ON \n";
welcome += "/led3_off to turn Relay3 OFF \n";
welcome += "/led4_on to turn Relay4 ON \n";
welcome += "/led4_off to turn Relay4 OFF \n";
welcome += "/led_all_on to turn Relay1,2,3,4 ON \n";
welcome += "/led_all_off to turn Relay1,2,3,4 OFF \n";
welcome += "/state to request current GPIO state \n";
bot.sendMessage(chat_id, welcome, "");
}
if (text == "/led1_on") {
bot.sendMessage(chat_id, "LED1 state set to ON", "");
ledState1 = HIGH;
digitalWrite(PIN_RELAY_1, ledState1);
}
if (text == "/led1_off") {
bot.sendMessage(chat_id, "LED1 state set to OFF", "");
ledState1 = LOW;
digitalWrite(PIN_RELAY_1, ledState1);
}
if (text == "/led2_on") {
bot.sendMessage(chat_id, "LED2 state set to ON", "");
ledState2 = HIGH;
digitalWrite(PIN_RELAY_2, ledState2);
}
if (text == "/led2_off") {
bot.sendMessage(chat_id, "LED2 state set to OFF", "");
ledState2 = LOW;
digitalWrite(PIN_RELAY_2, ledState2);
}
if (text == "/led3_on") {
bot.sendMessage(chat_id, "LED3 state set to ON", "");
ledState3 = HIGH;
digitalWrite(PIN_RELAY_3, ledState3);
}
if (text == "/led3_off") {
bot.sendMessage(chat_id, "LED3 state set to OFF", "");
ledState3 = LOW;
digitalWrite(PIN_RELAY_3, ledState3);
}
if (text == "/led4_on") {
bot.sendMessage(chat_id, "LED4 state set to ON", "");
ledState4 = HIGH;
digitalWrite(PIN_RELAY_4, ledState4);
}
if (text == "/led4_off") {
bot.sendMessage(chat_id, "LED4 state set to OFF", "");
ledState4 = LOW;
digitalWrite(PIN_RELAY_4, ledState4);
}
if (text == "/led_all_on") {
bot.sendMessage(chat_id, "4 LED1,2,3,4 state set to ON", "");
ledState1 = HIGH;
digitalWrite(PIN_RELAY_1, ledState1);
ledState2 = HIGH;
digitalWrite(PIN_RELAY_2, ledState2);
ledState3 = HIGH;
digitalWrite(PIN_RELAY_3, ledState3);
ledState4 = HIGH;
digitalWrite(PIN_RELAY_4, ledState4);
}
if (text == "/led_all_off") {
bot.sendMessage(chat_id, "4 LED1,2,3,4 state set to OFF", "");
ledState1 = LOW;
digitalWrite(PIN_RELAY_1, ledState1);
ledState2 = LOW;
digitalWrite(PIN_RELAY_2, ledState2);
ledState3 = LOW;
digitalWrite(PIN_RELAY_3, ledState3);
ledState4 = LOW;
digitalWrite(PIN_RELAY_4, ledState4);
}
if (text == "/state") {
if (digitalRead(PIN_RELAY_1)){
bot.sendMessage(chat_id, "LED1 is ON", "");
}
else{
bot.sendMessage(chat_id, "LED1 is OFF", "");
}
if (digitalRead(PIN_RELAY_2)){
bot.sendMessage(chat_id, "LED2 is ON", "");
}
else{
bot.sendMessage(chat_id, "LED2 is OFF", "");
}
if (digitalRead(PIN_RELAY_3)){
bot.sendMessage(chat_id, "LED3 is ON", "");
}
else{
bot.sendMessage(chat_id, "LED3 is OFF", "");
}
if (digitalRead(PIN_RELAY_4)){
bot.sendMessage(chat_id, "LED4 is ON", "");
}
else{
bot.sendMessage(chat_id, "LED4 is OFF", "");
}
}
if (text == "/t&h") {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
String Temp=("Temp: " + String(data.temperature, 2) + "°C");
Temp=Temp+(", Humidity: " + String(data.humidity, 1) + "%");
bot.sendMessage(chat_id, Temp, "");
}
}
}
void setup() {
Serial.begin(115200);
// initialize digital pin as an output.
pinMode(PIN_RELAY_1, OUTPUT);
pinMode(PIN_RELAY_2, OUTPUT);
pinMode(PIN_RELAY_3, OUTPUT);
pinMode(PIN_RELAY_4, OUTPUT);
digitalWrite(PIN_RELAY_1, ledState1);
digitalWrite(PIN_RELAY_2, ledState2);
digitalWrite(PIN_RELAY_3, ledState3);
digitalWrite(PIN_RELAY_4, ledState4);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
// Connect to Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
// Print ESP32 Local IP Address
Serial.println(WiFi.localIP());
Serial.println("Start telegram Led control command");
}
void loop() {
if (millis() > lastTimeBotRan + botRequestDelay) {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while(numNewMessages) {
Serial.println("got response");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
lastTimeBotRan = millis();
}
}





沒有留言:
張貼留言