Skip to content

Commit ae5ca2d

Browse files
committed
Add new GSMLocation API and example
1 parent a5eccf4 commit ae5ca2d

File tree

7 files changed

+280
-0
lines changed

7 files changed

+280
-0
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ script:
2727
- buildExampleSketch GPRSPing
2828
- buildExampleSketch GsmSSLWebClient
2929
- buildExampleSketch GPRSUdpNtpClient
30+
- buildExampleSketch GsmLocation
3031
- buildExampleToolsSketch BandManagement
3132
- buildExampleToolsSketch GsmScanNetworks
3233
- buildExampleToolsSketch PinManagement

examples/GsmLocation/GsmLocation.ino

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
Location
3+
4+
This sketch uses the celluar network to determine the location of a MKR GSM 1400 board
5+
and prints it to the Serial monitor. The location is based on the cellular towers in
6+
range, and requires a GPRS data connection to be enabled.
7+
8+
Circuit:
9+
MKR GSM 1400 board
10+
Antenna
11+
SIM card with a data plan
12+
13+
created 15 Dec 2017
14+
by Sandeep Mistry
15+
*/
16+
17+
// libraries
18+
#include <MKRGSM.h>
19+
20+
#include "arduino_secrets.h"
21+
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
22+
// PIN Number
23+
const char PINNUMBER[] = SECRET_PINNUMBER;
24+
// APN data
25+
const char GPRS_APN[] = SECRET_GPRS_APN;
26+
const char GPRS_LOGIN[] = SECRET_GPRS_LOGIN;
27+
const char GPRS_PASSWORD[] = SECRET_GPRS_PASSWORD;
28+
29+
// initialize the library instance
30+
GSMLocation location;
31+
GPRS gprs;
32+
GSM gsmAccess;
33+
34+
float latitude;
35+
float longitude;
36+
long altitude;
37+
long accuracy;
38+
39+
void setup() {
40+
// initialize serial communications and wait for port to open:
41+
Serial.begin(9600);
42+
while (!Serial) {
43+
; // wait for serial port to connect. Needed for native USB port only
44+
}
45+
46+
Serial.println("Starting GSM location.");
47+
// connection state
48+
boolean notConnected = true;
49+
50+
// After starting the modem with GSM.begin()
51+
// attach the shield to the GPRS network with the APN, login and password
52+
while (notConnected) {
53+
if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &
54+
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
55+
notConnected = false;
56+
} else {
57+
Serial.println("Not connected");
58+
delay(1000);
59+
}
60+
}
61+
62+
location.begin();
63+
}
64+
65+
void loop() {
66+
// update the location variables
67+
latitude = location.latitude();
68+
longitude = location.longitude();
69+
altitude = location.altitude();
70+
accuracy = location.accuracy();
71+
72+
Serial.print("Location: ");
73+
Serial.print(latitude, 7);
74+
Serial.print(", ");
75+
Serial.println(longitude, 7);
76+
77+
Serial.print("Altitude: ");
78+
Serial.print(altitude);
79+
Serial.println("m");
80+
81+
Serial.print("Accuracy: +/- ");
82+
Serial.print(accuracy);
83+
Serial.println("m");
84+
85+
Serial.println();
86+
87+
delay(1000);
88+
}
89+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#define SECRET_PINNUMBER ""
2+
#define SECRET_GPRS_APN "GPRS_APN" // replace your GPRS APN
3+
#define SECRET_GPRS_LOGIN "login" // replace with your GPRS login
4+
#define SECRET_GPRS_PASSWORD "password" // replace with your GPRS password

keywords.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ GSMPIN KEYWORD1
1717
GSMBand KEYWORD1
1818
GSMSSLClient KEYWORD1
1919
GSMUdp KEYWORD1
20+
GSMLocation KEYWORD1
2021

2122
#######################################
2223
# Methods and Functions
@@ -61,6 +62,10 @@ readDTMF KEYWORD2
6162
writeDTMF KEYWORD2
6263
enableI2SInput KEYWORD2
6364
disableI2SInput KEYWORD2
65+
latitude KEYWORD2
66+
longitude KEYWORD2
67+
altitude KEYWORD2
68+
accuracy KEYWORD2
6469

6570
#######################################
6671
# Constants

src/GSMLocation.cpp

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
This file is part of the MKR GSM library.
3+
Copyright (C) 2017 Arduino AG (http://www.arduino.cc/)
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include "GSMLocation.h"
21+
22+
#define GSM_LOCATION_UPDATE_INTERVAL 1100
23+
24+
GSMLocation::GSMLocation() :
25+
_locationUpdated(false),
26+
_lastRequestTime(0),
27+
_latitude(0),
28+
_longitude(0),
29+
_altitude(0),
30+
_uncertainty(0)
31+
{
32+
MODEM.addUrcHandler(this);
33+
}
34+
35+
GSMLocation::~GSMLocation()
36+
{
37+
MODEM.removeUrcHandler(this);
38+
}
39+
40+
int GSMLocation::begin()
41+
{
42+
MODEM.send("AT+ULOCCELL=1");
43+
44+
if (MODEM.waitForResponse() != 1) {
45+
return 0;
46+
}
47+
48+
updateIfNeeded();
49+
50+
for (unsigned long start = millis(); !_locationUpdated && millis() < (start + 10000); ) {
51+
MODEM.poll();
52+
}
53+
54+
return 1;
55+
}
56+
57+
float GSMLocation::latitude()
58+
{
59+
updateIfNeeded();
60+
61+
return _latitude;
62+
}
63+
64+
float GSMLocation::longitude()
65+
{
66+
updateIfNeeded();
67+
68+
return _longitude;
69+
}
70+
71+
long GSMLocation::altitude()
72+
{
73+
updateIfNeeded();
74+
75+
return _altitude;
76+
}
77+
78+
long GSMLocation::accuracy()
79+
{
80+
updateIfNeeded();
81+
82+
return _uncertainty;
83+
}
84+
85+
void GSMLocation::handleUrc(const String& urc)
86+
{
87+
if (urc.startsWith("+UULOC: ")) {
88+
String temp = urc;
89+
int lastCommaIndex;
90+
91+
_locationUpdated = true;
92+
93+
lastCommaIndex = temp.lastIndexOf(',');
94+
_uncertainty = temp.substring(lastCommaIndex + 1).toInt();
95+
temp.remove(lastCommaIndex);
96+
97+
lastCommaIndex = temp.lastIndexOf(',');
98+
_altitude = temp.substring(lastCommaIndex + 1).toInt();
99+
temp.remove(lastCommaIndex);
100+
101+
lastCommaIndex = temp.lastIndexOf(',');
102+
_longitude = temp.substring(lastCommaIndex + 1).toFloat();
103+
temp.remove(lastCommaIndex);
104+
105+
lastCommaIndex = temp.lastIndexOf(',');
106+
_latitude = temp.substring(lastCommaIndex + 1).toFloat();
107+
temp.remove(lastCommaIndex);
108+
}
109+
}
110+
111+
void GSMLocation::updateIfNeeded()
112+
{
113+
MODEM.poll();
114+
115+
if ((millis() - _lastRequestTime) < GSM_LOCATION_UPDATE_INTERVAL) {
116+
return;
117+
}
118+
119+
_lastRequestTime = millis();
120+
MODEM.send("AT+ULOC=2,2,0,1,1");
121+
122+
if (MODEM.waitForResponse() != 1) {
123+
return;
124+
}
125+
}

src/GSMLocation.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
This file is part of the MKR GSM library.
3+
Copyright (C) 2017 Arduino AG (http://www.arduino.cc/)
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef _GSM_LOCATION_H_INCLUDED
21+
#define _GSM_LOCATION_H_INCLUDED
22+
23+
#include <Arduino.h>
24+
25+
#include "Modem.h"
26+
27+
class GSMLocation : public ModemUrcHandler {
28+
29+
public:
30+
GSMLocation();
31+
virtual ~GSMLocation();
32+
33+
int begin();
34+
35+
float latitude();
36+
float longitude();
37+
long altitude();
38+
long accuracy();
39+
40+
void handleUrc(const String& urc);
41+
42+
private:
43+
void updateIfNeeded();
44+
45+
private:
46+
bool _locationUpdated;
47+
unsigned long _lastRequestTime;
48+
49+
float _latitude;
50+
float _longitude;
51+
long _altitude;
52+
long _uncertainty;
53+
};
54+
55+
#endif

src/MKRGSM.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@
3333

3434
#include "GSMSSLClient.h"
3535
#include "GSMUdp.h"
36+
#include "GSMLocation.h"
3637

3738
#endif

0 commit comments

Comments
 (0)