Hello,
Long time reader, first time user of this fine forum. I am working towards a vehicle tracker with these two components,
https://www.robotshop.com/ca/en/waveshare-uno-plus-microcontroler.html
using the supplied code:
While I am able to get the functions to work separately, upon combining them only receiving SMS and gps functions work with the following cobbled code.
I have tried data type conversions, simple messages, and a few other mods to send SMS without success. All lines before and after
sim808.sendSMS(PHONE_NUMBER, "location");
work well but the sendSMS function will not deliver the message.
Appreciate any assistance for this green programmer.
#include <DFRobot_sim808.h>
#include <SoftwareSerial.h>
#include <string.h>
#define PHONE_NUMBER "19*********3" //Mobile phone number,need to change
#define MESSAGE_LENGTH 160
char message[MESSAGE_LENGTH];
int messageIndex = 0;
String google, locationstring;
char phone[16];
char datetime[24];
char location[53];
char lat[9];
char lon[9];
DFRobot_SIM808 sim808(&Serial);
void setup() {
Serial.begin(9600);
google = String("http://www.google.com/maps/place/");
//******** Initialize sim808 module *************
while (!sim808.init()) {
Serial.print("Sim808 init error\r\n");
delay(1000);
}
delay(3000);
Serial.println("Init Success");
//************* Turn on the GPS power************
if ( sim808.attachGPS()) {
Serial.println("Open the GPS power success");
}
else {
Serial.println("Open the GPS power failure");
}
}
void loop() {
//************** Get GPS data ******************
if (sim808.getGPS()) {
Serial.print(sim808.GPSdata.year);
Serial.print("/");
Serial.print(sim808.GPSdata.month);
Serial.print("/");
Serial.print(sim808.GPSdata.day);
Serial.print(" ");
Serial.print(sim808.GPSdata.hour);
Serial.print(":");
Serial.print(sim808.GPSdata.minute);
Serial.print(":");
Serial.print(sim808.GPSdata.second);
Serial.print(":");
Serial.println(sim808.GPSdata.centisecond);
Serial.print("latitude :");
Serial.println(sim808.GPSdata.lat);
Serial.print("longitude :");
Serial.println(sim808.GPSdata.lon);
/* Serial.print("speed_kph :");
Serial.println(sim808.GPSdata.speed_kph);
Serial.print("heading :");
Serial.println(sim808.GPSdata.heading);
Serial.println();
*/
//************* Turn off the GPS power ************
// sim808.detachGPS();
delay(1000);
//*********** Detecting unread SMS ************************
messageIndex = sim808.isSMSunread();
Serial.print("messageIndex: ");
Serial.println(messageIndex);
delay(1000);
//*********** At least, there is one UNREAD SMS ***********
if (messageIndex > 0) {
sim808.readSMS(messageIndex, message, MESSAGE_LENGTH, phone, datetime);
//***********In order not to fill SIM Memory, is better to delete it**********
sim808.deleteSMS(messageIndex);
Serial.print("From number: ");
Serial.println(phone);
Serial.print("Datetime: ");
Serial.println(datetime);
Serial.print("Recieved Message: ");
Serial.println(message);
}
/*
Serial.print("lat");
Serial.println(sim808.GPSdata.lat);
Serial.print("lon");
Serial.println(sim808.GPSdata.lon);
*/
dtostrf(sim808.GPSdata.lon, 5, 5, lon); //switched the order of these two to get lat to work again
dtostrf(sim808.GPSdata.lat, 5, 5, lat);
locationstring = String(google + lat + "," + lon);
Serial.print("http://www.google.com/maps/place/");
Serial.print(lat);
Serial.print(",");
Serial.println(lon);
String sendtext(message);
locationstring.toCharArray(location, 53);
if (sendtext == "search") {
memset(message, 0, sizeof message); //clear message as to not resend location
//Serial.print("sending location - ");
//Serial.println(locationstring);
//Serial.println(location);
//Serial.println();
//******** define phone number and text **********
sendtexts(); //tried moving function out of main loop
}
}
}
void sendtexts(){
sim808.sendSMS(PHONE_NUMBER, "location");
Serial.println("sendtexts");
return;
}