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.

311 lines
9.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
5 years ago
7 years ago
5 years ago
7 years ago
7 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
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_SIM7600
  23. // #define TINY_GSM_MODEM_UBLOX
  24. // #define TINY_GSM_MODEM_SARAR4
  25. // #define TINY_GSM_MODEM_M95
  26. // #define TINY_GSM_MODEM_BG96
  27. // #define TINY_GSM_MODEM_A6
  28. // #define TINY_GSM_MODEM_A7
  29. // #define TINY_GSM_MODEM_M590
  30. // #define TINY_GSM_MODEM_MC60
  31. // #define TINY_GSM_MODEM_MC60E
  32. // #define TINY_GSM_MODEM_ESP8266
  33. // #define TINY_GSM_MODEM_XBEE
  34. // #define TINY_GSM_MODEM_SEQUANS_MONARCH
  35. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  36. #define SerialMon Serial
  37. // Set serial for AT commands (to the module)
  38. // Use Hardware Serial on Mega, Leonardo, Micro
  39. #define SerialAT Serial1
  40. // or Software Serial on Uno, Nano
  41. //#include <SoftwareSerial.h>
  42. //SoftwareSerial SerialAT(2, 3); // RX, TX
  43. // Increase RX buffer to capture the entire response
  44. // Chips without internal buffering (A6/A7, ESP8266, M590)
  45. // need enough space in the buffer for the entire response
  46. // else data will be lost (and the http library will fail).
  47. #define TINY_GSM_RX_BUFFER 1024
  48. // See all AT commands, if wanted
  49. //#define DUMP_AT_COMMANDS
  50. // Define the serial console for debug prints, if needed
  51. #define TINY_GSM_DEBUG SerialMon
  52. //#define LOGGING // <- Logging is for the HTTP library
  53. // Add a reception delay - may be needed for a fast processor at a slow baud rate
  54. //#define TINY_GSM_YIELD() { delay(2); }
  55. #define TINY_GSM_USE_GPRS true
  56. #define TINY_GSM_USE_WIFI false
  57. // set GSM PIN, if any
  58. #define GSM_PIN ""
  59. // Your GPRS credentials
  60. // Leave empty, if missing user or pass
  61. const char apn[] = "YourAPN";
  62. const char gprsUser[] = "";
  63. const char gprsPass[] = "";
  64. const char wifiSSID[] = "YourSSID";
  65. const char wifiPass[] = "YourWiFiPass";
  66. // Server details
  67. const char server[] = "vsh.pp.ua";
  68. const int port = 80;
  69. #include <TinyGsmClient.h>
  70. #include <CRC32.h>
  71. const char resource[] = "/TinyGSM/test_1k.bin";
  72. uint32_t knownCRC32 = 0x6f50d767;
  73. uint32_t knownFileSize = 1024; // In case server does not send it
  74. #ifdef DUMP_AT_COMMANDS
  75. #include <StreamDebugger.h>
  76. StreamDebugger debugger(SerialAT, SerialMon);
  77. TinyGsm modem(debugger);
  78. #else
  79. TinyGsm modem(SerialAT);
  80. #endif
  81. TinyGsmClient client(modem);
  82. void setup() {
  83. // Set console baud rate
  84. SerialMon.begin(115200);
  85. delay(10);
  86. // Set GSM module baud rate
  87. SerialAT.begin(115200);
  88. delay(3000);
  89. // Restart takes quite some time
  90. // To skip it, call init() instead of restart()
  91. SerialMon.println("Initializing modem...");
  92. modem.restart();
  93. String modemInfo = modem.getModemInfo();
  94. SerialMon.print("Modem: ");
  95. SerialMon.println(modemInfo);
  96. #if TINY_GSM_USE_GPRS
  97. // Unlock your SIM card with a PIN if needed
  98. if ( GSM_PIN && modem.getSimStatus() != 3 ) {
  99. modem.simUnlock(GSM_PIN);
  100. }
  101. #endif
  102. }
  103. void printPercent(uint32_t readLength, uint32_t contentLength) {
  104. // If we know the total length
  105. if (contentLength != (uint32_t)-1) {
  106. SerialMon.print("\r ");
  107. SerialMon.print((100.0 * readLength) / contentLength);
  108. SerialMon.print('%');
  109. } else {
  110. SerialMon.println(readLength);
  111. }
  112. }
  113. void loop() {
  114. #if defined TINY_GSM_USE_WIFI && defined TINY_GSM_MODEM_HAS_WIFI
  115. SerialMon.print(F("Setting SSID/password..."));
  116. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  117. SerialMon.println(" fail");
  118. delay(10000);
  119. return;
  120. }
  121. SerialMon.println(" success");
  122. #endif
  123. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  124. // The XBee must run the gprsConnect function BEFORE waiting for network!
  125. modem.gprsConnect(apn, gprsUser, gprsPass);
  126. #endif
  127. SerialMon.print("Waiting for network...");
  128. if (!modem.waitForNetwork()) {
  129. SerialMon.println(" fail");
  130. delay(10000);
  131. return;
  132. }
  133. SerialMon.println(" success");
  134. if (modem.isNetworkConnected()) {
  135. SerialMon.println("Network connected");
  136. }
  137. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_HAS_GPRS
  138. SerialMon.print(F("Connecting to "));
  139. SerialMon.print(apn);
  140. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  141. SerialMon.println(" fail");
  142. delay(10000);
  143. return;
  144. }
  145. SerialMon.println(" success");
  146. #endif
  147. SerialMon.print(F("Connecting to "));
  148. SerialMon.print(server);
  149. if (!client.connect(server, port)) {
  150. SerialMon.println(" fail");
  151. delay(10000);
  152. return;
  153. }
  154. SerialMon.println(" success");
  155. // Make a HTTP GET request:
  156. client.print(String("GET ") + resource + " HTTP/1.0\r\n");
  157. client.print(String("Host: ") + server + "\r\n");
  158. client.print("Connection: close\r\n\r\n");
  159. // Let's see what the entire elapsed time is, from after we send the request.
  160. unsigned long timeElapsed = millis();
  161. SerialMon.println(F("Waiting for response header"));
  162. // While we are still looking for the end of the header (i.e. empty line FOLLOWED by a newline),
  163. // continue to read data into the buffer, parsing each line (data FOLLOWED by a newline).
  164. // If it takes too long to get data from the client, we need to exit.
  165. const uint32_t clientReadTimeout = 5000;
  166. uint32_t clientReadStartTime = millis();
  167. String headerBuffer;
  168. bool finishedHeader = false;
  169. uint32_t contentLength = 0;
  170. while (!finishedHeader) {
  171. int nlPos;
  172. if (client.available()) {
  173. clientReadStartTime = millis();
  174. while (client.available()) {
  175. char c = client.read();
  176. headerBuffer += c;
  177. // Uncomment the lines below to see the data coming into the buffer
  178. // if (c < 16)
  179. // SerialMon.print('0');
  180. // SerialMon.print(c, HEX);
  181. // SerialMon.print(' ');
  182. // if (isprint(c))
  183. // SerialMon.print((char) c);
  184. // else
  185. // SerialMon.print('*');
  186. // SerialMon.print(' ');
  187. // Let's exit and process if we find a new line
  188. if (headerBuffer.indexOf(F("\r\n")) >= 0)
  189. break;
  190. }
  191. }
  192. else {
  193. if (millis() - clientReadStartTime > clientReadTimeout) {
  194. // Time-out waiting for data from client
  195. SerialMon.println(F(">>> Client Timeout !"));
  196. break;
  197. }
  198. }
  199. // See if we have a new line.
  200. nlPos = headerBuffer.indexOf(F("\r\n"));
  201. if (nlPos > 0) {
  202. headerBuffer.toLowerCase();
  203. // Check if line contains content-length
  204. if (headerBuffer.startsWith(F("content-length:"))) {
  205. contentLength = headerBuffer.substring(headerBuffer.indexOf(':') + 1).toInt();
  206. // SerialMon.print(F("Got Content Length: ")); // uncomment for
  207. // SerialMon.println(contentLength); // confirmation
  208. }
  209. headerBuffer.remove(0, nlPos + 2); // remove the line
  210. }
  211. else if (nlPos == 0) {
  212. // if the new line is empty (i.e. "\r\n" is at the beginning of the line), we are done with the header.
  213. finishedHeader = true;
  214. }
  215. }
  216. // The two cases which are not managed properly are as follows:
  217. // 1. The client doesn't provide data quickly enough to keep up with this loop.
  218. // 2. If the client data is segmented in the middle of the 'Content-Length: ' header,
  219. // then that header may be missed/damaged.
  220. //
  221. uint32_t readLength = 0;
  222. CRC32 crc;
  223. if (finishedHeader && contentLength == knownFileSize) {
  224. SerialMon.println(F("Reading response data"));
  225. clientReadStartTime = millis();
  226. printPercent(readLength, contentLength);
  227. while (readLength < contentLength && client.connected() && millis() - clientReadStartTime < clientReadTimeout) {
  228. while (client.available()) {
  229. uint8_t c = client.read();
  230. //SerialMon.print((char)c); // Uncomment this to show data
  231. crc.update(c);
  232. readLength++;
  233. if (readLength % (contentLength / 13) == 0) {
  234. printPercent(readLength, contentLength);
  235. }
  236. clientReadStartTime = millis();
  237. }
  238. }
  239. printPercent(readLength, contentLength);
  240. }
  241. timeElapsed = millis() - timeElapsed;
  242. SerialMon.println();
  243. // Shutdown
  244. client.stop();
  245. SerialMon.println(F("Server disconnected"));
  246. modem.gprsDisconnect();
  247. SerialMon.println(F("GPRS disconnected"));
  248. float duration = float(timeElapsed) / 1000;
  249. SerialMon.println();
  250. SerialMon.print("Content-Length: "); SerialMon.println(contentLength);
  251. SerialMon.print("Actually read: "); SerialMon.println(readLength);
  252. SerialMon.print("Calc. CRC32: 0x"); SerialMon.println(crc.finalize(), HEX);
  253. SerialMon.print("Known CRC32: 0x"); SerialMon.println(knownCRC32, HEX);
  254. SerialMon.print("Duration: "); SerialMon.print(duration); SerialMon.println("s");
  255. // Do nothing forevermore
  256. while (true) {
  257. delay(1000);
  258. }
  259. }