Advertisement
Zuhairy_Harry

Arduino ESP32 GPS and Ring

Jan 22nd, 2025
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.08 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <HTTPClient.h>
  3. #include <TinyGPSPlus.h>
  4. #include <HardwareSerial.h>
  5. #include <WiFiClientSecure.h>
  6. #include <UniversalTelegramBot.h>
  7.  
  8. // WiFi credentials
  9. const char* ssid = "hotspot saya"; // Replace with your WiFi SSID
  10. const char* password = "kingsoonkit"; // Replace with your WiFi password
  11.  
  12. // Define RX2 and TX2 pins for GPS
  13. #define RX2_PIN 16  // GPIO16 is RX2
  14. #define TX2_PIN 17  // GPIO17 is TX2
  15.  
  16. // Create a TinyGPS++ object
  17. TinyGPSPlus gps;
  18.  
  19. // Create a hardware serial object for UART2
  20. HardwareSerial gpsSerial(2);
  21.  
  22. // Telegram Bot Token (from BotFather)
  23. const char* BOT_TOKEN = "8051448663:AAG1ToDwVeXPaPi7nrCLb2WufkxLGnK7JVU";
  24.  
  25. // Chat ID (your Telegram user ID)
  26. const char* CHAT_ID = "-4738772667"; // Replace with your chat ID
  27.  
  28. // Create WiFi and Telegram bot objects
  29. WiFiClientSecure client;
  30. UniversalTelegramBot bot(BOT_TOKEN, client);
  31.  
  32. // API endpoint for location and GPS updates
  33. const char* serverURL = "http://192.168.121.225/e-bike/save_location.php";
  34. const char* serverURLGPS = "https://etourmersing.com/Ebike_API/save_location.php";
  35.  
  36. // Ring API endpoint
  37. const char* apiUrl = "https://etourmersing.com/Ebike_API/ring.php?endpoint=command"; // Command endpoint for Ring functionality
  38.  
  39. // Global variables to store GPS data
  40. String latitude = "N/A";
  41. String longitude = "N/A";
  42. int count = 0;
  43.  
  44. void setup() {
  45.     // Initialize serial communication for debugging
  46.     Serial.begin(115200);
  47.     Serial.println("Initializing GPS module...");
  48.  
  49.     // Initialize UART2 for GPS communication
  50.     gpsSerial.begin(9600, SERIAL_8N1, RX2_PIN, TX2_PIN);
  51.  
  52.     // Connect to WiFi
  53.     Serial.print("Connecting to WiFi...");
  54.     WiFi.begin(ssid, password);
  55.     while (WiFi.status() != WL_CONNECTED) {
  56.         delay(1000);
  57.         Serial.print(".");
  58.     }
  59.     Serial.println("\nConnected!");
  60.     Serial.println("IP Address: " + WiFi.localIP().toString());
  61. }
  62.  
  63. void loop() {
  64.     // Handle GPS updates
  65.     while (gpsSerial.available() > 0) {
  66.         char c = gpsSerial.read();
  67.         if (gps.encode(c)) { // Feed the GPS data to TinyGPS++
  68.             if (gps.location.isValid()) {
  69.                 latitude = String(gps.location.lat(), 6);
  70.                 longitude = String(gps.location.lng(), 6);
  71.  
  72.                 // Display the location in the Serial Monitor
  73.                 Serial.print("Latitude: ");
  74.                 Serial.print(latitude);
  75.                 Serial.print(" Longitude: ");
  76.                 Serial.println(longitude);
  77.  
  78.                 String message = "GPS Connected!! Latitude = " + latitude + " Longitude = " + longitude;
  79.                 bot.sendMessage(CHAT_ID, message, "");
  80.  
  81.                 // Send data to server
  82.                 sendToServer(latitude, longitude);
  83.                 testArduinoConnection(latitude, longitude, "Riding");
  84.             } else {
  85.                 Serial.println("Waiting for GPS signal...");
  86.                 client.setInsecure();
  87.                 String message = "GPS Signal...";
  88.                 bot.sendMessage(CHAT_ID, message, "");
  89.             }
  90.         }
  91.  
  92.         count = count + 1;
  93.         if (count > 2000) {
  94.             count = 0;
  95.         }
  96.     }
  97.  
  98.     if (gpsSerial.available() == 0) {
  99.         Serial.println("No GPS");
  100.         String message = "GPS still not connected!";
  101.         bot.sendMessage(CHAT_ID, message, "");
  102.     }
  103.  
  104.     // Poll the API for Ring command
  105.     pollRingCommand();
  106.  
  107.     delay(5000); // Delay to control loop frequency
  108. }
  109.  
  110. void sendToServer(String lat, String lng) {
  111.     if (WiFi.status() == WL_CONNECTED) {
  112.         HTTPClient http;
  113.  
  114.         http.begin(serverURL);
  115.         http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  116.         String payload = "bike_id=B25001&latitude=" + lat + "&longitude=" + lng;
  117.  
  118.         int httpResponseCode = http.POST(payload);
  119.  
  120.         if (httpResponseCode > 0) {
  121.             String response = http.getString();
  122.             Serial.println("Location Response: " + response);
  123.             String message = "Location sent!";
  124.             bot.sendMessage(CHAT_ID, message, "");
  125.         } else {
  126.             Serial.println("Error sending Location data: " + String(httpResponseCode));
  127.             String message = "Location not sent to database!";
  128.             bot.sendMessage(CHAT_ID, message, "");
  129.         }
  130.  
  131.         http.end();
  132.     } else {
  133.         Serial.println("WiFi not connected!");
  134.     }
  135. }
  136.  
  137. void testArduinoConnection(String lat, String lng, String status) {
  138.     if (WiFi.status() == WL_CONNECTED) {
  139.         HTTPClient http;
  140.  
  141.         http.begin(serverURLGPS);
  142.         http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  143.         String payload = "bike_id=B25001&latitude=" + lat + "&longitude=" + lng;
  144.  
  145.         int httpResponseCode = http.POST(payload);
  146.  
  147.         if (httpResponseCode > 0) {
  148.             String response = http.getString();
  149.             Serial.println("Server Response: " + response);
  150.             String message = "Location sent to hosting!";
  151.             bot.sendMessage(CHAT_ID, message, "");
  152.         } else {
  153.             Serial.println("Error sending data: " + String(httpResponseCode));
  154.             String message = "Failed to connect hosting";
  155.             bot.sendMessage(CHAT_ID, message, "");
  156.         }
  157.  
  158.         http.end();
  159.     } else {
  160.         Serial.println("WiFi not connected!");
  161.     }
  162. }
  163.  
  164. void pollRingCommand() {
  165.     if (WiFi.status() == WL_CONNECTED) {
  166.         HTTPClient http;
  167.  
  168.         http.begin(apiUrl);
  169.         int httpResponseCode = http.GET();
  170.  
  171.         if (httpResponseCode == HTTP_CODE_OK) {
  172.             String payload = http.getString();
  173.             Serial.println("Command received: " + payload);
  174.  
  175.             // Trigger the buzzer if the "ring" command is received
  176.             if (payload.indexOf("ring") != -1) {
  177.                 Serial.println("Bike is ringing!");
  178.                 // Add buzzer code here if needed
  179.             }
  180.         } else {
  181.             Serial.println("Failed to fetch command. HTTP code: " + String(httpResponseCode));
  182.         }
  183.  
  184.         http.end();
  185.     } else {
  186.         Serial.println("WiFi not connected!");
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement