Snorktracker
 All Data Structures Files Functions Variables Macros Pages
Sim808.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2018 SFini
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
24 #define TINY_GSM_MODEM_SIM808
25 #define TINY_GSM_DEBUG Serial
26 
27 #include <TinyGsmClient.h>
28 
32 class SmsData
33 {
34 public:
35  long index;
36  String status;
37  String phoneNumber;
38  String referenceNumber;
39  String dateTime;
40  String message;
41 };
42 
47 class MyGsmSim808 : public TinyGsmSim808
48 {
49 public:
50  MyGsmSim808(Stream &stream);
51 
52  bool getGps (MyGps &gps);
53  bool getGsmGps (MyGps &gps);
54  bool getSMS (SmsData &sms);
55  bool deleteSMS (long index);
56 };
57 
58 /* ******************************************** */
59 
61 MyGsmSim808::MyGsmSim808(Stream &stream)
62  : TinyGsmSim808(stream)
63 {
64 }
65 
68 {
69  gps.clear();
70 
71  sendAT(GF("+CGNSINF"));
72  if (waitResponse(GF(GSM_NL "+CGNSINF:")) != 1) {
73  return false;
74  }
75 
76  gps.setRunStatus (stream.readStringUntil(','));
77  gps.setFixStatus (stream.readStringUntil(','));
78  gps.setDateTime (stream.readStringUntil(','));
79  gps.setLatitude (stream.readStringUntil(','));
80  gps.setLongitude (stream.readStringUntil(','));
81  gps.setAltitude (stream.readStringUntil(','));
82  gps.setSpeed (stream.readStringUntil(','));
83  gps.setCourse (stream.readStringUntil(','));
84  gps.setFixMode (stream.readStringUntil(','));
85  /* reserved */ (stream.readStringUntil(','));
86  gps.setHdop (stream.readStringUntil(','));
87  gps.setPdop (stream.readStringUntil(','));
88  gps.setVdop (stream.readStringUntil(','));
89  /* reserved */ (stream.readStringUntil(','));
90  gps.setSatellitesInView (stream.readStringUntil(','));
91  gps.setSatellitesUsed (stream.readStringUntil(','));
92  stream.readStringUntil('\n');
93  waitResponse();
94 
95  return gps.fixStatus;
96 }
97 
103 {
104  gps.clear();
105 
106  sendAT(GF("+CIPGSMLOC=1,1"));
107  if (waitResponse(10000L, GF(GSM_NL "+CIPGSMLOC:")) != 1) {
108  return false;
109  }
110 
111  String locationCode = stream.readStringUntil(',');
112  String longitude = stream.readStringUntil(',');
113  String latitude = stream.readStringUntil(',');
114  String gsmDate = stream.readStringUntil(',');
115  String gsmTime = stream.readStringUntil(',');
116  stream.readStringUntil('\n');
117  waitResponse();
118 
119  locationCode.trim();
120  if (locationCode == "0") {
121  String dateTime = gsmDate + gsmTime;
122 
123  dateTime.replace("/", "");
124  dateTime.replace(":", "");
125  dateTime += ".000";
126  gps.setLatitude(latitude);
127  gps.setLongitude(longitude);
128  gps.setDateTime(dateTime);
129  gps.fixStatus = true;
130  }
131 
132  return gps.fixStatus;
133 }
134 
137 {
138  // PDU Mode (Hex)
139  sendAT(GF("+CMGF=1"));
140  if (waitResponse() != 1) {
141  return false;
142  }
143 
144  // Read unread sms
145  sendAT(GF("+CMGL=\"REC UNREAD\""));
146  int status = waitResponse(GFP(GSM_OK), GF("+CMGL:"));
147 
148  if (status == 2) {
149  sms.index = atoi(stream.readStringUntil(',').c_str());
150  sms.status = stream.readStringUntil(',');
151  sms.phoneNumber = stream.readStringUntil(',');
152  sms.referenceNumber = stream.readStringUntil(',');
153  sms.dateTime = stream.readStringUntil('\n');
154  sms.message = stream.readStringUntil('\n');
155  sms.message = Trim(sms.message, F("\r\n"));
156  waitResponse();
157  return true;
158  }
159 
160  return false;
161 }
162 
164 bool MyGsmSim808::deleteSMS(long index)
165 {
166  sendAT(GF("+CMGD=") + String(index));
167  if (waitResponse() != 1) {
168  return false;
169  }
170 
171  return true;
172 }
bool setLongitude(const String &data)
Definition: Gps.h:480
bool setAltitude(const String &data)
Definition: Gps.h:486
bool setHdop(const String &data)
Definition: Gps.h:516
String dateTime
DateTime of the sms.
Definition: Sim808.h:39
bool setCourse(const String &data)
Definition: Gps.h:498
String phoneNumber
Sms sender number.
Definition: Sim808.h:37
bool setVdop(const String &data)
Definition: Gps.h:522
bool getGsmGps(MyGps &gps)
Definition: Sim808.h:102
bool setRunStatus(const String &data)
Definition: Gps.h:454
String message
Sms content.
Definition: Sim808.h:40
void clear()
Definition: Gps.h:408
bool setSatellitesInView(const String &data)
Definition: Gps.h:528
bool deleteSMS(long index)
Definition: Sim808.h:164
bool getSMS(SmsData &sms)
Definition: Sim808.h:136
bool setDateTime(const String &data)
Definition: Gps.h:466
bool setSatellitesUsed(const String &data)
Definition: Gps.h:534
long index
Sms index on sim card.
Definition: Sim808.h:35
bool setFixMode(const String &data)
Definition: Gps.h:504
String status
Sms status like read or not.
Definition: Sim808.h:36
bool setSpeed(const String &data)
Definition: Gps.h:492
bool setLatitude(const String &data)
Definition: Gps.h:474
MyGsmSim808(Stream &stream)
Definition: Sim808.h:61
bool getGps(MyGps &gps)
Definition: Sim808.h:67
String referenceNumber
???
Definition: Sim808.h:38
bool setPdop(const String &data)
Definition: Gps.h:510
bool fixStatus
Are the gps is valid received?
Definition: Gps.h:133
bool setFixStatus(const String &data)
Definition: Gps.h:460
Definition: Sim808.h:32
Definition: Gps.h:129
String Trim(const String &data, const String &chars)
Definition: Utils.h:168