#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <DHT.h>
#include <LiquidCrystal.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Arduino_FreeRTOS.h>
Adafruit_MPU6050 mpu;
#define DHTPIN 12
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(19, 23, 18, 17, 16, 15);
const char *ssid = "Wokwi-GUEST"; // the name of the simulated network
const char *password = ""; // no password is required
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 25200); // Add the time offset for Vietnam
void setup() {
lcd.begin(16, 2);
dht.begin();
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
timeClient.begin();
Serial.begin(9600);
while(!Serial);
if(!mpu.begin()){
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
mpu.setAccelerometerRange(MPU6050_RANGE_2_G);
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
}
void loop() {
float humi = dht.readHumidity();
float tempC = dht.readTemperature();
lcd.setCursor(0,0);
if (tempC > 50){
lcd.print("Too hot");
} else if (tempC > 28 && tempC <= 50) {
lcd.print("Average");
} else if (tempC <= 28){
lcd.print("Getting too cold");
} else {
lcd.print("Error");
}
delay(500); // Wait for two seconds
lcd.clear(); // Clear the LCD
timeClient.update();
lcd.setCursor(0,0);
lcd.print("Time: ");
lcd.print(timeClient.getFormattedTime());
delay(500); // Wait for two seconds before the next loop iteration
lcd.clear();
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
Serial.print("Acceleration X: "); Serial.print(a.acceleration.x); Serial.println(" m/s^2");
Serial.print("Acceleration Y: "); Serial.print(a.acceleration.y); Serial.println(" m/s^2");
Serial.print("Acceleration Z: "); Serial.print(a.acceleration.z); Serial.println(" m/s^2");
Serial.print("Rotation X: "); Serial.print(g.gyro.x); Serial.println(" rad/s");
Serial.print("Rotation Y: "); Serial.print(g.gyro.y); Serial.println(" rad/s");
Serial.print("Rotation Z: "); Serial.print(g.gyro.z); Serial.println(" rad/s");
delay(100);
}