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.

119 lines
2.7 KiB

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