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.

114 lines
2.6 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
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. // #define TINY_GSM_MODEM_XBEE
  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. 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. // Unlock your SIM card with a PIN
  46. //modem.simUnlock("1234");
  47. }
  48. void loop() {
  49. Serial.print(F("Waiting for network..."));
  50. if (!modem.waitForNetwork()) {
  51. Serial.println(" fail");
  52. delay(10000);
  53. return;
  54. }
  55. Serial.println(" OK");
  56. Serial.print(F("Connecting to "));
  57. Serial.print(apn);
  58. if (!modem.gprsConnect(apn, user, pass)) {
  59. Serial.println(" fail");
  60. delay(10000);
  61. return;
  62. }
  63. Serial.println(" OK");
  64. Serial.print(F("Connecting to "));
  65. Serial.print(server);
  66. if (!client.connect(server, port)) {
  67. Serial.println(" fail");
  68. delay(10000);
  69. return;
  70. }
  71. Serial.println(" OK");
  72. // Make a HTTP GET request:
  73. client.print(String("GET ") + resource + " HTTP/1.0\r\n");
  74. client.print(String("Host: ") + server + "\r\n");
  75. client.print("Connection: close\r\n\r\n");
  76. unsigned long timeout = millis();
  77. while (client.connected() && millis() - timeout < 10000L) {
  78. // Print available data
  79. while (client.available()) {
  80. char c = client.read();
  81. Serial.print(c);
  82. timeout = millis();
  83. }
  84. }
  85. Serial.println();
  86. client.stop();
  87. Serial.println("Server disconnected");
  88. modem.gprsDisconnect();
  89. Serial.println("GPRS disconnected");
  90. // Do nothing forevermore
  91. while (true) {
  92. delay(1000);
  93. }
  94. }