Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include "time.h"
- #include <WiFiClientSecure.h>
- #include <UniversalTelegramBot.h>
- #define WIFI_SSID "Ne"
- #define WIFI_PASSWORD "Aa112233"
- #define BOT_TOKEN "6327970846:AAGs1mA9zWMEQOXbQE-0vu9CT1TX0NQB2D4"
- //6157918800:AAHa8i0n_8L8FpO0cDcWnOkhctY8CANGuac"
- String chat_id_0 = "-4061420365";
- //"985890015";
- WiFiClientSecure secured_client;
- UniversalTelegramBot bot(BOT_TOKEN, secured_client);
- const unsigned long BOT_MTBS = 1000;
- unsigned long bot_lasttime;
- bool Start = false;
- String currentTime;
- String setTime_pill_1 = "21:20:20";
- String setTime_pill_2 = "21:21:20";
- String setTime_pill_3 = "21:22:20";
- String setTime_pill_4 = "21:23:20";
- String setTime_pill_5 = "21:24:20";
- int flag_pill_1 = 0;
- int flag_pill_2 = 0;
- int flag_pill_3 = 0;
- int flag_pill_4 = 0;
- int flag_pill_5 = 0;
- #define redled_1 18
- #define redled_2 19
- #define redled_3 21
- #define redled_4 22
- #define redled_5 23
- #define LDR_1 32
- #define LDR_2 35
- #define LDR_3 34
- #define LDR_4 39
- #define LDR_5 36
- int Red_LED [5] = {redled_1, redled_2, redled_3, redled_4, redled_5};
- int LDR [5] = {LDR_1, LDR_2, LDR_3, LDR_4, LDR_5};
- int pill_Sensor [5] = {0, 0, 0, 0, 0};
- int ldr_Reading [5] = {0, 0, 0, 0, 0};
- unsigned long prevMillis = 0;
- const long interval = 10000;// 10000 ms = 10 sec
- void setup() {
- Serial.begin(115200);
- Serial.setDebugOutput(true);
- startWifi();
- initTime("<+03>-3");
- printLocalTime();
- Serial.println(currentTime);
- Setup_Inputs_Outputs();
- bot.sendMessage(chat_id_0, "bot started up !");
- }
- void loop() {
- //read_Telegram_msgs();
- Read_Sensors_and_Show_on_Serial_Monitor();
- printLocalTime();
- pill_1_Logic();
- pill_2_Logic();
- pill_3_Logic();
- pill_4_Logic();
- pill_5_Logic();
- }
- //---------------------------------------------------------------------------------------------------------------------------------
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////// pill_1_Logic
- void pill_1_Logic() {
- if (!strcmp(currentTime.c_str(), setTime_pill_1.c_str()) && flag_pill_1 == 0) {
- Serial.println(currentTime);
- Serial.println("Take pill #1");
- bot.sendMessage(chat_id_0, "Please Take pill #1 Now !!");
- digitalWrite(Red_LED[0], 1);
- flag_pill_1 = 1;
- prevMillis = millis();
- delay(5000);
- }
- if (flag_pill_1 == 1 && pill_Sensor [0] == 1) {
- // Send reminder to Telegram To take Pill
- // Turn on Pill LED
- digitalWrite(Red_LED[0], 1);
- // Start timer to send reminder again if pill not taken
- if (millis() - prevMillis >= interval) {
- prevMillis = millis();
- Serial.println("10 seconds have passed");
- Serial.println(currentTime);
- // Send reminder to Telegram To take Pill
- bot.sendMessage(chat_id_0, "Reminder - Please Take pill #1 Now !!");
- }
- }
- else if (flag_pill_1 == 1 && pill_Sensor [0] == 0) {
- flag_pill_1 = 0;
- digitalWrite(Red_LED[0], 0);
- // Turn off Pill LED
- // Send reminder to Telegram that pill has been taken
- bot.sendMessage(chat_id_0, "Pill #1 has been taken thank you..");
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////// pill_1_Logic
- //---------------------------------------------------------------------------------------------------------------------------------
- //---------------------------------------------------------------------------------------------------------------------------------
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////// pill_2_Logic
- void pill_2_Logic() {
- if (!strcmp(currentTime.c_str(), setTime_pill_2.c_str()) && flag_pill_2 == 0) {
- Serial.println(currentTime);
- Serial.println("Take pill #2");
- bot.sendMessage(chat_id_0, "Please Take pill #2 Now !!");
- digitalWrite(Red_LED[1], 1);
- flag_pill_2 = 1;
- prevMillis = millis();
- delay(5000);
- }
- if (flag_pill_2 == 1 && pill_Sensor [1] == 1) {
- // Send reminder to Telegram To take Pill
- // Turn on Pill LED
- digitalWrite(Red_LED[1], 1);
- // Start timer to send reminder again if pill not taken
- if (millis() - prevMillis >= interval) {
- prevMillis = millis();
- Serial.println("10 seconds have passed");
- Serial.println(currentTime);
- // Send reminder to Telegram To take Pill
- bot.sendMessage(chat_id_0, "Reminder - Please Take pill #2 Now !!");
- }
- }
- else if (flag_pill_2 == 1 && pill_Sensor [1] == 0) {
- flag_pill_2 = 0;
- digitalWrite(Red_LED[1], 0);
- // Turn off Pill LED
- // Send reminder to Telegram that pill has been taken
- bot.sendMessage(chat_id_0, "Pill #2 has been taken thank you..");
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////// pill_2_Logic
- //---------------------------------------------------------------------------------------------------------------------------------
- //---------------------------------------------------------------------------------------------------------------------------------
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////// pill_3_Logic
- void pill_3_Logic() {
- if (!strcmp(currentTime.c_str(), setTime_pill_3.c_str()) && flag_pill_3 == 0) {
- Serial.println(currentTime);
- Serial.println("Take pill #3");
- bot.sendMessage(chat_id_0, "Please Take pill #3 Now !!");
- digitalWrite(Red_LED[2], 1);
- flag_pill_3 = 1;
- prevMillis = millis();
- delay(5000);
- }
- if (flag_pill_3 == 1 && pill_Sensor [2] == 1) {
- // Send reminder to Telegram To take Pill
- // Turn on Pill LED
- digitalWrite(Red_LED[2], 1);
- // Start timer to send reminder again if pill not taken
- if (millis() - prevMillis >= interval) {
- prevMillis = millis();
- Serial.println("10 seconds have passed");
- Serial.println(currentTime);
- // Send reminder to Telegram To take Pill
- bot.sendMessage(chat_id_0, "Reminder - Please Take pill #3 Now !!");
- }
- }
- else if (flag_pill_3 == 1 && pill_Sensor [2] == 0) {
- flag_pill_3 = 0;
- digitalWrite(Red_LED[2], 0);
- // Turn off Pill LED
- // Send reminder to Telegram that pill has been taken
- bot.sendMessage(chat_id_0, "Pill #3 has been taken thank you..");
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////// pill_3_Logic
- //---------------------------------------------------------------------------------------------------------------------------------
- //---------------------------------------------------------------------------------------------------------------------------------
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////// pill_4_Logic
- void pill_4_Logic() {
- if (!strcmp(currentTime.c_str(), setTime_pill_4.c_str()) && flag_pill_4 == 0) {
- Serial.println(currentTime);
- Serial.println("Take pill #4");
- bot.sendMessage(chat_id_0, "Please Take pill #4 Now !!");
- digitalWrite(Red_LED[3], 1);
- flag_pill_4 = 1;
- prevMillis = millis();
- delay(5000);
- }
- if (flag_pill_4 == 1 && pill_Sensor [3] == 1) {
- // Send reminder to Telegram To take Pill
- // Turn on Pill LED
- digitalWrite(Red_LED[3], 1);
- // Start timer to send reminder again if pill not taken
- if (millis() - prevMillis >= interval) {
- prevMillis = millis();
- Serial.println("10 seconds have passed");
- Serial.println(currentTime);
- // Send reminder to Telegram To take Pill
- bot.sendMessage(chat_id_0, "Reminder - Please Take pill #4 Now !!");
- }
- }
- else if (flag_pill_4 == 1 && pill_Sensor [3] == 0) {
- flag_pill_4 = 0;
- digitalWrite(Red_LED[3], 0);
- // Turn off Pill LED
- // Send reminder to Telegram that pill has been taken
- bot.sendMessage(chat_id_0, "Pill #4 has been taken thank you..");
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////// pill_4_Logic
- //---------------------------------------------------------------------------------------------------------------------------------
- //---------------------------------------------------------------------------------------------------------------------------------
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////// pill_5_Logic
- void pill_5_Logic() {
- if (!strcmp(currentTime.c_str(), setTime_pill_5.c_str()) && flag_pill_5 == 0) {
- Serial.println(currentTime);
- Serial.println("Take pill #5");
- bot.sendMessage(chat_id_0, "Please Take pill #5 Now !!");
- digitalWrite(Red_LED[4], 1);
- flag_pill_5 = 1;
- prevMillis = millis();
- delay(5000);
- }
- if (flag_pill_5 == 1 && pill_Sensor [4] == 1) {
- // Send reminder to Telegram To take Pill
- // Turn on Pill LED
- digitalWrite(Red_LED[4], 1);
- // Start timer to send reminder again if pill not taken
- if (millis() - prevMillis >= interval) {
- prevMillis = millis();
- Serial.println("10 seconds have passed");
- Serial.println(currentTime);
- // Send reminder to Telegram To take Pill
- bot.sendMessage(chat_id_0, "Reminder - Please Take pill #5 Now !!");
- }
- }
- else if (flag_pill_5 == 1 && pill_Sensor [4] == 0) {
- flag_pill_5 = 0;
- digitalWrite(Red_LED[4], 0);
- // Turn off Pill LED
- // Send reminder to Telegram that pill has been taken
- bot.sendMessage(chat_id_0, "Pill #5 has been taken thank you..");
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////// pill_5_Logic
- //---------------------------------------------------------------------------------------------------------------------------------
- void setTimezone(String timezone) {
- Serial.printf(" Setting Timezone to %s\n", timezone.c_str());
- setenv("TZ", timezone.c_str(), 1); // Now adjust the TZ. Clock settings are adjusted to show the new local time
- tzset();
- }
- void initTime(String timezone) {
- struct tm timeinfo;
- Serial.println("Setting up time");
- configTime(0, 0, "pool.ntp.org"); // First connect to NTP server, with 0 TZ offset
- if (!getLocalTime(&timeinfo)) {
- Serial.println(" Failed to obtain time");
- return;
- }
- Serial.println(" Got the time from NTP");
- // Now we can set the real timezone
- setTimezone(timezone);
- }
- void printLocalTime() {
- struct tm timeinfo;
- if (!getLocalTime(&timeinfo)) {
- Serial.println("Failed to obtain time 1");
- return;
- }
- //Serial.println(&timeinfo, "%H:%M:%S");//"%A, %B %d %Y %H:%M:%S zone %Z %z "
- // Use String formatting similar to sprintf()
- currentTime = String() + timeinfo.tm_hour + ":" + timeinfo.tm_min + ":" + timeinfo.tm_sec;
- // Print the String
- Serial.println(currentTime);
- }
- void startWifi() {
- WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
- secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
- Serial.println("Connecting Wifi");
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print(".");
- delay(500);
- }
- Serial.print("Wifi RSSI=");
- Serial.println(WiFi.RSSI());
- Serial.print("Retrieving time: ");
- configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
- time_t now = time(nullptr);
- while (now < 24 * 3600)
- {
- Serial.print(".");
- delay(100);
- now = time(nullptr);
- }
- Serial.println(now);
- }
- void setTime(int yr, int month, int mday, int hr, int minute, int sec, int isDst) {
- struct tm tm;
- tm.tm_year = yr - 1900; // Set date
- tm.tm_mon = month - 1;
- tm.tm_mday = mday;
- tm.tm_hour = hr; // Set time
- tm.tm_min = minute;
- tm.tm_sec = sec;
- tm.tm_isdst = isDst; // 1 or 0
- time_t t = mktime(&tm);
- Serial.printf("Setting time: %s", asctime(&tm));
- struct timeval now = { .tv_sec = t };
- settimeofday(&now, NULL);
- }
- void Setup_Inputs_Outputs() {
- for (int i = 0; i < 5; i++) {
- pinMode(Red_LED[i], OUTPUT);
- pinMode(LDR[i], INPUT);
- }
- for (int i = 0; i < 3; i++) {
- for (int i = 0; i < 5; i++) {
- digitalWrite(Red_LED[i], 1);
- delay(500);
- digitalWrite(Red_LED[i], 0);
- delay(500);
- }
- }
- }
- void Read_Sensors_and_Show_on_Serial_Monitor() {
- for (int i = 0; i < 5; i++) {
- ldr_Reading [i] = analogRead(LDR[i]);
- if (ldr_Reading[i] > 1000) {
- pill_Sensor [i] = 1;
- }
- else if (ldr_Reading[i] < 1000) {
- pill_Sensor [i] = 0;
- }
- if (i == 4) {
- //Serial.print(ldr_Reading[i]);
- //Serial.print(pill_Sensor[i]);
- }
- else {
- // Serial.print(String(ldr_Reading[i]) + ", ");
- //Serial.print(String(pill_Sensor[i]) + ", ");
- }
- }
- //Serial.println("\n------------------------------");
- }
- void handleNewMessages(int numNewMessages)
- {
- Serial.println("handleNewMessages");
- Serial.println(String(numNewMessages));
- for (int i = 0; i < numNewMessages; i++)
- {
- String chat_id = 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 == "/send_test_action")
- {
- bot.sendChatAction(chat_id, "typing");
- delay(4000);
- bot.sendMessage(chat_id, "Did you see the action message?");
- // You can't use own message, just choose from one of bellow
- //typing for text messages
- //upload_photo for photos
- //record_video or upload_video for videos
- //record_audio or upload_audio for audio files
- //upload_document for general files
- //find_location for location data
- //more info here - https://core.telegram.org/bots/api#sendchataction
- }
- if (text == "/start")
- {
- String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
- welcome += "This is Chat Action Bot example.\n\n";
- welcome += "/send_test_action : to send test chat action message\n";
- bot.sendMessage(chat_id, welcome);
- }
- }
- }
- void read_Telegram_msgs() {
- 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();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement