#include #include #include #include #include // OLED display width and height #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 // Create display object Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); #define VOLTAGE_SENSOR_PIN A2 // ADC pin for voltage sensor #define CURRENT_SENSOR_PIN A0 // ADC pin for ACS712 current sensor #define REF_VOLTAGE 3.3 // ESP32-C3 operates on 3.3V #define ADC_RESOLUTION 4095 // 12-bit ADC (0-4095) #define SENSITIVITY 185 // Set this based on your ACS712 module: (5A = 185mV/A, 20A = 100mV/A, 30A = 66mV/A) #define VOLTAGE_DIVIDER_FACTOR 4.3 // Adjust based on calibration const int numSamples = 200; float zeroPoint = 0; // Replace with your WiFi credentials const char* ssid = "FABLAB"; const char* password = "12345678"; // Replace with your ThingSpeak Write API Key const char* apiKey = "9HO2SSQ4PUUDVWAY"; // ThingSpeak API URL const char* server = "http://api.thingspeak.com/update"; void setup() { Serial.begin(115200); if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // 0x3C is the I2C address Serial.println(F("SSD1306 allocation failed")); while (1); // Halt execution } display.clearDisplay(); display.setTextSize(2); display.setTextColor(SSD1306_WHITE); pinMode(VOLTAGE_SENSOR_PIN, INPUT); pinMode(CURRENT_SENSOR_PIN, INPUT); delay(500); // Calibrate Zero Point (Idle Voltage when NO Load) long sum = 0; for (int i = 0; i < numSamples; i++) { sum += analogReadMilliVolts(CURRENT_SENSOR_PIN); delay(5); } zeroPoint = sum / numSamples; Serial.print("Zero Point (No Load): "); Serial.print(zeroPoint); Serial.println(" mV"); WiFi.begin(ssid, password); Serial.print("Connecting to WiFi..."); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(100); } Serial.println("\nConnected to WiFi!"); } // Function to measure AC Current RMS correctly float readACCurrent() { float sumSquared = 0; for (int i = 0; i < numSamples; i++) { float voltage = analogReadMilliVolts(CURRENT_SENSOR_PIN); float current = (voltage - zeroPoint) / SENSITIVITY; sumSquared += current * current; delayMicroseconds(50); // Sampling rate improvement } return sqrt(sumSquared / numSamples); // RMS Current Calculation } // Function to measure Voltage float readVoltage() { int adcValue = analogRead(VOLTAGE_SENSOR_PIN); float voltage = (adcValue * REF_VOLTAGE) / ADC_RESOLUTION; return voltage * VOLTAGE_DIVIDER_FACTOR; // Adjust with divider factor } void loop() { if (WiFi.status() == WL_CONNECTED) { HTTPClient http; float voltage = readVoltage(); float current = readACCurrent(); float power = voltage * current; // Power Calculation: P = V * I display.clearDisplay(); display.setCursor(0, 0); display.print("volt="); display.setCursor(70, 0); display.print(voltage); display.setCursor(0, 18); display.print("Amp="); display.setCursor(60, 18); display.print(current); display.setCursor(0, 40); display.print("power="); display.setCursor(80, 40); display.print(power); display.display(); Serial.print("Voltage: "); Serial.print(voltage); Serial.print(" V | AC Current: "); Serial.print(current, 3); Serial.print(" A | Power: "); Serial.print(power, 3); Serial.println(" W"); // Generate random values float value1 = voltage; // Example: Temperature float value2 = current; // Example: Humidity float value3 =power; // Example: Light Intensity // Construct the request URL String url = String(server) + "?api_key=" + apiKey + "&field1=" + String(value1) + "&field2=" + String(value2) + "&field3=" + String(value3); Serial.println("Sending data to ThingSpeak..."); Serial.println(url); // Send data to ThingSpeak http.begin(url); int httpCode = http.GET(); if (httpCode > 0) { Serial.println("Data sent successfully!"); } else { Serial.println("Failed to send data."); } http.end(); } else { Serial.println("WiFi Disconnected!"); } delay(500); // ThingSpeak allows updates every 15 seconds }