The pins are setup as in the diagram above, and the UART switch is set to Arduino - but the trivial code below just gets a return code of -1 from mySerial.read(), presumably because we're not connected. Can anyone tell me how to get it to connect properly over serial?
Thanks...
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
Serial.print("Initialising hardware..\n");
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initialising hardware2..\n");
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
delay(200);
}
void loop() // run over and over
{
delay(2000);
mySerial.println("AT");
Do your jumper blocks connect SIM_TX to D2 (mySerial RX pin) and SIM_RX to D3 (muSerial TX pin)?
You could also try NeoSWSerial on pins 2 & 3. It's much more efficient than SoftwareSerial:
NeoSWSerial shield( 2, 3 );
However...
A Mega has 4 HardwareSerial ports: Serial, Serial1, Serial2 and Serial3. They are on pins 0/1, 18/19, 16/17 and 14/15. You shouldn't be using SoftwareSerial on this board. I don't see any measurements, but it looks like the shield may be blocking some or all of the extra HardwareSerial ports. You might be able to access Serial1 (pins 18 & 19) from the top.
If you can get to 18 & 19, don't use the jumper blocks to connect SIM RX to pin 0, 1, 2 or 3. Instead, use a M-to-F jumper wire (aka Dupont wire)to connect any of the SIM RX header pins (the top row) to one of the HardwareSerial ports' TX pins (e.g., 18). Likewise, use a M-to-F wire to connect any of the SIM TX header pins (bottom row) to the corresponding HardwareSerial port's RX pins.
If pins 18 & 19 are blocked, you could cut a Female wire in half, and solder the wire end to the pads on the underside of the board. The connect the Female socket to the SIM_RX/SIM_TX pins on top. You might be able to sneak it into the socket on top, under the shield. Be sure that none of the bare wire can touch the bottom of the shield.
All the documentation for this shield is at the Waveshare wiki.
Once you get it connected, the GPS data could be parsed by my NeoGPS library. It's smaller, faster and more accurate than all other libraries.
I had the same problem, I am using sim808 shield, when I try to arduino mega and upload sketch out excampel adafruit_fona result is always a "time out" could find Fona "maybe someone can help
I've verified that the TX/RX pair are going to RX/TX with SoftwareSerial and also by directly connecting Serial1 with 18->SIM TX and 19->SIM RX rows, in both cases I'm getting the same result. A serial read is returning -1.
GND should be connected by the shield itself, so I'm not sure what I'm missing? I'm using IORef 5v with the UART switch in the Arduino position and Serial1 connection looks like the attached photo.
Any ideas?
Thanks.
PS. NeoGPS looks very neat - if I can manage to talk to the board I'll use it.
Here's a quick little Finite-State Machine that steps through the sequence without using delay:
* After 10 seconds, power on the GPS device * Wait 5 seconds * Enable the NMEA sentence output * Wait until a fix is received (could get stuck here) * Disable the NMEA sentence output, and * Power off the GPS device.
Repeat.
#define SIMCOM Serial1 /* direct connection is best */
//#include <AltSoftSerial.h>
//AltSoftSerial SIMCOM; // 8 & 9 is second best
//#include <NeoSWSerial.h>
//NeoSWSerial SIMCOM( 2, 3 ); // 3rd best..
//SoftwareSerial NOT recommended
#include <NMEAGPS.h>
NMEAGPS gps;
gps_fix fix; // the parsed pieces from the GPRMC
#define DEBUG(x) Serial.println(x)
//#define DEBUG(x) /* to build without any debug prints */
enum state_t
{
WAITING,
GPS_PWR_ON, GPS_PWR_ON_DELAY,
GPS_PWR_OFF, GPS_PWR_OFF_DELAY,
GPS_PKT_ON, GPS_PKT_ON_DELAY,
GPS_PKT_OFF, GPS_PKT_OFF_DELAY,
GET_GPS, GET_GPS_DELAY
};
state_t state = WAITING;
uint32_t stateTime;
void setup() {
Serial.begin(9600);
SIMCOM.begin(9600);
}
void loop() {
switch (state) {
case WAITING:
serialClear();
if (millis() - stateTime >= 10000) // once every 10 seconds
state = GPS_PWR_ON;
break;
case GPS_PWR_ON:
DEBUG("GPS PWR ON");
SIMCOM.println( F("AT+CGPSPWR=1") );
state = GPS_PWR_ON_DELAY;
stateTime = millis();
break;
case GPS_PWR_ON_DELAY:
serialClear();
if (millis() - stateTime >= 5000)
state = GPS_PKT_ON;
break;
case GPS_PKT_ON:
DEBUG("GPS PKT ON");
SIMCOM.println( F("AT+CGPSOUT=1") );
state = GPS_PKT_ON_DELAY;
stateTime = millis();
break;
case GPS_PKT_ON_DELAY:
serialClear();
if (millis() - stateTime >= 200)
state = GET_GPS;
break;
case GET_GPS:
if (gps.available( SIMCOM )) {
fix = gps.read(); // received a new fix structure!
// You might want to make sure there is valid data from the GPS.
// It may have bad reception, so you need to decide what to do when
// there is no valid data.
// You could stay in this state until the fix becomes valid,
// and then send the message. Your program could get stuck here
// if the GPS reception is bad. Maybe that's what it should do.
// Or you could go ahead and send a message without a lat/lon.
// With the if test commented out, it will send a message now,
// perhaps *without* a valid lat/lon value.
//if (fix.valid.date && fix.valid.time && fix.valid.location) {
// What to do with the GPS data?
Serial.print( fix.dateTime.seconds );
Serial.print( ' ' );
Serial.println( fix.latitude(), 6 );
state = GET_GPS_DELAY;
stateTime = millis();
//} else {
// ???
//}
}
break;
case GET_GPS_DELAY:
serialClear();
if (millis() - stateTime >= 200)
state = GPS_PKT_OFF;
break;
case GPS_PKT_OFF:
DEBUG("GPS PKT OFF");
SIMCOM.println( F("AT+CGPSOUT=0") );
state = GPS_PKT_OFF_DELAY;
stateTime = millis();
break;
case GPS_PKT_OFF_DELAY:
serialClear();
if (millis() - stateTime >= 200)
state = GPS_PWR_OFF;
break;
case GPS_PWR_OFF:
DEBUG("GPS PWR OFF");
SIMCOM.println( F("AT+CGPSPWR=0") );
state = GPS_PWR_OFF_DELAY;
stateTime = millis();
break;
case GPS_PWR_OFF_DELAY:
serialClear();
if (millis() - stateTime >= 500) {
stateTime = millis();
state = WAITING;
}
break;
}
} // loop
void serialClear() {
// Empty the input buffer.
while (SIMCOM.available()) {
char c = SIMCOM.read();
Serial.write( c ); // to see the response, if you want
}
// while (Serial.available())
// Serial.read();
}
I think you are not sending the CGPSOUT command to make it emit NMEA sentences.