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.

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