This circuit is designed to read data from various sensors including a turbidity sensor, pH meter, temperature sensor, and an HC-SR04 ultrasonic sensor. The data is processed by an Arduino UNO microcontroller and sent to a server via a WiFi module (ESP8266-01). The circuit also includes a 12V battery for power supply. Alerts are sent if sensor values are out of specified ranges.
Arduino UNO
HC-SR04 Ultrasonic Sensor
Turbidity Sensor
12V Battery
Temperature Sensor
Wifi module ESP8266-01
PH Meter
/*
* This Arduino Sketch reads data from various sensors (turbidity, pH,
* temperature, and distance) and sends the data to a server. It also
* sends alerts if sensor values are out of specified ranges. The code
* ensures functionality even when WiFi is disconnected.
*/
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
const char* serverName = "http://yourserver.com/api/data";
const char* alertServerName = "http://yourserver.com/api/alert";
const int turbidityPin = A2; // Turbidity sensor output connected to A2
const int phPin = A3; // pH meter signal connected to A3
const int tempPin = A1; // Temperature sensor data connected to A1
const int trigPin = 7; // HC-SR04 TRIG pin connected to D7
const int echoPin = 6; // HC-SR04 ECHO pin connected to D6
unsigned long previousMillis = 0;
const long interval = 1800000; // 30 minutes
void setup() {
Serial.begin(9600);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
int turbidityValue = analogRead(turbidityPin);
int phValue = analogRead(phPin);
float voltage = phValue * (5.0 / 1023.0);
float ph = 7 + ((2.5 - voltage) / 0.18);
int tempValue = analogRead(tempPin);
float tempVoltage = tempValue * (5.0 / 1023.0);
float temperature = (tempVoltage - 0.5) * 100.0;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
float distance = (duration * 0.034) / 2;
Serial.print("Turbidity Value: ");
Serial.println(turbidityValue);
Serial.print("pH Value: ");
Serial.println(ph);
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" C");
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(serverName);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String httpRequestData = "turbidity=" + String(turbidityValue) + "&ph=" +
String(ph) + "&temperature=" + String(temperature) +
"&distance=" + String(distance);
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
} else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("WiFi Disconnected");
}
if (ph < 6.5 || ph > 8.5 || turbidityValue > 5 || distance < 50) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(alertServerName);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String alertData = "alert=1&turbidity=" + String(turbidityValue) + "&ph=" +
String(ph) + "&temperature=" + String(temperature) +
"&distance=" + String(distance);
int alertResponseCode = http.POST(alertData);
if (alertResponseCode > 0) {
String response = http.getString();
Serial.println(alertResponseCode);
Serial.println(response);
}