I have created a alarm system which sends out sms if motion is detected. (For my boat). I can also control the system with sms commands. For example: Can turn off the PIR motion function. Can request temperature and humidity by sending 'temp' and 'hum' with sms.
I am not wize with the code.. please help if any comments. Any more ideas on this project or inprovements??
#include "SIM900.h"
#include "sms.h"
#include "SoftwareSerial.h"
#include "DHT.h"
#define DHTPIN A1 // DHT conected to this pin
#define DHTTYPE DHT22 // DHT 22 (AM2302)
SMSGSM sms;
DHT dht(DHTPIN, DHTTYPE);
boolean started=false;
long previousMillis = 0;
long interval = 120000; // interval at which to blink (milliseconds)
char smsbuffer[10];
char n[18];
char string[30];
int val = 0;
int inputPin = A0; // PIR sensor
int pirState = LOW; // we start, assuming no motion detected
int pir = 10;
void setup()
{
//Serial connection.
Serial.begin(9600);
pinMode( 10, OUTPUT );
digitalWrite( 10, HIGH );
dht.begin();
pinMode(inputPin, INPUT);
Serial.println("GSM Shield testing.");
if (gsm.begin(2400)){
Serial.println("\nstatus=READY");
started=true;
}
else Serial.println("\nstatus=IDLE");
if(started){
delsms();
}
}
void loop()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
int pos=0;
int temp= (t);
int temp1=(t*10)-(temp*10);
int hum= (h);
int hum1=(h*10)-(hum*10);
sprintf(string,"Temp: %d.%dc | Hum: %d.%d", temp, temp1, hum, hum1 );
if(started){
pos=sms.IsSMSPresent(SMS_ALL);
if(pos){
Serial.println("Receiving SMS");
Serial.println(pos);
sms.GetSMS(pos,n,smsbuffer,10);
Serial.println(n);
Serial.println(smsbuffer);
if(!strcmp(smsbuffer,"TEMP")){
sms.SendSMS(n,string);
Serial.println("\nSMS sent OK");
}
if(!strcmp(smsbuffer,"PIRON")){
digitalWrite(pir, HIGH);
Serial.println("PIR on");
}
if(!strcmp(smsbuffer,"PIROFF")){
digitalWrite(pir, LOW);
Serial.println("PIR off");
}
delsms();
}
}
val = digitalRead(inputPin);
unsigned long currentMillis = millis();
if (val == HIGH) {
delay(150);
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
if (pirState == LOW) {
Serial.println("Motion detected!");
pirState = HIGH;
if(started){
if (sms.SendSMS("+47xxxxxxxx", "Motion detected!"))
Serial.println("\nSMS sent OK");
}
}
}
}
else {
delay(300);
if (pirState == HIGH){
Serial.println("Motion ended!");
pirState = LOW;
}
}
}
void delsms(){
Serial.println("Checking SIM card for messages...");
for (int i=0; i<10; i++){
int pos=sms.IsSMSPresent(SMS_ALL);
if (pos!=0){
Serial.print("\nFound SMS at the position ");
Serial.println(pos);
if (sms.DeleteSMS(pos)==1){
Serial.print("\nDeleted SMS at the position ");
Serial.println(pos);
}
else
{
Serial.print("\nCant delete SMS at the position ");
Serial.println(pos);
}
}
}
}