#include <WiFi.h>
#include <FirebaseESP32.h>

#define _SSID "Professor Moriarty"                                        // Your WiFi SSID
#define _PASSWORD "who are you?"                                          // Your WiFi Password
#define REFERENCE_URL "https://gokart-iot-35f2c-default-rtdb.firebaseio.com"

Firebase firebase(REFERENCE_URL);

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

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

  // Connect to WiFi
  Serial.println();
  Serial.println();
  Serial.print("Connecting to: ");
  Serial.println(_SSID);
  //WiFi.begin(_SSID, _PASSWORD);
  WiFi.begin("Wokwi-GUEST", "", 6); // Uncomment this if you are using Wokwi-GUEST

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

  Serial.println("");
  Serial.println("WiFi Connected");

  // Print the IP address
  Serial.print("IP Address: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");

  Serial.println("Firebase initialized");
}

void loop() {
  // Generate random data
  int motorSpeed = random(10, 101); // Speed between 10 and 100
  int motorVoltage = random(50, 101); // Voltage between 50 and 100
  int motorCurrent = random(10, 51); // Current between 10 and 50
  int motorEnergy = random(1, 11);   // Energy between 1 and 10
  int motorPower = random(100, 501); // Power between 100 and 500

  int batteryVoltage = random(40, 61); // Voltage between 40 and 60
  int batteryCurrent = random(10, 21); // Current between 10 and 20

  // Create JSON structure
  FirebaseJson json;
  FirebaseJson motorData;
  motorData.set("speed", motorSpeed);
  motorData.set("voltage", motorVoltage);
  motorData.set("current", motorCurrent);
  motorData.set("energy", motorEnergy);
  motorData.set("power", motorPower);

  FirebaseJson batteryData;
  batteryData.set("voltage", batteryVoltage);
  batteryData.set("current", batteryCurrent);

  json.set("motorData", motorData);
  json.set("batteryData", batteryData);

  // Send JSON data to Firebase
  if (Firebase.pushJSON(firebaseData, "", json)) {
    Serial.println("Data sent to Firebase");
    Serial.println(firebaseData.dataPath());
  } else {
    Serial.print("Failed to send data: ");
    Serial.println(firebaseData.errorReason());
  }

  // Delay for 5 seconds before sending new data
  delay(5000);
}
esp:0
esp:1
esp:2
esp:3
esp:4
esp:5
esp:6
esp:7
esp:8
esp:9
esp:10
esp:18
esp:19
esp:GND.1
esp:3V3.1
esp:3V3.2
esp:GND.2
esp:RST
esp:GND.3
esp:GND.4
esp:5V.1
esp:5V.2
esp:GND.5
esp:GND.6
esp:GND.7
esp:GND.8
esp:GND.9
esp:RX
esp:TX
esp:GND.10