89Views0Replies

Author Options:

Arduino project Answered

Hi

Wonder if someone can help me with this code.Im using the arduino uno board with the sim808.

Everything is working fine but can only work in stages. When i send "Track" it sends me back my location,when sending sms "ON" relay swiches on etc.When i start the module and want to find out the "STATE" its not working.

Must first send "Track" then "ON" then "OFF" and the i can get the State reply.Why is it working in steps?

My Code:

#include

#include

#include

SoftwareSerial gps(0,1);

DFRobot_SIM808 sim808(&Serial);

const int relay = 3;

String lampState = "HIGH";

#define samples 10

#define minVal -50

#define MaxVal 50

String msg = String("");

int i=0,k=0;

int gps_status=0;

float latitude=0;

float logitude=0;

String Speed="";

String gpsString="";

char *test="$GPRMC";

void initModule(String cmd, char *res, int t)

{

while(1)

{

Serial.println(cmd);

Serial.println(cmd);

delay(100);

while(Serial.available()>0)

{

if(Serial.find(res))

{

Serial.println(res);

delay(t);

return;

}

else

{

Serial.println("Error");

}

}

delay(t);

}

}

void setup()

{

pinMode(relay, OUTPUT);

digitalWrite(relay, HIGH);

//mySerial.begin(9600);

Serial.begin(9600);

gps.begin(9600);

while(!sim808.init()) {

Serial.print("Sim808 init error\r\n");

delay(1000);

}

delay(3000);

Serial.println("Init Success, please call or send SMS message to me!");

//************* Turn on the GPS power************

if( sim808.attachGPS())

Serial.println("Open the GPS power success");

else

Serial.println("Open the GPS power failure");

{

Serial.println("Initializing....");

initModule("AT","OK",1000);

initModule("ATE1","OK",1000);

initModule("AT+CPIN?","READY",1000);

initModule("AT+CMGF=1","OK",1000);

initModule("AT+CNMI=2,2,0,0,0","OK",1000);

Serial.println("Initialized Successfully");

for(int i=0;i

delay(2000);

get_gps();

show_coordinate();

delay(2000);

Serial.println("System Ready..");

}

}

void sendSMS(String message){

Serial.println("AT");

delay(500);

Serial.println("AT+CMGF=1");

delay(500);

Serial.print("AT+CMGS=");

Serial.print('"');

Serial.print("1111111111");

Serial.println('"');

delay(100);

// Send the SMS

Serial.println(message);

delay(100);

Serial.println((char)26);

delay(100);

Serial.println();

delay(5000);

}

void loop()

{

if(Serial.available() )

{

if(Serial.find("Track"))

get_gps();

show_coordinate();

Serial.println("Sending SMS");

Send();

Serial.println("SMS Sent");

delay(2000);

}

if (Serial.find("ON")) {

// Turn on relay and save current state

digitalWrite(relay, LOW);

lampState = "on";

Serial.println("Relay set to ON");

msg = "";

}

if (Serial.find("OFF")) {

// Turn off relay and save current state

digitalWrite(relay, HIGH);

lampState = "off";

Serial.println("Relay set to OFF");

msg = "";

}

if (Serial.find("STATE")) {

String message = "Lamp is " + lampState;

sendSMS(message);

Serial.println("Lamp state resquest");

msg = "";

}

}

void gpsEvent()

{

gpsString="";

while(1)

{

while (gps.available()>0) //Serial incoming data from GPS

{

char inChar = (char)gps.read();

gpsString+= inChar; //store incoming data from GPS to temparary string str[]

i++;

// Serial.print(inChar);

if (i < 7)

{

if(gpsString[i-1] != test[i-1]) //check for right string

{

i=0;

gpsString="";

}

}

if(inChar=='\r')

{

if(i>60)

{

gps_status=1;

break;

}

else

{

i=0;

}

}

}

if(gps_status)

break;

}

}

void get_gps()

{

gps_status=0;

int x=0;

while(gps_status==0)

{

gpsEvent();

int str_lenth=i;

coordinate2dec();

i=0;x=0;

str_lenth=0;

}

}

void show_coordinate()

{

Serial.print("Latitude:");

Serial.println(latitude);

Serial.print("Longitude:");

Serial.println(logitude);

Serial.print("Speed(in kmph)=");

Serial.println(Speed);

}

void coordinate2dec()

{

String lat_degree="";

for(i=20;i<=21;i++)

lat_degree+=gpsString[i];

String lat_minut="";

for(i=22;i<=28;i++)

lat_minut+=gpsString[i];

String log_degree="";

for(i=32;i<=34;i++)

log_degree+=gpsString[i];

String log_minut="";

for(i=35;i<=41;i++)

log_minut+=gpsString[i];

Speed="";

for(i=45;i<48;i++) //extract longitude from string

Speed+=gpsString[i];

float minut= lat_minut.toFloat();

minut=minut/60;

float degree=lat_degree.toFloat();

latitude=degree+minut;

minut= log_minut.toFloat();

minut=minut/60;

degree=log_degree.toFloat();

logitude=degree+minut;

}

void Send()

{

Serial.println("AT");

delay(500);

Serial.println("AT+CMGF=1");

delay(500);

Serial.print("AT+CMGS=");

Serial.print('"');

Serial.print("0742296587"); //mobile no. for SMS alert

Serial.println('"');

Serial.print("Latitude:");

Serial.println(latitude);

delay(500);

Serial.print(" longitude:");

Serial.println(logitude);

delay(500);

Serial.print(" Speed:");

Serial.print(Speed);

Serial.println("kmph");

delay(500);

Serial.print("http://maps.google.com/maps?&z=15&mrt=yp&t=k&q=");

Serial.print(-latitude,6);

Serial.print("+"); //28.612953, 77.231545 //28.612953,77.2293563

Serial.print(logitude,6);

Serial.write(26);

delay(2000);

}

void serialPrint()

{

while(Serial.available()>0)

{

Serial.print(Serial.read());

}

}

Thanks

Hein

Discussions