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.

207 lines
5.0 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. // 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. #define TINY_GSM_USE_GPRS true
  43. #define TINY_GSM_USE_WIFI false
  44. // set GSM PIN, if any
  45. #define GSM_PIN ""
  46. // Your GPRS credentials
  47. // Leave empty, if missing user or pass
  48. const char apn[] = "YourAPN";
  49. const char gprsUser[] = "";
  50. const char gprsPass[] = "";
  51. const char wifiSSID[] = "YourSSID";
  52. const char wifiPass[] = "SSIDpw";
  53. // Server details
  54. const char server[] = "vsh.pp.ua";
  55. const char resource[] = "/TinyGSM/logo.txt";
  56. #include <TinyGsmClient.h>
  57. #ifdef DUMP_AT_COMMANDS
  58. #include <StreamDebugger.h>
  59. StreamDebugger debugger(SerialAT, SerialMon);
  60. TinyGsm modem(debugger);
  61. #else
  62. TinyGsm modem(SerialAT);
  63. #endif
  64. #ifdef USE_SSL
  65. TinyGsmClientSecure client(modem);
  66. const int port = 443;
  67. #else
  68. TinyGsmClient client(modem);
  69. const int port = 80;
  70. #endif
  71. void setup() {
  72. // Set console baud rate
  73. SerialMon.begin(115200);
  74. delay(10);
  75. // Set your reset, enable, power pins here
  76. pinMode(20, OUTPUT);
  77. digitalWrite(20, HIGH);
  78. pinMode(23, OUTPUT);
  79. digitalWrite(23, HIGH);
  80. SerialMon.println("Wait...");
  81. // Set GSM module baud rate
  82. // TinyGsmAutoBaud(SerialAT,GSM_AUTOBAUD_MIN,GSM_AUTOBAUD_MAX);
  83. SerialAT.begin(9600);
  84. delay(3000);
  85. // Restart takes quite some time
  86. // To skip it, call init() instead of restart()
  87. SerialMon.println("Initializing modem...");
  88. modem.restart();
  89. // modem.init();
  90. String modemInfo = modem.getModemInfo();
  91. SerialMon.print("Modem: ");
  92. SerialMon.println(modemInfo);
  93. #if TINY_GSM_USE_GPRS
  94. // Unlock your SIM card with a PIN if needed
  95. if ( GSM_PIN && modem.getSimStatus() != 3 ) {
  96. modem.simUnlock(GSM_PIN);
  97. }
  98. #endif
  99. void loop() {
  100. #if TINY_GSM_USE_WIFI
  101. SerialMon.print(F("Setting SSID/password..."));
  102. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  103. SerialMon.println(" fail");
  104. delay(10000);
  105. return;
  106. }
  107. SerialMon.println(" OK");
  108. #endif
  109. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  110. // The XBee must run the gprsConnect function BEFORE waiting for network!
  111. modem.gprsConnect(apn, gprsUser, gprsPass);
  112. #endif
  113. SerialMon.print("Waiting for network...");
  114. if (!modem.waitForNetwork(240000L)) {
  115. SerialMon.println(" fail");
  116. delay(10000);
  117. return;
  118. }
  119. SerialMon.println(" OK");
  120. if (modem.isNetworkConnected()) {
  121. SerialMon.println("Network connected");
  122. }
  123. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_HAS_GPRS
  124. SerialMon.print(F("Connecting to "));
  125. SerialMon.print(apn);
  126. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  127. SerialMon.println(" fail");
  128. delay(10000);
  129. return;
  130. }
  131. SerialMon.println(" OK");
  132. #endif
  133. SerialMon.print("Connecting to ");
  134. SerialMon.println(server);
  135. if (!client.connect(server, port)) {
  136. SerialMon.println(" fail");
  137. delay(10000);
  138. return;
  139. }
  140. SerialMon.println(" OK");
  141. // Make a HTTP GET request:
  142. SerialMon.println("Performing HTTP GET request...");
  143. client.print(String("GET ") + resource + " HTTP/1.1\r\n");
  144. client.print(String("Host: ") + server + "\r\n");
  145. client.print("Connection: close\r\n\r\n");
  146. client.println();
  147. unsigned long timeout = millis();
  148. while (client.connected() && millis() - timeout < 10000L) {
  149. // Print available data
  150. while (client.available()) {
  151. char c = client.read();
  152. SerialMon.print(c);
  153. timeout = millis();
  154. }
  155. }
  156. SerialMon.println();
  157. // Shutdown
  158. client.stop();
  159. SerialMon.println(F("Server disconnected"));
  160. #if TINY_GSM_USE_WIFI
  161. modem.networkDisconnect();
  162. SerialMon.println(F("WiFi disconnected"));
  163. #endif
  164. #if TINY_GSM_USE_GPRS
  165. modem.gprsDisconnect();
  166. SerialMon.println(F("GPRS disconnected"));
  167. #endif
  168. // Do nothing forevermore
  169. while (true) {
  170. delay(1000);
  171. }
  172. }