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.

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