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.

217 lines
5.2 KiB

6 years ago
8 years ago
5 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
5 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
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_MODEM_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. // See all AT commands, if wanted
  31. // #define DUMP_AT_COMMANDS
  32. // Define the serial console for debug prints, if needed
  33. #define TINY_GSM_DEBUG SerialMon
  34. // Range to attempt to autobaud
  35. #define GSM_AUTOBAUD_MIN 9600
  36. #define GSM_AUTOBAUD_MAX 115200
  37. // Add a reception delay, if needed
  38. // #define TINY_GSM_YIELD() { delay(1); }
  39. // Uncomment this if you want to use SSL
  40. //#define USE_SSL
  41. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  42. #define SerialMon Serial
  43. // Set serial for AT commands (to the module)
  44. // Use Hardware Serial on Mega, Leonardo, Micro
  45. #define SerialAT Serial1
  46. // or Software Serial on Uno, Nano
  47. //#include <SoftwareSerial.h>
  48. //SoftwareSerial SerialAT(2, 3); // RX, TX
  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[] = "YourAPN";
  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. // modem.init();
  97. String modemInfo = modem.getModemInfo();
  98. SerialMon.print("Modem: ");
  99. SerialMon.println(modemInfo);
  100. #if TINY_GSM_USE_GPRS
  101. // Unlock your SIM card with a PIN if needed
  102. if ( GSM_PIN && modem.getSimStatus() != 3 ) {
  103. modem.simUnlock(GSM_PIN);
  104. }
  105. #endif
  106. void loop() {
  107. #if TINY_GSM_USE_WIFI
  108. SerialMon.print(F("Setting SSID/password..."));
  109. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  110. SerialMon.println(" fail");
  111. delay(10000);
  112. return;
  113. }
  114. SerialMon.println(" OK");
  115. #endif
  116. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  117. // The XBee must run the gprsConnect function BEFORE waiting for network!
  118. modem.gprsConnect(apn, gprsUser, gprsPass);
  119. #endif
  120. SerialMon.print("Waiting for network...");
  121. if (!modem.waitForNetwork(240000L)) {
  122. SerialMon.println(" fail");
  123. delay(10000);
  124. return;
  125. }
  126. SerialMon.println(" OK");
  127. if (modem.isNetworkConnected()) {
  128. SerialMon.println("Network connected");
  129. }
  130. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_HAS_GPRS
  131. SerialMon.print(F("Connecting to "));
  132. SerialMon.print(apn);
  133. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  134. SerialMon.println(" fail");
  135. delay(10000);
  136. return;
  137. }
  138. SerialMon.println(" OK");
  139. #endif
  140. SerialMon.print("Connecting to ");
  141. SerialMon.println(server);
  142. if (!client.connect(server, port)) {
  143. SerialMon.println(" fail");
  144. delay(10000);
  145. return;
  146. }
  147. SerialMon.println(" OK");
  148. // Make a HTTP GET request:
  149. SerialMon.println("Performing HTTP GET request...");
  150. client.print(String("GET ") + resource + " HTTP/1.1\r\n");
  151. client.print(String("Host: ") + server + "\r\n");
  152. client.print("Connection: close\r\n\r\n");
  153. client.println();
  154. unsigned long timeout = millis();
  155. while (client.connected() && millis() - timeout < 10000L) {
  156. // Print available data
  157. while (client.available()) {
  158. char c = client.read();
  159. SerialMon.print(c);
  160. timeout = millis();
  161. }
  162. }
  163. SerialMon.println();
  164. // Shutdown
  165. client.stop();
  166. SerialMon.println(F("Server disconnected"));
  167. #if TINY_GSM_USE_WIFI
  168. modem.networkDisconnect();
  169. SerialMon.println(F("WiFi disconnected"));
  170. #endif
  171. #if TINY_GSM_USE_GPRS
  172. modem.gprsDisconnect();
  173. SerialMon.println(F("GPRS disconnected"));
  174. #endif
  175. // Do nothing forevermore
  176. while (true) {
  177. delay(1000);
  178. }
  179. }