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.

103 lines
2.3 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 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. #include <TinyGsmClient.h>
  11. // Your GPRS credentials
  12. // Leave empty, if missing user or pass
  13. const char apn[] = "YourAPN";
  14. const char user[] = "";
  15. const 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. const char server[] = "cdn.rawgit.com";
  24. const 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. // To skip it, call init() instead of restart()
  34. modem.restart();
  35. // Unlock your SIM card with a PIN
  36. //modem.simUnlock("1234");
  37. }
  38. void loop() {
  39. Serial.print("Waiting for network...");
  40. if (!modem.waitForNetwork()) {
  41. Serial.println(" fail");
  42. delay(10000);
  43. return;
  44. }
  45. Serial.println(" OK");
  46. Serial.print("Connecting to ");
  47. Serial.print(apn);
  48. if (!modem.gprsConnect(apn, user, pass)) {
  49. Serial.println(" fail");
  50. delay(10000);
  51. return;
  52. }
  53. Serial.println(" OK");
  54. Serial.print("Connecting to ");
  55. Serial.print(server);
  56. if (!client.connect(server, 80)) {
  57. Serial.println(" fail");
  58. delay(10000);
  59. return;
  60. }
  61. Serial.println(" OK");
  62. // Make a HTTP GET request:
  63. client.print(String("GET ") + resource + " HTTP/1.0\r\n");
  64. client.print(String("Host: ") + server + "\r\n");
  65. client.print("Connection: close\r\n\r\n");
  66. unsigned long timeout = millis();
  67. while (client.connected() && millis() - timeout < 10000L) {
  68. // Print available data
  69. while (client.available()) {
  70. char c = client.read();
  71. Serial.print(c);
  72. timeout = millis();
  73. }
  74. }
  75. Serial.println();
  76. client.stop();
  77. Serial.println("Server disconnected");
  78. modem.gprsDisconnect();
  79. Serial.println("GPRS disconnected");
  80. // Do nothing forevermore
  81. while (true) {
  82. delay(1000);
  83. }
  84. }