Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please help to solve errors in this Arduino code #134

Open
sri415 opened this issue Jul 30, 2023 · 0 comments
Open

Please help to solve errors in this Arduino code #134

sri415 opened this issue Jul 30, 2023 · 0 comments

Comments

@sri415
Copy link

sri415 commented Jul 30, 2023

#include <PulseSensorPlayground.h> // Pulse Sensor library
#include <SoftwareSerial.h> // Library for GSM module
#include <TinyGPS++.h> // Library for GPS module
#include <LiquidCrystal_I2C.h> // Library for I2C LCD

#define HEARTBEAT_PIN A0 // Connect the heartbeat sensor to this pin
#define SOUND_PIN A1 // Connect the sound sensor to this pin
#define GSM_RX_PIN 10 // GSM module RX pin
#define GSM_TX_PIN 11 // GSM module TX pin

// Create objects for the required sensors
PulseSensorPlayground pulseSensor;
SoftwareSerial gsmSerial(GSM_RX_PIN, GSM_TX_PIN);
TinyGPSPlus gps;
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change the address if necessary

// Set threshold values for heartbeat and sound sensors
const int heartbeatThreshold = 90; // Adjust this value as needed
const int soundThreshold = 500; // Adjust this value as needed

// Variables to store sensor readings
int heartbeatValue = 0;
int soundValue = 0;

// Variables to manage emergency alerts
bool emergencyAlertSent = false;
unsigned long lastAlertTime = 0;
const unsigned long alertCooldown = 30000; // Minimum time between consecutive alerts (30 seconds)

void sendSMS(const char* recipient, const char* message) {
gsmSerial.println("AT+CMGF=1"); // Set SMS mode to text
delay(100);
gsmSerial.print("AT+CMGS="");
gsmSerial.print(recipient);
gsmSerial.println(""");
delay(100);
gsmSerial.print(message);
delay(100);
gsmSerial.write(26); // End SMS message
delay(1000);
}

// Function to send an emergency alert via GSM
void sendEmergencyAlert() {
if (!emergencyAlertSent && (millis() - lastAlertTime >= alertCooldown)) {
// Get child's location
if (gps.location.isValid() && gps.location.age() < 2000) {
float latitude = gps.location.lat();
float longitude = gps.location.lng();

  // Send the emergency SMS with the Google Maps link
  char googleMapsUrl[100];
  snprintf(googleMapsUrl, sizeof(googleMapsUrl),
           "https://www.google.com/maps?q=%.6f,%.6f", latitude, longitude);

  char smsMessage[160];
  snprintf(smsMessage, sizeof(smsMessage),
           "Emergency! Child needs help! Check their location: %s", googleMapsUrl);

  sendSMS("+919676475713", smsMessage); // Replace with the recipient's phone number
  emergencyAlertSent = true;
  lastAlertTime = millis();
}

}
}

void setup() {
lcd.begin(16, 2);
lcd.print("Heartbeat: ");

pulseSensor.setup(HEARTBEAT_PIN);
gsmSerial.begin(9600);
// Initialize other sensors and modules (GPS, GSM)
// Add necessary setup for GPS module, if required
}

void loop() {
pulseSensor.update(); // Update the pulse sensor data
soundValue = analogRead(SOUND_PIN); // Read sound sensor data

// Check for incoming SMS
if (gsmSerial.available()) {
String sms = gsmSerial.readStringUntil('\n');
sms.trim();
// Check if the received SMS contains the "LOC" command
if (sms.equalsIgnoreCase("LOC")) {
// Send the child's location to the sender's phone number
if (gps.location.isValid() && gps.location.age() < 2000) {
float latitude = gps.location.lat();
float longitude = gps.location.lng();
char googleMapsUrl[100];
snprintf(googleMapsUrl, sizeof(googleMapsUrl),
"https://www.google.com/maps?q=%.6f,%.6f", latitude, longitude);

    char smsMessage[160];
    snprintf(smsMessage, sizeof(smsMessage),
             "Child's Location: %s", googleMapsUrl);
    sendSMS("+919676475713", smsMessage); // Replace with the sender's phone number
  } else {
    // GPS data is not valid or too old
    sendSMS("+919676475713", "Unable to get child's location.");
  }
}

}

// Check for emergency conditions and send alerts
if (pulseSensor.sawNewSample()) {
heartbeatValue = pulseSensor.getBeatsPerMinute();
if (heartbeatValue > heartbeatThreshold) {
sendEmergencyAlert();
}
}

if (soundValue > soundThreshold) {
sendEmergencyAlert();
}

// Reset emergency alert flag if conditions are back to normal
if (emergencyAlertSent && heartbeatValue <= heartbeatThreshold && soundValue <= soundThreshold) {
emergencyAlertSent = false;
}

// Display the heartbeat value on the LCD
lcd.setCursor(11, 0); // Move the cursor to the 12th column of the first row
lcd.print(heartbeatValue);
lcd.print(" bpm");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant