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.

100 lines
2.2 KiB

  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. #include <TinyGsmClient.h>
  11. // Your GPRS credentials
  12. // Leave empty, if missing user or pass
  13. char apn[] = "YourAPN";
  14. char user[] = "";
  15. char pass[] = "";
  16. // Use Hardware Serial on Mega, Leonardo, Micro
  17. #define SerialAT Serial1
  18. // or Software Serial on Uno, Nano
  19. //#include <SoftwareSerial.h>
  20. //SoftwareSerial SerialAT(2, 3); // RX, TX
  21. TinyGsm modem(SerialAT);
  22. TinyGsmClient client(modem);
  23. char server[] = "cdn.rawgit.com";
  24. char resource[] = "/vshymanskyy/tinygsm/master/extras/logo.txt";
  25. void setup() {
  26. // Set console baud rate
  27. Serial.begin(115200);
  28. delay(10);
  29. // Set GSM module baud rate
  30. SerialAT.begin(115200);
  31. delay(3000);
  32. // Restart takes quite some time
  33. // You can skip it in many cases
  34. modem.restart();
  35. }
  36. void loop() {
  37. Serial.print("Waiting for network...");
  38. if (!modem.waitForNetwork()) {
  39. Serial.println(" fail");
  40. delay(10000);
  41. return;
  42. }
  43. Serial.println(" OK");
  44. Serial.print("Connecting to ");
  45. Serial.print(apn);
  46. if (!modem.gprsConnect(apn, user, pass)) {
  47. Serial.println(" fail");
  48. delay(10000);
  49. return;
  50. }
  51. Serial.println(" OK");
  52. Serial.print("Connecting to ");
  53. Serial.print(server);
  54. if (!client.connect(server, 80)) {
  55. Serial.println(" failed");
  56. delay(10000);
  57. return;
  58. }
  59. Serial.println(" OK");
  60. // Make a HTTP GET request:
  61. client.print(String("GET ") + resource + " HTTP/1.0\r\n");
  62. client.print(String("Host: ") + server + "\r\n");
  63. client.print("Connection: close\r\n\r\n");
  64. unsigned long timeout = millis();
  65. while (client.connected() && millis() - timeout < 10000L) {
  66. // Print available data
  67. while (client.available()) {
  68. char c = client.read();
  69. Serial.print(c);
  70. timeout = millis();
  71. }
  72. }
  73. Serial.println();
  74. client.stop();
  75. Serial.println("Server disconnected");
  76. modem.gprsDisconnect();
  77. Serial.println("GPRS disconnected");
  78. // Do nothing forevermore
  79. while (true) {
  80. delay(1000);
  81. }
  82. }