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