-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFreematicsCell.h
240 lines (223 loc) · 6.61 KB
/
FreematicsCell.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*************************************************************************
* Telematics Data Logger Class
* Distributed under BSD license
* Developed by Stanley Huang https://www.facebook.com/stanleyhuangyc
*************************************************************************/
#include <Arduino.h>
#define PIN_BEE_UART_RXD 16
#define PIN_BEE_UART_TXD 17
#define PIN_BEE_PWR 27
#define BEE_UART_NUM UART_NUM_1
#define UART_BUF_SIZE 256
#define BEE_BAUDRATE 115200
#define HTTP_CONN_TIMEOUT 10000
typedef enum {
METHOD_GET = 0,
METHOD_POST,
} HTTP_METHOD;
typedef enum {
HTTP_DISCONNECTED = 0,
HTTP_CONNECTED,
HTTP_SENT,
HTTP_ERROR,
} HTTP_STATES;
typedef struct {
float lat;
float lng;
uint8_t year; /* year past 2000, e.g. 15 for 2015 */
uint8_t month;
uint8_t day;
uint8_t hour;
uint8_t minute;
uint8_t second;
} NET_LOCATION;
typedef struct {
uint32_t ts;
uint32_t date;
uint32_t time = 0;
float lat;
float lng;
float alt = -1; /* meter */
float speed = -1; /* knot */
uint16_t heading = -1; /* degree */
uint8_t gps_sat;
uint8_t glonass_sat;
uint8_t beidou_sat;
uint8_t sat = -1;
float pe = -1;
float he = -1;
float ve = -1;
uint16_t sentences;
uint16_t errors;
uint8_t stat = -1; // used for GSM status
} GPS_DATA;
class FreematicsBee
{
public:
// start xBee UART communication
bool xbBegin(unsigned long baudrate = BEE_BAUDRATE, int pinRx = PIN_BEE_UART_RXD, int pinTx = PIN_BEE_UART_TXD);
void xbEnd();
// read data to xBee UART
int xbRead(char* buffer, int bufsize, unsigned int timeout = 1000);
// send data to xBee UART
void xbWrite(const char* cmd);
// send data to xBee UART
void xbWrite(const char* data, int len);
// receive data from xBee UART (returns 0/1/2)
int xbReceive(char* buffer, int bufsize, unsigned int timeout = 1000, const char** expected = 0, byte expectedCount = 0);
// purge xBee UART buffer
void xbPurge();
// toggle xBee module power
void xbTogglePower(int pinPowerKey = PIN_BEE_PWR);
};
class HTTPClient
{
public:
HTTP_STATES state() { return m_state; }
protected:
String genHeader(HTTP_METHOD method, const char* path, bool keepAlive, const char* payload, int payloadSize);
HTTP_STATES m_state = HTTP_DISCONNECTED;
String m_host;
};
class ClientSIM800 : public FreematicsBee
{
public:
bool begin();
void end();
bool setup(const char* apn, bool gps = false, unsigned int timeout = 60000);
String getIP();
int getSignal();
String getOperatorName();
bool checkSIM();
bool getLocation(NET_LOCATION* loc);
String queryIP(const char* host);
char* getBuffer() { return m_buffer; }
const char* deviceName() { return "SIM800"; }
const char* IMEI = "N/A";
protected:
bool sendCommand(const char* cmd, unsigned int timeout = 1000, const char* expected = "\r\nOK");
char m_buffer[256] = {0};
};
class UDPClientSIM800 : public ClientSIM800
{
public:
bool open(const char* host, uint16_t port);
bool send(const char* data, unsigned int len);
void close();
char* receive(int* pbytes = 0, unsigned int timeout = 5000);
private:
char* checkIncoming(int* pbytes);
};
class HTTPClientSIM800 : public HTTPClient, public ClientSIM800
{
public:
bool open(const char* host, uint16_t port);
bool send(HTTP_METHOD method, const char* path, bool keepAlive, const char* payload = 0, int payloadSize = 0);
char* receive(int* pbytes = 0, unsigned int timeout = HTTP_CONN_TIMEOUT);
void close();
protected:
String m_host;
uint16_t m_port;
};
class ClientSIM5360 : public FreematicsBee
{
public:
virtual bool begin();
virtual void end();
virtual bool setup(const char* apn, unsigned int timeout = 30000);
virtual bool setGPS(bool on);
String getIP();
int getSignal();
String getSystemMode();
String getOperatorName();
String getAvailableOperators();
bool checkSIM();
String queryIP(const char* host);
bool getLocation(GPS_DATA** pgd)
{
if (m_gps) {
if (pgd) *pgd = m_gps;
return m_gps->ts != 0;
} else {
return false;
}
}
GPS_DATA getGPS();
GPS_DATA getGSM();
char* getBuffer() { return m_buffer; }
const char* deviceName() { return m_model; }
char IMEI[16] = {0};
void checkGPS();
bool setVerizon();
protected:
// send command and check for expected response
bool sendCommand(const char* cmd, unsigned int timeout = 1000, const char* expected = "\r\nOK\r\n");
float parseDegree(const char* s);
char m_buffer[384] = {0};
char m_model[12] = {0};
GPS_DATA* m_gps = 0;
};
class UDPClientSIM5360 : public ClientSIM5360
{
public:
bool open(const char* host, uint16_t port);
void close();
bool send(const char* data, unsigned int len);
char* receive(int* pbytes = 0, unsigned int timeout = 5000);
protected:
char* checkIncoming(int* pbytes);
String udpIP;
uint16_t udpPort = 0;
};
class HTTPClientSIM5360 : public HTTPClient, public ClientSIM5360
{
public:
bool open(const char* host = 0, uint16_t port = 0);
void close();
bool send(HTTP_METHOD method, const char* path, bool keepAlive, const char* payload = 0, int payloadSize = 0);
char* receive(int* pbytes = 0, unsigned int timeout = HTTP_CONN_TIMEOUT);
};
class ClientSIM7600 : public ClientSIM5360
{
public:
int setup(const char* apn, unsigned int timeout = 180000, int mode = 2);
String checkErr();
bool checkConnection();
bool checkConnection(int signal);
void end();
bool setGPS(bool on);
};
class UDPClientSIM7600 : public ClientSIM7600
{
public:
bool open(const char* host, uint16_t port);
void close();
bool send(const char* data, unsigned int len);
char* receive(int* pbytes = 0, unsigned int timeout = 5000);
protected:
char* checkIncoming(int* pbytes);
String udpIP;
uint16_t udpPort = 0;
};
class HTTPClientSIM7600 : public HTTPClient, public ClientSIM7600
{
public:
bool open(const char* host = 0, uint16_t port = 0);
void close();
bool send(HTTP_METHOD method, const char* path, bool keepAlive, const char* payload = 0, int payloadSize = 0);
char* receive(int* pbytes = 0, unsigned int timeout = HTTP_CONN_TIMEOUT);
bool openTCP(const char* host, uint16_t port, int link);
bool closeTCP(int link);
bool closeServer(int index);
char* checkTCP();
char* checkServer();
char* findOpenTCP();
char* sendCommandRaw(const char* command);
bool sendTCP(const char* data, unsigned int len, int socket);
char* receiveTCP(int* pbytes = 0, unsigned int timeout = 5000);
bool startTCP(int port, int index);
protected:
char* checkIncomingTCP(int* pbytes);
String tcpIP;
uint16_t tcpPort = 0;
};