//ICT CLASS PROGRAM
// Library
#include <WiFi.h> //enables network connection (local and Internet)
#include <FILENAME> //A client library for MQTT messaging
#include <DHT.h> //Arduino library for DHT11, DHT22, etc Temp & Humidity Sensors
// WiFi
// ssid is the name you want to give to the ESP32 access point
// SMA ABBS Surakarta or Wokwi-GUEST
const char* ssid = "SMA ABBS Surakarta";
const char* password = ""; //password variable is the password for the access point
// MQTT Broker
const char* mqtt_server = "broker.emqx.io";
const int mqtt_port = 1883;
const char* mqtt_username = "emqx";
const char* mqtt_password = "public";
// DHT11 Topic
const char* temperatureTopic = "emqx/xii4suhus";
const char* humidityTopic = "emqx/xii4humds";
// LDR & Brightness
const char* ldrTopic = "emqx/xii4ldr";
const char* brightnessTopic = "emqx/xii4cahayas";
// SOIL Moisture
const char* soilTopic = "emqx/xii4tanahs";
// DHT 11 Declaration
#define DHTPIN 15
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// LDR PIN
#define LdrPin 32
// Soil Moisture
#define soilMoisturePin 34
int soilMoistureValue = 0; //constanta
//variable for storing the pushbutton status
long now = millis();
long lastMeasure = 0;
// WiFi & Client Publish Declaration
WiFiClient espClient;
PubSubClient client(espClient);
// Topic Calling Function
void callback(char *topic, byte *payload, unsigned int length){
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char) payload[i]);
}
Serial.println();
}
// LDR Function
float calculateBrightness(int ldrRawValue) {
const float gama = 0.7;
const float rl10 = 50;
ldrRawValue = map(ldrRawValue, 4095, 0, 1024, 0); //mengubah nilai pembacaan sensor LDR dari nilai ADC arduino menjadi nilai ADC ESP32
float voltase = ldrRawValue / 1024.*5;
float resistansi = 2000 * voltase / (1-voltase/5);
float kecerahan = pow(rl10*1e3*pow(10,gama)/resistansi,(1/gama));
return kecerahan;
}
void setup(){
// Set software serial baud to 115200;
Serial.begin(9600);
// Connecting to a WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
//delay(500);
delay(1000);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the Wi-Fi network");
//Connecting to a mqtt server
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
//Connecting Proccess
while (!client.connected()) {
String client_id = "esp32-client-";
client_id += String(WiFi.macAddress());
Serial.printf("The client %s connects to the public MQTT broker\n", client_id.c_str());
//WiFi Condition
if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
Serial.println("Public EMQX MQTT broker connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
//Starting DHT11 scanning
dht.begin();
}
void loop() {
//mengulang pengiriman data sensor
client.loop();
//untuk menjalankan waktu internal setiap milli seconds pada ESP32 secara independent..
//ketika millis di baca maka millis akan terus menghitung waktu walau pun ESP32 nya sedang menjalan kan program yang lain
now = millis();
//DHT11 Scanning Process
if(now - lastMeasure > 2000){
lastMeasure = now;
//DHT11 Scanning
float h = dht.readHumidity();
float t = dht.readTemperature();
float hic = dht.computeHeatIndex(t, h, false);
//LDR Sensor Scanning
int ldrRawValue = analogRead(LdrPin);
float brightness = calculateBrightness(ldrRawValue);
//Soil Moisture
soilMoistureValue = analogRead(soilMoisturePin);
//Check if any reads failed and exit early (to try again(;
if(isnan(h) || isnan(t)){
Serial.println("Failed to read from DHT & LDR sensor!");
return;
}
// Publish DHT11 value and brightness to their respective topics
client.publish(humidityTopic, String(h).c_str()); //mengonversi string
client.publish(temperatureTopic, String(t).c_str());
// Publish LDR value and brightness to their respective topics
client.publish(ldrTopic, String(ldrRawValue).c_str());
client.publish(brightnessTopic, String(brightness).c_str());
// Publish Soil to their respective topics
client.publish(soilTopic, String(soilMoistureValue).c_str());
// DHT11 Output in Serial Monitor
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.println(" *C ");
// LDR Output in Serial Monitor
Serial.print("LDR Value: ");
Serial.println(ldrRawValue);
Serial.print("Brightness: ");
Serial.print(brightness);
Serial.println(" lux ");
// Soil Output
Serial.print("Soil Moisture: ");
Serial.print(soilMoistureValue);
Serial.println(" cm ");
}
}