#include <WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
#include <DHT.h>
// Pin Definitions
#define WEIGHTPIN A5 // Weight sensor pin
#define DHTPIN 2 // Pin where the DHT22 sensor is connected
#define LedPinR 23 // Red LED pin
#define LedPinG 18 // Green LED pin
#define ledPinY 19 // Yellow LED pin
#define BUZZER_PIN 4 // Buzzer pin
// Sensor and MQTT Settings
long previousMillisA = 0;
long previousMillisW = 0;
long previousMillisBuzzer = 0;
int intervalA = 1000;
int intervalW = 1000;
int intervalBuzzer = 2000; // Buzzer interval
DHT dht(DHTPIN, DHT22);
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* hostname = "broker.hivemq.com";
WiFiClient espClient;
PubSubClient client(espClient);
const char* espClientName = "esp32Client_S10245564";
int PORTNUM = 1883;
bool buzzerActive = false;
void setup_wifi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println();
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("Given IP by the router to ESP32 is ");
Serial.println(WiFi.localIP());
}
void connectMQTT() {
while (!client.connected()) {
Serial.println("Connecting to MQTT ...");
if (client.connect(espClientName)) {
Serial.println("Connected");
MQTTSubscribe();
} else {
Serial.print("Failed with state ");
Serial.print(client.state());
delay(2000);
}
}
}
void callback(String topic, byte* tpayLoad, unsigned int length) {
String payload;
for (int i = 0; i < length; i++) {
payload += (char)tpayLoad[i];
}
// Handle your topics and payload here
if (topic == "s10245564/LT51G/ADC") {
// Handle temperature-related actions here
Serial.print("Received ADC Reading: ");
Serial.println(payload);
} else if (topic == "s10245564/LT51G/Weight") {
// Handle weight-related actions here
Serial.print("Received Weight Reading: ");
Serial.println(payload);
}
}
void MQTTSubscribe() {
// Subscribe to your topics here
client.subscribe("s10245564/LT51G/ADC");
client.subscribe("s10245564/LT51G/Weight");
}
void setup_MQTT() {
client.setServer(hostname, PORTNUM);
client.setCallback(callback);
}
void setup() {
Serial.begin(9600);
delay(5000);
setup_wifi();
setup_MQTT();
pinMode(ledPinY, OUTPUT);
pinMode(LedPinR, OUTPUT);
pinMode(LedPinG, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
}
void loop() {
if (!client.connected()) {
connectMQTT();
}
client.loop();
// Temperature (ADC) sensor code
long currentMillisA = millis();
if (currentMillisA - previousMillisA >= intervalA) {
previousMillisA = currentMillisA;
int adcRes = analogRead(A5);
Serial.print("Analog Value = ");
Serial.println(adcRes);
float temperature = map(adcRes, 0, 1365, 0, 30);
if (!isnan(temperature)) {
Serial.print("Temperature = ");
Serial.print(temperature);
Serial.println(" °C");
client.publish("s10245564/LT51G/ADC", String(adcRes).c_str());
client.publish("s10245564/LT51G/Temperature", String(temperature).c_str());
// Control LEDs based on ADC reading
if (adcRes >= 0 && adcRes <= 1365) {
// Blink Green LED
digitalWrite(LedPinG, HIGH);
digitalWrite(LedPinR, LOW);
digitalWrite(ledPinY, LOW);
delay(500);
digitalWrite(LedPinG, LOW);
delay(500);
} else if (adcRes > 1365 && adcRes <= 2730) {
// Blink Yellow LED
digitalWrite(LedPinG, LOW);
digitalWrite(LedPinR, LOW);
digitalWrite(ledPinY, HIGH);
delay(500);
digitalWrite(ledPinY, LOW);
delay(500);
} else if (adcRes > 2730 && adcRes <= 4095) {
// Blink Red LED
digitalWrite(LedPinG, LOW);
digitalWrite(LedPinR, HIGH);
digitalWrite(ledPinY, LOW);
delay(500);
digitalWrite(LedPinR, LOW);
delay(500);
}
// Check temperature and weight conditions for the buzzer
if (temperature >= 90 && map(analogRead(WEIGHTPIN), 0, 1365, 0, 30) >= 90) {
if (!buzzerActive) {
// Sound the buzzer
digitalWrite(BUZZER_PIN, HIGH);
previousMillisBuzzer = millis();
buzzerActive = true;
} else {
// Turn off the buzzer after the specified interval
if (millis() - previousMillisBuzzer >= intervalBuzzer) {
digitalWrite(BUZZER_PIN, LOW);
buzzerActive = false;
}
}
}
}
}
// Weight sensor code
long currentMillisW = millis();
if (currentMillisW - previousMillisW >= intervalW) {
previousMillisW = currentMillisW;
int weightValue = analogRead(WEIGHTPIN);
Serial.print("Weight Value = ");
Serial.println(weightValue);
float weight = map(weightValue, 0, 1365, 0, 30);
if (!isnan(weight)) {
Serial.print("Weight = ");
Serial.print(weight);
Serial.println(" Gallons");
client.publish("s10245564/LT51G/Weight", String(weight).c_str());
digitalWrite(LedPinG, LOW);
digitalWrite(LedPinR, LOW);
digitalWrite(ledPinY, LOW);
if (weight >= 0 && weight <= 30) {
digitalWrite(LedPinG, HIGH);
} else if (weight > 30 && weight <= 60) {
digitalWrite(ledPinY, HIGH);
} else if (weight > 60 && weight <= 100) {
digitalWrite(LedPinR, HIGH);
}
delay(500);
}
}
}