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.

206 lines
4.8 KiB

6 years ago
8 years ago
6 years ago
6 years ago
8 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
8 years ago
7 years ago
8 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /**************************************************************
  2. *
  3. * This sketch connects to a website and downloads a page.
  4. * It can be used to perform HTTP/RESTful API calls.
  5. *
  6. * TinyGSM Getting Started guide:
  7. * https://tiny.cc/tinygsm-readme
  8. *
  9. **************************************************************/
  10. // Select your modem:
  11. #define TINY_GSM_MODEMs_SIM800
  12. // #define TINY_GSM_MODEM_SIM808
  13. // #define TINY_GSM_MODEM_SIM868
  14. // #define TINY_GSM_MODEM_SIM900
  15. // #define TINY_GSM_MODEM_SIM7000
  16. // #define TINY_GSM_MODEM_UBLOX
  17. // #define TINY_GSM_MODEM_SARAR4
  18. // #define TINY_GSM_MODEM_M95
  19. // #define TINY_GSM_MODEM_BG96
  20. // #define TINY_GSM_MODEM_A6
  21. // #define TINY_GSM_MODEM_A7
  22. // #define TINY_GSM_MODEM_M590
  23. // #define TINY_GSM_MODEM_MC60
  24. // #define TINY_GSM_MODEM_MC60E
  25. // #define TINY_GSM_MODEM_ESP8266
  26. // #define TINY_GSM_MODEM_XBEE
  27. // #define TINY_GSM_MODEM_SEQUANS_MONARCH
  28. // Increase RX buffer if needed
  29. // #define TINY_GSM_RX_BUFFER 512
  30. // Add a reception delay, if needed
  31. // #define TINY_GSM_YIELD() { delay(1); }
  32. // Uncomment this if you want to use SSL
  33. //#define USE_SSL
  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. // See all AT commands, if wanted
  43. //#define DUMP_AT_COMMANDS
  44. // See the debugging, if wanted
  45. // #define TINY_GSM_DEBUG SerialMon
  46. // Range to attempt to autobaud
  47. #define GSM_AUTOBAUD_MIN 9600
  48. #define GSM_AUTOBAUD_MAX 38400
  49. #define TINY_GSM_USE_GPRS true
  50. #define TINY_GSM_USE_WIFI false
  51. // set GSM PIN, if any
  52. #define GSM_PIN ""
  53. // Your GPRS credentials
  54. // Leave empty, if missing user or pass
  55. const char apn[] = "hologram";
  56. const char gprsUser[] = "";
  57. const char gprsPass[] = "";
  58. const char wifiSSID[] = "YourSSID";
  59. const char wifiPass[] = "SSIDpw";
  60. // Server details
  61. const char server[] = "vsh.pp.ua";
  62. const char resource[] = "/TinyGSM/logo.txt";
  63. #include <TinyGsmClient.h>
  64. #ifdef DUMP_AT_COMMANDS
  65. #include <StreamDebugger.h>
  66. StreamDebugger debugger(SerialAT, SerialMon);
  67. TinyGsm modem(debugger);
  68. #else
  69. TinyGsm modem(SerialAT);
  70. #endif
  71. #ifdef USE_SSL
  72. TinyGsmClientSecure client(modem);
  73. const int port = 443;
  74. #else
  75. TinyGsmClient client(modem);
  76. const int port = 80;
  77. #endif
  78. void setup() {
  79. // Set console baud rate
  80. SerialMon.begin(115200);
  81. delay(10);
  82. // Set your reset, enable, power pins here
  83. pinMode(20, OUTPUT);
  84. digitalWrite(20, HIGH);
  85. pinMode(23, OUTPUT);
  86. digitalWrite(23, HIGH);
  87. SerialMon.println("Wait...");
  88. // Set GSM module baud rate
  89. // TinyGsmAutoBaud(SerialAT,GSM_AUTOBAUD_MIN,GSM_AUTOBAUD_MAX);
  90. SerialAT.begin(9600);
  91. delay(3000);
  92. // Restart takes quite some time
  93. // To skip it, call init() instead of restart()
  94. SerialMon.println("Initializing modem...");
  95. modem.restart();
  96. String modemInfo = modem.getModemInfo();
  97. SerialMon.print("Modem: ");
  98. SerialMon.println(modemInfo);
  99. // Unlock your SIM card with a PIN
  100. //modem.simUnlock("1234");
  101. }
  102. void loop() {
  103. #if TINY_GSM_USE_WIFI
  104. SerialMon.print(F("Setting SSID/password..."));
  105. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  106. SerialMon.println(" fail");
  107. delay(10000);
  108. return;
  109. }
  110. SerialMon.println(" OK");
  111. #endif
  112. SerialMon.print("Waiting for network...");
  113. if (!modem.waitForNetwork()) {
  114. SerialMon.println(" fail");
  115. delay(10000);
  116. return;
  117. }
  118. SerialMon.println(" OK");
  119. if (modem.isNetworkConnected()) {
  120. SerialMon.print("Network connected");
  121. }
  122. #if TINY_GSM_USE_GPRS
  123. SerialMon.print(F("Connecting to "));
  124. SerialMon.print(apn);
  125. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  126. SerialMon.println(" fail");
  127. delay(10000);
  128. return;
  129. }
  130. SerialMon.println(" OK");
  131. #endif
  132. SerialMon.print("Connecting to ");
  133. SerialMon.print(server);
  134. if (!client.connect(server, port)) {
  135. SerialMon.println(" fail");
  136. delay(10000);
  137. return;
  138. }
  139. SerialMon.println(" OK");
  140. // Make a HTTP GET request:
  141. client.print(String("GET ") + resource + " HTTP/1.0\r\n");
  142. client.print(String("Host: ") + server + "\r\n");
  143. client.print("Connection: close\r\n\r\n");
  144. unsigned long timeout = millis();
  145. while (client.connected() && millis() - timeout < 10000L) {
  146. // Print available data
  147. while (client.available()) {
  148. char c = client.read();
  149. SerialMon.print(c);
  150. timeout = millis();
  151. }
  152. }
  153. SerialMon.println();
  154. // Shutdown
  155. client.stop();
  156. SerialMon.println(F("Server disconnected"));
  157. #if TINY_GSM_USE_WIFI
  158. modem.networkDisconnect();
  159. SerialMon.println(F("WiFi disconnected"));
  160. #endif
  161. #if TINY_GSM_USE_GPRS
  162. modem.gprsDisconnect();
  163. SerialMon.println(F("GPRS disconnected"));
  164. #endif
  165. // Do nothing forevermore
  166. while (true) {
  167. delay(1000);
  168. }
  169. }