#include <WiFi.h>
#include <ESP32Firebase.h>

// WiFi and Firebase configuration
#define WIFI_SSID "Wokwi-GUEST"            // Your WiFi SSID
#define WIFI_PASSWORD ""                   // Your WiFi password (empty for Wokwi-GUEST)
#define FIREBASE_URL "https://gokart-iot-35f2c-default-rtdb.firebaseio.com/" // Firebase URL

Firebase firebase(FIREBASE_URL);

// Define analog pins
#define BCURRENT_PIN 34
#define BVOLTAGE_PIN 35
#define CURRENT_PIN 36
#define ENERGY_PIN 33
#define POWER_PIN 36
#define SPEED_PIN 39
#define VOLTAGE_PIN 26

void setup() {
  Serial.begin(9600);

  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(1000);

  // Connect to WiFi
  Serial.println();
  Serial.println();
  Serial.print("Connecting to: ");
  Serial.println(WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println();
  Serial.println("WiFi Connected");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  // Read analog values from pins
  float bcurrent = analogRead(BCURRENT_PIN) / 4095.0 * 3.3;  // Convert to voltage
  float bvoltage = analogRead(BVOLTAGE_PIN) / 4095.0 * 3.3; // Convert to voltage
  float current = analogRead(CURRENT_PIN) / 4095.0 * 3.3;  // Convert to voltage
  float energy = analogRead(ENERGY_PIN) / 4095.0 * 3.3; // Convert to voltage
  float power = analogRead(POWER_PIN) / 4095.0 * 3.3;    // Convert to voltage
  float speed = analogRead(SPEED_PIN) / 4095.0 * 3.3;    // Convert to voltage
  float voltage = analogRead(VOLTAGE_PIN) / 4095.0 * 3.3; // Convert to voltage
  String time = "00:00:00"; // Fixed for demonstration

  // Write data to Firebase
  if (firebase.setFloat("/data/bcurrent", bcurrent) &&
      firebase.setFloat("/data/bvoltage", bvoltage) &&
      firebase.setFloat("/data/current", current) &&
      firebase.setFloat("/data/energy", energy) &&
      firebase.setFloat("/data/power", power) &&
      firebase.setFloat("/data/speed", speed) &&
      firebase.setFloat("/data/voltage", voltage) &&
      firebase.setString("/data/time", time)) {
    Serial.println("Successfully updated Firebase with sensor values.");
  } else {
    Serial.println("Failed to update Firebase.");
  }

  // Print the values to the serial monitor
  Serial.print("bcurrent: "); Serial.println(bcurrent);
  Serial.print("bvoltage: "); Serial.println(bvoltage);
  Serial.print("current: "); Serial.println(current);
  Serial.print("energy: "); Serial.println(energy);
  Serial.print("power: "); Serial.println(power);
  Serial.print("speed: "); Serial.println(speed);
  Serial.print("voltage: "); Serial.println(voltage);
  Serial.print("time: "); Serial.println(time);

  delay(5000); // Delay for 5 seconds before updating again
}
esp:0
esp:1
esp:2
esp:3
esp:4
esp:5
esp:6
esp:7
esp:8
esp:9
esp:10
esp:11
esp:12
esp:13
esp:14
esp:15
esp:16
esp:17
esp:18
esp:19
esp:20
esp:21
esp:26
esp:33
esp:34
esp:35
esp:36
esp:37
esp:38
esp:39
esp:40
esp:41
esp:42
esp:45
esp:46
esp:3V3
esp:5V
esp:GND.1
esp:TX
esp:RX
esp:RST
esp:GND.2
$abcdeabcde151015202530354045505560fghijfghij
pot1:VCC
pot1:SIG
pot1:GND
pot2:VCC
pot2:SIG
pot2:GND
pot3:VCC
pot3:SIG
pot3:GND
pot4:VCC
pot4:SIG
pot4:GND
pot5:VCC
pot5:SIG
pot5:GND
pot6:VCC
pot6:SIG
pot6:GND
pot7:VCC
pot7:SIG
pot7:GND