Skip to content

Commit 2b8b968

Browse files
committed
Add available() API
1 parent ae5ca2d commit 2b8b968

File tree

3 files changed

+41
-61
lines changed

3 files changed

+41
-61
lines changed

examples/GsmLocation/GsmLocation.ino

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Location
33
44
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
5+
and prints it to the Serial monitor. The location is based on the cellular towers in
66
range, and requires a GPRS data connection to be enabled.
77
88
Circuit:
@@ -31,11 +31,6 @@ GSMLocation location;
3131
GPRS gprs;
3232
GSM gsmAccess;
3333

34-
float latitude;
35-
float longitude;
36-
long altitude;
37-
long accuracy;
38-
3934
void setup() {
4035
// initialize serial communications and wait for port to open:
4136
Serial.begin(9600);
@@ -63,27 +58,21 @@ void setup() {
6358
}
6459

6560
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);
61+
if (location.available()) {
62+
Serial.print("Location: ");
63+
Serial.print(location.latitude(), 7);
64+
Serial.print(", ");
65+
Serial.println(location.longitude(), 7);
7666

77-
Serial.print("Altitude: ");
78-
Serial.print(altitude);
79-
Serial.println("m");
67+
Serial.print("Altitude: ");
68+
Serial.print(location.altitude());
69+
Serial.println("m");
8070

81-
Serial.print("Accuracy: +/- ");
82-
Serial.print(accuracy);
83-
Serial.println("m");
71+
Serial.print("Accuracy: +/- ");
72+
Serial.print(location.accuracy());
73+
Serial.println("m");
8474

85-
Serial.println();
86-
87-
delay(1000);
75+
Serial.println();
76+
}
8877
}
8978

src/GSMLocation.cpp

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
#define GSM_LOCATION_UPDATE_INTERVAL 1100
2323

2424
GSMLocation::GSMLocation() :
25-
_locationUpdated(false),
26-
_lastRequestTime(0),
25+
_commandSent(false),
26+
_locationAvailable(false),
2727
_latitude(0),
2828
_longitude(0),
2929
_altitude(0),
@@ -45,40 +45,48 @@ int GSMLocation::begin()
4545
return 0;
4646
}
4747

48-
updateIfNeeded();
48+
return 1;
49+
}
50+
51+
int GSMLocation::available()
52+
{
53+
MODEM.poll();
54+
55+
if (!_commandSent) {
56+
_commandSent = true;
57+
_locationAvailable = false;
4958

50-
for (unsigned long start = millis(); !_locationUpdated && millis() < (start + 10000); ) {
51-
MODEM.poll();
59+
MODEM.send("AT+ULOC=2,2,0,1,1");
60+
MODEM.waitForResponse();
5261
}
5362

54-
return 1;
63+
if (_locationAvailable) {
64+
_commandSent = false;
65+
_locationAvailable = false;
66+
67+
return 1;
68+
}
69+
70+
return 0;
5571
}
5672

5773
float GSMLocation::latitude()
5874
{
59-
updateIfNeeded();
60-
6175
return _latitude;
6276
}
6377

6478
float GSMLocation::longitude()
6579
{
66-
updateIfNeeded();
67-
6880
return _longitude;
6981
}
7082

7183
long GSMLocation::altitude()
7284
{
73-
updateIfNeeded();
74-
7585
return _altitude;
7686
}
7787

7888
long GSMLocation::accuracy()
7989
{
80-
updateIfNeeded();
81-
8290
return _uncertainty;
8391
}
8492

@@ -88,7 +96,7 @@ void GSMLocation::handleUrc(const String& urc)
8896
String temp = urc;
8997
int lastCommaIndex;
9098

91-
_locationUpdated = true;
99+
_locationAvailable = true;
92100

93101
lastCommaIndex = temp.lastIndexOf(',');
94102
_uncertainty = temp.substring(lastCommaIndex + 1).toInt();
@@ -107,19 +115,3 @@ void GSMLocation::handleUrc(const String& urc)
107115
temp.remove(lastCommaIndex);
108116
}
109117
}
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: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class GSMLocation : public ModemUrcHandler {
3232

3333
int begin();
3434

35+
int available();
36+
3537
float latitude();
3638
float longitude();
3739
long altitude();
@@ -40,11 +42,8 @@ class GSMLocation : public ModemUrcHandler {
4042
void handleUrc(const String& urc);
4143

4244
private:
43-
void updateIfNeeded();
44-
45-
private:
46-
bool _locationUpdated;
47-
unsigned long _lastRequestTime;
45+
bool _commandSent;
46+
bool _locationAvailable;
4847

4948
float _latitude;
5049
float _longitude;

0 commit comments

Comments
 (0)