Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifdef ESP32
- #include <WiFi.h>
- #else
- #include <ESP8266WiFi.h>
- #endif
- #include <WiFiClientSecure.h>
- #include <UniversalTelegramBot.h>
- #include <ArduinoJson.h>
- #include <NTPClient.h>
- #include <WiFiUdp.h>
- #define motor1 23
- #define motor2_pin1 14
- #define motor2_pin2 21
- String formattedDate;
- String dayStamp;
- String timeStamp;
- String h ;
- String m ;
- String h1;
- String m1;
- #define BOTtoken "6843843585:AAFEFtbzcDKLJeTUmW0QknVSXAHtB-1guSg"
- #define CHAT_ID "-1001997751986"
- const char* ssid = "Khalidiphone";
- const char* password = "00000000";
- WiFiUDP ntpUDP;
- NTPClient timeClient(ntpUDP);
- #ifdef ESP8266
- X509List cert(TELEGRAM_CERTIFICATE_ROOT);
- #endif
- WiFiClientSecure client;
- UniversalTelegramBot bot(BOTtoken, client);
- int botRequestDelay = 1000;
- unsigned long lastTimeBotRan;
- 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;
- h1=text.substring(0,2);
- m1=text.substring(3,5);
- bot.sendMessage(CHAT_ID, " time set " + String(h1 && m1));
- Serial.println(text);
- String from_name = bot.messages[i++].from_name;
- }
- }
- void setup() {
- // put your setup code here, to run once:
- pinMode(motor1,OUTPUT);
- digitalWrite(motor1,LOW);
- pinMode(motor2_pin2,OUTPUT);
- digitalWrite(motor2_pin2,1);
- pinMode(motor2_pin1,OUTPUT);
- digitalWrite(motor2_pin1,1);
- #ifdef ESP8266
- configTime(0, 0, "pool.ntp.org");
- client.setTrustAnchors(&cert);
- #endif
- // Initialize Serial Monitor
- Serial.begin(115200);
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- // Print local IP address and start web server
- Serial.println("");
- Serial.println("WiFi connected.");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- // Initialize a NTPClient to get time
- timeClient.begin();
- // Set offset time in seconds to adjust for your timezone, for example:
- // GMT +1 = 3600
- // GMT +8 = 28800
- // GMT -1 = -3600
- // GMT 0 = 0
- timeClient.setTimeOffset(10800);
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- #ifdef ESP32
- client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
- #endif
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.println("Connecting to WiFi..");
- }
- // Print ESP32 Local IP Address
- Serial.println(WiFi.localIP());
- bot.sendMessage(CHAT_ID, "Bot started up", "");
- }
- void loop() {
- // put your main code here, to run repeatedly:
- 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();}
- while(!timeClient.update()) {
- timeClient.forceUpdate();
- }
- // The formattedDate comes with the following format:
- // 2018-05-28T16:00:13Z
- // We need to extract date and time
- formattedDate = timeClient.getFormattedDate();
- //Serial.println(formattedDate);
- // Extract date
- int splitT = formattedDate.indexOf("T");
- dayStamp = formattedDate.substring(0, splitT);
- //Serial.print("DATE: ");
- //Serial.println(dayStamp);
- // Extract time
- timeStamp = formattedDate.substring(splitT+1, formattedDate.length()-1);
- //Serial.print("HOUR: ");
- //Serial.println(timeStamp);
- h=timeStamp.substring(0,2);
- m=timeStamp.substring(3,5);
- Serial.println(h);
- Serial.println(m);
- delay(1000);
- if(h1== h && m1== m)
- {
- digitalWrite(motor1,HIGH);
- delay(500);
- digitalWrite(motor2_pin2,HIGH);
- digitalWrite(motor2_pin1,LOW);
- }
- else {
- digitalWrite(motor1,LOW);
- digitalWrite(motor2_pin1,1);
- digitalWrite(motor2_pin2,1);}
- }
Add Comment
Please, Sign In to add comment