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.

110 lines
2.5 KiB

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