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.

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