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.

196 lines
5.1 KiB

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