Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include "MAX30105.h"
- #include "heartRate.h"
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
- #include <WiFi.h>
- #include <WiFiClientSecure.h>
- #include <UniversalTelegramBot.h>
- #define BOT_TOKEN "6988502937:AAGBPyxFT_5HA_S9hX0H2wSvGiHS5GJhfEg"
- String chat_id = "-1002037340786";
- WiFiClientSecure secured_client;
- UniversalTelegramBot bot(BOT_TOKEN, secured_client);
- // GPIO where the DS18B20 is connected to
- const int oneWireBus = 10;
- const unsigned long BOT_MTBS = 5000; // mean time between scan messages
- unsigned long bot_lasttime; // last time messages' scan has been done
- bool Start = false;
- // Setup a oneWire instance to communicate with any OneWire devices
- OneWire oneWire(oneWireBus);
- // Pass our oneWire reference to Dallas Temperature sensor
- DallasTemperature sensors(&oneWire);
- MAX30105 particleSensor;
- byte rateSpot = 0;
- long lastBeat = 0;
- float beatsPerMinute;
- int beatAvg;
- float temperature;
- unsigned long dataMillis = 0;
- long irValue;
- long delta;
- float temperatureC;
- const byte RATE_SIZE = 4;
- byte rates[RATE_SIZE];
- unsigned long previousMillis = 0;
- int value2 = 0;
- int value3 = 0;
- int hr_flag = 0;
- int temp_flag = 0;
- 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";
- // Find position of ',' and '='
- int posB = text.indexOf("b=");
- int posC = text.indexOf("c=");
- if (posB != -1 && posC != -1) {
- // Extract values of a and b
- String value2String = text.substring(posB + 2, posC - 1);
- String value3String = text.substring(posC + 2);
- // Convert string values to integers
- value2 = value2String.toInt();
- value3 = value3String.toInt();
- }
- // Print extracted values for debugging
- Serial.print("Value of b: ");
- Serial.println(value2);
- Serial.print("Value of c: ");
- Serial.println(value3);
- if (text == "/data")
- {
- String temp_str = "Temp: " + String(temperatureC) + "Β°C \n";
- String beatAvg_str = "Heart Beat: " + String(beatAvg) + " BPM \n";
- bot.sendMessage(chat_id, temp_str );
- bot.sendMessage(chat_id, beatAvg_str);
- }
- //////////////////////////////////////////////////////////
- if (text == "/options")
- {
- String keyboardJson = "[[\"/data\"]]";
- bot.sendMessageWithReplyKeyboard(chat_id, "Choose from one of the following options", "", keyboardJson, true);
- }
- 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 setup() {
- Serial.begin(115200);
- //delay(10000);
- Serial.println("11111");
- init_heart_rate_sensor();
- // Start the DS18B20 sensor
- sensors.begin();
- Connect_To_WiFi();
- Connect_To_Telegram();
- bot.sendMessage(chat_id, "HR - [OK]");
- bot.sendMessage(chat_id, "Temp - [OK]");
- }
- void loop() {
- //readTempSensor();
- ReConnect_To_WiFi();
- handel_messages_from_Telegram_Users();
- read_heart_rate_sensor();
- }
- void init_heart_rate_sensor() {
- Serial.println("Initializing...");
- // Initialize sensor
- if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
- {
- Serial.println("MAX30105 was not found. Please check wiring/power. ");
- while (1);
- }
- Serial.println("Place your index finger on the sensor with steady pressure.");
- particleSensor.setup(); //Configure sensor with default settings
- particleSensor.enableDIETEMPRDY(); //Enable the temp ready interrupt. This is required.
- particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running
- particleSensor.setPulseAmplitudeGreen(0); //Turn off Green LED
- }
- void read_heart_rate_sensor() {
- irValue = particleSensor.getIR();
- if (checkForBeat(irValue) == true)
- {
- //We sensed a beat!
- delta = millis() - lastBeat;
- lastBeat = millis();
- beatsPerMinute = 60 / (delta / 1000.0);
- if (beatsPerMinute < 255 && beatsPerMinute > 20) {
- rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
- rateSpot %= RATE_SIZE; //Wrap variable
- //Take average of readings
- beatAvg = 0;
- for (byte x = 0 ; x < RATE_SIZE ; x++)
- beatAvg += rates[x];
- beatAvg /= RATE_SIZE;
- }
- }
- Serial.print("IR=");
- Serial.print(irValue);
- Serial.print(", BPM=");
- Serial.print(beatsPerMinute);
- Serial.print(", Avg BPM=");
- Serial.print(beatAvg);
- //temperature = particleSensor.readTemperature();
- //Serial.print(", temperatureC=");
- //Serial.print(temperature, 4);
- if (millis() - previousMillis >= 10000) {
- Serial.print(", temperatureC=");
- readTempSensor();
- previousMillis = millis();
- hr_flag = 0;
- temp_flag = 0;
- }
- if (beatAvg < value2 && beatAvg != 0 && hr_flag == 0 && irValue > 50000) {
- bot.sendMessage(chat_id, "ALERT - [Heart Beat Problem]");
- Serial.println("SEND ALERT MSG To TELEGRAM [Heart Beat Problem]");
- hr_flag = 1;
- }
- if (temperatureC > value3 && temp_flag == 0) {
- bot.sendMessage(chat_id, "ALERT - [Temperature Problem]");
- Serial.println("SEND ALERT MSG To TELEGRAM [Temperature Problem]");
- temp_flag = 1;
- }
- if (irValue < 50000)
- Serial.print(" No finger?");
- Serial.println();
- }
- void readTempSensor() {
- sensors.requestTemperatures();
- temperatureC = sensors.getTempCByIndex(0) + 2;
- Serial.print(temperatureC);
- Serial.println("ΒΊC");
- }
- void ReConnect_To_WiFi() {
- while (WiFi.status() != WL_CONNECTED) {
- //delay(500);
- Serial.print(".");
- Connect_To_WiFi();
- }
- }
- void Connect_To_WiFi() {
- WiFiManager wm;
- bool res;
- res = wm.autoConnect("[Setup WiFi]"); // password protected ap
- if (!res) {
- Serial.println("Failed to Connect!!");
- // ESP.restart();
- }
- else {
- //if you get here you have connected to the WiFi
- Serial.println("Connected...");
- }
- }
- void handel_messages_from_Telegram_Users() {
- 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();
- }
- }
- void Connect_To_Telegram() {
- secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
- 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);
- }
Advertisement
Add Comment
Please, Sign In to add comment