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.

134 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. // #define TINY_GSM_RX_BUFFER 512
  14. #include <TinyGsmClient.h>
  15. // Uncomment this if you want to see all AT commands
  16. // #define DUMP_AT_COMMANDS
  17. // Uncomment this if you want to use SSL
  18. // #define USE_SSL
  19. // Set serial for debug console (to the Serial Monitor, speed 115200)
  20. #define SerialMon Serial
  21. // We'll be using SerialSystem in Passthrough mode
  22. #define SerialAT SerialSystem
  23. // Your GPRS credentials
  24. // Leave empty, if missing user or pass
  25. const char apn[] = "YourAPN";
  26. const char user[] = "";
  27. const char pass[] = "";
  28. // Server details
  29. const char server[] = "vsh.pp.ua";
  30. const char resource[] = "/TinyGSM/logo.txt";
  31. #ifdef DUMP_AT_COMMANDS
  32. #include <StreamDebugger.h>
  33. StreamDebugger debugger(SerialAT, SerialMon);
  34. TinyGsm mdm(debugger);
  35. #else
  36. TinyGsm mdm(SerialAT);
  37. #endif
  38. #ifdef USE_SSL
  39. TinyGsmClientSecure client(mdm);
  40. const int port = 443;
  41. #else
  42. TinyGsmClient client(mdm);
  43. const int port = 80;
  44. #endif
  45. void setup() {
  46. // Set console baud rate
  47. SerialMon.begin(115200);
  48. delay(10);
  49. // Set up Passthrough
  50. HologramCloud.enterPassthrough();
  51. delay(3000);
  52. // Restart takes quite some time
  53. // To skip it, call init() instead of restart()
  54. SerialMon.println(F("Initializing modem..."));
  55. mdm.restart();
  56. String modemInfo = mdm.getModemInfo();
  57. SerialMon.print(F("Modem: "));
  58. SerialMon.println(modemInfo);
  59. // Unlock your SIM card with a PIN
  60. //mdm.simUnlock("1234");
  61. }
  62. void loop() {
  63. SerialMon.print(F("Waiting for network..."));
  64. if (!mdm.waitForNetwork()) {
  65. SerialMon.println(" fail");
  66. delay(10000);
  67. return;
  68. }
  69. SerialMon.println(" success");
  70. SerialMon.print(F("Connecting to "));
  71. SerialMon.print(apn);
  72. if (!mdm.gprsConnect(apn, user, pass)) {
  73. SerialMon.println(" fail");
  74. delay(10000);
  75. return;
  76. }
  77. SerialMon.println(" success");
  78. SerialMon.print(F("Connecting to "));
  79. SerialMon.print(server);
  80. if (!client.connect(server, port)) {
  81. SerialMon.println(" fail");
  82. delay(10000);
  83. return;
  84. }
  85. SerialMon.println(" success");
  86. // Make a HTTP GET request:
  87. client.print(String("GET ") + resource + " HTTP/1.0\r\n");
  88. client.print(String("Host: ") + server + "\r\n");
  89. client.print("Connection: close\r\n\r\n");
  90. uint32_t timeout = millis();
  91. while (client.connected() && millis() - timeout < 10000L) {
  92. // Print available data
  93. while (client.available()) {
  94. char c = client.read();
  95. SerialMon.print(c);
  96. timeout = millis();
  97. }
  98. }
  99. SerialMon.println();
  100. // Shutdown
  101. client.stop();
  102. SerialMon.println(F("Server disconnected"));
  103. mdm.gprsDisconnect();
  104. SerialMon.println(F("GPRS disconnected"));
  105. // Do nothing forevermore
  106. while (true) {
  107. delay(1000);
  108. }
  109. }