I can’t seem to get more than one serial up and running in my program. I’m trying to get some GPS data and then send it via SMS. I’m using sparkfun’s GPS shield and the cell shield in conjunction with new soft serial.
I know you can’t run two at a time but I thought my program addressed this problem. Anyway, any input would be greatly appreciated.
The code is as follows:
#include <NewSoftSerial.h>
#include <TinyGPS.h>
/* This sample code demonstrates the normal use of a TinyGPS object.
It requires the use of NewSoftSerial, and assumes that you have a
4800-baud serial GPS device hooked up on pins 2(rx) and 3(tx).
*/
//GPS setup
TinyGPS gps;
NewSoftSerial nss(13, 12);
void gpsdump(TinyGPS &gps);
bool feedgps();
void printFloat(double f, int digits = 2);
//Cell setup
NewSoftSerial cell(2,3);
const int buttonPin = 7;
int buttonState = 0;
char incoming_char=0; //Will hold the incoming character from the Serial Port.
//Button Setup
void setup()
{
//begin the serial connections
Serial.begin(115200);
nss.begin(4800);
//cell.begin(9600);
//setup the button
pinMode(buttonPin, INPUT);
Serial.print("Using TinyGPS library v. "); Serial.println(TinyGPS::library_version());
Serial.println();
Serial.print("Sizeof(gpsobject) = "); Serial.println(sizeof(TinyGPS));
Serial.println();
Serial.println("SYSTEM: Starting chip comms");
}
void loop()
{
buttonState = digitalRead(buttonPin);
bool newdata = false;
unsigned long sysTime = millis();
checkButton(gps);
//if(cell.available() >0)
{
//incoming_char=cell.read(); //incoming char fetch.
//Serial.print(incoming_char); //Print the sucker
}
//and the same thing...but backwards, check to see if it's coming from the system to the cell
if(Serial.available() >0)
{
incoming_char=Serial.read(); //Get the character coming from the terminal
//cell.print(incoming_char); //Send the character to the cellular module.
}
}
void checkButton(TinyGPS gps)
{
if(buttonState == LOW and feedgps())
{
Serial.println();
Serial.println("SYSTEM: Button Pushed");
sendData(gps);
delay(100);
}
}
void sendData(TinyGPS gps) //uses cell as the active device
{
long lat, lon;
unsigned long age;
gps.get_position(&lat, &lon, &age);
cell.begin(9600);
cell.begin(9600);
if(cell.available())
{
//begin the print statements
cell.print("AT+CMGF=1\r");
Serial.print("AT+CMGF=1\r");
delay(100);
cell.print("AT+CMGS=\"2403426240\"\r");
Serial.print("AT+CMGS=\"2403426240\"");
delay(100);
String latString = lat;
String lonString = lon;
String printString = (latString);
cell.print(printString);
Serial.print(printString);
}
}
bool feedgps()
{
while (nss.available())
{
if (gps.encode(nss.read()))
return true;
}
return false;
}