#include #include #define POT_PIN D0 // Potentiometer connected to pin D0 // Define the structure for sending data typedef struct struct_message { int value; } struct_message; struct_message myData; // Define the peer address (MAC address of the receiver) uint8_t broadcastAddress[] = {0xD4, 0xF9, 0x8D, 0x00, 0xF9, 0x18}; // Replace with the receiver's MAC address // Callback function that gets called when data is sent void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { Serial.print("Last Packet Send Status: "); Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); } // Global variables int bpm = 60; // Default BPM unsigned long lastToggleTime = 0; unsigned long lastReadTime = 0; const unsigned long readInterval = 500; // Delay for reading the potentiometer (in milliseconds) void setup() { // Initialize Serial Monitor Serial.begin(115200); // Set device as a Wi-Fi Station WiFi.mode(WIFI_STA); Serial.println("WiFi mode set to STA"); // Init ESP-NOW if (esp_now_init() != ESP_OK) { Serial.println("Error initializing ESP-NOW"); return; } Serial.println("ESP-NOW initialized"); // Register the send callback esp_now_register_send_cb(OnDataSent); // Register peer esp_now_peer_info_t peerInfo; memcpy(peerInfo.peer_addr, broadcastAddress, 6); peerInfo.channel = 0; peerInfo.encrypt = false; // Add peer if (esp_now_add_peer(&peerInfo) != ESP_OK) { Serial.println("Failed to add peer"); return; } Serial.println("Peer added"); } void loop() { // Get the current time unsigned long currentTime = millis(); // Check if it's time to read the potentiometer and send data if (currentTime - lastReadTime >= readInterval) { // Read the analog value from the potentiometer int analogValue = analogRead(POT_PIN); // Pin D1 corresponds to ADC1 (GPIO1) // Map the analog value to the range 30 to 180 bpm = map(analogValue, 0, 4095, 30, 180); // Assuming a 12-bit ADC resolution // Print the values for debugging Serial.print("Analog Value: "); Serial.print(analogValue); Serial.print(" | Mapped Value: "); Serial.println(bpm); // Prepare data to send myData.value = bpm; // Send message via ESP-NOW esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData)); if (result == ESP_OK) { Serial.println("Sent with success"); } else { Serial.println("Error sending the data"); } // Update the last read time lastReadTime = currentTime; } }