void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}
package com.example.firebaseapp;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.database.DataSnapshot;
public class MainActivity extends AppCompatActivity {
// Firebase Database reference
private FirebaseDatabase mDatabase;
private DatabaseReference mRef;
private TextView textView; // TextView to display data
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize Firebase
mDatabase = FirebaseDatabase.getInstance();
mRef = mDatabase.getReference("test"); // The "test" node from your Firebase Realtime Database
textView = findViewById(R.id.textView); // Linking the TextView from XML
// Read data from Firebase Realtime Database
mRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get the data from Firebase
String ledStatus = dataSnapshot.child("LED").getValue(String.class); // Get LED status
String intData = dataSnapshot.child("int").getValue(String.class); // Get int data
String floatData = dataSnapshot.child("float").getValue(String.class); // Get float data
// Display the data in the TextView
String displayText = "LED Status: " + ledStatus + "\nInt: " + intData + "\nFloat: " + floatData;
textView.setText(displayText);
}
@Override
public void onCancelled(com.google.firebase.database.DatabaseError databaseError) {
// If there's an error in retrieving the data
Toast.makeText(MainActivity.this, "Failed to read value.", Toast.LENGTH_SHORT).show();
}
});
}
}