Hi all,
I'm been working on a project using arduino nano, adafruit ultimate GPS breakout and sim 800l evb.
The idea is to create a GPS tracker and also be able to send an sms and remotely kill power using a relay in case it was lost or stolen.
i been working on tracker side code in the last few week and since all work fine i now start to implement relay and sms code.
The problem I'm having at the moment is merging the two codes into a single sketch
this is my code so far, at the moment i'm just using an "on/off led via sms" code that i found on the net
and i'm not including tracker code into void loop as it would be too long and i'm sure the problem is not
there.
But let me explain and show you better:
#include <Adafruit_GPS.h>
#include <gprs.h>
#include <SoftwareSerial.h>
#define SIM800_TX_PIN 8
#define SIM800_RX_PIN 7
SoftwareSerial serialSIM800(SIM800_TX_PIN,SIM800_RX_PIN);
SoftwareSerial serialGPS(3, 2);
Adafruit_GPS GPS(&serialGPS);
boolean usingInterrupt = false;
void useInterrupt(boolean);
#define TIMEOUT 5000
#define LED_PIN 13
GPRS gprs;
int fixflag = 0;
String apn_name = "everywhere";
String apn_user = "";
String apn_pwd = "";
void listenToSerial(long waitTime) {
long timeNow = millis();
do {
if (serialSIM800.available()) {
Serial.write(serialSIM800.read());
}
if (Serial.available()) {
serialSIM800.write(Serial.read());
}
char c = GPS.read();
c = GPS.read();
if (GPS.newNMEAreceived()) {
if (!GPS.parse(GPS.lastNMEA()))
return;
}
}
while ((timeNow + waitTime) > millis());
}
char currentLine[500] = "";
int currentLineIndex = 0;
bool nextLineIsMessage = false;
bool ledStatus = LOW;
int firstComma = -1;
int secondComma = -1;
bool isInPhonebook = false;
char *number = NULL;
void setupHTTPstuff() {
listenToSerial(10000);
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate
GPS.sendCommand(PGCMD_ANTENNA);
useInterrupt(true);
delay(1000);
serialGPS.println(PMTK_Q_RELEASE);
serialSIM800.write("AT+SAPBR=0,1\n");
listenToSerial(4000);
serialSIM800.write("AT+SAPBR=3,1,\"APN\",\"everywhere\"\n");
listenToSerial(1000);
serialSIM800.write("AT+SAPBR=3,1,\"USER\",\"\"\n");
listenToSerial(1000);
serialSIM800.write("AT+SAPBR=3,1,\"PWD\",\" \"\n");
listenToSerial(1000);
serialSIM800.write("AT+HTTPTERM\n");
listenToSerial(2000);
serialSIM800.write("AT+HTTPINIT\n");
listenToSerial(4000);
serialSIM800.write("AT+SAPBR=1,1\n");
listenToSerial(4000);
serialSIM800.write("AT+HTTPPARA=\"CID\",1\n");
listenToSerial(4000);
}
void setupsmsStuff() {
Serial.println("Starting SIM800 SMS Command Processor");
gprs.preInit();
delay(1000);
while (0 != gprs.init()) {
delay(1000);
Serial.print("init error\r\n");
}
if (0 != gprs.sendCmdAndWaitForResp("AT+CMGF=1\r\n", "OK", TIMEOUT)) {
ERROR("ERROR:CMGF");
return;
}
if (0 != gprs.sendCmdAndWaitForResp("AT+CNMI=1,2,0,0,0\r\n", "OK", TIMEOUT)) {
ERROR("ERROR:CNMI");
return;
}
Serial.println("Init success");
Serial.println(ledStatus);
}
void setup() {
Serial.begin(9600);
while (!Serial);
//Being serial communication with Arduino and SIM800
serialSIM800.begin(9600);
// initialize digital pin 13 as an output.
pinMode(LED_PIN, OUTPUT);
setupHTTPstuff();
setupsmsStuff();
GPS.begin(9600);
}
SIGNAL(TIMER0_COMPA_vect) {
char c = GPS.read();
}
void useInterrupt(boolean v) {
if (v) {
// Timer0 is already used for millis() - we'll just interrupt somewhere
// in the middle and call the "Compare A" function above
OCR0A = 0xAF;
TIMSK0 |= _BV(OCIE0A);
usingInterrupt = true;
} else {
// do not call the interrupt function COMPA anymore
TIMSK0 &= ~_BV(OCIE0A);
usingInterrupt = false;
}
}
uint32_t timer = millis();
void loop() {
digitalWrite(LED_PIN, ledStatus);
static char phonebook[13];
int firstQuote = -1;
int secondQuote = -1;
int j = 0;
//If there is serial output from SIM800
if (gprs.serialSIM800.available()) {
char lastCharRead = gprs.serialSIM800.read();
if (lastCharRead == '\r' || lastCharRead == '\n') {
String lastLine = String(currentLine);
if (lastLine.startsWith("+CMT:")) {
firstComma = lastLine.indexOf(',');
Serial.println(firstComma); //For debugging
secondComma = lastLine.indexOf(',', firstComma + 1);
Serial.println(secondComma); //For debugging
firstQuote = lastLine.indexOf(34); //ASCII code for ""
Serial.println(firstQuote); //For debugging
secondQuote = lastLine.indexOf(34, firstQuote + 1);
Serial.println(secondQuote); //For debugging
for (int i = firstQuote+1; i < secondQuote; ++i) {
phonebook[j] = lastLine[i];
++j;
}
phonebook[j] = '\0';
Serial.println(phonebook);
number = phonebook;
if (secondComma > 22) {
Serial.println("In Phonebook");
isInPhonebook = true;
Serial.println(isInPhonebook);
} else {
Serial.println("Not in Phonebook");
}
Serial.println(lastLine);
nextLineIsMessage = true;
} else if ((lastLine.length() > 0) && (isInPhonebook)) {
if (nextLineIsMessage) {
Serial.println(lastLine);
if (lastLine.indexOf("LED ON") >= 0) {
ledStatus = 1;
Serial.println(phonebook);
gprs.sendSMS(number, "Done!");
Serial.println(number);
} else if (lastLine.indexOf("LED OFF") >= 0) {
ledStatus = 0;
Serial.println(phonebook);
gprs.sendSMS(number, "Done!");
Serial.println(number);
}
nextLineIsMessage = false;
isInPhonebook = false;
}
}
for ( int i = 0; i < sizeof(currentLine); ++i ) {
currentLine[i] = (char)0;
}
currentLineIndex = 0;
} else {
currentLine[currentLineIndex++] = lastCharRead;
}
}
}
Now when i merge this two code i have a conflict because clearly arduino is trying to
Being serial communication with SIM800 whit this two comand
gprs.preInit();
&
serialSIM800.begin(9600);
When I cancel one of the two lines, however, it does not function or one of the two parts does not initialize.
I know it might be a stupid question but, your help would be very much appreciated.
Hope to have been clear enough in the explanation.
Many Thanks
Riccardo