Advertisement
safwan092

Untitled

Oct 23rd, 2023
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.50 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include "time.h"
  3. #include <WiFiClientSecure.h>
  4. #include <UniversalTelegramBot.h>
  5.  
  6. #define WIFI_SSID "Ne"
  7. #define WIFI_PASSWORD "Aa112233"
  8.  
  9. #define BOT_TOKEN "6327970846:AAGs1mA9zWMEQOXbQE-0vu9CT1TX0NQB2D4"
  10. //6157918800:AAHa8i0n_8L8FpO0cDcWnOkhctY8CANGuac"
  11. String chat_id_0 = "-4061420365";
  12. //"985890015";
  13.  
  14. WiFiClientSecure secured_client;
  15. UniversalTelegramBot bot(BOT_TOKEN, secured_client);
  16. const unsigned long BOT_MTBS = 1000;
  17. unsigned long bot_lasttime;
  18. bool Start = false;
  19.  
  20. String currentTime;
  21. String setTime_pill_1 = "21:20:20";
  22. String setTime_pill_2 = "21:21:20";
  23. String setTime_pill_3 = "21:22:20";
  24. String setTime_pill_4 = "21:23:20";
  25. String setTime_pill_5 = "21:24:20";
  26.  
  27. int flag_pill_1 = 0;
  28. int flag_pill_2 = 0;
  29. int flag_pill_3 = 0;
  30. int flag_pill_4 = 0;
  31. int flag_pill_5 = 0;
  32.  
  33. #define redled_1 18
  34. #define redled_2 19
  35. #define redled_3 21
  36. #define redled_4 22
  37. #define redled_5 23
  38. #define LDR_1 32
  39. #define LDR_2 35
  40. #define LDR_3 34
  41. #define LDR_4 39
  42. #define LDR_5 36
  43.  
  44. int Red_LED [5] = {redled_1, redled_2, redled_3, redled_4, redled_5};
  45. int LDR [5] = {LDR_1, LDR_2, LDR_3, LDR_4, LDR_5};
  46. int pill_Sensor [5] = {0, 0, 0, 0, 0};
  47. int ldr_Reading [5] = {0, 0, 0, 0, 0};
  48.  
  49.  
  50. unsigned long prevMillis = 0;
  51. const long interval = 10000;// 10000 ms = 10 sec
  52.  
  53. void setup() {
  54. Serial.begin(115200);
  55. Serial.setDebugOutput(true);
  56. startWifi();
  57. initTime("<+03>-3");
  58. printLocalTime();
  59. Serial.println(currentTime);
  60. Setup_Inputs_Outputs();
  61. bot.sendMessage(chat_id_0, "bot started up !");
  62. }
  63.  
  64. void loop() {
  65. //read_Telegram_msgs();
  66. Read_Sensors_and_Show_on_Serial_Monitor();
  67. printLocalTime();
  68. pill_1_Logic();
  69. pill_2_Logic();
  70. pill_3_Logic();
  71. pill_4_Logic();
  72. pill_5_Logic();
  73. }
  74.  
  75. //---------------------------------------------------------------------------------------------------------------------------------
  76. ///////////////////////////////////////////////////////////////////////////////////////////////////////////// pill_1_Logic
  77.  
  78. void pill_1_Logic() {
  79. if (!strcmp(currentTime.c_str(), setTime_pill_1.c_str()) && flag_pill_1 == 0) {
  80. Serial.println(currentTime);
  81. Serial.println("Take pill #1");
  82. bot.sendMessage(chat_id_0, "Please Take pill #1 Now !!");
  83. digitalWrite(Red_LED[0], 1);
  84. flag_pill_1 = 1;
  85. prevMillis = millis();
  86. delay(5000);
  87. }
  88. if (flag_pill_1 == 1 && pill_Sensor [0] == 1) {
  89. // Send reminder to Telegram To take Pill
  90. // Turn on Pill LED
  91. digitalWrite(Red_LED[0], 1);
  92. // Start timer to send reminder again if pill not taken
  93. if (millis() - prevMillis >= interval) {
  94. prevMillis = millis();
  95. Serial.println("10 seconds have passed");
  96. Serial.println(currentTime);
  97. // Send reminder to Telegram To take Pill
  98. bot.sendMessage(chat_id_0, "Reminder - Please Take pill #1 Now !!");
  99. }
  100. }
  101. else if (flag_pill_1 == 1 && pill_Sensor [0] == 0) {
  102. flag_pill_1 = 0;
  103. digitalWrite(Red_LED[0], 0);
  104. // Turn off Pill LED
  105. // Send reminder to Telegram that pill has been taken
  106. bot.sendMessage(chat_id_0, "Pill #1 has been taken thank you..");
  107. }
  108. }
  109.  
  110. ///////////////////////////////////////////////////////////////////////////////////////////////////////////// pill_1_Logic
  111. //---------------------------------------------------------------------------------------------------------------------------------
  112. //---------------------------------------------------------------------------------------------------------------------------------
  113. ///////////////////////////////////////////////////////////////////////////////////////////////////////////// pill_2_Logic
  114.  
  115. void pill_2_Logic() {
  116. if (!strcmp(currentTime.c_str(), setTime_pill_2.c_str()) && flag_pill_2 == 0) {
  117. Serial.println(currentTime);
  118. Serial.println("Take pill #2");
  119. bot.sendMessage(chat_id_0, "Please Take pill #2 Now !!");
  120. digitalWrite(Red_LED[1], 1);
  121. flag_pill_2 = 1;
  122. prevMillis = millis();
  123. delay(5000);
  124. }
  125. if (flag_pill_2 == 1 && pill_Sensor [1] == 1) {
  126. // Send reminder to Telegram To take Pill
  127. // Turn on Pill LED
  128. digitalWrite(Red_LED[1], 1);
  129. // Start timer to send reminder again if pill not taken
  130. if (millis() - prevMillis >= interval) {
  131. prevMillis = millis();
  132. Serial.println("10 seconds have passed");
  133. Serial.println(currentTime);
  134. // Send reminder to Telegram To take Pill
  135. bot.sendMessage(chat_id_0, "Reminder - Please Take pill #2 Now !!");
  136. }
  137. }
  138. else if (flag_pill_2 == 1 && pill_Sensor [1] == 0) {
  139. flag_pill_2 = 0;
  140. digitalWrite(Red_LED[1], 0);
  141. // Turn off Pill LED
  142. // Send reminder to Telegram that pill has been taken
  143. bot.sendMessage(chat_id_0, "Pill #2 has been taken thank you..");
  144. }
  145. }
  146.  
  147. ///////////////////////////////////////////////////////////////////////////////////////////////////////////// pill_2_Logic
  148. //---------------------------------------------------------------------------------------------------------------------------------
  149. //---------------------------------------------------------------------------------------------------------------------------------
  150. ///////////////////////////////////////////////////////////////////////////////////////////////////////////// pill_3_Logic
  151.  
  152. void pill_3_Logic() {
  153. if (!strcmp(currentTime.c_str(), setTime_pill_3.c_str()) && flag_pill_3 == 0) {
  154. Serial.println(currentTime);
  155. Serial.println("Take pill #3");
  156. bot.sendMessage(chat_id_0, "Please Take pill #3 Now !!");
  157. digitalWrite(Red_LED[2], 1);
  158. flag_pill_3 = 1;
  159. prevMillis = millis();
  160. delay(5000);
  161. }
  162. if (flag_pill_3 == 1 && pill_Sensor [2] == 1) {
  163. // Send reminder to Telegram To take Pill
  164. // Turn on Pill LED
  165. digitalWrite(Red_LED[2], 1);
  166. // Start timer to send reminder again if pill not taken
  167. if (millis() - prevMillis >= interval) {
  168. prevMillis = millis();
  169. Serial.println("10 seconds have passed");
  170. Serial.println(currentTime);
  171. // Send reminder to Telegram To take Pill
  172. bot.sendMessage(chat_id_0, "Reminder - Please Take pill #3 Now !!");
  173. }
  174. }
  175. else if (flag_pill_3 == 1 && pill_Sensor [2] == 0) {
  176. flag_pill_3 = 0;
  177. digitalWrite(Red_LED[2], 0);
  178. // Turn off Pill LED
  179. // Send reminder to Telegram that pill has been taken
  180. bot.sendMessage(chat_id_0, "Pill #3 has been taken thank you..");
  181. }
  182. }
  183.  
  184. ///////////////////////////////////////////////////////////////////////////////////////////////////////////// pill_3_Logic
  185. //---------------------------------------------------------------------------------------------------------------------------------
  186. //---------------------------------------------------------------------------------------------------------------------------------
  187. ///////////////////////////////////////////////////////////////////////////////////////////////////////////// pill_4_Logic
  188.  
  189. void pill_4_Logic() {
  190. if (!strcmp(currentTime.c_str(), setTime_pill_4.c_str()) && flag_pill_4 == 0) {
  191. Serial.println(currentTime);
  192. Serial.println("Take pill #4");
  193. bot.sendMessage(chat_id_0, "Please Take pill #4 Now !!");
  194. digitalWrite(Red_LED[3], 1);
  195. flag_pill_4 = 1;
  196. prevMillis = millis();
  197. delay(5000);
  198. }
  199. if (flag_pill_4 == 1 && pill_Sensor [3] == 1) {
  200. // Send reminder to Telegram To take Pill
  201. // Turn on Pill LED
  202. digitalWrite(Red_LED[3], 1);
  203. // Start timer to send reminder again if pill not taken
  204. if (millis() - prevMillis >= interval) {
  205. prevMillis = millis();
  206. Serial.println("10 seconds have passed");
  207. Serial.println(currentTime);
  208. // Send reminder to Telegram To take Pill
  209. bot.sendMessage(chat_id_0, "Reminder - Please Take pill #4 Now !!");
  210. }
  211. }
  212. else if (flag_pill_4 == 1 && pill_Sensor [3] == 0) {
  213. flag_pill_4 = 0;
  214. digitalWrite(Red_LED[3], 0);
  215. // Turn off Pill LED
  216. // Send reminder to Telegram that pill has been taken
  217. bot.sendMessage(chat_id_0, "Pill #4 has been taken thank you..");
  218. }
  219. }
  220.  
  221. ///////////////////////////////////////////////////////////////////////////////////////////////////////////// pill_4_Logic
  222. //---------------------------------------------------------------------------------------------------------------------------------
  223. //---------------------------------------------------------------------------------------------------------------------------------
  224. ///////////////////////////////////////////////////////////////////////////////////////////////////////////// pill_5_Logic
  225.  
  226. void pill_5_Logic() {
  227. if (!strcmp(currentTime.c_str(), setTime_pill_5.c_str()) && flag_pill_5 == 0) {
  228. Serial.println(currentTime);
  229. Serial.println("Take pill #5");
  230. bot.sendMessage(chat_id_0, "Please Take pill #5 Now !!");
  231. digitalWrite(Red_LED[4], 1);
  232. flag_pill_5 = 1;
  233. prevMillis = millis();
  234. delay(5000);
  235. }
  236. if (flag_pill_5 == 1 && pill_Sensor [4] == 1) {
  237. // Send reminder to Telegram To take Pill
  238. // Turn on Pill LED
  239. digitalWrite(Red_LED[4], 1);
  240. // Start timer to send reminder again if pill not taken
  241. if (millis() - prevMillis >= interval) {
  242. prevMillis = millis();
  243. Serial.println("10 seconds have passed");
  244. Serial.println(currentTime);
  245. // Send reminder to Telegram To take Pill
  246. bot.sendMessage(chat_id_0, "Reminder - Please Take pill #5 Now !!");
  247. }
  248. }
  249. else if (flag_pill_5 == 1 && pill_Sensor [4] == 0) {
  250. flag_pill_5 = 0;
  251. digitalWrite(Red_LED[4], 0);
  252. // Turn off Pill LED
  253. // Send reminder to Telegram that pill has been taken
  254. bot.sendMessage(chat_id_0, "Pill #5 has been taken thank you..");
  255. }
  256. }
  257.  
  258. ///////////////////////////////////////////////////////////////////////////////////////////////////////////// pill_5_Logic
  259. //---------------------------------------------------------------------------------------------------------------------------------
  260.  
  261.  
  262.  
  263. void setTimezone(String timezone) {
  264. Serial.printf(" Setting Timezone to %s\n", timezone.c_str());
  265. setenv("TZ", timezone.c_str(), 1); // Now adjust the TZ. Clock settings are adjusted to show the new local time
  266. tzset();
  267. }
  268.  
  269. void initTime(String timezone) {
  270. struct tm timeinfo;
  271.  
  272. Serial.println("Setting up time");
  273. configTime(0, 0, "pool.ntp.org"); // First connect to NTP server, with 0 TZ offset
  274. if (!getLocalTime(&timeinfo)) {
  275. Serial.println(" Failed to obtain time");
  276. return;
  277. }
  278. Serial.println(" Got the time from NTP");
  279. // Now we can set the real timezone
  280. setTimezone(timezone);
  281. }
  282.  
  283. void printLocalTime() {
  284. struct tm timeinfo;
  285. if (!getLocalTime(&timeinfo)) {
  286. Serial.println("Failed to obtain time 1");
  287. return;
  288. }
  289.  
  290. //Serial.println(&timeinfo, "%H:%M:%S");//"%A, %B %d %Y %H:%M:%S zone %Z %z "
  291.  
  292. // Use String formatting similar to sprintf()
  293. currentTime = String() + timeinfo.tm_hour + ":" + timeinfo.tm_min + ":" + timeinfo.tm_sec;
  294.  
  295. // Print the String
  296. Serial.println(currentTime);
  297. }
  298.  
  299. void startWifi() {
  300. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  301. secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
  302. Serial.println("Connecting Wifi");
  303. while (WiFi.status() != WL_CONNECTED) {
  304. Serial.print(".");
  305. delay(500);
  306. }
  307. Serial.print("Wifi RSSI=");
  308. Serial.println(WiFi.RSSI());
  309. Serial.print("Retrieving time: ");
  310. configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
  311. time_t now = time(nullptr);
  312. while (now < 24 * 3600)
  313. {
  314. Serial.print(".");
  315. delay(100);
  316. now = time(nullptr);
  317. }
  318. Serial.println(now);
  319. }
  320.  
  321. void setTime(int yr, int month, int mday, int hr, int minute, int sec, int isDst) {
  322. struct tm tm;
  323.  
  324. tm.tm_year = yr - 1900; // Set date
  325. tm.tm_mon = month - 1;
  326. tm.tm_mday = mday;
  327. tm.tm_hour = hr; // Set time
  328. tm.tm_min = minute;
  329. tm.tm_sec = sec;
  330. tm.tm_isdst = isDst; // 1 or 0
  331. time_t t = mktime(&tm);
  332. Serial.printf("Setting time: %s", asctime(&tm));
  333. struct timeval now = { .tv_sec = t };
  334. settimeofday(&now, NULL);
  335. }
  336.  
  337. void Setup_Inputs_Outputs() {
  338. for (int i = 0; i < 5; i++) {
  339. pinMode(Red_LED[i], OUTPUT);
  340. pinMode(LDR[i], INPUT);
  341. }
  342. for (int i = 0; i < 3; i++) {
  343. for (int i = 0; i < 5; i++) {
  344. digitalWrite(Red_LED[i], 1);
  345. delay(500);
  346. digitalWrite(Red_LED[i], 0);
  347. delay(500);
  348. }
  349. }
  350. }
  351.  
  352. void Read_Sensors_and_Show_on_Serial_Monitor() {
  353. for (int i = 0; i < 5; i++) {
  354. ldr_Reading [i] = analogRead(LDR[i]);
  355. if (ldr_Reading[i] > 1000) {
  356. pill_Sensor [i] = 1;
  357. }
  358. else if (ldr_Reading[i] < 1000) {
  359. pill_Sensor [i] = 0;
  360. }
  361. if (i == 4) {
  362. //Serial.print(ldr_Reading[i]);
  363. //Serial.print(pill_Sensor[i]);
  364. }
  365. else {
  366. // Serial.print(String(ldr_Reading[i]) + ", ");
  367. //Serial.print(String(pill_Sensor[i]) + ", ");
  368. }
  369. }
  370. //Serial.println("\n------------------------------");
  371. }
  372.  
  373.  
  374. void handleNewMessages(int numNewMessages)
  375. {
  376. Serial.println("handleNewMessages");
  377. Serial.println(String(numNewMessages));
  378.  
  379. for (int i = 0; i < numNewMessages; i++)
  380. {
  381. String chat_id = bot.messages[i].chat_id;
  382. String text = bot.messages[i].text;
  383.  
  384. String from_name = bot.messages[i].from_name;
  385. if (from_name == "")
  386. from_name = "Guest";
  387.  
  388. if (text == "/send_test_action")
  389. {
  390. bot.sendChatAction(chat_id, "typing");
  391. delay(4000);
  392. bot.sendMessage(chat_id, "Did you see the action message?");
  393.  
  394. // You can't use own message, just choose from one of bellow
  395.  
  396. //typing for text messages
  397. //upload_photo for photos
  398. //record_video or upload_video for videos
  399. //record_audio or upload_audio for audio files
  400. //upload_document for general files
  401. //find_location for location data
  402.  
  403. //more info here - https://core.telegram.org/bots/api#sendchataction
  404. }
  405.  
  406. if (text == "/start")
  407. {
  408. String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
  409. welcome += "This is Chat Action Bot example.\n\n";
  410. welcome += "/send_test_action : to send test chat action message\n";
  411. bot.sendMessage(chat_id, welcome);
  412. }
  413. }
  414. }
  415.  
  416. void read_Telegram_msgs() {
  417. if (millis() - bot_lasttime > BOT_MTBS)
  418. {
  419. int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  420.  
  421. while (numNewMessages)
  422. {
  423. Serial.println("got response");
  424. handleNewMessages(numNewMessages);
  425. numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  426. }
  427.  
  428. bot_lasttime = millis();
  429. }
  430. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement