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.

138 lines
3.2 KiB

8 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 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
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
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. * http://tiny.cc/tiny-gsm-readme
  8. *
  9. **************************************************************/
  10. // Select your modem:
  11. #define TINY_GSM_MODEM_SIM800
  12. // #define TINY_GSM_MODEM_SIM808
  13. // #define TINY_GSM_MODEM_SIM900
  14. // #define TINY_GSM_MODEM_A6
  15. // #define TINY_GSM_MODEM_A7
  16. // #define TINY_GSM_MODEM_M590
  17. // #define TINY_GSM_MODEM_ESP8266
  18. // Increase RX buffer if needed
  19. //#define TINY_GSM_RX_BUFFER 512
  20. #include <TinyGsmClient.h>
  21. // Uncomment this if you want to see all AT commands
  22. //#define DUMP_AT_COMMANDS
  23. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  24. #define SerialMon Serial
  25. // Use Hardware Serial on Mega, Leonardo, Micro
  26. #define SerialAT Serial1
  27. // or Software Serial on Uno, Nano
  28. //#include <SoftwareSerial.h>
  29. //SoftwareSerial SerialAT(2, 3); // RX, TX
  30. // Your GPRS credentials
  31. // Leave empty, if missing user or pass
  32. const char apn[] = "YourAPN";
  33. const char user[] = "";
  34. const char pass[] = "";
  35. // Server details
  36. const char server[] = "vsh.pp.ua";
  37. const char resource[] = "/TinyGSM/logo.txt";
  38. const int port = 80;
  39. #ifdef DUMP_AT_COMMANDS
  40. #include <StreamDebugger.h>
  41. StreamDebugger debugger(SerialAT, SerialMon);
  42. TinyGsm modem(debugger);
  43. #else
  44. TinyGsm modem(SerialAT);
  45. #endif
  46. TinyGsmClient client(modem);
  47. void setup() {
  48. // Set console baud rate
  49. SerialMon.begin(115200);
  50. delay(10);
  51. // Set GSM module baud rate
  52. SerialAT.begin(115200);
  53. delay(3000);
  54. // Restart takes quite some time
  55. // To skip it, call init() instead of restart()
  56. SerialMon.println(F("Initializing modem..."));
  57. modem.restart();
  58. String modemInfo = modem.getModemInfo();
  59. SerialMon.print(F("Modem: "));
  60. SerialMon.println(modemInfo);
  61. // Unlock your SIM card with a PIN
  62. //modem.simUnlock("1234");
  63. }
  64. void loop() {
  65. SerialMon.print(F("Waiting for network..."));
  66. if (!modem.waitForNetwork()) {
  67. SerialMon.println(" fail");
  68. delay(10000);
  69. return;
  70. }
  71. SerialMon.println(" OK");
  72. SerialMon.print(F("Connecting to "));
  73. SerialMon.print(apn);
  74. if (!modem.gprsConnect(apn, user, pass)) {
  75. SerialMon.println(" fail");
  76. delay(10000);
  77. return;
  78. }
  79. SerialMon.println(" OK");
  80. SerialMon.print(F("Connecting to "));
  81. SerialMon.print(server);
  82. if (!client.connect(server, port)) {
  83. SerialMon.println(" fail");
  84. delay(10000);
  85. return;
  86. }
  87. SerialMon.println(" OK");
  88. // Make a HTTP GET request:
  89. client.print(String("GET ") + resource + " HTTP/1.0\r\n");
  90. client.print(String("Host: ") + server + "\r\n");
  91. client.print("Connection: close\r\n\r\n");
  92. unsigned long timeout = millis();
  93. while (client.connected() && millis() - timeout < 10000L) {
  94. // Print available data
  95. while (client.available()) {
  96. char c = client.read();
  97. SerialMon.print(c);
  98. timeout = millis();
  99. }
  100. }
  101. SerialMon.println();
  102. // Shutdown
  103. client.stop();
  104. SerialMon.println(F("Server disconnected"));
  105. modem.gprsDisconnect();
  106. SerialMon.println(F("GPRS disconnected"));
  107. // Do nothing forevermore
  108. while (true) {
  109. delay(1000);
  110. }
  111. }