#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define BOT_TOKEN "6803695240:AAHEHVvjgXYmI_assmS8r1Rj0YXbnQ6FO2w"
#define ledPin 5
bool ledState = LOW;
String text = "";
const unsigned long BOT_MTBS = 100; // mean time between scan messages
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
unsigned long bot_lasttime; // last time messages' scan has been done
bool Start = false;
void handleNewMessages(int numNewMessages)
{
for (int i = 0; i < numNewMessages; i++)
{
String chat_id = bot.messages[i].chat_id;
text = bot.messages[i].text;
if (text == "/on") {
bot.sendMessage(chat_id, "LED state set to ON", "");
digitalWrite(ledPin, HIGH);
}
if (text == "/off") {
bot.sendMessage(chat_id, "LED state set to OFF", "");
digitalWrite(ledPin, LOW);
}
if (text == "Selam") {
bot.sendMessage(chat_id, "Merhabalar", "");
}
if (text == "ismin ne") {
bot.sendMessage(chat_id, "ismim cabbar", "");
}
//if (text == "boyun kac") {
//bot.sendMessage(chat_id, "", "");
}
}
#define TFT_DC 2
#define TFT_CS 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
void setup() {
Serial.begin(115200);
tft.begin();
WiFi.begin("Wokwi-GUEST", "", 6);
tft.setRotation(1);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
while (WiFi.status() != WL_CONNECTED) {
delay(250);
}
pinMode(ledPin, OUTPUT);
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
if (millis() - bot_lasttime > BOT_MTBS)
{
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);
tft.print(text);
}
bot_lasttime = millis();
}
}