Untitled
unknown
plain_text
10 months ago
1.7 kB
17
Indexable
#include <SoftwareSerial.h>
// Ultrasonic pins
const int trigPin = 7;
const int echoPin = 6;
// GSM pins
const int gsmRx = 8; // Arduino receives (SIM800 TX)
const int gsmTx = 9; // Arduino sends (SIM800 RX via divider)
SoftwareSerial gsm(gsmRx, gsmTx);
const char phoneNumber[] = "+1234567890"; // <-- replace with your phone number
const int floodLevel = 10; // cm threshold
unsigned long lastSmsTime = 0;
const unsigned long smsCooldown = 60000; // 1 min cooldown between SMS
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
gsm.begin(9600);
delay(1000);
sendAT("AT"); // Wake up
sendAT("ATE0"); // Disable echo
sendAT("AT+CMGF=1"); // SMS text mode
Serial.println("SIM800L Flood Alert System Ready");
}
void loop() {
long distance = readDistance();
Serial.print("Distance: ");
Serial.println(distance);
if (distance < floodLevel && millis() - lastSmsTime > smsCooldown) {
sendSMS("FLOOD ALERT! Water level is too high!");
lastSmsTime = millis();
}
delay(1000);
}
long readDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH, 30000);
return duration * 0.034 / 2;
}
void sendAT(const char *cmd) {
gsm.println(cmd);
delay(500);
while (gsm.available()) Serial.write(gsm.read());
}
void sendSMS(const char* msg) {
gsm.print("AT+CMGS=\"");
gsm.print(phoneNumber);
gsm.println("\"");
delay(500);
gsm.print(msg);
delay(500);
gsm.write(26); // Ctrl+Z ends the SMS
delay(5000);
Serial.println("SMS sent.");
}Editor is loading...
Leave a Comment