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.

290 lines
8.5 KiB

7 years ago
6 years ago
7 years ago
8 years ago
8 years ago
6 years ago
6 years ago
8 years ago
7 years ago
8 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
8 years ago
7 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 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_UBLOX
  22. // #define TINY_GSM_MODEM_SARAR4
  23. // #define TINY_GSM_MODEM_M95
  24. // #define TINY_GSM_MODEM_BG96
  25. // #define TINY_GSM_MODEM_A6
  26. // #define TINY_GSM_MODEM_A7
  27. // #define TINY_GSM_MODEM_M590
  28. // #define TINY_GSM_MODEM_MC60
  29. // #define TINY_GSM_MODEM_MC60E
  30. // #define TINY_GSM_MODEM_ESP8266
  31. // #define TINY_GSM_MODEM_XBEE
  32. // #define TINY_GSM_MODEM_SEQUANS_MONARCH
  33. // Increase RX buffer if needed
  34. #define TINY_GSM_RX_BUFFER 1024
  35. #include <TinyGsmClient.h>
  36. #include <CRC32.h>
  37. // Uncomment this if you want to see all AT commands
  38. //#define DUMP_AT_COMMANDS
  39. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  40. #define SerialMon Serial
  41. // Use Hardware Serial on Mega, Leonardo, Micro
  42. #define SerialAT Serial1
  43. // or Software Serial on Uno, Nano
  44. //#include <SoftwareSerial.h>
  45. //SoftwareSerial SerialAT(2, 3); // RX, TX
  46. // Your GPRS credentials
  47. // Leave empty, if missing user or pass
  48. const char apn[] = "YourAPN";
  49. const char user[] = "";
  50. const char pass[] = "";
  51. // Server details
  52. const char server[] = "vsh.pp.ua";
  53. const int port = 80;
  54. const char resource[] = "/TinyGSM/test_1k.bin";
  55. uint32_t knownCRC32 = 0x6f50d767;
  56. uint32_t knownFileSize = 1024; // In case server does not send it
  57. #ifdef DUMP_AT_COMMANDS
  58. #include <StreamDebugger.h>
  59. StreamDebugger debugger(SerialAT, SerialMon);
  60. TinyGsm modem(debugger);
  61. #else
  62. TinyGsm modem(SerialAT);
  63. #endif
  64. TinyGsmClient client(modem);
  65. void setup() {
  66. // Set console baud rate
  67. SerialMon.begin(115200);
  68. delay(10);
  69. // Set GSM module baud rate
  70. SerialAT.begin(115200);
  71. delay(3000);
  72. // Restart takes quite some time
  73. // To skip it, call init() instead of restart()
  74. SerialMon.println(F("Initializing modem..."));
  75. modem.restart();
  76. String modemInfo = modem.getModemInfo();
  77. SerialMon.print(F("Modem: "));
  78. SerialMon.println(modemInfo);
  79. // Unlock your SIM card with a PIN
  80. //modem.simUnlock("1234");
  81. }
  82. void printPercent(uint32_t readLength, uint32_t contentLength) {
  83. // If we know the total length
  84. if (contentLength != -1) {
  85. SerialMon.print("\r ");
  86. SerialMon.print((100.0 * readLength) / contentLength);
  87. SerialMon.print('%');
  88. } else {
  89. SerialMon.println(readLength);
  90. }
  91. }
  92. void loop() {
  93. SerialMon.print(F("Waiting for network..."));
  94. if (!modem.waitForNetwork()) {
  95. SerialMon.println(" fail");
  96. delay(10000);
  97. return;
  98. }
  99. SerialMon.println(" OK");
  100. SerialMon.print(F("Connecting to "));
  101. SerialMon.print(apn);
  102. if (!modem.gprsConnect(apn, user, pass)) {
  103. SerialMon.println(" fail");
  104. delay(10000);
  105. return;
  106. }
  107. SerialMon.println(" OK");
  108. SerialMon.print(F("Connecting to "));
  109. SerialMon.print(server);
  110. if (!client.connect(server, port)) {
  111. SerialMon.println(" fail");
  112. delay(10000);
  113. return;
  114. }
  115. SerialMon.println(" OK");
  116. // Make a HTTP GET request:
  117. client.print(String("GET ") + resource + " HTTP/1.0\r\n");
  118. client.print(String("Host: ") + server + "\r\n");
  119. client.print("Connection: close\r\n\r\n");
  120. // This timeout check is unneeded since there is a timeout handler in the data retrieval below
  121. // long timeout = millis();
  122. // while (client.available() == 0) {
  123. // if (millis() - timeout > 5000L) {
  124. // SerialMon.println(F(">>> Client Timeout !"));
  125. // client.stop();
  126. // delay(10000L);
  127. // return;
  128. // }
  129. // }
  130. // Let's see what the entire elapsed time is, from after we send the request.
  131. unsigned long timeElapsed = millis();
  132. SerialMon.println(F("Waiting for response header"));
  133. // While we are still looking for the end of the header (i.e. empty line FOLLOWED by a newline),
  134. // continue to read data into the buffer, parsing each line (data FOLLOWED by a newline).
  135. // If it takes too long to get data from the client, we need to exit.
  136. const uint32_t clientReadTimeout = 5000;
  137. uint32_t clientReadStartTime = millis();
  138. String headerBuffer;
  139. bool finishedHeader = false;
  140. uint32_t contentLength = 0;
  141. while (!finishedHeader) {
  142. int nlPos;
  143. if (client.available()) {
  144. clientReadStartTime = millis();
  145. while (client.available()) {
  146. char c = client.read();
  147. headerBuffer += c;
  148. // Uncomment the lines below to see the data coming into the buffer
  149. // if (c < 16)
  150. // SerialMon.print('0');
  151. // SerialMon.print(c, HEX);
  152. // SerialMon.print(' ');
  153. // if (isprint(c))
  154. // SerialMon.print((char) c);
  155. // else
  156. // SerialMon.print('*');
  157. // SerialMon.print(' ');
  158. // Let's exit and process if we find a new line
  159. if (headerBuffer.indexOf(F("\r\n")) >= 0)
  160. break;
  161. }
  162. }
  163. else {
  164. if (millis() - clientReadStartTime > clientReadTimeout) {
  165. // Time-out waiting for data from client
  166. SerialMon.println(F(">>> Client Timeout !"));
  167. break;
  168. }
  169. }
  170. // See if we have a new line.
  171. nlPos = headerBuffer.indexOf(F("\r\n"));
  172. if (nlPos > 0) {
  173. headerBuffer.toLowerCase();
  174. // Check if line contains content-length
  175. if (headerBuffer.startsWith(F("content-length:"))) {
  176. contentLength = headerBuffer.substring(headerBuffer.indexOf(':') + 1).toInt();
  177. // SerialMon.print(F("Got Content Length: ")); // uncomment for
  178. // SerialMon.println(contentLength); // confirmation
  179. }
  180. headerBuffer.remove(0, nlPos + 2); // remove the line
  181. }
  182. else if (nlPos == 0) {
  183. // if the new line is empty (i.e. "\r\n" is at the beginning of the line), we are done with the header.
  184. finishedHeader = true;
  185. }
  186. }
  187. // vv Broken vv
  188. // while (client.available()) { // ** race condition -- if the client doesn't have enough data, we will exit.
  189. // String line = client.readStringUntil('\n'); // ** Depending on what we get from the client, this could be a partial line.
  190. // line.trim();
  191. // //SerialMon.println(line); // Uncomment this to show response header
  192. // line.toLowerCase();
  193. // if (line.startsWith("content-length:")) {
  194. // contentLength = line.substring(line.lastIndexOf(':') + 1).toInt();
  195. // } else if (line.length() == 0) {
  196. // break;
  197. // }
  198. // }
  199. // ^^ Broken ^^
  200. //
  201. // The two cases which are not managed properly are as follows:
  202. // 1. The client doesn't provide data quickly enough to keep up with this loop.
  203. // 2. If the client data is segmented in the middle of the 'Content-Length: ' header,
  204. // then that header may be missed/damaged.
  205. //
  206. uint32_t readLength = 0;
  207. CRC32 crc;
  208. if (finishedHeader && contentLength == knownFileSize) {
  209. SerialMon.println(F("Reading response data"));
  210. clientReadStartTime = millis();
  211. printPercent(readLength, contentLength);
  212. while (readLength < contentLength && client.connected() && millis() - clientReadStartTime < clientReadTimeout) {
  213. while (client.available()) {
  214. uint8_t c = client.read();
  215. //SerialMon.print((char)c); // Uncomment this to show data
  216. crc.update(c);
  217. readLength++;
  218. if (readLength % (contentLength / 13) == 0) {
  219. printPercent(readLength, contentLength);
  220. }
  221. clientReadStartTime = millis();
  222. }
  223. }
  224. printPercent(readLength, contentLength);
  225. }
  226. timeElapsed = millis() - timeElapsed;
  227. SerialMon.println();
  228. // Shutdown
  229. client.stop();
  230. SerialMon.println(F("Server disconnected"));
  231. modem.gprsDisconnect();
  232. SerialMon.println(F("GPRS disconnected"));
  233. float duration = float(timeElapsed) / 1000;
  234. SerialMon.println();
  235. SerialMon.print("Content-Length: "); SerialMon.println(contentLength);
  236. SerialMon.print("Actually read: "); SerialMon.println(readLength);
  237. SerialMon.print("Calc. CRC32: 0x"); SerialMon.println(crc.finalize(), HEX);
  238. SerialMon.print("Known CRC32: 0x"); SerialMon.println(knownCRC32, HEX);
  239. SerialMon.print("Duration: "); SerialMon.print(duration); SerialMon.println("s");
  240. // Do nothing forevermore
  241. while (true) {
  242. delay(1000);
  243. }
  244. }