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.

346 lines
9.9 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
5 years ago
7 years ago
7 years ago
5 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
7 years ago
5 years ago
5 years ago
5 years ago
7 years ago
7 years ago
5 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 how you're planning to connect to the internet
  56. #define TINY_GSM_USE_GPRS true
  57. #define TINY_GSM_USE_WIFI false
  58. // set GSM PIN, if any
  59. #define GSM_PIN ""
  60. // Your GPRS credentials, if any
  61. const char apn[] = "YourAPN";
  62. const char gprsUser[] = "";
  63. const char gprsPass[] = "";
  64. // Your WiFi connection credentials, if applicable
  65. const char wifiSSID[] = "YourSSID";
  66. const char wifiPass[] = "YourWiFiPass";
  67. // Server details
  68. const char server[] = "vsh.pp.ua";
  69. const int port = 80;
  70. #include <TinyGsmClient.h>
  71. #include <CRC32.h>
  72. // Just in case someone defined the wrong thing..
  73. #if TINY_GSM_USE_GPRS && not defined TINY_GSM_MODEM_HAS_GPRS
  74. #undef TINY_GSM_USE_GPRS
  75. #undef TINY_GSM_USE_WIFI
  76. #define TINY_GSM_USE_GPRS false
  77. #define TINY_GSM_USE_WIFI true
  78. #endif
  79. #if TINY_GSM_USE_WIFI && not defined TINY_GSM_MODEM_HAS_WIFI
  80. #undef TINY_GSM_USE_GPRS
  81. #undef TINY_GSM_USE_WIFI
  82. #define TINY_GSM_USE_GPRS true
  83. #define TINY_GSM_USE_WIFI false
  84. #endif
  85. const char resource[] = "/TinyGSM/test_1k.bin";
  86. uint32_t knownCRC32 = 0x6f50d767;
  87. uint32_t knownFileSize = 1024; // In case server does not send it
  88. #ifdef DUMP_AT_COMMANDS
  89. #include <StreamDebugger.h>
  90. StreamDebugger debugger(SerialAT, SerialMon);
  91. TinyGsm modem(debugger);
  92. #else
  93. TinyGsm modem(SerialAT);
  94. #endif
  95. TinyGsmClient client(modem);
  96. void setup() {
  97. // Set console baud rate
  98. SerialMon.begin(115200);
  99. delay(10);
  100. // !!!!!!!!!!!
  101. // Set your reset, enable, power pins here
  102. // !!!!!!!!!!!
  103. SerialMon.println("Wait...");
  104. // Set GSM module baud rate
  105. SerialAT.begin(115200);
  106. delay(3000);
  107. // Restart takes quite some time
  108. // To skip it, call init() instead of restart()
  109. SerialMon.println("Initializing modem...");
  110. modem.restart();
  111. // modem.init();
  112. String modemInfo = modem.getModemInfo();
  113. SerialMon.print("Modem Info: ");
  114. SerialMon.println(modemInfo);
  115. #if TINY_GSM_USE_GPRS
  116. // Unlock your SIM card with a PIN if needed
  117. if ( GSM_PIN && modem.getSimStatus() != 3 ) {
  118. modem.simUnlock(GSM_PIN);
  119. }
  120. #endif
  121. }
  122. void printPercent(uint32_t readLength, uint32_t contentLength) {
  123. // If we know the total length
  124. if (contentLength != (uint32_t)-1) {
  125. SerialMon.print("\r ");
  126. SerialMon.print((100.0 * readLength) / contentLength);
  127. SerialMon.print('%');
  128. } else {
  129. SerialMon.println(readLength);
  130. }
  131. }
  132. void loop() {
  133. #if TINY_GSM_USE_WIFI
  134. // Wifi connection parameters must be set before waiting for the network
  135. SerialMon.print(F("Setting SSID/password..."));
  136. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  137. SerialMon.println(" fail");
  138. delay(10000);
  139. return;
  140. }
  141. SerialMon.println(" success");
  142. #endif
  143. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  144. // The XBee must run the gprsConnect function BEFORE waiting for network!
  145. modem.gprsConnect(apn, gprsUser, gprsPass);
  146. #endif
  147. SerialMon.print("Waiting for network...");
  148. if (!modem.waitForNetwork()) {
  149. SerialMon.println(" fail");
  150. delay(10000);
  151. return;
  152. }
  153. SerialMon.println(" success");
  154. if (modem.isNetworkConnected()) {
  155. SerialMon.println("Network connected");
  156. }
  157. #if TINY_GSM_USE_GPRS
  158. // GPRS connection parameters are usually set after network registration
  159. SerialMon.print(F("Connecting to "));
  160. SerialMon.print(apn);
  161. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  162. SerialMon.println(" fail");
  163. delay(10000);
  164. return;
  165. }
  166. SerialMon.println(" success");
  167. if (modem.isGprsConnected()) {
  168. SerialMon.println("GPRS connected");
  169. }
  170. #endif
  171. SerialMon.print(F("Connecting to "));
  172. SerialMon.print(server);
  173. if (!client.connect(server, port)) {
  174. SerialMon.println(" fail");
  175. delay(10000);
  176. return;
  177. }
  178. SerialMon.println(" success");
  179. // Make a HTTP GET request:
  180. client.print(String("GET ") + resource + " HTTP/1.0\r\n");
  181. client.print(String("Host: ") + server + "\r\n");
  182. client.print("Connection: close\r\n\r\n");
  183. // Let's see what the entire elapsed time is, from after we send the request.
  184. unsigned long timeElapsed = millis();
  185. SerialMon.println(F("Waiting for response header"));
  186. // While we are still looking for the end of the header (i.e. empty line FOLLOWED by a newline),
  187. // continue to read data into the buffer, parsing each line (data FOLLOWED by a newline).
  188. // If it takes too long to get data from the client, we need to exit.
  189. const uint32_t clientReadTimeout = 5000;
  190. uint32_t clientReadStartTime = millis();
  191. String headerBuffer;
  192. bool finishedHeader = false;
  193. uint32_t contentLength = 0;
  194. while (!finishedHeader) {
  195. int nlPos;
  196. if (client.available()) {
  197. clientReadStartTime = millis();
  198. while (client.available()) {
  199. char c = client.read();
  200. headerBuffer += c;
  201. // Uncomment the lines below to see the data coming into the buffer
  202. // if (c < 16)
  203. // SerialMon.print('0');
  204. // SerialMon.print(c, HEX);
  205. // SerialMon.print(' ');
  206. // if (isprint(c))
  207. // SerialMon.print((char) c);
  208. // else
  209. // SerialMon.print('*');
  210. // SerialMon.print(' ');
  211. // Let's exit and process if we find a new line
  212. if (headerBuffer.indexOf(F("\r\n")) >= 0)
  213. break;
  214. }
  215. }
  216. else {
  217. if (millis() - clientReadStartTime > clientReadTimeout) {
  218. // Time-out waiting for data from client
  219. SerialMon.println(F(">>> Client Timeout !"));
  220. break;
  221. }
  222. }
  223. // See if we have a new line.
  224. nlPos = headerBuffer.indexOf(F("\r\n"));
  225. if (nlPos > 0) {
  226. headerBuffer.toLowerCase();
  227. // Check if line contains content-length
  228. if (headerBuffer.startsWith(F("content-length:"))) {
  229. contentLength = headerBuffer.substring(headerBuffer.indexOf(':') + 1).toInt();
  230. // SerialMon.print(F("Got Content Length: ")); // uncomment for
  231. // SerialMon.println(contentLength); // confirmation
  232. }
  233. headerBuffer.remove(0, nlPos + 2); // remove the line
  234. }
  235. else if (nlPos == 0) {
  236. // if the new line is empty (i.e. "\r\n" is at the beginning of the line), we are done with the header.
  237. finishedHeader = true;
  238. }
  239. }
  240. // The two cases which are not managed properly are as follows:
  241. // 1. The client doesn't provide data quickly enough to keep up with this loop.
  242. // 2. If the client data is segmented in the middle of the 'Content-Length: ' header,
  243. // then that header may be missed/damaged.
  244. //
  245. uint32_t readLength = 0;
  246. CRC32 crc;
  247. if (finishedHeader && contentLength == knownFileSize) {
  248. SerialMon.println(F("Reading response data"));
  249. clientReadStartTime = millis();
  250. printPercent(readLength, contentLength);
  251. while (readLength < contentLength && client.connected() && millis() - clientReadStartTime < clientReadTimeout) {
  252. while (client.available()) {
  253. uint8_t c = client.read();
  254. //SerialMon.print((char)c); // Uncomment this to show data
  255. crc.update(c);
  256. readLength++;
  257. if (readLength % (contentLength / 13) == 0) {
  258. printPercent(readLength, contentLength);
  259. }
  260. clientReadStartTime = millis();
  261. }
  262. }
  263. printPercent(readLength, contentLength);
  264. }
  265. timeElapsed = millis() - timeElapsed;
  266. SerialMon.println();
  267. // Shutdown
  268. client.stop();
  269. SerialMon.println(F("Server disconnected"));
  270. #if TINY_GSM_USE_WIFI
  271. modem.networkDisconnect();
  272. SerialMon.println(F("WiFi disconnected"));
  273. #endif
  274. #if TINY_GSM_USE_GPRS
  275. modem.gprsDisconnect();
  276. SerialMon.println(F("GPRS disconnected"));
  277. #endif
  278. float duration = float(timeElapsed) / 1000;
  279. SerialMon.println();
  280. SerialMon.print("Content-Length: "); SerialMon.println(contentLength);
  281. SerialMon.print("Actually read: "); SerialMon.println(readLength);
  282. SerialMon.print("Calc. CRC32: 0x"); SerialMon.println(crc.finalize(), HEX);
  283. SerialMon.print("Known CRC32: 0x"); SerialMon.println(knownCRC32, HEX);
  284. SerialMon.print("Duration: "); SerialMon.print(duration); SerialMon.println("s");
  285. // Do nothing forevermore
  286. while (true) {
  287. delay(1000);
  288. }
  289. }