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.

210 lines
5.6 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
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_SIM900
  18. // #define TINY_GSM_MODEM_SIM808
  19. // #define TINY_GSM_MODEM_SIM868
  20. // #define TINY_GSM_MODEM_UBLOX
  21. // #define TINY_GSM_MODEM_M95
  22. // #define TINY_GSM_MODEM_BG96
  23. // #define TINY_GSM_MODEM_A6
  24. // #define TINY_GSM_MODEM_A7
  25. // #define TINY_GSM_MODEM_M590
  26. // #define TINY_GSM_MODEM_MC60
  27. // #define TINY_GSM_MODEM_MC60E
  28. // #define TINY_GSM_MODEM_ESP8266
  29. // #define TINY_GSM_MODEM_XBEE
  30. // Increase RX buffer if needed
  31. #define TINY_GSM_RX_BUFFER 1024
  32. #include <TinyGsmClient.h>
  33. #include <CRC32.h>
  34. // Uncomment this if you want to see all AT commands
  35. //#define DUMP_AT_COMMANDS
  36. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  37. #define SerialMon Serial
  38. // Use Hardware Serial on Mega, Leonardo, Micro
  39. #define SerialAT Serial1
  40. // or Software Serial on Uno, Nano
  41. //#include <SoftwareSerial.h>
  42. //SoftwareSerial SerialAT(2, 3); // RX, TX
  43. // Your GPRS credentials
  44. // Leave empty, if missing user or pass
  45. const char apn[] = "YourAPN";
  46. const char user[] = "";
  47. const char pass[] = "";
  48. // Server details
  49. const char server[] = "vsh.pp.ua";
  50. const int port = 80;
  51. const char resource[] = "/TinyGSM/test_1k.bin";
  52. uint32_t knownCRC32 = 0x6f50d767;
  53. uint32_t knownFileSize = 1024; // In case server does not send it
  54. #ifdef DUMP_AT_COMMANDS
  55. #include <StreamDebugger.h>
  56. StreamDebugger debugger(SerialAT, SerialMon);
  57. TinyGsm modem(debugger);
  58. #else
  59. TinyGsm modem(SerialAT);
  60. #endif
  61. TinyGsmClient client(modem);
  62. void setup() {
  63. // Set console baud rate
  64. SerialMon.begin(115200);
  65. delay(10);
  66. // Set GSM module baud rate
  67. SerialAT.begin(115200);
  68. delay(3000);
  69. // Restart takes quite some time
  70. // To skip it, call init() instead of restart()
  71. SerialMon.println(F("Initializing modem..."));
  72. modem.restart();
  73. String modemInfo = modem.getModemInfo();
  74. SerialMon.print(F("Modem: "));
  75. SerialMon.println(modemInfo);
  76. // Unlock your SIM card with a PIN
  77. //modem.simUnlock("1234");
  78. }
  79. void printPercent(uint32_t readLength, uint32_t contentLength) {
  80. // If we know the total length
  81. if (contentLength != -1) {
  82. SerialMon.print("\r ");
  83. SerialMon.print((100.0 * readLength) / contentLength);
  84. SerialMon.print('%');
  85. } else {
  86. SerialMon.println(readLength);
  87. }
  88. }
  89. void loop() {
  90. SerialMon.print(F("Waiting for network..."));
  91. if (!modem.waitForNetwork()) {
  92. SerialMon.println(" fail");
  93. delay(10000);
  94. return;
  95. }
  96. SerialMon.println(" OK");
  97. SerialMon.print(F("Connecting to "));
  98. SerialMon.print(apn);
  99. if (!modem.gprsConnect(apn, user, pass)) {
  100. SerialMon.println(" fail");
  101. delay(10000);
  102. return;
  103. }
  104. SerialMon.println(" OK");
  105. SerialMon.print(F("Connecting to "));
  106. SerialMon.print(server);
  107. if (!client.connect(server, port)) {
  108. SerialMon.println(" fail");
  109. delay(10000);
  110. return;
  111. }
  112. SerialMon.println(" OK");
  113. // Make a HTTP GET request:
  114. client.print(String("GET ") + resource + " HTTP/1.0\r\n");
  115. client.print(String("Host: ") + server + "\r\n");
  116. client.print("Connection: close\r\n\r\n");
  117. long timeout = millis();
  118. while (client.available() == 0) {
  119. if (millis() - timeout > 5000L) {
  120. SerialMon.println(F(">>> Client Timeout !"));
  121. client.stop();
  122. delay(10000L);
  123. return;
  124. }
  125. }
  126. SerialMon.println(F("Reading response header"));
  127. uint32_t contentLength = knownFileSize;
  128. while (client.available()) {
  129. String line = client.readStringUntil('\n');
  130. line.trim();
  131. //SerialMon.println(line); // Uncomment this to show response header
  132. line.toLowerCase();
  133. if (line.startsWith("content-length:")) {
  134. contentLength = line.substring(line.lastIndexOf(':') + 1).toInt();
  135. } else if (line.length() == 0) {
  136. break;
  137. }
  138. }
  139. SerialMon.println(F("Reading response data"));
  140. timeout = millis();
  141. uint32_t readLength = 0;
  142. CRC32 crc;
  143. unsigned long timeElapsed = millis();
  144. printPercent(readLength, contentLength);
  145. while (readLength < contentLength && client.connected() && millis() - timeout < 10000L) {
  146. while (client.available()) {
  147. uint8_t c = client.read();
  148. //SerialMon.print((char)c); // Uncomment this to show data
  149. crc.update(c);
  150. readLength++;
  151. if (readLength % (contentLength / 13) == 0) {
  152. printPercent(readLength, contentLength);
  153. }
  154. timeout = millis();
  155. }
  156. }
  157. printPercent(readLength, contentLength);
  158. timeElapsed = millis() - timeElapsed;
  159. SerialMon.println();
  160. // Shutdown
  161. client.stop();
  162. SerialMon.println(F("Server disconnected"));
  163. modem.gprsDisconnect();
  164. SerialMon.println(F("GPRS disconnected"));
  165. float duration = float(timeElapsed) / 1000;
  166. SerialMon.println();
  167. SerialMon.print("Content-Length: "); SerialMon.println(contentLength);
  168. SerialMon.print("Actually read: "); SerialMon.println(readLength);
  169. SerialMon.print("Calc. CRC32: 0x"); SerialMon.println(crc.finalize(), HEX);
  170. SerialMon.print("Known CRC32: 0x"); SerialMon.println(knownCRC32, HEX);
  171. SerialMon.print("Duration: "); SerialMon.print(duration); SerialMon.println("s");
  172. // Do nothing forevermore
  173. while (true) {
  174. delay(1000);
  175. }
  176. }