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.

258 lines
7.0 KiB

7 years ago
6 years ago
7 years ago
8 years ago
8 years ago
5 years ago
6 years ago
6 years ago
8 years ago
7 years ago
5 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
7 years ago
8 years ago
5 years ago
8 years ago
5 years ago
7 years ago
5 years ago
5 years ago
5 years ago
7 years ago
7 years ago
5 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 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
  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. * https://tiny.cc/tinygsm-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_SIM868
  19. // #define TINY_GSM_MODEM_SIM900
  20. // #define TINY_GSM_MODEM_SIM7000
  21. // #define TINY_GSM_MODEM_SIM5360
  22. // #define TINY_GSM_MODEM_UBLOX
  23. // #define TINY_GSM_MODEM_SARAR4
  24. // #define TINY_GSM_MODEM_M95
  25. // #define TINY_GSM_MODEM_BG96
  26. // #define TINY_GSM_MODEM_A6
  27. // #define TINY_GSM_MODEM_A7
  28. // #define TINY_GSM_MODEM_M590
  29. // #define TINY_GSM_MODEM_MC60
  30. // #define TINY_GSM_MODEM_MC60E
  31. // #define TINY_GSM_MODEM_ESP8266
  32. // #define TINY_GSM_MODEM_XBEE
  33. // #define TINY_GSM_MODEM_SEQUANS_MONARCH
  34. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  35. #define SerialMon Serial
  36. // Set serial for AT commands (to the module)
  37. // Use Hardware Serial on Mega, Leonardo, Micro
  38. #define SerialAT Serial1
  39. // or Software Serial on Uno, Nano
  40. //#include <SoftwareSerial.h>
  41. //SoftwareSerial SerialAT(2, 3); // RX, TX
  42. // Increase RX buffer to capture the entire response
  43. // Chips without internal buffering (A6/A7, ESP8266, M590)
  44. // need enough space in the buffer for the entire response
  45. // else data will be lost (and the http library will fail).
  46. #define TINY_GSM_RX_BUFFER 1024
  47. // See all AT commands, if wanted
  48. //#define DUMP_AT_COMMANDS
  49. // Define the serial console for debug prints, if needed
  50. #define TINY_GSM_DEBUG SerialMon
  51. //#define LOGGING // <- Logging is for the HTTP library
  52. // Add a reception delay, if needed
  53. //#define TINY_GSM_YIELD() { delay(2); }
  54. #define TINY_GSM_USE_GPRS true
  55. #define TINY_GSM_USE_WIFI false
  56. // set GSM PIN, if any
  57. #define GSM_PIN ""
  58. // Your GPRS credentials
  59. // Leave empty, if missing user or pass
  60. const char apn[] = "YourAPN";
  61. const char gprsUser[] = "";
  62. const char gprsPass[] = "";
  63. const char wifiSSID[] = "YourSSID";
  64. const char wifiPass[] = "YourWiFiPass";
  65. // Server details
  66. const char server[] = "vsh.pp.ua";
  67. const int port = 80;
  68. const char resource[] = "/TinyGSM/test_1k.bin";
  69. uint32_t knownCRC32 = 0x6f50d767;
  70. uint32_t knownFileSize = 1024; // In case server does not send it
  71. #include <TinyGsmClient.h>
  72. #include <CRC32.h>
  73. #ifdef DUMP_AT_COMMANDS
  74. #include <StreamDebugger.h>
  75. StreamDebugger debugger(SerialAT, SerialMon);
  76. TinyGsm modem(debugger);
  77. #else
  78. TinyGsm modem(SerialAT);
  79. #endif
  80. TinyGsmClient client(modem);
  81. void setup() {
  82. // Set console baud rate
  83. SerialMon.begin(115200);
  84. delay(10);
  85. // Set GSM module baud rate
  86. SerialAT.begin(115200);
  87. delay(3000);
  88. // Restart takes quite some time
  89. // To skip it, call init() instead of restart()
  90. SerialMon.println("Initializing modem...");
  91. modem.restart();
  92. String modemInfo = modem.getModemInfo();
  93. SerialMon.print("Modem: ");
  94. SerialMon.println(modemInfo);
  95. #if TINY_GSM_USE_GPRS
  96. // Unlock your SIM card with a PIN if needed
  97. if ( GSM_PIN && modem.getSimStatus() != 3 ) {
  98. modem.simUnlock(GSM_PIN);
  99. }
  100. #endif
  101. }
  102. void printPercent(uint32_t readLength, uint32_t contentLength) {
  103. // If we know the total length
  104. if (contentLength != -1) {
  105. SerialMon.print("\r ");
  106. SerialMon.print((100.0 * readLength) / contentLength);
  107. SerialMon.print('%');
  108. } else {
  109. SerialMon.println(readLength);
  110. }
  111. }
  112. void loop() {
  113. #if defined TINY_GSM_USE_WIFI && defined TINY_GSM_MODEM_HAS_WIFI
  114. SerialMon.print(F("Setting SSID/password..."));
  115. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  116. SerialMon.println(" fail");
  117. delay(10000);
  118. return;
  119. }
  120. SerialMon.println(" success");
  121. #endif
  122. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  123. // The XBee must run the gprsConnect function BEFORE waiting for network!
  124. modem.gprsConnect(apn, gprsUser, gprsPass);
  125. #endif
  126. SerialMon.print("Waiting for network...");
  127. if (!modem.waitForNetwork()) {
  128. SerialMon.println(" fail");
  129. delay(10000);
  130. return;
  131. }
  132. SerialMon.println(" success");
  133. if (modem.isNetworkConnected()) {
  134. SerialMon.println("Network connected");
  135. }
  136. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_HAS_GPRS
  137. SerialMon.print(F("Connecting to "));
  138. SerialMon.print(apn);
  139. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  140. SerialMon.println(" fail");
  141. delay(10000);
  142. return;
  143. }
  144. SerialMon.println(" success");
  145. #endif
  146. SerialMon.print(F("Connecting to "));
  147. SerialMon.print(server);
  148. if (!client.connect(server, port)) {
  149. SerialMon.println(" fail");
  150. delay(10000);
  151. return;
  152. }
  153. SerialMon.println(" success");
  154. // Make a HTTP GET request:
  155. client.print(String("GET ") + resource + " HTTP/1.0\r\n");
  156. client.print(String("Host: ") + server + "\r\n");
  157. client.print("Connection: close\r\n\r\n");
  158. long timeout = millis();
  159. while (client.available() == 0) {
  160. if (millis() - timeout > 5000L) {
  161. SerialMon.println(F(">>> Client Timeout !"));
  162. client.stop();
  163. delay(10000L);
  164. return;
  165. }
  166. }
  167. SerialMon.println(F("Reading response header"));
  168. uint32_t contentLength = knownFileSize;
  169. while (client.available()) {
  170. String line = client.readStringUntil('\n');
  171. line.trim();
  172. //SerialMon.println(line); // Uncomment this to show response header
  173. line.toLowerCase();
  174. if (line.startsWith("content-length:")) {
  175. contentLength = line.substring(line.lastIndexOf(':') + 1).toInt();
  176. } else if (line.length() == 0) {
  177. break;
  178. }
  179. }
  180. SerialMon.println(F("Reading response data"));
  181. timeout = millis();
  182. uint32_t readLength = 0;
  183. CRC32 crc;
  184. unsigned long timeElapsed = millis();
  185. printPercent(readLength, contentLength);
  186. while (readLength < contentLength && client.connected() && millis() - timeout < 10000L) {
  187. while (client.available()) {
  188. uint8_t c = client.read();
  189. //SerialMon.print((char)c); // Uncomment this to show data
  190. crc.update(c);
  191. readLength++;
  192. if (readLength % (contentLength / 13) == 0) {
  193. printPercent(readLength, contentLength);
  194. }
  195. timeout = millis();
  196. }
  197. }
  198. printPercent(readLength, contentLength);
  199. timeElapsed = millis() - timeElapsed;
  200. SerialMon.println();
  201. // Shutdown
  202. client.stop();
  203. SerialMon.println(F("Server disconnected"));
  204. modem.gprsDisconnect();
  205. SerialMon.println(F("GPRS disconnected"));
  206. float duration = float(timeElapsed) / 1000;
  207. SerialMon.println();
  208. SerialMon.print("Content-Length: "); SerialMon.println(contentLength);
  209. SerialMon.print("Actually read: "); SerialMon.println(readLength);
  210. SerialMon.print("Calc. CRC32: 0x"); SerialMon.println(crc.finalize(), HEX);
  211. SerialMon.print("Known CRC32: 0x"); SerialMon.println(knownCRC32, HEX);
  212. SerialMon.print("Duration: "); SerialMon.print(duration); SerialMon.println("s");
  213. // Do nothing forevermore
  214. while (true) {
  215. delay(1000);
  216. }
  217. }