#include <Arduino.h>
#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif
#include <Firebase_ESP_Client.h>
//Provide the token generation process info.
#include "addons/TokenHelper.h"
//Provide the RTDB payload printing info and other helper functions.
#include "addons/RTDBHelper.h"
const char* password = "";
const char* wid = "Wokwi-GUEST";
bool signupOK = false;
int deger = 0;
unsigned long sendDataPrevMillis = 0;
#define FIREBASE_HOST "https://denemedurus-default-rtdb.firebaseio.com/"
#define FIREBASE_AUTH "AIzaSyCf_s_6gMGLIRukpGHsOG7sBslHzfh1nr0"
#define FIREBASE_PROJECT_ID "67032272458"
FirebaseData firebasedata;
FirebaseAuth auth;
FirebaseConfig config;
void setup() {
// put your setup code here, to run once:
pinMode(17, INPUT);
Serial.begin(115200);
Serial.println("Hello, ESP32-S2!");
WiFi.begin(wid,password);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(1000);
}
/* Assign the api key (required) */
config.api_key = FIREBASE_AUTH;
/* Assign the RTDB URL (required) */
config.database_url = FIREBASE_HOST;
if (Firebase.signUp(&config, &auth, "", "")){
Serial.println("ok");
signupOK = true;
}
else{
Serial.printf("%s\n", config.signer.signupError.message.c_str());
}
config.token_status_callback = tokenStatusCallback;
Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);
}
void loop() {
// put your main code here, to run repeatedly:
delay(3000); // this speeds up the simulation
deger = analogRead(17);
if (Firebase.ready() && signupOK && (millis() - sendDataPrevMillis > 15000 || sendDataPrevMillis == 0)){
sendDataPrevMillis = millis();
// Write an Int number on the database path test/int
if (Firebase.RTDB.setInt(&firebasedata, "test/int", deger)){
Serial.println("PASSED");
Serial.println("PATH: " + firebasedata.dataPath());
Serial.println("TYPE: " + firebasedata.dataType());
}
else {
Serial.println("FAILED");
Serial.println("REASON: " + firebasedata.errorReason());
}
// Write an Float number on the database path test/float
if (Firebase.RTDB.setString(&firebasedata, "test/Hat", "x sensor akım degeri")){
Serial.println("PASSED");
Serial.println("PATH: " + firebasedata.dataPath());
Serial.println("TYPE: " + firebasedata.dataType());
}
else {
Serial.println("FAILED");
Serial.println("REASON: " + firebasedata.errorReason());
}
}
firestoreDataUpdate(deger,11);
}
void firestoreDataUpdate(double temp, double humi){
if(WiFi.status() == WL_CONNECTED && Firebase.ready()){
String documentPath = "House/Room_1";
FirebaseJson content;
content.set("fields/temperature/doubleValue", String(temp).c_str());
content.set("fields/humidity/doubleValue", String(humi).c_str());
if(Firebase.Firestore.patchDocument(&firebasedata, FIREBASE_PROJECT_ID, "", documentPath.c_str(), content.raw(), "temperature,humidity")){
Serial.printf("ok\n%s\n\n", firebasedata.payload().c_str());
return;
}else{
Serial.println(firebasedata.errorReason());
}
if(Firebase.Firestore.createDocument(&firebasedata, FIREBASE_PROJECT_ID, "", documentPath.c_str(), content.raw())){
Serial.printf("ok\n%s\n\n", firebasedata.payload().c_str());
return;
}else{
Serial.println(firebasedata.errorReason());
}
}
}