#include <WiFi.h> #include <esp_now.h> #define SENSOR_PIN 4 // ADC GPIO4 #define SENSITIVITY 185 // 185mV/A for 5A module (Change for your module) #define ADC_RESOLUTION 4096 // 12-bit ADC (ESP32) #define V_REF 3300 // ESP32 ADC Reference Voltage (mV) const int numSamples = 100; float zeroPoint; uint8_t receiverMAC[] = {0xD4, 0xF9, 0x8D, 0x04, 0x42, 0x64}; // Change to actual receiver MAC // Data structure to send typedef struct struct_message { float value; } struct_message; struct_message myData; // Callback when data is sent void onDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { Serial.print("Send Status: "); Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Success" : "Fail"); } void setup() { Serial.begin(115200); pinMode(SENSOR_PIN, INPUT); delay(500); // Measure idle voltage (zero current) long sum = 0; for (int i = 0; i < numSamples; i++) { sum += analogReadMilliVolts(SENSOR_PIN); delay(10); } zeroPoint = sum / numSamples; Serial.print("Zero Point (No Current): "); Serial.print(zeroPoint); Serial.println(" mV"); WiFi.mode(WIFI_STA); // Set ESP32 to Station mode // Initialize ESP-NOW if (esp_now_init() != ESP_OK) { Serial.println("ESP-NOW Init Failed!"); return; } esp_now_register_send_cb(onDataSent); // Register peer esp_now_peer_info_t peerInfo = {}; memcpy(peerInfo.peer_addr, receiverMAC, 6); peerInfo.channel = 0; peerInfo.encrypt = false; if (esp_now_add_peer(&peerInfo) != ESP_OK) { Serial.println("Failed to add peer"); return; } } float readACCurrent() { float sumSquared = 0; for (int i = 0; i < numSamples; i++) { float voltage = analogReadMilliVolts(SENSOR_PIN); float current = (voltage - zeroPoint) / SENSITIVITY; sumSquared += current * current; delay(1); } return sqrt(sumSquared / numSamples); } void loop() { float acCurrent = readACCurrent(); Serial.print("Current R MS: "); Serial.print(acCurrent, 3); Serial.println(" A"); myData.value =acCurrent; // Send data esp_err_t result = esp_now_send(receiverMAC, (uint8_t *)&myData, sizeof(myData)); if (result == ESP_OK) { Serial.println("Sent successfully"); } else { Serial.println("Error sending data"); } delay(200); // Send every 2 seconds }