safwan092

Untitled

May 2nd, 2024
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.26 KB | None | 0 0
  1. #include <Wire.h>
  2. #include "MAX30105.h"
  3. #include "heartRate.h"
  4. #include <OneWire.h>
  5. #include <DallasTemperature.h>
  6. #include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
  7. #include <WiFi.h>
  8. #include <WiFiClientSecure.h>
  9. #include <UniversalTelegramBot.h>
  10.  
  11. #define BOT_TOKEN "6988502937:AAGBPyxFT_5HA_S9hX0H2wSvGiHS5GJhfEg"
  12. String chat_id = "-1002037340786";
  13.  
  14. WiFiClientSecure secured_client;
  15. UniversalTelegramBot bot(BOT_TOKEN, secured_client);
  16.  
  17. // GPIO where the DS18B20 is connected to
  18. const int oneWireBus = 10;
  19.  
  20. const unsigned long BOT_MTBS = 5000; // mean time between scan messages
  21. unsigned long bot_lasttime; // last time messages' scan has been done
  22. bool Start = false;
  23.  
  24. // Setup a oneWire instance to communicate with any OneWire devices
  25. OneWire oneWire(oneWireBus);
  26.  
  27. // Pass our oneWire reference to Dallas Temperature sensor
  28. DallasTemperature sensors(&oneWire);
  29.  
  30. MAX30105 particleSensor;
  31. byte rateSpot = 0;
  32. long lastBeat = 0;
  33. float beatsPerMinute;
  34. int beatAvg;
  35. float temperature;
  36. unsigned long dataMillis = 0;
  37. long irValue;
  38. long delta;
  39. float temperatureC;
  40. const byte RATE_SIZE = 4;
  41. byte rates[RATE_SIZE];
  42. unsigned long previousMillis = 0;
  43.  
  44. int value2 = 0;
  45. int value3 = 0;
  46. int hr_flag = 0;
  47. int temp_flag = 0;
  48.  
  49. void handleNewMessages(int numNewMessages)
  50. {
  51. Serial.println("handleNewMessages");
  52. Serial.println(String(numNewMessages));
  53. for (int i = 0; i < numNewMessages; i++)
  54. {
  55. String chat_id = bot.messages[i].chat_id;
  56. String text = bot.messages[i].text;
  57.  
  58. String from_name = bot.messages[i].from_name;
  59. if (from_name == "")
  60. from_name = "Guest";
  61.  
  62.  
  63. // Find position of ',' and '='
  64. int posB = text.indexOf("b=");
  65. int posC = text.indexOf("c=");
  66.  
  67. if (posB != -1 && posC != -1) {
  68. // Extract values of a and b
  69. String value2String = text.substring(posB + 2, posC - 1);
  70. String value3String = text.substring(posC + 2);
  71.  
  72. // Convert string values to integers
  73. value2 = value2String.toInt();
  74. value3 = value3String.toInt();
  75. }
  76.  
  77. // Print extracted values for debugging
  78. Serial.print("Value of b: ");
  79. Serial.println(value2);
  80. Serial.print("Value of c: ");
  81. Serial.println(value3);
  82.  
  83. if (text == "/data")
  84. {
  85. String temp_str = "Temp: " + String(temperatureC) + "Β°C \n";
  86. String beatAvg_str = "Heart Beat: " + String(beatAvg) + " BPM \n";
  87. bot.sendMessage(chat_id, temp_str );
  88. bot.sendMessage(chat_id, beatAvg_str);
  89. }
  90.  
  91. //////////////////////////////////////////////////////////
  92.  
  93. if (text == "/options")
  94. {
  95. String keyboardJson = "[[\"/data\"]]";
  96. bot.sendMessageWithReplyKeyboard(chat_id, "Choose from one of the following options", "", keyboardJson, true);
  97. }
  98. if (text == "/start")
  99. {
  100. String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
  101. welcome += "This is Chat Action Bot example.\n\n";
  102. welcome += "/send_test_action : to send test chat action message\n";
  103. bot.sendMessage(chat_id, welcome);
  104. }
  105. }
  106. }
  107. ////////////////////////////////////////////////////////////////////////
  108.  
  109. void setup() {
  110. Serial.begin(115200);
  111. //delay(10000);
  112. Serial.println("11111");
  113.  
  114. init_heart_rate_sensor();
  115.  
  116. // Start the DS18B20 sensor
  117. sensors.begin();
  118. Connect_To_WiFi();
  119. Connect_To_Telegram();
  120. bot.sendMessage(chat_id, "HR - [OK]");
  121. bot.sendMessage(chat_id, "Temp - [OK]");
  122. }
  123.  
  124. void loop() {
  125. //readTempSensor();
  126. ReConnect_To_WiFi();
  127. handel_messages_from_Telegram_Users();
  128. read_heart_rate_sensor();
  129. }
  130.  
  131. void init_heart_rate_sensor() {
  132. Serial.println("Initializing...");
  133. // Initialize sensor
  134. if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
  135. {
  136. Serial.println("MAX30105 was not found. Please check wiring/power. ");
  137. while (1);
  138. }
  139. Serial.println("Place your index finger on the sensor with steady pressure.");
  140.  
  141. particleSensor.setup(); //Configure sensor with default settings
  142. particleSensor.enableDIETEMPRDY(); //Enable the temp ready interrupt. This is required.
  143. particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running
  144. particleSensor.setPulseAmplitudeGreen(0); //Turn off Green LED
  145. }
  146.  
  147. void read_heart_rate_sensor() {
  148. irValue = particleSensor.getIR();
  149.  
  150.  
  151. if (checkForBeat(irValue) == true)
  152. {
  153. //We sensed a beat!
  154. delta = millis() - lastBeat;
  155. lastBeat = millis();
  156.  
  157. beatsPerMinute = 60 / (delta / 1000.0);
  158.  
  159. if (beatsPerMinute < 255 && beatsPerMinute > 20) {
  160. rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
  161. rateSpot %= RATE_SIZE; //Wrap variable
  162.  
  163. //Take average of readings
  164. beatAvg = 0;
  165. for (byte x = 0 ; x < RATE_SIZE ; x++)
  166. beatAvg += rates[x];
  167. beatAvg /= RATE_SIZE;
  168. }
  169. }
  170.  
  171. Serial.print("IR=");
  172. Serial.print(irValue);
  173. Serial.print(", BPM=");
  174. Serial.print(beatsPerMinute);
  175. Serial.print(", Avg BPM=");
  176. Serial.print(beatAvg);
  177. //temperature = particleSensor.readTemperature();
  178. //Serial.print(", temperatureC=");
  179. //Serial.print(temperature, 4);
  180. if (millis() - previousMillis >= 10000) {
  181. Serial.print(", temperatureC=");
  182. readTempSensor();
  183. previousMillis = millis();
  184. hr_flag = 0;
  185. temp_flag = 0;
  186. }
  187.  
  188. if (beatAvg < value2 && beatAvg != 0 && hr_flag == 0 && irValue > 50000) {
  189. bot.sendMessage(chat_id, "ALERT - [Heart Beat Problem]");
  190. Serial.println("SEND ALERT MSG To TELEGRAM [Heart Beat Problem]");
  191. hr_flag = 1;
  192. }
  193.  
  194. if (temperatureC > value3 && temp_flag == 0) {
  195. bot.sendMessage(chat_id, "ALERT - [Temperature Problem]");
  196. Serial.println("SEND ALERT MSG To TELEGRAM [Temperature Problem]");
  197. temp_flag = 1;
  198. }
  199.  
  200. if (irValue < 50000)
  201. Serial.print(" No finger?");
  202. Serial.println();
  203. }
  204.  
  205. void readTempSensor() {
  206. sensors.requestTemperatures();
  207. temperatureC = sensors.getTempCByIndex(0) + 2;
  208. Serial.print(temperatureC);
  209. Serial.println("ΒΊC");
  210. }
  211.  
  212.  
  213.  
  214.  
  215. void ReConnect_To_WiFi() {
  216. while (WiFi.status() != WL_CONNECTED) {
  217. //delay(500);
  218. Serial.print(".");
  219. Connect_To_WiFi();
  220. }
  221. }
  222.  
  223. void Connect_To_WiFi() {
  224. WiFiManager wm;
  225. bool res;
  226. res = wm.autoConnect("[Setup WiFi]"); // password protected ap
  227.  
  228. if (!res) {
  229. Serial.println("Failed to Connect!!");
  230. // ESP.restart();
  231. }
  232. else {
  233. //if you get here you have connected to the WiFi
  234. Serial.println("Connected...");
  235. }
  236. }
  237.  
  238. void handel_messages_from_Telegram_Users() {
  239. if (millis() - bot_lasttime > BOT_MTBS)
  240. {
  241. int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  242.  
  243. while (numNewMessages)
  244. {
  245. Serial.println("got response");
  246. handleNewMessages(numNewMessages);
  247. numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  248. }
  249.  
  250. bot_lasttime = millis();
  251. }
  252. }
  253.  
  254.  
  255. void Connect_To_Telegram() {
  256. secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
  257. Serial.print("Retrieving time: ");
  258. configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
  259. time_t now = time(nullptr);
  260. while (now < 24 * 3600)
  261. {
  262. Serial.print(".");
  263. delay(100);
  264. now = time(nullptr);
  265. }
  266. Serial.println(now);
  267. }
Advertisement
Add Comment
Please, Sign In to add comment