// #include "esp_camera.h"
#include <WiFi.h>
// #include "PubSubClient.h"
// // #include <HTTPClient.h>
// // HTTPClient http;
// const char * mqtt_server = "broker.hivemq.com";
// const char * mqtt_id = "8393d86d-5435-4bb9-b49d-4821ca47b6b8";
// int mqtt_port = 1883;
// int led = 14;
// WiFiClient espClient;
// PubSubClient client(espClient);
#include <FirebaseESP32.h>
// #define CAMERA_MODEL_AI_THINKER
// #include "DHT.h"
// #define DHTPIN 12 // what digital pin the DHT sensor is connected to
// #define DHTTYPE DHT22 // there are multiple kinds of DHT sensors
// DHT dht(DHTPIN, DHTTYPE);
int postId=0;
#define FIREBASE_HOST "https://sensor-data-95a63-default-rtdb.asia-southeast1.firebasedatabase.app"
#define FIREBASE_AUTH "AIzaSyB96WzdkvcJwW5-TqyHWeWteJ1WHl_aGGA"
FirebaseData firebaseData;
FirebaseJson json;
// int data5base;
String nhietdo = "/nhietdo";
String doam = "/doam";
void setup_wifi()
{
Serial.println("Connecting to SSID: Wokwi-GUEST");
WiFi.begin("Redmi Note 11S", "123456aA@");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected");
Serial.print(", IP address: ");
Serial.println(WiFi.localIP());
}
void setup(){
Serial.begin(115200);
Serial.println("DHT22 test!");
// pinMode(led, OUTPUT);
setup_wifi();
// dht.begin();
// client.setServer(mqtt_server, mqtt_port);
// client.setCallback(callback);
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.reconnectWiFi(true);
}
// void callback(char* topic, byte* message, unsigned int length) {
// Serial.print("Message arrived on topic: ");
// Serial.println(topic);
// Serial.print("Message: ");
// String stMessage;
// for (int i = 0; i < length; i++) {
// stMessage += (char)message[i];
// }
// Serial.println(stMessage);
// if (stMessage == "on") {
// digitalWrite(led, HIGH);
// }
// else if (stMessage == "off") {
// digitalWrite(led, LOW);
// }
// }
// void reconnect()
// {
// while (!client.connected()) {
// if (client.connect(mqtt_id)) {
// client.subscribe("IoT_led");
// client.subscribe("IoT_tem");
// client.subscribe("IoT_hum");
// client.subscribe("IoT_time");
// Serial.println("");
// } else {
// Serial.print("failed, rc=");
// Serial.print(client.state());
// Serial.println(" try again in 5 seconds");
// delay(5000);
// }
// }
// }
void loop(){
// if (!client.connected())// Kiểm tra kết nối
// reconnect();
// client.loop();
// Serial.print("Gui tin nhan \"Xin chao\" vao topic IoT47_MQTT_Test");
// client.publish("IoT47_MQTT_Test", "Đã thành công!"); // gửi dữ liệu lên topic IoT47_MQTT_Test
// delay(2000);
// float h = dht.readHumidity();
// // Read temperature as Celsius (the default)
// float t = dht.readTemperature();
// // Read temperature as Fahrenheit (isFahrenheit = true)
// float f = dht.readTemperature(true);
// // Check if any reads failed and exit early (to try again).
// if (isnan(h) || isnan(t) || isnan(f)) {
// Serial.println("Failed to read from DHT sensor!");
// return;
// }
// String tem = String(t);
// String hum = String(h);
// Serial.print("Temperature: ");
// Serial.print(tem);
// Serial.print(" *C ");
// Serial.print("Humidity: ");
// Serial.print(hum);
// Serial.print(" % ");
// client.publish("IoT_time", "time");
// client.publish("IoT_tem", (char*) tem.c_str());
// client.publish("IoT_hum", (char*) hum.c_str());
// // // HTTPClient http;
// // http.begin("https://api.thingspeak.com/update?api_key=A2RR0BKZBSP0FYA6&field1=" + tem + "&field2=" + hum);
// // int httpCode = http.GET();
// // if (httpCode > 0) {
// // String payload = http.getString();
// // Serial.println(httpCode);
// // Serial.println(payload);
// // }
// // else {
// // Serial.println("Error on HTTP request");
// // }
// // http.end();
int t = 25 + rand() % (40 + 1 - 25);
int h = 50 + rand() % (75 + 1 -50);
// Serial.println("Nhiệt độ: ");
// Serial.println(t);
// Serial.println("C");
// Serial.println("Độ ẩm:");
// Serial.println(h);
// Firebase.setInt(firebaseData, "/nhietdo", t);
// Firebase.setInt(firebaseData, "/doam", h);
// String chuoiJS="{nhietdo: "+String(t)+", doam:"+String(h)+"}";
postId=postId+1;
Firebase.setInt(firebaseData, "/"+String(postId)+"/nhietdo", t);
Firebase.setInt(firebaseData, "/"+String(postId)+"/doam", h);
Serial.println("set data done!");
delay(2000);
}