mikroavr

hhtp_client_modem_lte

Nov 12th, 2020
186
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define TINY_GSM_MODEM_SIM7600
  2.  
  3. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  4. #define SerialMon Serial
  5.  
  6. // Set serial for AT commands (to the module)
  7. // Use Hardware Serial on Mega, Leonardo, Micro
  8. #define SerialAT Serial1
  9.  
  10. #if !defined(TINY_GSM_RX_BUFFER)
  11. #define TINY_GSM_RX_BUFFER 650
  12. #endif
  13.  
  14. // See all AT commands, if wanted
  15. // #define DUMP_AT_COMMANDS
  16.  
  17. // Define the serial console for debug prints, if needed
  18. #define TINY_GSM_DEBUG SerialMon
  19. // #define LOGGING  // <- Logging is for the HTTP library
  20.  
  21. // Range to attempt to autobaud
  22. #define GSM_AUTOBAUD_MIN 9600
  23. #define GSM_AUTOBAUD_MAX 115200
  24.  
  25. // Add a reception delay - may be needed for a fast processor at a slow baud rate
  26. // #define TINY_GSM_YIELD() { delay(2); }
  27.  
  28. // Define how you're planning to connect to the internet
  29. #define TINY_GSM_USE_GPRS true
  30. #define TINY_GSM_USE_WIFI false
  31.  
  32. // set GSM PIN, if any
  33. #define GSM_PIN ""
  34.  
  35. // Your GPRS credentials, if any
  36. const char apn[]  = "Internet";
  37. const char gprsUser[] = "";
  38. const char gprsPass[] = "";
  39.  
  40. // Your WiFi connection credentials, if applicable
  41. const char wifiSSID[]  = "YourSSID";
  42. const char wifiPass[] = "YourWiFiPass";
  43.  
  44. // Server details
  45. const char server[] = "vsh.pp.ua";
  46. const char resource[] = "/TinyGSM/logo.txt";
  47. const int  port = 80;
  48.  
  49. #include <TinyGsmClient.h>
  50. #include <ArduinoHttpClient.h>
  51.  
  52. // Just in case someone defined the wrong thing..
  53. #if TINY_GSM_USE_GPRS && not defined TINY_GSM_MODEM_HAS_GPRS
  54. #undef TINY_GSM_USE_GPRS
  55. #undef TINY_GSM_USE_WIFI
  56. #define TINY_GSM_USE_GPRS false
  57. #define TINY_GSM_USE_WIFI true
  58. #endif
  59. #if TINY_GSM_USE_WIFI && not defined TINY_GSM_MODEM_HAS_WIFI
  60. #undef TINY_GSM_USE_GPRS
  61. #undef TINY_GSM_USE_WIFI
  62. #define TINY_GSM_USE_GPRS true
  63. #define TINY_GSM_USE_WIFI false
  64. #endif
  65.  
  66. #ifdef DUMP_AT_COMMANDS
  67. #include <StreamDebugger.h>
  68. StreamDebugger debugger(SerialAT, SerialMon);
  69. TinyGsm modem(debugger);
  70. #else
  71. TinyGsm modem(SerialAT);
  72. #endif
  73.  
  74. TinyGsmClient client(modem);
  75. HttpClient http(client, server, port);
  76.  
  77. void setup() {
  78.   // Set console baud rate
  79.   SerialMon.begin(115200);
  80.   delay(10);
  81.  
  82.   // !!!!!!!!!!!
  83.   // Set your reset, enable, power pins here
  84.   // !!!!!!!!!!!
  85.  
  86.   SerialMon.println("Wait...");
  87.  
  88.   // Set GSM module baud rate
  89.   //TinyGsmAutoBaud(SerialAT, GSM_AUTOBAUD_MIN, GSM_AUTOBAUD_MAX);
  90.   // SerialAT.begin(9600);
  91.   pinMode(4, OUTPUT);
  92.   SerialAT.begin(9600, SERIAL_8N1, 17, 16);
  93.   delay(6000);
  94.  
  95.   // Restart takes quite some time
  96.   // To skip it, call init() instead of restart()
  97.   SerialMon.println("Initializing modem...");
  98.   modem.restart();
  99.   //modem.init();
  100.   digitalWrite(4, HIGH);delay(1000);
  101.   digitalWrite(4, LOW);delay(1000);
  102.   digitalWrite(4, HIGH);delay(1000);
  103.   delay(5000);
  104.   delay(5000);
  105.  
  106.   String modemInfo = modem.getModemInfo();
  107.   SerialMon.print("Modem Info: ");
  108.   SerialMon.println(modemInfo);
  109.  
  110. #if TINY_GSM_USE_GPRS
  111.   // Unlock your SIM card with a PIN if needed
  112.   if ( GSM_PIN && modem.getSimStatus() != 3 ) {
  113.     modem.simUnlock(GSM_PIN);
  114.   }
  115. #endif
  116. }
  117.  
  118. void loop() {
  119.  
  120. #if TINY_GSM_USE_WIFI
  121.   // Wifi connection parameters must be set before waiting for the network
  122.   SerialMon.print(F("Setting SSID/password..."));
  123.   if (!modem.networkConnect(wifiSSID, wifiPass)) {
  124.     SerialMon.println(" fail");
  125.     delay(10000);
  126.     return;
  127.   }
  128.   SerialMon.println(" success");
  129. #endif
  130.  
  131. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  132.   // The XBee must run the gprsConnect function BEFORE waiting for network!
  133.   modem.gprsConnect(apn, gprsUser, gprsPass);
  134. #endif
  135.  
  136.   SerialMon.print("Waiting for network...");
  137.   if (!modem.waitForNetwork()) {
  138.     SerialMon.println(" fail");
  139.     delay(10000);
  140.     return;
  141.   }
  142.   SerialMon.println(" success");
  143.  
  144.   if (modem.isNetworkConnected()) {
  145.     SerialMon.println("Network connected");
  146.   }
  147.  
  148. #if TINY_GSM_USE_GPRS
  149.   // GPRS connection parameters are usually set after network registration
  150.   SerialMon.print(F("Connecting to "));
  151.   SerialMon.print(apn);
  152.   if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  153.     SerialMon.println(" fail");
  154.     delay(10000);
  155.     return;
  156.   }
  157.   SerialMon.println(" success");
  158.  
  159.   if (modem.isGprsConnected()) {
  160.     SerialMon.println("GPRS connected");
  161.   }
  162. #endif
  163.  
  164.   SerialMon.print(F("Performing HTTP GET request... "));
  165.   int err = http.get(resource);
  166.   if (err != 0) {
  167.     SerialMon.println(F("failed to connect"));
  168.     delay(10000);
  169.     return;
  170.   }
  171.  
  172.   int status = http.responseStatusCode();
  173.   SerialMon.print(F("Response status code: "));
  174.   SerialMon.println(status);
  175.   if (!status) {
  176.     delay(10000);
  177.     return;
  178.   }
  179.  
  180.   SerialMon.println(F("Response Headers:"));
  181.   while (http.headerAvailable()) {
  182.     String headerName = http.readHeaderName();
  183.     String headerValue = http.readHeaderValue();
  184.     SerialMon.println("    " + headerName + " : " + headerValue);
  185.   }
  186.  
  187.   int length = http.contentLength();
  188.   if (length >= 0) {
  189.     SerialMon.print(F("Content length is: "));
  190.     SerialMon.println(length);
  191.   }
  192.   if (http.isResponseChunked()) {
  193.     SerialMon.println(F("The response is chunked"));
  194.   }
  195.  
  196.   String body = http.responseBody();
  197.   SerialMon.println(F("Response:"));
  198.   SerialMon.println(body);
  199.  
  200.   SerialMon.print(F("Body length is: "));
  201.   SerialMon.println(body.length());
  202.  
  203.   // Shutdown
  204.  
  205.   http.stop();
  206.   SerialMon.println(F("Server disconnected"));
  207.  
  208. #if TINY_GSM_USE_WIFI
  209.   modem.networkDisconnect();
  210.   SerialMon.println(F("WiFi disconnected"));
  211. #endif
  212. #if TINY_GSM_USE_GPRS
  213.   modem.gprsDisconnect();
  214.   SerialMon.println(F("GPRS disconnected"));
  215. #endif
  216.   delay(3000);
  217. }
RAW Paste Data