You are on page 1of 1

#include <SoftwareSerial.

h>

// Set up the software serial port for the SIM800 module


SoftwareSerial sim800Serial(2, 3); // RX, TX

// Set up the PIR motion sensor


int pirPin = 4;

// Set up the phone number to send the message to


String phoneNumber = "+1234567890"; // Replace with your own phone number

void setup() {
// Initialize the serial communication with the computer
Serial.begin(9600);

// Initialize the software serial port for the SIM800 module


sim800Serial.begin(9600);

// Configure the PIR motion sensor pin as an input


pinMode(pirPin, INPUT);
}

void loop() {
// Check if motion is detected by the PIR motion sensor
if (digitalRead(pirPin) == HIGH) {
// Motion detected, send an SMS message
sendSMS(phoneNumber, "Motion detected!");
}
delay(1000);
}

// Function to send an SMS message using the SIM800 module


void sendSMS(String phoneNumber, String message) {
// Set the SIM800 module to text mode
sim800Serial.println("AT+CMGF=1");

// Set the phone number to send the message to


sim800Serial.print("AT+CMGS=\"");
sim800Serial.print(phoneNumber);
sim800Serial.println("\"");

// Send the message


sim800Serial.print(message);
sim800Serial.write(26); // End the message with Ctrl+Z

// Wait for the SIM800 module to send the message


delay(5000);

// Print the response from the SIM800 module


while (sim800Serial.available()) {
Serial.write(sim800Serial.read());
}
}

You might also like