The circuit in question is designed to interface a SIM800L GSM module, a Neo 6M GPS module, and a pushbutton with an ESP32 microcontroller. The circuit is powered by a 9V battery and includes a resistor for voltage regulation. The primary function of this circuit is to send an SMS message with GPS coordinates when the pushbutton is pressed, indicating an emergency situation that requires attention.
#include <TinyGPS++.h>
// GSM module connection - use ESP32's hardware serial 1
#define GSM_SERIAL_NUM 1
#define GSM_RX_PIN 18
#define GSM_TX_PIN 19
// GPS module connection - use ESP32's hardware serial 2
#define GPS_SERIAL_NUM 2
#define GPS_RX_PIN 16
#define GPS_TX_PIN 17
// Pin definitions
const int buttonPin = 4; // Push button connected to GPIO 4
// TinyGPS++ object
TinyGPSPlus gps;
// Variables
int buttonState = 0;
bool messageSent = false;
const char* phoneNumber = "+1234567890"; // Replace with the desired phone number
const char* message = "Emergency! Help needed at this location: ";
void setup() {
Serial.begin(115200);
// Initialize GSM serial
Serial1.begin(9600, SERIAL_8N1, GSM_RX_PIN, GSM_TX_PIN);
// Initialize GPS serial
Serial2.begin(9600, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN);
// Set up button pin
pinMode(buttonPin, INPUT_PULLUP);
// Initialize GSM module
initGSM();
}
void loop() {
// Update GPS data
while (Serial2.available() > 0) {
gps.encode(Serial2.read());
}
buttonState = digitalRead(buttonPin);
if (buttonState == LOW && !messageSent) {
sendSMS();
messageSent = true;
}
if (buttonState == HIGH) {
messageSent = false;
}
}
void initGSM() {
Serial.println("Initializing GSM module...");
delay(1000);
Serial1.println("AT");
delay(1000);
Serial1.println("AT+CMGF=1"); // Set SMS text mode
delay(1000);
}
void sendSMS() {
Serial.println("Button pressed, sending SMS with location...");
String locationMessage = message;
if (gps.location.isValid()) {
locationMessage += "http://maps.google.com/maps?q=";
locationMessage += String(gps.location.lat(), 6);
locationMessage += ",";
locationMessage += String(gps.location.lng(), 6);
} else {
locationMessage += "GPS location not available";
}
// Send SMS
Serial1.println("AT+CMGS=\"" + String(phoneNumber) + "\"");
delay(1000);
Serial1.print(locationMessage);
delay(100);
Serial1.write(26); // End SMS with Ctrl+Z character
Serial.println("SMS sent with location!");
delay(1000);
}
This code is designed to run on the ESP32 microcontroller. It initializes communication with the SIM800L GSM module and the Neo 6M GPS module, reads the GPS data, and sends an SMS with the current location when the pushbutton is pressed. The code uses the TinyGPS++ library to parse the GPS data.