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.

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