#include <WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
// WOKWI
String wifiSSID = "Wokwi-GUEST";
String wifiPassword = "";
// ARDUINO IDE
//tring wifiSSID = "TP-Link_435E";
//tring wifiPassword = "17625357";
String mqttBroker = "broker.hivemq.com";
WiFiClient client;
PubSubClient mqtt(client);
void connectWifi();
void connect_mqtt();
#define SERVO_PIN 15
#define BUZZER_PIN 13
#define LOADCELL_DOUT_PIN 4
#define LOADCELL_SCK_PIN 5
#include <ESP32Servo.h>
#include <Wire.h>
#include <RTClib.h>
#include <HX711.h>
#include <LiquidCrystal_I2C.h>
// Initialize the I2C LCD with its address (0x27 for most displays) and dimensions (16x2)
LiquidCrystal_I2C lcd(0x27,16,2);
// Create a Servo object
Servo myservo;
// Create an RTC object
RTC_DS1307 rtc;
// Create an HX711 object
HX711 scale;
// Desired weight in grams
float targetWeight = 5;
int jam,menit,detik,tanggal,bulan,tahun,hari;
char temp[60];
char nama_hari[7][7] = {"Minggu","Senin","Selasa","Rabu","Kamis","Jumat","Sabtu"};
void setup()
{
Serial.begin(9600);
connectWifi();
mqtt.setServer(mqttBroker.c_str(), 1883);
myservo.attach(15);
myservo.write(0);
// Set buzzer pin as output
pinMode(BUZZER_PIN, OUTPUT);
// Initialize I2C communication
Wire.begin();
lcd.backlight(); lcd.init();// Turn on the backlight (if available on your display)
lcd.setCursor(0,0); lcd.print(" JAM DIGITAL ");
if (!rtc.begin()) {
lcd.setCursor(0,1); lcd.print(" TIDAK CONNECT ");
}
else {
lcd.setCursor(0,1); lcd.print(" MAKAN ");
}
rtc.adjust(DateTime(2024,9,25,3,0,0));
// Initialize RTC
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
// Check if RTC lost power and set the time
if (!rtc.isrunning()) {
Serial.println("RTC is not running, let's set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// Initialize HX711
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale();
scale.tare(); // Reset the scale to 0
}
int x=0;
void loop()
{
if (!mqtt.connected()){
connect_mqtt();
Serial.println("MQTT Connected");
mqtt.publish("frz/datatemp","ESP32 terhubung");
}
mqtt.loop();
// Get current time
DateTime now = rtc.now();
//jam = now.hour();
//menit = now.minute();
//detik = now.second();
//tanggal = now.day();
//bulan = now.month();
//tahun = now.year();
//hari = now.dayOfTheWeek();
// Define the time when you want the servo to activate (HH, MM, SS)
int activateHour = 3;
int activateMinute = 0;
int activateSecond = 25;
// Compare current time with activation time
if (now.hour() == activateHour && now.minute() == activateMinute && now.second() == activateSecond) {
// Activate the servo
Serial.println("Activating Servo and Buzzer...");
myservo.write(180); // Set servo to 90 degrees
tone(BUZZER_PIN, 1000); // Start buzzer with 1000 Hz frequency
// Continue dispensing until desired weight is reached
while (scale.get_units() < targetWeight) {
delay(100); // Check weight every 100 ms
}
// Deactivate the servo once the desired weight is reached
myservo.write(0); // Set servo back to 0 degrees
noTone(BUZZER_PIN); // Stop the buzzer
Serial.println("Servo and Buzzer deactivated due to target weight reached.");
}
// sensor berat + kalibrasi
scale.set_scale(); // Set ulang faktor kalibrasi jika diperlukan
float Berat = scale.get_units(10) / 420.00;
int weight = Berat * 1000;
// Display time and weight on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(now.timestamp(DateTime::TIMESTAMP_TIME)); // Display time
lcd.setCursor(0, 1);
lcd.print("Weight: ");
lcd.print(weight);
lcd.print(" g"); // Display weight in grams
// Wait for a moment before checking again
delay(500); // Check every second
}
void connect_mqtt()
{
while(!mqtt.connected()){
Serial.println("Connecting MQTT...");
if (mqtt.connect("frzESP32")){
mqtt.subscribe("frz/datatemp");
}
}
}
void connectWifi()
{
Serial.println("Connecting To Wifi");
WiFi.begin(wifiSSID.c_str(), wifiPassword.c_str());
while (WiFi.status() != WL_CONNECTED)
{
Serial.println(".");
delay(500);
}
Serial.println("Wifi Connected");
Serial.println(WiFi.SSID());
Serial.println(WiFi.RSSI());
Serial.println(WiFi.macAddress());
Serial.println(WiFi.localIP());
Serial.println(WiFi.gatewayIP());
Serial.println(WiFi.dnsIP());
}