#include <WiFi.h>
#include <WiFiClient.h>
#include <HTTPClient.h>


#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
const char* firebaseHost = "esp8266-e1753-default-rtdb.firebaseio.com";
const char* firebaseKey = "AIzaSyDHUHCInzdvk814SPnQLLpXW5c1bYyZiyo";

// Defining the WiFi channel speeds up the connection:
#define WIFI_CHANNEL 6
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
  Serial.print("Connecting to WiFi ");
  Serial.print(WIFI_SSID);
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.print(".");
  }
  Serial.println(" Connected!");

  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  sendDataToFirebase("sensorValue", "123");
}

void sendDataToFirebase(const char* field, const char* value) {
  WiFiClient client;
  HTTPClient http;

  String url = "https://esp8266-e1753-default-rtdb.firebaseio.com/";

  http.begin(client, url);
  http.addHeader("Content-Type", "application/json");

  String payload = "{\"" + String(field) + "\":\"" + String(value) + "\"}";
  
  int httpResponseCode = http.POST(payload);

  if (httpResponseCode > 0) {
    Serial.printf("HTTP POST request sent to Firebase. Response code: %d\n", httpResponseCode);
  } else {
    Serial.printf("Error sending HTTP POST request to Firebase. Error code: %d\n", httpResponseCode);
  }

  http.end();
}


void loop() {
  // put your main code here, to run repeatedly:
  delay(10); // this speeds up the simulation
}