#include <Wire.h>
#include <WiFi.h>
#include "UbidotsEsp32Mqtt.h"
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#define DHTPIN 15 // Pin connected to the DHT22 sensor
#define DHTTYPE DHT22 // Define sensor type as DHT22
#define TRIG_PIN 5 // Pin for ultrasonic sensor Trig
#define ECHO_PIN 18 // Pin for ultrasonic sensor Echo
#define LED_PIN 27 // LED connected to pin 27
/****************************************
* Define Constants
****************************************/
const char *UBIDOTS_TOKEN = "BBUS-YXxJXOlmJRol9qYEZeIy9BHkQuFTQ8"; // Put here your Ubidots TOKEN
const char *WIFI_SSID = "Wokwi-GUEST"; // Put here your Wi-Fi SSID
const char *WIFI_PASS = ""; // Put here your Wi-Fi password
const char *DEVICE_LABEL = "esp32"; // Put here your Device label to which data will be published
const char *VARIABLE_LABEL = "Suhu dan Kelembaban"; // Put here your Variable label to which data will be published
const int lcdAddress = 0x27;
const int PUBLISH_FREQUENCY = 2000; // Update rate in milliseconds
unsigned long timer;
uint8_t analogPin = 34; // Pin used to read data from GPIO34 ADC_CH6.
uint8_t PinLed = 27;
uint8_t conta = 0;
Ubidots ubidots(UBIDOTS_TOKEN);
DHT dht(DHTPIN, DHTTYPE); // Tambahkan objek DHT di sini
LiquidCrystal_I2C lcd(lcdAddress, 16, 2); // Inisialisasi LCD I2C (16x2 karakter)
/****************************************
* Auxiliar Functions
****************************************/
void callback(char *topic, byte *payload, unsigned int length)
{
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
}
Serial.println();
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
// ubidots.setDebug(true); // uncomment this to make debug messages available
ubidots.connectToWifi(WIFI_SSID, WIFI_PASS);
ubidots.setCallback(callback);
ubidots.setup();
ubidots.reconnect();
pinMode(PinLed, OUTPUT);
// Inisialisasi DHT sensor
dht.begin();
lcd.begin(16, 2); // Inisialisasi LCD dengan jumlah kolom dan baris (16x2 karakter)
lcd.backlight(); // Nyalakan lampu latar LCD
lcd.setCursor(0, 0);
lcd.print("IoT Project");
// Initialize the ultrasonic sensor pins
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
// Initialize the LED pin as output
pinMode(LED_PIN, OUTPUT);
timer = millis();
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor"));
return; // Keluar dari fungsi loop jika pembacaan gagal
}
// Menampilkan data suhu dan kelembapan di LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(t);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Hum: ");
lcd.print(h);
lcd.print(" %");
ubidots.add("Hum", h);
ubidots.add("Temp", t);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" Temperature: ");
Serial.println(t);
// put your main code here, to run repeatedly:
if (!ubidots.connected())
{
ubidots.reconnect();
}
if ((millis() - timer) > PUBLISH_FREQUENCY) // triggers the routine every 5s
{
ubidots.add(VARIABLE_LABEL, t); // Insert your var Label and the value to send
ubidots.publish(DEVICE_LABEL);
float value = random(2000, 3000) / 100.0;
float hume = random(1000, 10000) / 100.0;
// Read temperature and humidity from DHT22
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
conta++;
ubidots.publish(DEVICE_LABEL);
timer = millis();
// Read distance from ultrasonic sensor
long duration, distance;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = duration * 0.034 / 2;
// Turn on LED if the distance is less than 10 cm
if (distance < 10) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
// Display temperature, humidity, and distance on the LCD
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Hum: ");
lcd.print(humidity);
lcd.print(" %");
delay(1000); // Delay for readability
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Dist: ");
lcd.print(distance);
lcd.print(" cm");
delay(1000); // Delay before next reading
}
Serial.print("Humidity: ");
Serial.print("Temperature: ");
Serial.print(" *C ");
ubidots.loop();
delay(5000);
}