You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

310 lines
8.9 KiB

7 years ago
6 years ago
7 years ago
8 years ago
8 years ago
5 years ago
6 years ago
6 years ago
8 years ago
7 years ago
5 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
5 years ago
8 years ago
5 years ago
7 years ago
5 years ago
5 years ago
5 years ago
7 years ago
7 years ago
5 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
7 years ago
  1. /**************************************************************
  2. *
  3. * For this example, you need to install CRC32 library:
  4. * https://github.com/bakercp/CRC32
  5. * or from http://librarymanager/all#CRC32+checksum
  6. *
  7. * TinyGSM Getting Started guide:
  8. * https://tiny.cc/tinygsm-readme
  9. *
  10. * ATTENTION! Downloading big files requires of knowledge of
  11. * the TinyGSM internals and some modem specifics,
  12. * so this is for more experienced developers.
  13. *
  14. **************************************************************/
  15. // Select your modem:
  16. #define TINY_GSM_MODEM_SIM800
  17. // #define TINY_GSM_MODEM_SIM808
  18. // #define TINY_GSM_MODEM_SIM868
  19. // #define TINY_GSM_MODEM_SIM900
  20. // #define TINY_GSM_MODEM_SIM7000
  21. // #define TINY_GSM_MODEM_SIM5360
  22. // #define TINY_GSM_MODEM_UBLOX
  23. // #define TINY_GSM_MODEM_SARAR4
  24. // #define TINY_GSM_MODEM_M95
  25. // #define TINY_GSM_MODEM_BG96
  26. // #define TINY_GSM_MODEM_A6
  27. // #define TINY_GSM_MODEM_A7
  28. // #define TINY_GSM_MODEM_M590
  29. // #define TINY_GSM_MODEM_MC60
  30. // #define TINY_GSM_MODEM_MC60E
  31. // #define TINY_GSM_MODEM_ESP8266
  32. // #define TINY_GSM_MODEM_XBEE
  33. // #define TINY_GSM_MODEM_SEQUANS_MONARCH
  34. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  35. #define SerialMon Serial
  36. // Set serial for AT commands (to the module)
  37. // Use Hardware Serial on Mega, Leonardo, Micro
  38. #define SerialAT Serial1
  39. // or Software Serial on Uno, Nano
  40. //#include <SoftwareSerial.h>
  41. //SoftwareSerial SerialAT(2, 3); // RX, TX
  42. // Increase RX buffer to capture the entire response
  43. // Chips without internal buffering (A6/A7, ESP8266, M590)
  44. // need enough space in the buffer for the entire response
  45. // else data will be lost (and the http library will fail).
  46. #define TINY_GSM_RX_BUFFER 1024
  47. // See all AT commands, if wanted
  48. //#define DUMP_AT_COMMANDS
  49. // Define the serial console for debug prints, if needed
  50. #define TINY_GSM_DEBUG SerialMon
  51. //#define LOGGING // <- Logging is for the HTTP library
  52. // Add a reception delay, if needed
  53. //#define TINY_GSM_YIELD() { delay(2); }
  54. #define TINY_GSM_USE_GPRS true
  55. #define TINY_GSM_USE_WIFI false
  56. // set GSM PIN, if any
  57. #define GSM_PIN ""
  58. // Your GPRS credentials
  59. // Leave empty, if missing user or pass
  60. const char apn[] = "YourAPN";
  61. const char gprsUser[] = "";
  62. const char gprsPass[] = "";
  63. const char wifiSSID[] = "YourSSID";
  64. const char wifiPass[] = "YourWiFiPass";
  65. // Server details
  66. const char server[] = "vsh.pp.ua";
  67. const int port = 80;
  68. #include <TinyGsmClient.h>
  69. #include <CRC32.h>
  70. const char resource[] = "/TinyGSM/test_1k.bin";
  71. uint32_t knownCRC32 = 0x6f50d767;
  72. uint32_t knownFileSize = 1024; // In case server does not send it
  73. #ifdef DUMP_AT_COMMANDS
  74. #include <StreamDebugger.h>
  75. StreamDebugger debugger(SerialAT, SerialMon);
  76. TinyGsm modem(debugger);
  77. #else
  78. TinyGsm modem(SerialAT);
  79. #endif
  80. TinyGsmClient client(modem);
  81. void setup() {
  82. // Set console baud rate
  83. SerialMon.begin(115200);
  84. delay(10);
  85. // Set GSM module baud rate
  86. SerialAT.begin(115200);
  87. delay(3000);
  88. // Restart takes quite some time
  89. // To skip it, call init() instead of restart()
  90. SerialMon.println("Initializing modem...");
  91. modem.restart();
  92. String modemInfo = modem.getModemInfo();
  93. SerialMon.print("Modem: ");
  94. SerialMon.println(modemInfo);
  95. #if TINY_GSM_USE_GPRS
  96. // Unlock your SIM card with a PIN if needed
  97. if ( GSM_PIN && modem.getSimStatus() != 3 ) {
  98. modem.simUnlock(GSM_PIN);
  99. }
  100. #endif
  101. }
  102. void printPercent(uint32_t readLength, uint32_t contentLength) {
  103. // If we know the total length
  104. if (contentLength != (uint32_t)-1) {
  105. SerialMon.print("\r ");
  106. SerialMon.print((100.0 * readLength) / contentLength);
  107. SerialMon.print('%');
  108. } else {
  109. SerialMon.println(readLength);
  110. }
  111. }
  112. void loop() {
  113. #if defined TINY_GSM_USE_WIFI && defined TINY_GSM_MODEM_HAS_WIFI
  114. SerialMon.print(F("Setting SSID/password..."));
  115. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  116. SerialMon.println(" fail");
  117. delay(10000);
  118. return;
  119. }
  120. SerialMon.println(" success");
  121. #endif
  122. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  123. // The XBee must run the gprsConnect function BEFORE waiting for network!
  124. modem.gprsConnect(apn, gprsUser, gprsPass);
  125. #endif
  126. SerialMon.print("Waiting for network...");
  127. if (!modem.waitForNetwork()) {
  128. SerialMon.println(" fail");
  129. delay(10000);
  130. return;
  131. }
  132. SerialMon.println(" success");
  133. if (modem.isNetworkConnected()) {
  134. SerialMon.println("Network connected");
  135. }
  136. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_HAS_GPRS
  137. SerialMon.print(F("Connecting to "));
  138. SerialMon.print(apn);
  139. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  140. SerialMon.println(" fail");
  141. delay(10000);
  142. return;
  143. }
  144. SerialMon.println(" success");
  145. #endif
  146. SerialMon.print(F("Connecting to "));
  147. SerialMon.print(server);
  148. if (!client.connect(server, port)) {
  149. SerialMon.println(" fail");
  150. delay(10000);
  151. return;
  152. }
  153. SerialMon.println(" success");
  154. // Make a HTTP GET request:
  155. client.print(String("GET ") + resource + " HTTP/1.0\r\n");
  156. client.print(String("Host: ") + server + "\r\n");
  157. client.print("Connection: close\r\n\r\n");
  158. // Let's see what the entire elapsed time is, from after we send the request.
  159. unsigned long timeElapsed = millis();
  160. SerialMon.println(F("Waiting for response header"));
  161. // While we are still looking for the end of the header (i.e. empty line FOLLOWED by a newline),
  162. // continue to read data into the buffer, parsing each line (data FOLLOWED by a newline).
  163. // If it takes too long to get data from the client, we need to exit.
  164. const uint32_t clientReadTimeout = 5000;
  165. uint32_t clientReadStartTime = millis();
  166. String headerBuffer;
  167. bool finishedHeader = false;
  168. uint32_t contentLength = 0;
  169. while (!finishedHeader) {
  170. int nlPos;
  171. if (client.available()) {
  172. clientReadStartTime = millis();
  173. while (client.available()) {
  174. char c = client.read();
  175. headerBuffer += c;
  176. // Uncomment the lines below to see the data coming into the buffer
  177. // if (c < 16)
  178. // SerialMon.print('0');
  179. // SerialMon.print(c, HEX);
  180. // SerialMon.print(' ');
  181. // if (isprint(c))
  182. // SerialMon.print((char) c);
  183. // else
  184. // SerialMon.print('*');
  185. // SerialMon.print(' ');
  186. // Let's exit and process if we find a new line
  187. if (headerBuffer.indexOf(F("\r\n")) >= 0)
  188. break;
  189. }
  190. }
  191. else {
  192. if (millis() - clientReadStartTime > clientReadTimeout) {
  193. // Time-out waiting for data from client
  194. SerialMon.println(F(">>> Client Timeout !"));
  195. break;
  196. }
  197. }
  198. // See if we have a new line.
  199. nlPos = headerBuffer.indexOf(F("\r\n"));
  200. if (nlPos > 0) {
  201. headerBuffer.toLowerCase();
  202. // Check if line contains content-length
  203. if (headerBuffer.startsWith(F("content-length:"))) {
  204. contentLength = headerBuffer.substring(headerBuffer.indexOf(':') + 1).toInt();
  205. // SerialMon.print(F("Got Content Length: ")); // uncomment for
  206. // SerialMon.println(contentLength); // confirmation
  207. }
  208. headerBuffer.remove(0, nlPos + 2); // remove the line
  209. }
  210. else if (nlPos == 0) {
  211. // if the new line is empty (i.e. "\r\n" is at the beginning of the line), we are done with the header.
  212. finishedHeader = true;
  213. }
  214. }
  215. // The two cases which are not managed properly are as follows:
  216. // 1. The client doesn't provide data quickly enough to keep up with this loop.
  217. // 2. If the client data is segmented in the middle of the 'Content-Length: ' header,
  218. // then that header may be missed/damaged.
  219. //
  220. uint32_t readLength = 0;
  221. CRC32 crc;
  222. if (finishedHeader && contentLength == knownFileSize) {
  223. SerialMon.println(F("Reading response data"));
  224. clientReadStartTime = millis();
  225. printPercent(readLength, contentLength);
  226. while (readLength < contentLength && client.connected() && millis() - clientReadStartTime < clientReadTimeout) {
  227. while (client.available()) {
  228. uint8_t c = client.read();
  229. //SerialMon.print((char)c); // Uncomment this to show data
  230. crc.update(c);
  231. readLength++;
  232. if (readLength % (contentLength / 13) == 0) {
  233. printPercent(readLength, contentLength);
  234. }
  235. clientReadStartTime = millis();
  236. }
  237. }
  238. printPercent(readLength, contentLength);
  239. }
  240. timeElapsed = millis() - timeElapsed;
  241. SerialMon.println();
  242. // Shutdown
  243. client.stop();
  244. SerialMon.println(F("Server disconnected"));
  245. modem.gprsDisconnect();
  246. SerialMon.println(F("GPRS disconnected"));
  247. float duration = float(timeElapsed) / 1000;
  248. SerialMon.println();
  249. SerialMon.print("Content-Length: "); SerialMon.println(contentLength);
  250. SerialMon.print("Actually read: "); SerialMon.println(readLength);
  251. SerialMon.print("Calc. CRC32: 0x"); SerialMon.println(crc.finalize(), HEX);
  252. SerialMon.print("Known CRC32: 0x"); SerialMon.println(knownCRC32, HEX);
  253. SerialMon.print("Duration: "); SerialMon.print(duration); SerialMon.println("s");
  254. // Do nothing forevermore
  255. while (true) {
  256. delay(1000);
  257. }
  258. }