mikroavr

http_get_modem_meter

Feb 9th, 2021
104
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. pinMode(5, OUTPUT);
  93.  
  94. SerialAT.begin(115200, SERIAL_8N1, 17, 16);
  95. delay(6000);
  96.  
  97. // Restart takes quite some time
  98. // To skip it, call init() instead of restart()
  99. SerialMon.println("Initializing modem...");
  100. modem.restart();
  101. //modem.init();
  102. digitalWrite(5, LOW);
  103. digitalWrite(4, HIGH);delay(1000);
  104. digitalWrite(4, LOW);delay(1000);
  105. digitalWrite(4, HIGH);delay(1000);
  106. delay(5000);
  107. delay(5000);
  108.  
  109. String modemInfo = modem.getModemInfo();
  110. SerialMon.print("Modem Info: ");
  111. SerialMon.println(modemInfo);
  112.  
  113. #if TINY_GSM_USE_GPRS
  114. // Unlock your SIM card with a PIN if needed
  115. if ( GSM_PIN && modem.getSimStatus() != 3 ) {
  116. modem.simUnlock(GSM_PIN);
  117. }
  118. #endif
  119. }
  120.  
  121. void loop() {
  122.  
  123. #if TINY_GSM_USE_WIFI
  124. // Wifi connection parameters must be set before waiting for the network
  125. SerialMon.print(F("Setting SSID/password..."));
  126. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  127. SerialMon.println(" fail");
  128. delay(10000);
  129. return;
  130. }
  131. SerialMon.println(" success");
  132. #endif
  133.  
  134. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  135. // The XBee must run the gprsConnect function BEFORE waiting for network!
  136. modem.gprsConnect(apn, gprsUser, gprsPass);
  137. #endif
  138.  
  139. SerialMon.print("Waiting for network...");
  140. if (!modem.waitForNetwork()) {
  141. SerialMon.println(" fail");
  142. delay(10000);
  143. return;
  144. }
  145. SerialMon.println(" success");
  146.  
  147. if (modem.isNetworkConnected()) {
  148. SerialMon.println("Network connected");
  149. }
  150.  
  151. #if TINY_GSM_USE_GPRS
  152. // GPRS connection parameters are usually set after network registration
  153. SerialMon.print(F("Connecting to "));
  154. SerialMon.print(apn);
  155. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  156. SerialMon.println(" fail");
  157. delay(10000);
  158. return;
  159. }
  160. SerialMon.println(" success");
  161.  
  162. if (modem.isGprsConnected()) {
  163. SerialMon.println("GPRS connected");
  164. }
  165. #endif
  166.  
  167. SerialMon.print(F("Performing HTTP GET request... "));
  168. int err = http.get(resource);
  169. if (err != 0) {
  170. SerialMon.println(F("failed to connect"));
  171. delay(10000);
  172. return;
  173. }
  174.  
  175. int status = http.responseStatusCode();
  176. SerialMon.print(F("Response status code: "));
  177. SerialMon.println(status);
  178. if (!status) {
  179. delay(10000);
  180. return;
  181. }
  182.  
  183. SerialMon.println(F("Response Headers:"));
  184. while (http.headerAvailable()) {
  185. String headerName = http.readHeaderName();
  186. String headerValue = http.readHeaderValue();
  187. SerialMon.println(" " + headerName + " : " + headerValue);
  188. }
  189.  
  190. int length = http.contentLength();
  191. if (length >= 0) {
  192. SerialMon.print(F("Content length is: "));
  193. SerialMon.println(length);
  194. }
  195. if (http.isResponseChunked()) {
  196. SerialMon.println(F("The response is chunked"));
  197. }
  198.  
  199. String body = http.responseBody();
  200. SerialMon.println(F("Response:"));
  201. SerialMon.println(body);
  202.  
  203. SerialMon.print(F("Body length is: "));
  204. SerialMon.println(body.length());
  205.  
  206. // Shutdown
  207.  
  208. http.stop();
  209. SerialMon.println(F("Server disconnected"));
  210.  
  211. #if TINY_GSM_USE_WIFI
  212. modem.networkDisconnect();
  213. SerialMon.println(F("WiFi disconnected"));
  214. #endif
  215. #if TINY_GSM_USE_GPRS
  216. modem.gprsDisconnect();
  217. SerialMon.println(F("GPRS disconnected"));
  218. #endif
  219. delay(3000);
  220. }
RAW Paste Data