Advertisement
Zuhairy_Harry

Arduino ESP8266 Ring

Jan 22nd, 2025
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClientSecure.h>
  3. #include <ESP8266HTTPClient.h>
  4.  
  5. #define BUZZER_PIN D2 // GPIO pin for the buzzer (low-level trigger)
  6.  
  7. // WiFi credentials
  8. const char* ssid = "Galaxy A34 5G A1EB";         // Replace with your WiFi SSID
  9. const char* password = "galaxy1234"; // Replace with your WiFi password
  10.  
  11. // API URL
  12. const char* apiUrl = "https://etourmersing.com/Ebike_API/ring.php?endpoint=command";
  13.  
  14. void setup() {
  15.   Serial.begin(115200);
  16.  
  17.   // Set up the buzzer pin
  18.   pinMode(BUZZER_PIN, OUTPUT);
  19.   digitalWrite(BUZZER_PIN, HIGH); // Keep the buzzer OFF initially (low-level trigger)
  20.  
  21.   // Connect to WiFi
  22.   Serial.println("Connecting to WiFi...");
  23.   WiFi.begin(ssid, password);
  24.   while (WiFi.status() != WL_CONNECTED) {
  25.     delay(1000);
  26.     Serial.print(".");
  27.   }
  28.   Serial.println("\nWiFi connected!");
  29.   Serial.print("IP Address: ");
  30.   Serial.println(WiFi.localIP()); // Print the ESP8266's IP address
  31. }
  32.  
  33. void loop() {
  34.   if (WiFi.status() == WL_CONNECTED) {
  35.     WiFiClientSecure client; // Secure client for HTTPS
  36.     client.setInsecure();    // Disable SSL certificate validation (for testing purposes)
  37.  
  38.     HTTPClient http; // Create an HTTPClient object
  39.  
  40.     Serial.println("Sending GET request to API...");
  41.     http.begin(client, apiUrl); // Connect to the API server
  42.  
  43.     int httpCode = http.GET(); // Send a GET request to the API
  44.  
  45.     if (httpCode == HTTP_CODE_OK) {
  46.       String payload = http.getString(); // Get the response from the server
  47.       Serial.println("Command received: " + payload);
  48.  
  49.       // Check if the command is "ring"
  50.       if (payload.indexOf("ring") != -1) {
  51.         ringBuzzer(); // Trigger the buzzer
  52.       }
  53.     } else {
  54.       Serial.println("Failed to fetch command. HTTP code: " + String(httpCode));
  55.     }
  56.  
  57.     http.end(); // End the HTTP connection
  58.   } else {
  59.     Serial.println("WiFi not connected. Retrying...");
  60.   }
  61.  
  62.   delay(1000); // Poll the server every 5 seconds
  63. }
  64.  
  65. void ringBuzzer() {
  66.   Serial.println("Buzzer is ringing!");
  67.   digitalWrite(BUZZER_PIN, LOW); // Turn the buzzer ON (low-level trigger)
  68.   delay(1000);                   // Keep it ON for 1 second
  69.   digitalWrite(BUZZER_PIN, HIGH); // Turn the buzzer OFF
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement