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.

193 lines
4.5 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
6 years ago
8 years ago
7 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
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_SIM900
  13. // #define TINY_GSM_MODEM_SIM808
  14. // #define TINY_GSM_MODEM_SIM868
  15. // #define TINY_GSM_MODEM_UBLOX
  16. // #define TINY_GSM_MODEM_M95
  17. // #define TINY_GSM_MODEM_BG96
  18. // #define TINY_GSM_MODEM_A6
  19. // #define TINY_GSM_MODEM_A7
  20. // #define TINY_GSM_MODEM_M590
  21. // #define TINY_GSM_MODEM_MC60
  22. // #define TINY_GSM_MODEM_MC60E
  23. // #define TINY_GSM_MODEM_ESP8266
  24. // #define TINY_GSM_MODEM_XBEE
  25. // Increase RX buffer if needed
  26. //#define TINY_GSM_RX_BUFFER 512
  27. // See the debugging, if wanted
  28. //#define TINY_GSM_DEBUG Serial
  29. //#define LOGGING
  30. // Add a reception delay, if needed
  31. //#define TINY_GSM_YIELD() { delay(1); }
  32. #include <TinyGsmClient.h>
  33. // Uncomment this if you want to see all AT commands
  34. //#define DUMP_AT_COMMANDS
  35. // Uncomment this if you want to use SSL
  36. //#define USE_SSL
  37. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  38. #define SerialMon Serial
  39. // Use Hardware Serial on Mega, Leonardo, Micro
  40. #define SerialAT Serial1
  41. // or Software Serial on Uno, Nano
  42. //#include <SoftwareSerial.h>
  43. //SoftwareSerial SerialAT(2, 3); // RX, TX
  44. // Your GPRS credentials
  45. // Leave empty, if missing user or pass
  46. const char apn[] = "YourAPN";
  47. const char gprsUser[] = "";
  48. const char gprsPass[] = "";
  49. const char wifiSSID[] = "YourSSID";
  50. const char wifiPass[] = "SSIDpw";
  51. // Server details
  52. const char server[] = "vsh.pp.ua";
  53. const char resource[] = "/TinyGSM/logo.txt";
  54. #ifdef DUMP_AT_COMMANDS
  55. #include <StreamDebugger.h>
  56. StreamDebugger debugger(SerialAT, SerialMon);
  57. TinyGsm modem(debugger);
  58. #else
  59. TinyGsm modem(SerialAT);
  60. #endif
  61. #ifdef USE_SSL
  62. TinyGsmClientSecure client(modem);
  63. const int port = 443;
  64. #else
  65. TinyGsmClient client(modem);
  66. const int port = 80;
  67. #endif
  68. void setup() {
  69. // Set console baud rate
  70. SerialMon.begin(115200);
  71. delay(10);
  72. SerialMon.println(F("Wait..."));
  73. pinMode(23, OUTPUT);
  74. digitalWrite(23, LOW);
  75. // Set GSM module baud rate
  76. SerialAT.begin(9600);
  77. delay(3000);
  78. // Restart takes quite some time
  79. // To skip it, call init() instead of restart()
  80. SerialMon.println(F("Initializing modem..."));
  81. modem.restart();
  82. String modemInfo = modem.getModemInfo();
  83. SerialMon.print(F("Modem: "));
  84. SerialMon.println(modemInfo);
  85. // Unlock your SIM card with a PIN
  86. //modem.simUnlock("1234");
  87. }
  88. void loop() {
  89. if (modem.hasWifi()) {
  90. SerialMon.print(F("Setting SSID/password..."));
  91. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  92. SerialMon.println(" fail");
  93. delay(10000);
  94. return;
  95. }
  96. SerialMon.println(" OK");
  97. }
  98. else if (modem.getModemName().indexOf("XBee") >= 0) {
  99. SerialMon.print(F("Setting APN"));
  100. if (!modem.gprsConnect(apn)) {
  101. SerialMon.println(" fail");
  102. delay(10000);
  103. return;
  104. }
  105. SerialMon.println(" OK");
  106. }
  107. SerialMon.print(F("Waiting for network..."));
  108. if (!modem.waitForNetwork()) {
  109. SerialMon.println(" fail");
  110. delay(10000);
  111. return;
  112. }
  113. SerialMon.println(" OK");
  114. if (modem.hasGPRS()) {
  115. SerialMon.print(F("Connecting to "));
  116. SerialMon.print(apn);
  117. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  118. SerialMon.println(" fail");
  119. delay(10000);
  120. return;
  121. }
  122. SerialMon.println(" OK");
  123. }
  124. SerialMon.print(F("Connecting to "));
  125. SerialMon.print(server);
  126. if (!client.connect(server, port)) {
  127. SerialMon.println(" fail");
  128. delay(10000);
  129. return;
  130. }
  131. SerialMon.println(" OK");
  132. // Make a HTTP GET request:
  133. client.print(String("GET ") + resource + " HTTP/1.0\r\n");
  134. client.print(String("Host: ") + server + "\r\n");
  135. client.print("Connection: close\r\n\r\n");
  136. unsigned long timeout = millis();
  137. while (client.connected() && millis() - timeout < 10000L) {
  138. // Print available data
  139. while (client.available()) {
  140. char c = client.read();
  141. SerialMon.print(c);
  142. timeout = millis();
  143. }
  144. }
  145. SerialMon.println();
  146. // Shutdown
  147. client.stop();
  148. SerialMon.println(F("Server disconnected"));
  149. if (modem.hasWifi()) {
  150. modem.networkDisconnect();
  151. SerialMon.println(F("WiFi disconnected"));
  152. }
  153. else {
  154. modem.gprsDisconnect();
  155. SerialMon.println(F("GPRS disconnected"));
  156. }
  157. // Do nothing forevermore
  158. while (true) {
  159. delay(1000);
  160. }
  161. }