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.

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