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.

221 lines
5.5 KiB

6 years ago
8 years ago
5 years ago
5 years ago
6 years ago
6 years ago
8 years ago
5 years ago
5 years ago
5 years ago
7 years ago
6 years ago
7 years ago
5 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
5 years ago
5 years ago
5 years ago
8 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_SIM5360
  17. // #define TINY_GSM_MODEM_UBLOX
  18. // #define TINY_GSM_MODEM_SARAR4
  19. // #define TINY_GSM_MODEM_M95
  20. // #define TINY_GSM_MODEM_BG96
  21. // #define TINY_GSM_MODEM_A6
  22. // #define TINY_GSM_MODEM_A7
  23. // #define TINY_GSM_MODEM_M590
  24. // #define TINY_GSM_MODEM_MC60
  25. // #define TINY_GSM_MODEM_MC60E
  26. // #define TINY_GSM_MODEM_ESP8266
  27. // #define TINY_GSM_MODEM_XBEE
  28. // #define TINY_GSM_MODEM_SEQUANS_MONARCH
  29. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  30. #define SerialMon Serial
  31. // Set serial for AT commands (to the module)
  32. // Use Hardware Serial on Mega, Leonardo, Micro
  33. #define SerialAT Serial1
  34. // or Software Serial on Uno, Nano
  35. //#include <SoftwareSerial.h>
  36. //SoftwareSerial SerialAT(2, 3); // RX, TX
  37. // Increase RX buffer to capture the entire response
  38. // Chips without internal buffering (A6/A7, ESP8266, M590)
  39. // need enough space in the buffer for the entire response
  40. // else data will be lost (and the http library will fail).
  41. #define TINY_GSM_RX_BUFFER 650
  42. // See all AT commands, if wanted
  43. //#define DUMP_AT_COMMANDS
  44. // Define the serial console for debug prints, if needed
  45. #define TINY_GSM_DEBUG SerialMon
  46. // Range to attempt to autobaud
  47. #define GSM_AUTOBAUD_MIN 9600
  48. #define GSM_AUTOBAUD_MAX 115200
  49. // Add a reception delay, if needed
  50. //#define TINY_GSM_YIELD() { delay(2); }
  51. // Uncomment this if you want to use SSL
  52. //#define USE_SSL
  53. #define TINY_GSM_USE_GPRS true
  54. #define TINY_GSM_USE_WIFI false
  55. // set GSM PIN, if any
  56. #define GSM_PIN ""
  57. // Your GPRS credentials
  58. // Leave empty, if missing user or pass
  59. const char apn[] = "YourAPN";
  60. const char gprsUser[] = "";
  61. const char gprsPass[] = "";
  62. const char wifiPass[] = "YourWiFiPass";
  63. // Server details
  64. const char server[] = "vsh.pp.ua";
  65. const char resource[] = "/TinyGSM/logo.txt";
  66. #include <TinyGsmClient.h>
  67. #ifdef DUMP_AT_COMMANDS
  68. #include <StreamDebugger.h>
  69. StreamDebugger debugger(SerialAT, SerialMon);
  70. TinyGsm modem(debugger);
  71. #else
  72. TinyGsm modem(SerialAT);
  73. #endif
  74. #ifdef USE_SSL
  75. TinyGsmClientSecure client(modem);
  76. const int port = 443;
  77. #else
  78. TinyGsmClient client(modem);
  79. const int port = 80;
  80. #endif
  81. void setup() {
  82. // Set console baud rate
  83. SerialMon.begin(115200);
  84. delay(10);
  85. // Set your reset, enable, power pins here
  86. pinMode(20, OUTPUT);
  87. digitalWrite(20, HIGH);
  88. pinMode(23, OUTPUT);
  89. digitalWrite(23, HIGH);
  90. SerialMon.println("Wait...");
  91. // Set GSM module baud rate
  92. // TinyGsmAutoBaud(SerialAT,GSM_AUTOBAUD_MIN,GSM_AUTOBAUD_MAX);
  93. SerialAT.begin(9600);
  94. delay(3000);
  95. // Restart takes quite some time
  96. // To skip it, call init() instead of restart()
  97. SerialMon.println("Initializing modem...");
  98. modem.restart();
  99. // modem.init();
  100. String modemInfo = modem.getModemInfo();
  101. SerialMon.print("Modem: ");
  102. SerialMon.println(modemInfo);
  103. #if TINY_GSM_USE_GPRS
  104. // Unlock your SIM card with a PIN if needed
  105. if ( GSM_PIN && modem.getSimStatus() != 3 ) {
  106. modem.simUnlock(GSM_PIN);
  107. }
  108. #endif
  109. }
  110. void loop() {
  111. #if defined TINY_GSM_USE_WIFI && defined TINY_GSM_MODEM_HAS_WIFI
  112. SerialMon.print(F("Setting SSID/password..."));
  113. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  114. SerialMon.println(" fail");
  115. delay(10000);
  116. return;
  117. }
  118. SerialMon.println(" success");
  119. #endif
  120. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  121. // The XBee must run the gprsConnect function BEFORE waiting for network!
  122. modem.gprsConnect(apn, gprsUser, gprsPass);
  123. #endif
  124. SerialMon.print("Waiting for network...");
  125. if (!modem.waitForNetwork()) {
  126. SerialMon.println(" fail");
  127. delay(10000);
  128. return;
  129. }
  130. SerialMon.println(" success");
  131. if (modem.isNetworkConnected()) {
  132. SerialMon.println("Network connected");
  133. }
  134. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_HAS_GPRS
  135. SerialMon.print(F("Connecting to "));
  136. SerialMon.print(apn);
  137. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  138. SerialMon.println(" fail");
  139. delay(10000);
  140. return;
  141. }
  142. SerialMon.println(" success");
  143. #endif
  144. SerialMon.print("Connecting to ");
  145. SerialMon.println(server);
  146. if (!client.connect(server, port)) {
  147. SerialMon.println(" fail");
  148. delay(10000);
  149. return;
  150. }
  151. SerialMon.println(" success");
  152. // Make a HTTP GET request:
  153. SerialMon.println("Performing HTTP GET request...");
  154. client.print(String("GET ") + resource + " HTTP/1.1\r\n");
  155. client.print(String("Host: ") + server + "\r\n");
  156. client.print("Connection: close\r\n\r\n");
  157. client.println();
  158. unsigned long timeout = millis();
  159. while (client.connected() && millis() - timeout < 10000L) {
  160. // Print available data
  161. while (client.available()) {
  162. char c = client.read();
  163. SerialMon.print(c);
  164. timeout = millis();
  165. }
  166. }
  167. SerialMon.println();
  168. // Shutdown
  169. client.stop();
  170. SerialMon.println(F("Server disconnected"));
  171. #if TINY_GSM_USE_WIFI
  172. modem.networkDisconnect();
  173. SerialMon.println(F("WiFi disconnected"));
  174. #endif
  175. #if TINY_GSM_USE_GPRS
  176. modem.gprsDisconnect();
  177. SerialMon.println(F("GPRS disconnected"));
  178. #endif
  179. // Do nothing forevermore
  180. while (true) {
  181. delay(1000);
  182. }
  183. }