GSM Kontrol

SMS ile Ev Otomasyonu

SMS ile Otomasyon

SMS Gönder

Bu örnek arduinohaber kanalından alınmıştır.

#include <SoftwareSerial.h>
SoftwareSerial gsm(2, 3);
String smsMetni = "";
char gelen;

#define LED_PIN 12
#define ISIK_PIN 11
#define BEKLE 3

void SMSgonder(String mesaj) {
  gsm.print("AT+CMGF=1\r");
  delay(100);
  gsm.println("AT+CMGS=\"+905444742644\"");// telefon numarasi degistir
  delay(100);
  gsm.println(mesaj);
  delay(100);
  gsm.println((char)26);
  delay(100);
  gsm.println();
  delay(100);
  gsm.println("AT+CMGD=1,4");
  delay(100);
  gsm.println("AT+CMGF=1");
  delay(100);
  gsm.println("AT+CNMI=1,2,0,0,0");
  delay(200);
  smsMetni = "";
}


void smscoz() {
  while (gsm.available()) {
    delay(BEKLE);
    gelen = gsm.read();
    if (gelen == '#') {
      if (gsm.available()) {
        delay(BEKLE);
        gelen = gsm.read();
        if (gelen == 'L') {
          if (gsm.available()) {
            delay(BEKLE);
            gelen = gsm.read();
            if (gelen == 'E') {
              if (gsm.available()) {
                delay(BEKLE);
                gelen = gsm.read();
                if (gelen == 'D') {
                  if (gsm.available()) {
                    delay(BEKLE);
                    gelen = gsm.read();
                    if (gelen == 'a') {
                      digitalWrite(LED_PIN, HIGH);
                      SMSgonder("LED acildi");
                    }
                    else if (gelen == 'k') {
                      digitalWrite(LED_PIN, LOW);
                      SMSgonder("LED kapatildi");
                    }
                  }
                }
              }
            }
          }
        }
        else if (gelen == 'I') {
          if (gsm.available()) {
            delay(BEKLE);
            gelen = gsm.read();
            if (gelen == 'S') {
              if (gsm.available()) {
                delay(BEKLE);
                gelen = gsm.read();
                if (gelen == 'I') {
                  if (gsm.available()) {
                    delay(BEKLE);
                    gelen = gsm.read();
                    if (gelen == 'K') {
                      if (gsm.available()) {
                        delay(BEKLE);
                        gelen = gsm.read();
                        if (gelen == 'a') {
                          digitalWrite(ISIK_PIN, LOW);
                          smsMetni = "Isik acildi";
                          SMSgonder(smsMetni);
                        }
                        else if (gelen == 'k') {
                          digitalWrite(ISIK_PIN, HIGH);
                          smsMetni = "Isik kapatildi";
                          SMSgonder(smsMetni);
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

void setup() {
  pinMode(LED_PIN, OUTPUT);
  pinMode(ISIK_PIN, OUTPUT);
  digitalWrite(ISIK_PIN, HIGH);
  pinMode(7, OUTPUT);
  digitalWrite(7, HIGH);
  delay(3000);
  digitalWrite(7, LOW);
  gsm.begin(9600);
  delay(2000);
  gsm.println("AT+CMGF=1");
  delay(100);
  gsm.println("AT+CNMI=1,2,0,0,0");
  delay(100);
  gsm.println("AT+CMGD=1,4");
  delay(1000);
  SMSgonder("Sistem Acildi");
  gsm.flush();
  delay(100);
}

void loop() {
  smscoz();
  delay(1);
}