Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <HTTPClient.h>
- #include <TinyGPSPlus.h>
- #include <HardwareSerial.h>
- #include <WiFiClientSecure.h>
- #include <UniversalTelegramBot.h>
- // WiFi credentials
- const char* ssid = "hotspot saya"; // Replace with your WiFi SSID
- const char* password = "kingsoonkit"; // Replace with your WiFi password
- // Define RX2 and TX2 pins for GPS
- #define RX2_PIN 16 // GPIO16 is RX2
- #define TX2_PIN 17 // GPIO17 is TX2
- // Create a TinyGPS++ object
- TinyGPSPlus gps;
- // Create a hardware serial object for UART2
- HardwareSerial gpsSerial(2);
- // Telegram Bot Token (from BotFather)
- const char* BOT_TOKEN = "8051448663:AAG1ToDwVeXPaPi7nrCLb2WufkxLGnK7JVU";
- // Chat ID (your Telegram user ID)
- const char* CHAT_ID = "-4738772667"; // Replace with your chat ID
- // Create WiFi and Telegram bot objects
- WiFiClientSecure client;
- UniversalTelegramBot bot(BOT_TOKEN, client);
- // API endpoint for location and GPS updates
- const char* serverURL = "http://192.168.121.225/e-bike/save_location.php";
- const char* serverURLGPS = "https://etourmersing.com/Ebike_API/save_location.php";
- // Ring API endpoint
- const char* apiUrl = "https://etourmersing.com/Ebike_API/ring.php?endpoint=command"; // Command endpoint for Ring functionality
- // Global variables to store GPS data
- String latitude = "N/A";
- String longitude = "N/A";
- int count = 0;
- void setup() {
- // Initialize serial communication for debugging
- Serial.begin(115200);
- Serial.println("Initializing GPS module...");
- // Initialize UART2 for GPS communication
- gpsSerial.begin(9600, SERIAL_8N1, RX2_PIN, TX2_PIN);
- // Connect to WiFi
- Serial.print("Connecting to WiFi...");
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.print(".");
- }
- Serial.println("\nConnected!");
- Serial.println("IP Address: " + WiFi.localIP().toString());
- }
- void loop() {
- // Handle GPS updates
- while (gpsSerial.available() > 0) {
- char c = gpsSerial.read();
- if (gps.encode(c)) { // Feed the GPS data to TinyGPS++
- if (gps.location.isValid()) {
- latitude = String(gps.location.lat(), 6);
- longitude = String(gps.location.lng(), 6);
- // Display the location in the Serial Monitor
- Serial.print("Latitude: ");
- Serial.print(latitude);
- Serial.print(" Longitude: ");
- Serial.println(longitude);
- String message = "GPS Connected!! Latitude = " + latitude + " Longitude = " + longitude;
- bot.sendMessage(CHAT_ID, message, "");
- // Send data to server
- sendToServer(latitude, longitude);
- testArduinoConnection(latitude, longitude, "Riding");
- } else {
- Serial.println("Waiting for GPS signal...");
- client.setInsecure();
- String message = "GPS Signal...";
- bot.sendMessage(CHAT_ID, message, "");
- }
- }
- count = count + 1;
- if (count > 2000) {
- count = 0;
- }
- }
- if (gpsSerial.available() == 0) {
- Serial.println("No GPS");
- String message = "GPS still not connected!";
- bot.sendMessage(CHAT_ID, message, "");
- }
- // Poll the API for Ring command
- pollRingCommand();
- delay(5000); // Delay to control loop frequency
- }
- void sendToServer(String lat, String lng) {
- if (WiFi.status() == WL_CONNECTED) {
- HTTPClient http;
- http.begin(serverURL);
- http.addHeader("Content-Type", "application/x-www-form-urlencoded");
- String payload = "bike_id=B25001&latitude=" + lat + "&longitude=" + lng;
- int httpResponseCode = http.POST(payload);
- if (httpResponseCode > 0) {
- String response = http.getString();
- Serial.println("Location Response: " + response);
- String message = "Location sent!";
- bot.sendMessage(CHAT_ID, message, "");
- } else {
- Serial.println("Error sending Location data: " + String(httpResponseCode));
- String message = "Location not sent to database!";
- bot.sendMessage(CHAT_ID, message, "");
- }
- http.end();
- } else {
- Serial.println("WiFi not connected!");
- }
- }
- void testArduinoConnection(String lat, String lng, String status) {
- if (WiFi.status() == WL_CONNECTED) {
- HTTPClient http;
- http.begin(serverURLGPS);
- http.addHeader("Content-Type", "application/x-www-form-urlencoded");
- String payload = "bike_id=B25001&latitude=" + lat + "&longitude=" + lng;
- int httpResponseCode = http.POST(payload);
- if (httpResponseCode > 0) {
- String response = http.getString();
- Serial.println("Server Response: " + response);
- String message = "Location sent to hosting!";
- bot.sendMessage(CHAT_ID, message, "");
- } else {
- Serial.println("Error sending data: " + String(httpResponseCode));
- String message = "Failed to connect hosting";
- bot.sendMessage(CHAT_ID, message, "");
- }
- http.end();
- } else {
- Serial.println("WiFi not connected!");
- }
- }
- void pollRingCommand() {
- if (WiFi.status() == WL_CONNECTED) {
- HTTPClient http;
- http.begin(apiUrl);
- int httpResponseCode = http.GET();
- if (httpResponseCode == HTTP_CODE_OK) {
- String payload = http.getString();
- Serial.println("Command received: " + payload);
- // Trigger the buzzer if the "ring" command is received
- if (payload.indexOf("ring") != -1) {
- Serial.println("Bike is ringing!");
- // Add buzzer code here if needed
- }
- } else {
- Serial.println("Failed to fetch command. HTTP code: " + String(httpResponseCode));
- }
- http.end();
- } else {
- Serial.println("WiFi not connected!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement