/* Group Assignment - 2 Sensors
* Group Members:
* Name 1: NG YONG XIANG (8587093A)
* Name 2:
* Name 3:
* Room: DEE 1
*/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <NewPing.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
// HC-SR04 Ultrasonic Sensor pins
#define TRIG_PIN 12
#define ECHO_PIN 14
#define MAX_DISTANCE 400 // Max distance in cm
// Pin configuration
const int oneWireBus = 27; // DS18B20 data pin
const int buzzerPin = 16; // Buzzer pin
#define RLED_PIN 17 // RED LED pins
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);
// Initialize NewPing
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
// WiFi and MQTT configuration
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* hostname = "broker.hivemq.com";
WiFiClient espClient;
PubSubClient client(espClient);
const char* espClientName = "esp32Client_JonasNG";
int PORTNUM = 1883;
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* payload, unsigned int length)
{
String messageTemp;
Serial.print("Message received in topic: ");
Serial.print(topic);
Serial.print(" length is: ");
Serial.println(length);
Serial.print("Data received from broker: ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
messageTemp += (char)payload[i];
}
// Handle your topics and payload here
if (topic == "IOT/temperature")
{
Serial.print("Turning to ");
if (messageTemp == "open")
{
digitalWrite(buzzerPin, HIGH);
Serial.println("buzzer open");
}
else if (messageTemp == "close")
{
digitalWrite(buzzerPin, LOW);
Serial.println("buzzer close");
}
else if (messageTemp == "on")
{
digitalWrite(RLED_PIN, HIGH);
Serial.println("led on");
}
else if (messageTemp == "off")
{
digitalWrite(RLED_PIN, LOW);
Serial.println("led off");
}
}
}
void MQTTSubscribe()
{
client.subscribe("IOT/temperature");
}
void setup_MQTT()
{
client.setServer(hostname, PORTNUM);
client.setCallback(callback);
}
void setup()
{
Serial.begin(9600);
delay(5000);
pinMode(buzzerPin, OUTPUT);
sensors.begin();
pinMode(RLED_PIN, OUTPUT);
Serial.println("System Initialized");
setup_wifi();
setup_MQTT();
}
volatile bool RESET1 = false;
void RESET()
{
RESET1 = true;
}
void loop()
{
if (!client.connected())
{
connectMQTT();
}
client.loop();
// Read temperature from DS18B20
sensors.requestTemperatures();
float temperatureC = sensors.getTempCByIndex(0);
if(RESET1)
{
digitalWrite(buzzerPin, LOW);
}
else
{
digitalWrite(buzzerPin, HIGH);
}
// Print temperature to Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println(" °C");
// Publish triggers based on temperature readings
if (temperatureC >= 50)
{
client.publish("IOT/temperature", "open");
}
else if (temperatureC < 50)
{
client.publish("IOT/temperature", "close");
}
// Measure distance from HC-SR04
delay(50); // Small delay before reading again
int distance = sonar.ping_cm(); // Measure distance in cm
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// If distance is less than 200mm (20 cm), turn on LED
if (distance > 0 && distance < 20)
{
client.publish("IOT/temperature", "on");
}
else
{
client.publish("IOT/temperature", "off");
}
delay(1000);
}