#include <WiFi.h>
#include <FirebaseESP32.h>
#include "DHT.h"
#include <addons/RTDBHelper.h>
// #define WIFI_SSID "5P26_2.4Ghz"
// #define WIFI_PASSWORD "luangay1"
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define FIREBASE_HOST "https://esp32-51b05-default-rtdb.firebaseio.com"
#define FIREBASE_AUTH "pFOw8cuDHnyYHrEtZt5BWQGbaWKzaLOW4k8IEkhP"
#define DHTPIN 27
#define DHTTYPE DHT22
#define BUTTON_PIN 33
#define LED_PIN 13
DHT dht(DHTPIN, DHTTYPE);
FirebaseData fbdo;
unsigned long dataMillis = 0;
int led_state = LOW; // the current state of LED
int button_state; // the current state of button
int last_button_state; // the previous state of button
void TaskBlink(void *pvParameters);
void TaskAnalogReadA3(void *pvParameters);
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
button_state = digitalRead(BUTTON_PIN);
dht.begin();
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(300);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
// Now set up two tasks to run independently.
xTaskCreate(
TaskButton, "TaskButton" // A name just for humans
,
2048 // This stack size can be checked & adjusted by reading the Stack Highwater
,
NULL, 2 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
,
NULL);
xTaskCreate(
TaskFirebase, "Firebase", 15000 // Stack size
,
NULL, 1 // Priority
,
NULL);
// Now the task scheduler, which takes over control of scheduling individual tasks, is automatically started.
}
void loop() {
}
void read_data() {
float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");}
if (millis() - dataMillis > 3000) {
Serial.printf("Nhiet do: %f\n", t);
Serial.printf("Do am: %f\n", h);
Serial.printf("Update nhiet do %s\n", Firebase.setFloat(fbdo, "/data/nhietdo", t) ? "Ok" : fbdo.errorReason().c_str());
Serial.printf("Update do am %s\n", Firebase.setFloat(fbdo, "/data/doam", h) ? "Ok" : fbdo.errorReason().c_str());
dataMillis = millis();
}
}
void TaskButton(void *pvParameters) // This is a task.
{
(void)pvParameters;
for (;;) // A Task shall never return or exit.
{
last_button_state = button_state; // save the last state
button_state = digitalRead(BUTTON_PIN); // read new state
if (last_button_state == HIGH && button_state == LOW) {
Serial.println("The button is pressed");
// toggle state of LED
led_state = !led_state;
digitalWrite(LED_PIN, led_state);
}
if (last_button_state == LOW && button_state == HIGH) {
led_state = 0;
digitalWrite(LED_PIN, led_state);
}
vTaskDelay(100); // one tick delay (15ms) in between reads for stability
}
}
void TaskFirebase(void *pvParameters) // This is a task.
{
(void)pvParameters;
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.reconnectWiFi(true);
Firebase.setReadTimeout(fbdo, 1000 * 60);
Firebase.setwriteSizeLimit(fbdo, "tiny");
while (1) {
read_data();
vTaskDelay(100);
// timer.run();
}
}