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.

231 lines
5.8 KiB

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