-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathFileUtilsHttpDownload.ino
217 lines (172 loc) · 5.74 KB
/
FileUtilsHttpDownload.ino
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
/*
Download a large file and store it into the GSM module filesystem.
This sketch connects to a website through a MKR GSM 1400 board and
downloads a large file and stores it into the filesystem of the GSM
module.
The file is processed in blocks of 512 bytes in order to save RAM.
A block of data is read from the GSM module and the appended to a
file created by the sketch.
Circuit:
* MKR GSM 1400 board
* Antenna
* SIM card with a data plan
created 19 June 2020
by Giampaolo Mancini
*/
// libraries
#include <MKRGSM.h>
GSMFileUtils fileUtils(false);
#include "Helpers.h"
#include "arduino_secrets.h"
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
// PIN Number
const char PINNUMBER[] = SECRET_PINNUMBER;
// APN data
const char GPRS_APN[] = SECRET_GPRS_APN;
const char GPRS_LOGIN[] = SECRET_GPRS_LOGIN;
const char GPRS_PASSWORD[] = SECRET_GPRS_PASSWORD;
// initialize the library instance
GSMClient client;
GPRS gprs;
GSM gsmAccess;
// URL, path and port (for example: example.org)
void setup()
{
// initialize serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Starting Arduino web client.");
fileUtils.begin();
// List files on the GSM module's filesystem
auto numberOfFiles = fileUtils.fileCount();
Serial.print("Number of Files: ");
Serial.println(numberOfFiles);
Serial.println();
printFiles(fileUtils);
auto server = promptAndReadLine("Please, enter server name:", "arduino.tips");
auto port = promptAndReadInt("Please, enter server port:", 80);
auto filename = promptAndReadLine("Please, enter file name:", "asciilogo.txt");
auto filesize = promptAndReadInt("Please, enter file size:", 2263);
Serial.println("Connecting...");
// connection state
bool connected = false;
// After starting the modem with GSM.begin()
// attach the shield to the GPRS network with the APN, login and password
while (!connected) {
if ((gsmAccess.begin(PINNUMBER) == GSM_READY) && (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
connected = true;
} else {
Serial.println("Not connected");
delay(1000);
}
}
// if you get a connection, report back via serial:
if (client.connect(server.c_str(), port)) {
Serial.println("connected");
// Make a HTTP request:
client.print("GET /");
client.print(filename);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
} else {
// if you didn't get a connection to the server:
Serial.println("Connection failed");
}
// Download and store block-by-block
storeFileBuffered(filename, filesize);
auto updateBinSize = fileUtils.listFile(filename);
Serial.print(filename);
Serial.print(" downloaded size: ");
Serial.println(updateBinSize);
numberOfFiles = fileUtils.fileCount();
Serial.print("Number of Files: ");
Serial.println(numberOfFiles);
Serial.println();
printFiles(fileUtils);
}
void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char r = client.read();
if (r < 16)
Serial.print(0);
Serial.print(r, HEX);
}
// if the server's disconnected, stop the client:
if (!client.available() && !client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
for (;;)
;
}
}
void storeFileBuffered(String filename, uint32_t totalLen)
{
Serial.print("Ready to download \"");
Serial.print(filename);
Serial.print("\" - len: ");
Serial.print(totalLen);
Serial.println(" bytes.");
constexpr uint32_t len { 512 };
uint32_t cycles = totalLen / len;
uint32_t spares = totalLen % len;
int totalRead { 0 };
fileUtils.deleteFile(filename);
Serial.print("Saving file in ");
Serial.print(cycles + 1);
Serial.print(" blocks. [");
Serial.print(cycles);
Serial.print(' ');
Serial.print(len);
Serial.print(" -bytes blocks and ");
Serial.print(spares);
Serial.println(" bytes].");
bool is_header_complete = false;
String http_header;
// Skip the HTTP header
while (!is_header_complete) {
while (client.available()) {
const char c = client.read();
http_header += c;
if (http_header.endsWith("\r\n\r\n")) {
Serial.println("Header Complete");
is_header_complete = true;
break;
}
}
}
// Define download and save lambda
auto downloadAndSaveTrunk = [filename](uint32_t len) {
char buf[len] { 0 };
uint32_t written { 0 };
if (client.available())
written = client.readBytes(buf, len);
fileUtils.appendFile(filename, buf, written);
return written;
};
// Define wrapper function
auto saveTrunk = [&totalRead, downloadAndSaveTrunk](size_t iter, uint32_t len) {
Serial.print("Block ");
if (iter < 10) Serial.print(' '); if (iter < 100) Serial.print(' ');
Serial.print(iter);
totalRead += downloadAndSaveTrunk(len);
Serial.print(": ");
Serial.print(len);
Serial.print(" - ");
Serial.print(totalRead);
Serial.println();
};
// Download and save complete trunks + spares
for (auto c = 0; c <= cycles; c++)
saveTrunk(c, len);
Serial.println();
}