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.

177 lines
4.6 KiB

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