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.

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