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.

205 lines
5.4 KiB

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