#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
// Initialize Wifi connection to the router
char ssid[] = "***"; // نام روتر وایفای
char password[] = "123456789"; // پسورد وایفای
// Initialize Telegram BOT
#define BOTtoken "441099696:AAE**************nBb9Qp4NnqpH2XvuE" // توکن ربات تلگرام
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
int Bot_mtbs = 1000; //mean time between scan messages
long Bot_lasttime; //last time messages' scan has been done
bool Start = false;
const int ledPin = D2;
int ledStatus = 0;
void handleNewMessages(int numNewMessages) {
Serial.println("handleNewMessages");
Serial.println(String(numNewMessages));
for (int i=0; i<numNewMessages; i++) {
String chat_id = String(bot.messages[i].chat_id);
String text = bot.messages[i].text;
String from_name = bot.messages[i].from_name;
if (from_name == "") from_name = "Guest";
if (text == "/ledon") {
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
ledStatus = 1;
bot.sendMessage(chat_id, "Led is ON", "");
}
if (text == "/ledoff") {
ledStatus = 0;
digitalWrite(ledPin, LOW); // turn the LED off (LOW is the voltage level)
bot.sendMessage(chat_id, "Led is OFF", "");
}
if (text == "/status") {
if(ledStatus){
bot.sendMessage(chat_id, "Led is ON", "");
} else {
bot.sendMessage(chat_id, "Led is OFF", "");
}
}
if (text == "/ip") {
IPAddress ip;
char buffer[20];
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
ip = WiFi.localIP();
sprintf(buffer,"ESP Ip Adress : %d.%d.%d.%d", ip[0],ip[1],ip[2],ip[3]);
Serial.println(buffer);
bot.sendMessage(chat_id, buffer, "");
}
if (text == "/start") {
String welcome = "به برنامه ارتباط اینترنت اشیاء با ربات تلگرام خوش آمدید" + @MicroDroidPrj + ".\n";
welcome += "برنامه نمونه برای مدیریت ال ای دی.\n\n";
welcome += "/ip : جهت دریافت مقدار آی پی از این دستور استفاده کنید.\n";
welcome += "/ledon : جهت روشن کردن ال ای دی از این دستور استفاده کنید.\n";
welcome += "/ledoff : جهت خاموش کردن ال ای دی از این دستور استفاده کنید.\n";
welcome += "/status : چنانچه نمیدانید ال ای دی در چه وضعیتی است از این دستور استفاده کنید.\n";
welcome += "چت آی دی شما :\n";
welcome += chat_id ;
welcome += "\n MicroDroidPrj.ir" ;
bot.sendMessage(chat_id, welcome, "Markdown");
}
}
}
void setup() {
Serial.begin(115200);
// Set WiFi to station mode and disconnect from an AP if it was Previously
// connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
// attempt to connect to Wifi network:
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
pinMode(ledPin, OUTPUT); // initialize digital ledPin as an output.
delay(10);
digitalWrite(ledPin, LOW); // initialize pin as off
}
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);
}
Bot_lasttime = millis();
}
}
سلام خسته نباشید من میخوام ماژول وای فای esp8266 را برنامه نویسی کنم باید چکار کنم
سلام آموزشش تو سایت هست هم با آردوینو و هم با لدر