#include<LiquidCrystal_I2C.h> //Library LCD I2C
#include<HX711_ADC.h> //Library Load Cell
#include<RTClib.h> //Library RTC
#include<DHT.h> //Library DHT
#include <Adafruit_Sensor.h>
#include<EEPROM.h>
#include<Wire.h>
//LCD I2C
LiquidCrystal_I2C lcdtimeanddate(0x27,16,2);
LiquidCrystal_I2C lcdairquality(0x26,16,2);
LiquidCrystal_I2C lcdhmdtyandtemp(0x25,16,2);
LiquidCrystal_I2C lcdload(0x24,16,2);
//Load Cell
const int HX711_dout = 12;
const int HX711_sck = 13;
HX711_ADC LoadCell(HX711_dout, HX711_sck);
const int calVal_eepromAdress = 0;
long x;
//RTC
RTC_DS3231 rtc;
char dataHari[7][12] = {"Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu"};
String hari;
int tanggal, bulan, tahun, jam, menit, detik;
float suhu;
//DHT
#define DHTPIN 7 // Pin untuk DHT
#define DHTTYPE DHT22 // Jenis DHT (DHT 22)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
int h; //Kelembapan
int t; //Suhu
//MQ135 (AQ Sensor)
int buz = 8; //buzzer
const int aqsensor = A0; //output of mq135 connected to A0 pin of Arduino
int threshold = 250; //Threshold level for Air Quality
void setup() {
Serial.begin(9600);
//Load Cell
pinMode(HX711_dout, INPUT);
delay(10);
Serial.println();
Serial.println("Memulai...");
lcdload.init();
LoadCell.begin();
lcdload.setCursor(0,0);
lcdload.print("Brt: ");
lcdload.setCursor(12,0);
lcdload.print("0");
lcdload.setCursor(14,0);
lcdload.print("gr");
float calibrationValue;
calibrationValue = 696.0;
EEPROM.get(calVal_eepromAdress, calibrationValue);
long stabilizingtime = 2000;
boolean _tare = true;
LoadCell.start(stabilizingtime, _tare);
if (LoadCell.getTareTimeoutFlag()) {
Serial.println("Timeout, cek kabel MCU>HX711 pastikan sudah tepat");
while (1);
}
else {
LoadCell.setCalFactor(calibrationValue);
Serial.println("Startup selesai");
}
//RTC
if (! rtc.begin()) {
Serial.println("RTC Tidak Ditemukan");
Serial.flush();
abort();
}
//Atur Waktu, upload untuk set waktu, kemudian beri komentar lalu upload kembali
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
//rtc.adjust(DateTime(2022,5,01,13,57,0));
//DHT
dht.begin();
Serial.println("Temperature and Humidity Sensor Test");
lcdhmdtyandtemp.init(); //initialize the lcd
lcdhmdtyandtemp.backlight(); //open the backlight
//MQ135 (AQ SENSOR)
pinMode (buz,OUTPUT); // buzzer is connected as Output from Arduino
pinMode (aqsensor,INPUT); // MQ135 is connected as INPUT to arduino
lcdairquality.clear(); // clear lcd
lcdairquality.init (); // consider 16,2 lcd
}
void loop() {
//Load Cell
static boolean newDataReady = 0;
const int serialPrintInterval = 0;
if (LoadCell.update()) newDataReady = true;
if (newDataReady) {
if (millis() > x + serialPrintInterval) {
int i = LoadCell.getData();
if(i<0){
i=0;
}
tampil(i);
newDataReady = 0;
t = millis();
}
}
if(Serial.available() > 0){
float i;
char inByte = Serial.read();
if (inByte == 'x') LoadCell.tareNoDelay();
}
if (LoadCell.getTareStatus() == true) {
Serial.println("Tara selesai");
}
//RTC
DateTime now = rtc.now();
hari = dataHari[now.dayOfTheWeek()];
tanggal = now.day();
bulan = now.month();
tahun = now.year();
jam = now.hour();
menit = now.minute();
detik = now.second();
suhu = rtc.getTemperature();
lcdtimeanddate.setCursor(0,0);
lcdtimeanddate.print(String() +hari+"," +tanggal+"-"+bulan+"-"+tahun);
lcdtimeanddate.setCursor(0,1);
lcdtimeanddate.print(String() +jam+":" +menit+":"+detik+" " +suhu+".C ");
delay(1000);
//DHT
h = dht.readHumidity();
t = dht.readTemperature();
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %, Temp: ");
Serial.print(t);
Serial.println(" ° Celsius");
lcdhmdtyandtemp.setCursor(0, 0);
lcdhmdtyandtemp.print("TEMP :");
lcdhmdtyandtemp.print(t);
lcdhmdtyandtemp.print("C");
lcdhmdtyandtemp.setCursor(1, 0);
lcdhmdtyandtemp.print("HMDTY:");
lcdhmdtyandtemp.print(h);
lcdhmdtyandtemp.print("%");
delay(1000);
//MQ135 (AQ Sensor)
int ppm = analogRead(aqsensor); //read MQ135 analog outputs at A0 and store it in ppm
Serial.print("Air Quality: "); //print message in serail monitor
Serial.println(ppm); //print value of ppm in serial monitor
lcdairquality.setCursor(0,0); // set cursor of lcd to 1st row and 1st column
lcdairquality.print("AQ : "); // print message on lcd
lcdairquality.print(ppm); // print value of MQ135
if (ppm > threshold) // check is ppm is greater than threshold or not
{
lcdairquality.setCursor(1,0); //jump here if ppm is greater than threshold
lcdairquality.print("Level:HIGH");
Serial.println("Level:HIGH");
tone(1000,200); //blink led with turn on time 1000mS, turn off time 200mS
digitalWrite(buz,HIGH); //Turn ON Buzzer
}
else
{
digitalWrite(buz,LOW); //Turn off Buzzer
lcdairquality.setCursor(1,0);
lcdairquality.print ("Level:Good");
Serial.println("Level:Good");
}
delay (500);
}
void tampil(int j){
lcdload.setCursor(4,0);
lcdload.print(" ");
if(j<10){
lcdload.setCursor(12,0);
}else if(j<100 && j>=10){
lcdload.setCursor(11,0);
}else if(j<1000 && j>=100){
lcdload.setCursor(10,0);
}else if(j<10000 && j>=1000){
lcdload.setCursor(9,0);
}else if(j<100000 && j>=10000){
lcdload.setCursor(8,0);
}else if(j<1000000 && j>=100000){
lcdload.setCursor(7,0);
}else if(j<10000000 && j>=1000000){
lcdload.setCursor(6,0);
}else if(j<100000000 && j>=10000000){
lcdload.setCursor(5,0);
}else{
lcdload.setCursor(4,0);
}
lcdload.print(j);
}