Skip to content

Commit 4574367

Browse files
committed
Refactor modules, add Result and FTP class
1 parent acc5be1 commit 4574367

11 files changed

Lines changed: 717 additions & 365 deletions

File tree

Ftp.cpp

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
/*
2+
* Ftp.cpp
3+
* Ftp library for the SIM800L board
4+
*
5+
* Copyright 2019 Antonio Carrasco
6+
*
7+
* The MIT License (MIT)
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#include "Ftp.h"
29+
#include <string.h>
30+
31+
const char AT_FTPCID[] PROGMEM = "AT+FTPCID=1\r\n";
32+
const char AT_FTPSERV[] PROGMEM = "AT+FTPSERV=\"%s\"\r\n";
33+
const char AT_FTPUN[] PROGMEM = "AT+FTPUN=\"%s\"\r\n";
34+
const char AT_FTPPW[] PROGMEM = "AT+FTPPW=\"%s\"\r\n";
35+
const char AT_FTPPUTNAME[] PROGMEM = "AT+FTPPUTNAME=\"%s\"\r\n";
36+
const char AT_FTPPUTPATH[] PROGMEM = "AT+FTPPUTPATH=\"%s\"\r\n";
37+
const char AT_FTPPUT1[] PROGMEM = "AT+FTPPUT=1\r\n";
38+
const char AT_FTPPUT2[] PROGMEM = "AT+FTPPUT=2,%d\r\n";
39+
const char AT_FTPPUT20[] PROGMEM = "AT+FTPPUT=2,0\r\n";
40+
const char BEARER_PROFILE_GPRS[] PROGMEM = "AT+SAPBR=3,1,\"Contype\",\"GPRS\"\r\n";
41+
const char BEARER_PROFILE_APN[] PROGMEM = "AT+SAPBR=3,1,\"APN\",\"%s\"\r\n";
42+
const char QUERY_BEARER[] PROGMEM = "AT+SAPBR=2,1\r\n";
43+
const char OPEN_GPRS_CONTEXT[] PROGMEM = "AT+SAPBR=1,1\r\n";
44+
const char CLOSE_GPRS_CONTEXT[] PROGMEM = "AT+SAPBR=0,1\r\n";
45+
const char REGISTRATION_STATUS[] PROGMEM = "AT+CREG?\r\n";
46+
const char SLEEP_MODE_2[] PROGMEM = "AT+CSCLK=2\r\n";
47+
48+
const char OK[] PROGMEM = "OK";
49+
const char AT_FTPPUT1_RESP[] PROGMEM = "1,1";
50+
const char AT_FTPPUT2_RESP[] PROGMEM = "+FTPPUT: 2";
51+
const char AT_FTPPUT20_RESP[] PROGMEM = "1,0";
52+
const char CONNECTED[] PROGMEM = "+CREG: 0,1";
53+
const char ROAMING[] PROGMEM = "+CREG: 0,5";
54+
const char BEARER_OPEN[] PROGMEM = "+SAPBR: 1,1";
55+
56+
Result FTP::configureBearer(const char *apn)
57+
{
58+
Result result = SUCCESS;
59+
char buffer[64];
60+
char resp1[12];
61+
char resp2[12];
62+
unsigned int attempts = 0;
63+
unsigned int MAX_ATTEMPTS = 10;
64+
65+
sendATTest();
66+
67+
strcpy_P(buffer, REGISTRATION_STATUS);
68+
strcpy_P(resp1, CONNECTED);
69+
strcpy_P(resp2, ROAMING);
70+
while ((sendCmdAndWaitForResp(buffer, resp1, 2000) != TRUE &&
71+
sendCmdAndWaitForResp(buffer, resp2, 2000) != TRUE) &&
72+
attempts < MAX_ATTEMPTS)
73+
{
74+
attempts++;
75+
delay(1000 * attempts);
76+
if (attempts == MAX_ATTEMPTS)
77+
{
78+
attempts = 0;
79+
preInit();
80+
}
81+
}
82+
83+
strcpy_P(buffer, BEARER_PROFILE_GPRS);
84+
strcpy_P(resp1, OK);
85+
if (sendCmdAndWaitForResp(buffer, resp1, 2000) == FALSE)
86+
result = ERROR_BEARER_PROFILE_GPRS;
87+
88+
sprintf_P(buffer, BEARER_PROFILE_APN, apn);
89+
strcpy_P(resp1, OK);
90+
if (sendCmdAndWaitForResp(buffer, resp1, 2000) == FALSE)
91+
result = ERROR_BEARER_PROFILE_APN;
92+
93+
return result;
94+
}
95+
96+
Result FTP::putBegin(const char *fileName,
97+
const char *server,
98+
const char *usr,
99+
const char *pass,
100+
const char *path)
101+
{
102+
Result result;
103+
104+
char buffer[64];
105+
char resp[12];
106+
107+
//sendCmdAndWaitForResp("AT+FTPSSL=1\r\n", "OK", 2000);
108+
//sendCmdAndWaitForResp("AT+SSLOPT=1,1\r\n", "OK", 2000);
109+
110+
delay(10000);
111+
strcpy_P(buffer, AT_FTPCID);
112+
strcpy_P(resp, OK);
113+
if (sendCmdAndWaitForResp(buffer, resp, 2000) == FALSE)
114+
{
115+
return ERROR_FTPCID;
116+
}
117+
118+
sprintf_P(buffer, AT_FTPSERV, server);
119+
strcpy_P(resp, OK);
120+
if (sendCmdAndWaitForResp(buffer, resp, 2000) == FALSE)
121+
{
122+
return ERROR_FTPSERV;
123+
}
124+
125+
sprintf_P(buffer, AT_FTPUN, usr);
126+
strcpy_P(resp, OK);
127+
if (sendCmdAndWaitForResp(buffer, resp, 2000) == FALSE)
128+
{
129+
return ERROR_FTPUN;
130+
}
131+
132+
sprintf_P(buffer, AT_FTPPW, pass);
133+
strcpy_P(resp, OK);
134+
if (sendCmdAndWaitForResp(buffer, resp, 2000) == FALSE)
135+
{
136+
return ERROR_FTPPW;
137+
}
138+
139+
sprintf_P(buffer, AT_FTPPUTNAME, fileName);
140+
strcpy_P(resp, OK);
141+
if (sendCmdAndWaitForResp(buffer, resp, 2000) == FALSE)
142+
{
143+
return ERROR_FTPPUTNAME;
144+
}
145+
146+
sprintf_P(buffer, AT_FTPPUTPATH, path);
147+
strcpy_P(resp, OK);
148+
if (sendCmdAndWaitForResp(buffer, resp, 2000) == FALSE)
149+
{
150+
return ERROR_FTPPUTPATH;
151+
}
152+
153+
strcpy_P(buffer, AT_FTPPUT1);
154+
strcpy_P(resp, AT_FTPPUT1_RESP);
155+
if (sendCmdAndWaitForResp(buffer, resp, 10000) == FALSE)
156+
{
157+
return ERROR_FTPPUT1;
158+
}
159+
160+
return result;
161+
}
162+
163+
Result FTP::putWrite(const char *data, unsigned int size)
164+
{
165+
Result result;
166+
167+
char buffer[32];
168+
char resp[32];
169+
170+
sprintf_P(buffer, AT_FTPPUT2, size);
171+
strcpy_P(resp, AT_FTPPUT2_RESP);
172+
if (sendCmdAndWaitForResp(buffer, resp, 2000) == FALSE)
173+
{
174+
return ERROR_FTPPUT2;
175+
}
176+
else
177+
{
178+
write(data, size);
179+
180+
strcpy_P(resp, AT_FTPPUT1_RESP);
181+
waitForResp(resp, 2000);
182+
}
183+
184+
return result;
185+
}
186+
187+
Result FTP::putEnd()
188+
{
189+
Result result;
190+
serialSIM800.flush();
191+
192+
char buffer[32];
193+
char resp[12];
194+
195+
strcpy_P(buffer, AT_FTPPUT20);
196+
strcpy_P(resp, AT_FTPPUT20_RESP);
197+
if (sendCmdAndWaitForResp(AT_FTPPUT20, AT_FTPPUT20_RESP, 2000) == FALSE)
198+
{
199+
return ERROR_FTPPUT20;
200+
}
201+
return result;
202+
}

Ftp.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Ftp.h
3+
* FTP library for the SIM800L board
4+
*
5+
* Copyright 2019 Antonio Carrasco
6+
*
7+
* The MIT License (MIT)
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#ifndef __FTP_H__
29+
#define __FTP_H__
30+
31+
#include "Sim800.h"
32+
#include "Result.h"
33+
34+
class FTP : public SIM800
35+
{
36+
37+
public:
38+
FTP(unsigned int baudRate,
39+
unsigned int rxPin,
40+
unsigned int txPin,
41+
unsigned int rstPin,
42+
bool debug = TRUE) : SIM800(baudRate, rxPin, txPin, rstPin, debug){};
43+
Result configureBearer(const char *apn);
44+
Result putBegin(const char *fileName,
45+
const char *server,
46+
const char *usr,
47+
const char *pass,
48+
const char *path = "/");
49+
Result putWrite(const char *data, unsigned int size);
50+
Result putEnd();
51+
};
52+
53+
#endif

Geo.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,16 @@
3131

3232
#define READ_GPS "AT+CIPGSMLOC=1,1\r\n"
3333

34-
void Geo::readGpsLocation(char *gps){
34+
void Geo::readGpsLocation(char *gps)
35+
{
3536
char buffer[80];
3637
cleanBuffer(buffer, sizeof(buffer));
3738
cleanBuffer(gps, sizeof(gps));
3839

3940
sendCmd(READ_GPS);
4041

41-
if (readBuffer(buffer, sizeof(buffer)) == TRUE){
42+
if (readBuffer(buffer, sizeof(buffer)) == TRUE)
43+
{
4244
parseATResponse(buffer, 19, 4, gps);
4345
}
4446
}

Geo.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,21 @@
2525
* THE SOFTWARE.
2626
*/
2727

28-
2928
#ifndef __GEO_H__
3029
#define __GEO_H__
3130

3231
#include "Sim800.h"
3332

34-
class Geo : public SIM800 {
33+
class Geo : public SIM800
34+
{
3535

36-
public:
37-
Geo(unsigned int baudRate, unsigned int rxPin, unsigned int txPin, unsigned int rstPin, bool debug = TRUE):SIM800(baudRate, rxPin, txPin, rstPin, debug){};
38-
void readGpsLocation(char *gps);
36+
public:
37+
Geo(unsigned int baudRate,
38+
unsigned int rxPin,
39+
unsigned int txPin,
40+
unsigned int rstPin,
41+
bool debug = TRUE) : SIM800(baudRate, rxPin, txPin, rstPin, debug){};
42+
void readGpsLocation(char *gps);
3943
};
4044

4145
#endif

0 commit comments

Comments
 (0)