Advertisement
pleasedontcode

"Activate GNSS" rev_66

Jun 24th, 2024
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Activate GNSS"
  13.     - Source Code compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-06-24 21:35:54
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Use TinyGSM library to initialize modem with APN */
  21.     /* "airtelgprs.com", establish GSM connection, enable */
  22.     /* GNSS module for location data capture, and display */
  23.     /* all activity logs on Serial Monitor. */
  24. /****** SYSTEM REQUIREMENT 2 *****/
  25.     /* add #define TINY_GSM_MODEM_SIM7600 */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28.  
  29. /********* User code review feedback **********
  30. #### Feedback 1 ####
  31. - do nothing
  32. ********* User code review feedback **********/
  33. /****** DEFINITION OF LIBRARIES *****/
  34. #define TINY_GSM_MODEM_SIM7600  // Define GSM modem model before including the library
  35. #include <TinyGsmClient.h>
  36.  
  37. /****** FUNCTION PROTOTYPES *****/
  38. void setup(void);
  39. void loop(void);
  40.  
  41. /***** DEFINITION OF Hardware Serial *****/
  42. #define SerialMon Serial
  43. #define MODEM_RST 5
  44. #define MODEM_PWRKEY 4
  45. #define MODEM_POWER_ON 23
  46. #define MODEM_TX 27
  47. #define MODEM_RX 26
  48. #define I2C_SDA 21
  49. #define I2C_SCL 22
  50.  
  51. /***** SYSTEM REQUIREMENT 1 *****/
  52. #define TINY_GSM_DEBUG SerialMon
  53.  
  54. // Your GPRS credentials
  55. const char apn[] = "airtelgprs.com";
  56. const char gprsUser[] = "";
  57. const char gprsPass[] = "";
  58.  
  59. // Initialize the TinyGSM modem instance
  60. TinyGsm modem(Serial2);
  61. TinyGsmClient client(modem);
  62.  
  63. void setup(void)
  64. {
  65.     // Set console baud rate
  66.     SerialMon.begin(115200);
  67.     delay(10);
  68.  
  69.     // Set GSM module baud rate
  70.     Serial2.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
  71.     delay(3000);
  72.  
  73.     // Restart takes quite some time
  74.     // To skip it, call init() instead of restart()
  75.     SerialMon.println("Initializing modem...");
  76.     modem.restart();
  77.  
  78.     // Display modem info
  79.     String modemInfo = modem.getModemInfo();
  80.     SerialMon.print("Modem Info: ");
  81.     SerialMon.println(modemInfo);
  82.  
  83.     // Unlock your SIM card with a PIN if needed
  84.     // modem.simUnlock("1234");
  85.  
  86.     // Connect to the GPRS network
  87.     SerialMon.print("Connecting to APN: ");
  88.     SerialMon.println(apn);
  89.     if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  90.         SerialMon.println("Failed to connect to GPRS");
  91.         while (true);
  92.     }
  93.     SerialMon.println("GPRS connected");
  94.  
  95.     // Enable GNSS
  96.     SerialMon.println("Enabling GNSS...");
  97.     modem.enableGPS();
  98. }
  99.  
  100. void loop(void)
  101. {
  102.     // Check if GNSS is enabled and get location data
  103.     float lat, lon;
  104.     if (modem.getGPS(&lat, &lon)) {
  105.         SerialMon.print("Latitude: ");
  106.         SerialMon.println(lat, 6);
  107.         SerialMon.print("Longitude: ");
  108.         SerialMon.println(lon, 6);
  109.     } else {
  110.         SerialMon.println("Failed to get GPS data or GNSS is not enabled");
  111.     }
  112.  
  113.     delay(10000); // Wait for 10 seconds before the next loop
  114. }
  115.  
  116. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement