/*******************************
IOT MINI PROJECT CODE
PG50 Ayush Sharma
**********************************/
// Include the necessary libraries
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <WiFi.h>
#include <DHT.h>
#include <Adafruit_NeoPixel.h>
#include <LiquidCrystal_I2C.h> // Library for LCD 2004 I2C
const char* ssid = "Wokwi-GUEST";
const char* password = "";
/******************____PH Sensor___*****************/
const int sensorPin = 36; // Analog pin for pH sensor
float voltage, pH;
/******************____LDR___*****************/
const int ldrPin = 34; // Define the analog input pin for the LDR sensor
/******************____LED___*****************/
const int LED_PIN = 13; // Pin no.
#define NUM_LEDS 35 // No of LEDs
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
/******************____DHT11___*****************/
#define DHTPIN 5
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
/******************____Relay___*****************/
#define RELAY_PIN 2 // GPIO pin connected to the relay
#define ON_TIME 10000 // Relay ON duration in ms
#define OFF_TIME 10000 // Relay OFF duration in ms
/******************____LCD___*****************/
LiquidCrystal_I2C lcd(0x27, 20, 4); // LCD I2C address 0x27, 20 columns, 4 rows
/******************____DS18B20___*****************/
#define ONE_WIRE_BUS 4 // GPIO pin connected to DS18B20 sensor
OneWire oneWire(ONE_WIRE_BUS); // OneWire instance
DallasTemperature sensors(&oneWire); // DallasTemperature instance
/******************____HC-SR04___*****************/
#define TRIG_PIN 18 // Trig pin
#define ECHO_PIN 19 // Echo pin
long duration; // Pulse duration
float distanceCm; // Distance in cm
TaskHandle_t Task1; // Task handle for the second core
void loop2(void *parameter) { // Second loop working parallel to the main loop
for (;;) {
digitalWrite(RELAY_PIN, HIGH); // Turn on relay
delay(ON_TIME);
digitalWrite(RELAY_PIN, LOW); // Turn off relay
delay(OFF_TIME);
}
}
// Setup function
void setup() {
xTaskCreatePinnedToCore(
loop2,
"buttonCheck",
1000,
NULL,
1,
&Task1,
0
);
Serial.begin(115200); // Start serial communication
WiFi.begin(ssid, password);
int retry_count = 0;
while (WiFi.status() != WL_CONNECTED && retry_count < 20) {
delay(1000);
Serial.println("Attempting to connect to WiFi...");
retry_count++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("WiFi connected successfully!");
} else {
Serial.println("Failed to connect to WiFi.");
}
/******************____LED___*****************/
strip.begin();
strip.setBrightness(255);
strip.show();
/******************____DHT11___*****************/
dht.begin();
/******************____Relay___*****************/
pinMode(RELAY_PIN, OUTPUT);
/******************____DS18B20___*****************/
sensors.begin();
/******************____HC-SR04___*****************/
pinMode(TRIG_PIN, OUTPUT); // Set Trig as OUTPUT
pinMode(ECHO_PIN, INPUT); // Set Echo as INPUT
/******************____LCD___*****************/
lcd.init();
lcd.backlight(); // Turn on LCD backlight
lcd.setCursor(0, 0);
lcd.print("System Initialized");
delay(2000);
}
// Main loop
void loop() {
delay(1000);
/******************____HC-SR04___*****************/
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distanceCm = duration / 58.2;
/******************____PH Sensor___*****************/
voltage = analogRead(sensorPin) * 5.0 / 1024.0;
pH = 7 - (voltage - 2.5) / 0.18;
/******************____LDR___*****************/
int rawValue = analogRead(ldrPin);
int percentage = map(rawValue, 0, 4095, 0, 100);
int brightness = map(percentage, 0, 100, 255, 0);
/******************____LED___*****************/
strip.setBrightness(brightness);
for (int i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, 75, 0, 130);
}
strip.show();
/******************____DHT11___*****************/
float h = dht.readHumidity();
float t = dht.readTemperature();
/******************____DS18B20___*****************/
sensors.requestTemperatures();
float temperatureC = sensors.getTempCByIndex(0);
/******************____SerialPrint___*****************/
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(" LightIntensity: ");
Serial.print(percentage);
Serial.print(" Water Level Distance: ");
Serial.print(distanceCm);
Serial.print(" cm");
Serial.print("pH: ");
Serial.print(pH);
/******************____LCD Display___*****************/
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(t);
lcd.print("°C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(h);
lcd.print("%");
lcd.setCursor(0, 2);
lcd.print("pH: ");
lcd.print(pH);
lcd.setCursor(0, 3);
lcd.print(" WL: ");
lcd.print(distanceCm);
lcd.print("cm");
}