Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WiFiClientSecure.h>
- #include <ESP8266HTTPClient.h>
- #define BUZZER_PIN D2 // GPIO pin for the buzzer (low-level trigger)
- // WiFi credentials
- const char* ssid = "Galaxy A34 5G A1EB"; // Replace with your WiFi SSID
- const char* password = "galaxy1234"; // Replace with your WiFi password
- // API URL
- const char* apiUrl = "https://etourmersing.com/Ebike_API/ring.php?endpoint=command";
- void setup() {
- Serial.begin(115200);
- // Set up the buzzer pin
- pinMode(BUZZER_PIN, OUTPUT);
- digitalWrite(BUZZER_PIN, HIGH); // Keep the buzzer OFF initially (low-level trigger)
- // Connect to WiFi
- Serial.println("Connecting to WiFi...");
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.print(".");
- }
- Serial.println("\nWiFi connected!");
- Serial.print("IP Address: ");
- Serial.println(WiFi.localIP()); // Print the ESP8266's IP address
- }
- void loop() {
- if (WiFi.status() == WL_CONNECTED) {
- WiFiClientSecure client; // Secure client for HTTPS
- client.setInsecure(); // Disable SSL certificate validation (for testing purposes)
- HTTPClient http; // Create an HTTPClient object
- Serial.println("Sending GET request to API...");
- http.begin(client, apiUrl); // Connect to the API server
- int httpCode = http.GET(); // Send a GET request to the API
- if (httpCode == HTTP_CODE_OK) {
- String payload = http.getString(); // Get the response from the server
- Serial.println("Command received: " + payload);
- // Check if the command is "ring"
- if (payload.indexOf("ring") != -1) {
- ringBuzzer(); // Trigger the buzzer
- }
- } else {
- Serial.println("Failed to fetch command. HTTP code: " + String(httpCode));
- }
- http.end(); // End the HTTP connection
- } else {
- Serial.println("WiFi not connected. Retrying...");
- }
- delay(1000); // Poll the server every 5 seconds
- }
- void ringBuzzer() {
- Serial.println("Buzzer is ringing!");
- digitalWrite(BUZZER_PIN, LOW); // Turn the buzzer ON (low-level trigger)
- delay(1000); // Keep it ON for 1 second
- digitalWrite(BUZZER_PIN, HIGH); // Turn the buzzer OFF
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement