Read GPS satellite fix, latitude, longitude, MSL altitude, speed (Km/Hour) over ground coordinates and heading (0-360deg).
Dependencies: Adafruit_FONA_SIMCOM_Library mbed
main.cpp@0:4c181b7daee9, 2018-09-28 (annotated)
- Committer:
- 213468891
- Date:
- Fri Sep 28 08:53:13 2018 +0000
- Revision:
- 0:4c181b7daee9
SIMCOM SIM808
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
213468891 | 0:4c181b7daee9 | 1 | /*################################################################################# |
213468891 | 0:4c181b7daee9 | 2 | |
213468891 | 0:4c181b7daee9 | 3 | Program Name : SIM808 GPS |
213468891 | 0:4c181b7daee9 | 4 | Author : Crispin Mukalay |
213468891 | 0:4c181b7daee9 | 5 | Date Modified : 31/08/2018 |
213468891 | 0:4c181b7daee9 | 6 | Compiler : ARMmbed |
213468891 | 0:4c181b7daee9 | 7 | Tested On : NUCLEO-F446RE |
213468891 | 0:4c181b7daee9 | 8 | |
213468891 | 0:4c181b7daee9 | 9 | Description : Demonstrates the use of the SIM808 Shield to read the GPS |
213468891 | 0:4c181b7daee9 | 10 | satellite fix, latitude, longitude, MSL altitude, |
213468891 | 0:4c181b7daee9 | 11 | speed (Km/Hour) over ground coordinates and heading (0-360deg). |
213468891 | 0:4c181b7daee9 | 12 | |
213468891 | 0:4c181b7daee9 | 13 | Requirements : * NUCLEO-F446RE Board |
213468891 | 0:4c181b7daee9 | 14 | * SIM808 GSM/GPS Module |
213468891 | 0:4c181b7daee9 | 15 | |
213468891 | 0:4c181b7daee9 | 16 | Circuit : * The SIM808 module is connected as follows: |
213468891 | 0:4c181b7daee9 | 17 | VCC/5V/VIN - 5V |
213468891 | 0:4c181b7daee9 | 18 | GND - GND |
213468891 | 0:4c181b7daee9 | 19 | RXD - PA0 (F446's Serial4_TX pin) |
213468891 | 0:4c181b7daee9 | 20 | TXD - PA1 (F446's Serial4_RX pin) |
213468891 | 0:4c181b7daee9 | 21 | |
213468891 | 0:4c181b7daee9 | 22 | ####################################################################################*/ |
213468891 | 0:4c181b7daee9 | 23 | |
213468891 | 0:4c181b7daee9 | 24 | #include "mbed.h" |
213468891 | 0:4c181b7daee9 | 25 | #include "Adafruit_FONA.h" |
213468891 | 0:4c181b7daee9 | 26 | |
213468891 | 0:4c181b7daee9 | 27 | Adafruit_FONA gps(PA_0, PA_1, PA_6, PA_7); //Create a Adafruit_FONA object called gps |
213468891 | 0:4c181b7daee9 | 28 | // TX RX RST RI |
213468891 | 0:4c181b7daee9 | 29 | //Not all SIM808-based boards have a RST (Reset) and RI (Ring Indicator) pin, |
213468891 | 0:4c181b7daee9 | 30 | //but when initializing the object one needs to specify them, even if they |
213468891 | 0:4c181b7daee9 | 31 | //are not there. In our case we just used two unused pins (PA_6 & PA_7) as "dummy" pins. |
213468891 | 0:4c181b7daee9 | 32 | |
213468891 | 0:4c181b7daee9 | 33 | Serial pc(SERIAL_TX, SERIAL_RX); //to use the PC as a console (display output) |
213468891 | 0:4c181b7daee9 | 34 | |
213468891 | 0:4c181b7daee9 | 35 | int main() { |
213468891 | 0:4c181b7daee9 | 36 | |
213468891 | 0:4c181b7daee9 | 37 | int SIM808Success; |
213468891 | 0:4c181b7daee9 | 38 | bool gpsSuccess, gpsCoordinates; |
213468891 | 0:4c181b7daee9 | 39 | float fix, latitude, longitude, MSL_altitude, speed_kph, heading; |
213468891 | 0:4c181b7daee9 | 40 | |
213468891 | 0:4c181b7daee9 | 41 | pc.printf("Program Started...\n"); |
213468891 | 0:4c181b7daee9 | 42 | |
213468891 | 0:4c181b7daee9 | 43 | /* create connection to SIM808 */ |
213468891 | 0:4c181b7daee9 | 44 | pc.printf("\nConnecting to SIM808...\n"); |
213468891 | 0:4c181b7daee9 | 45 | SIM808Success = gps.begin(9600); //connect to SIM808 device at 9600 baud rate and return if successful(=1) or not (=0) |
213468891 | 0:4c181b7daee9 | 46 | |
213468891 | 0:4c181b7daee9 | 47 | if(SIM808Success == 1) |
213468891 | 0:4c181b7daee9 | 48 | pc.printf("\nSIM808 detected successfully...\n"); |
213468891 | 0:4c181b7daee9 | 49 | else { |
213468891 | 0:4c181b7daee9 | 50 | pc.printf("\nSIM808 not detected!\n"); |
213468891 | 0:4c181b7daee9 | 51 | while(1); //program stops/freezes at this point if SIM808 not detected |
213468891 | 0:4c181b7daee9 | 52 | } |
213468891 | 0:4c181b7daee9 | 53 | |
213468891 | 0:4c181b7daee9 | 54 | /* Enable GPS*/ |
213468891 | 0:4c181b7daee9 | 55 | gpsSuccess = gps.enableGPS(true); |
213468891 | 0:4c181b7daee9 | 56 | |
213468891 | 0:4c181b7daee9 | 57 | if (gpsSuccess == true) |
213468891 | 0:4c181b7daee9 | 58 | pc.printf("\nGPS enable successful...\n"); |
213468891 | 0:4c181b7daee9 | 59 | else |
213468891 | 0:4c181b7daee9 | 60 | pc.printf("\nGPS enable failed!\n"); |
213468891 | 0:4c181b7daee9 | 61 | |
213468891 | 0:4c181b7daee9 | 62 | |
213468891 | 0:4c181b7daee9 | 63 | while(1) { |
213468891 | 0:4c181b7daee9 | 64 | wait(2); |
213468891 | 0:4c181b7daee9 | 65 | |
213468891 | 0:4c181b7daee9 | 66 | /* Read coordinates from GPS*/ |
213468891 | 0:4c181b7daee9 | 67 | gpsCoordinates = gps.getGPS(&fix, &latitude, &longitude, &MSL_altitude, &speed_kph, &heading); |
213468891 | 0:4c181b7daee9 | 68 | |
213468891 | 0:4c181b7daee9 | 69 | if(gpsCoordinates == true){ |
213468891 | 0:4c181b7daee9 | 70 | |
213468891 | 0:4c181b7daee9 | 71 | if(fix == 1.0f){ |
213468891 | 0:4c181b7daee9 | 72 | pc.printf("\n"); |
213468891 | 0:4c181b7daee9 | 73 | pc.printf("Latitude: %.6f\n", latitude); |
213468891 | 0:4c181b7daee9 | 74 | pc.printf("Longitude: %.6f\n", longitude); |
213468891 | 0:4c181b7daee9 | 75 | pc.printf("MSL altitude: %.6f\n", MSL_altitude); |
213468891 | 0:4c181b7daee9 | 76 | pc.printf("Speed: %.6f kph\n", speed_kph); |
213468891 | 0:4c181b7daee9 | 77 | pc.printf("Heading: %.6f \n", heading); |
213468891 | 0:4c181b7daee9 | 78 | }else{ |
213468891 | 0:4c181b7daee9 | 79 | pc.printf("Waiting for satellite fix...\n"); |
213468891 | 0:4c181b7daee9 | 80 | } |
213468891 | 0:4c181b7daee9 | 81 | |
213468891 | 0:4c181b7daee9 | 82 | }else{ |
213468891 | 0:4c181b7daee9 | 83 | pc.printf("Waiting for satellite fix...\n"); |
213468891 | 0:4c181b7daee9 | 84 | } |
213468891 | 0:4c181b7daee9 | 85 | } |
213468891 | 0:4c181b7daee9 | 86 | } |