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.

136 lines
3.1 KiB

6 years ago
6 years ago
6 years ago
6 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. // Hologram Dash uses UBLOX U2 modems
  11. #define TINY_GSM_MODEM_UBLOX
  12. // Increase RX buffer if needed
  13. #if !defined(TINY_GSM_RX_BUFFER)
  14. #define TINY_GSM_RX_BUFFER 512
  15. #endif
  16. #include <TinyGsmClient.h>
  17. // Uncomment this if you want to see all AT commands
  18. // #define DUMP_AT_COMMANDS
  19. // Uncomment this if you want to use SSL
  20. // #define USE_SSL
  21. // Set serial for debug console (to the Serial Monitor, speed 115200)
  22. #define SerialMon Serial
  23. // We'll be using SerialSystem in Passthrough mode
  24. #define SerialAT SerialSystem
  25. // Your GPRS credentials
  26. // Leave empty, if missing user or pass
  27. const char apn[] = "YourAPN";
  28. const char user[] = "";
  29. const char pass[] = "";
  30. // Server details
  31. const char server[] = "vsh.pp.ua";
  32. const char resource[] = "/TinyGSM/logo.txt";
  33. #ifdef DUMP_AT_COMMANDS
  34. #include <StreamDebugger.h>
  35. StreamDebugger debugger(SerialAT, SerialMon);
  36. TinyGsm mdm(debugger);
  37. #else
  38. TinyGsm mdm(SerialAT);
  39. #endif
  40. #ifdef USE_SSL
  41. TinyGsmClientSecure client(mdm);
  42. const int port = 443;
  43. #else
  44. TinyGsmClient client(mdm);
  45. const int port = 80;
  46. #endif
  47. void setup() {
  48. // Set console baud rate
  49. SerialMon.begin(115200);
  50. delay(10);
  51. // Set up Passthrough
  52. HologramCloud.enterPassthrough();
  53. delay(6000);
  54. // Restart takes quite some time
  55. // To skip it, call init() instead of restart()
  56. SerialMon.println(F("Initializing modem..."));
  57. mdm.restart();
  58. String modemInfo = mdm.getModemInfo();
  59. SerialMon.print(F("Modem: "));
  60. SerialMon.println(modemInfo);
  61. // Unlock your SIM card with a PIN
  62. //mdm.simUnlock("1234");
  63. }
  64. void loop() {
  65. SerialMon.print(F("Waiting for network..."));
  66. if (!mdm.waitForNetwork()) {
  67. SerialMon.println(" fail");
  68. delay(10000);
  69. return;
  70. }
  71. SerialMon.println(" success");
  72. SerialMon.print(F("Connecting to "));
  73. SerialMon.print(apn);
  74. if (!mdm.gprsConnect(apn, user, pass)) {
  75. SerialMon.println(" fail");
  76. delay(10000);
  77. return;
  78. }
  79. SerialMon.println(" success");
  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(" success");
  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. uint32_t 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. mdm.gprsDisconnect();
  106. SerialMon.println(F("GPRS disconnected"));
  107. // Do nothing forevermore
  108. while (true) {
  109. delay(1000);
  110. }
  111. }