Im newbie here , i just want to know how to merge the 2 codes that i have , i'd already try to merge it but I think its far from reality lol. sorry for my bad english. Thanks in Advance by the way
#include <SoftwareSerial.h>
#define DEBUG true
SoftwareSerial sim808(7, 8); // RX, TX
int Relay = 10;
String data[5];
char coming;
char phone_no[] = "09480875916";
String text = "";
String state, timegps, latitude, longitude;
const int buzzer = 3;
int vibrationPin = 2;
bool flag=false;
String ph1="+639480875916"; //Enter your phone number to call #01
void setup() {
pinMode(vibrationPin,INPUT);
digitalWrite(vibrationPin,LOW);
//System Calibration
for(int i=0;i<10;i++)
{
delay(200);
}
pinMode(buzzer, OUTPUT);
pinMode(Relay, OUTPUT);
digitalWrite(Relay, LOW);
sim808.begin(9600);
Serial.begin(9600);// Start software serial
delay(100);
sim808.println("AT+CMGF=1"); // Iset ang GSM para sa text
delay(100);
sim808.println("AT+CNMI=2,2,0,0,0"); // mag notify pag may bagong dating na message
delay(1000);
sim808.println("AT+CMGD=1,4"); // AT+CMGD=1,4 Range ng sms ng SIM card na pwedeng idelete
delay(100);
sendData( "AT+CGNSPWR=1", 1000, DEBUG); // pag power on ng GPS
delay(100);
sendData( "AT+CGNSSEQ=RMC", 1000, DEBUG); // Pag send ng isang line NMEA sentence
delay(100);
SMSsend("SYSTEM IS TURN ON");
delay(100);
Serial.println("System OK, Waiting for text Message");
delay(50);
sim808.flush();
delay(50);
}
void loop() {
int a=vibration();
if(a>15000){
flag=true;
}
if (flag) {
call1();
message1(); //Generate the Alert Message First Mobile
//delay(1000);
//message2(); //Generate the Alert Message Second Mobile
//delay(1000); //1 minute delay
//Call to First mobile
//delay(1000);
//call2(); //call to Second mobile
}
flag=false;
sms();
}
void SMSsend(String massage) {
sim808.print("AT+CMGF=1\r"); // Carriage return
delay(100);
sim808.println("AT+CMGS=\"09480875916\"");
delay(100);
sim808.println(massage);
delay(100);
sim808.println((char)26); //ctrl+z pag send ng message
delay(100);
sim808.println();
delay(100);
sim808.println("AT+CMGD=1,4");
delay(100);
sim808.println("AT+CMGF=1");
delay(100);
sim808.println("AT+CNMI=1,2,0,0,0");
delay(200);
text = "";
}
void sms() {
while (sim808.available()) {
delay(10);
coming = sim808.read();
if (coming == '#') {
if (sim808.available()) {
delay(10);
coming = sim808.read();
if (coming == 'R') {
if (sim808.available()) {
delay(10);
coming = sim808.read();
if (coming == 'L') {
if (sim808.available()) {
delay(10);
coming = sim808.read();
if (coming == 'Y') {
if (sim808.available()) {
delay(10);
coming = sim808.read();
if (coming == 'o') {
sendData( "AT+CGNSPWR=1", 1000, DEBUG); // pag power on ng GPS
delay(100);
sendData( "AT+CGNSSEQ=RMC", 1000, DEBUG); // Pag send ng isang line NMEA sentence
delay(100);
sendTabData("AT+CGNSINF", 1000, DEBUG);
delay(100);
if (state != 0) {
Serial.println("State :" + state);
Serial.println("Time :" + timegps);
Serial.println("Latitude :" + latitude);
Serial.println("Longitude :" + longitude);
sim808.print("AT+CMGS=\"");
sim808.print(phone_no);
sim808.println("\"");
delay(300);
sim808.print("http://maps.google.com/maps?q=loc:");
sim808.print(latitude);
sim808.print(",");
sim808.print (longitude);
delay(200);
sim808.println((char)26); // End AT command with a ^Z, ASCII code 26
delay(200);
sim808.println();
delay(10000);
sim808.flush();
}
}
else if (coming == 'c') {
digitalWrite(Relay, HIGH); // pag off ng makina
Serial.println("Relay Off");
//SMSsend("Motorcycle has been Shutdown");
Serial.println("Waiting for Text Message...");
}
else if (coming == 't') {
digitalWrite(Relay, LOW); // pag on ng makina
Serial.println("Relay On");
//SMSsend("Motorcycle is Starting again");
Serial.println("Waiting for Text Message...");
}
}
}
}
}
}
}
}
}
}
}
void sendTabData(String command , const int timeout , boolean debug) {
sim808.println(command);
long int time = millis();
int i = 0;
while ((time + timeout) > millis()) {
while (sim808.available()) {
char c = sim808.read();
if (c != ',') {
data[i] += c; // SA PAG DUKOT NG MGA STRING SA NMEA DATA NA BINATBATO
// delay(100);
} else {
i++;
}
if (i == 5) {
// delay(100);
goto exitL;
}
}
} exitL:
if (debug) {
state = data[1];
data[1] = "";
timegps = data[2];
data[2] = ""; // PARA IDELETE YUNG MGA LUMANG DATA, AT IPASOK YUNG MGA BAGONG DATA
latitude = data[3];
data[3] = "";
longitude = data[4];
data[4] = "";
}
}
void message1() //Method to send message #01
{
sim808.println("AT+CMGS=\"");
sim808.println(ph1);
sim808.println("\"\r\n");
delay(2000);
sim808.println("---ALERT---\n Security Breach");
sim808.write(0x1A);
}
void call1() //Method to get call #01
{
sim808.println("ATD");
sim808.println(ph1);
sim808.println(";\r\n");
delay(30000);
sim808.println("ATH");
}
int vibration(){
delay(10);
int vibrationDetection=pulseIn(vibrationPin,HIGH); //wait for the pin to get HIGH and returns measurement
return vibrationDetection;
}
String sendData (String command , const int timeout , boolean debug) {
String response = "";
sim808.println(command);
long int time = millis();
int i = 0;
while ( (time + timeout ) > millis()) {
while (sim808.available()) {
char c = sim808.read();
response += c;
}
}
if (debug) {
Serial.print(response);
}
return response;
}
2codes.ino (7.08 KB)