Untitled
unknown
plain_text
a year ago
4.5 kB
27
Indexable
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
// === LCD Setup ===
LiquidCrystal_I2C lcd(0x27, 16, 2);
// === Sensor Pins ===
const int tempPin = A2;
const int ecPin = A0;
const int phPin = A1;
const int buzzerPin = 9;
// === ESP8266 Setup ===
SoftwareSerial esp8266(10, 11); // RX, TX
// === Timing Variables ===
unsigned long lastToneTime = 0;
int toneStep = 0;
unsigned long lastSendTime = 0;
const unsigned long sendInterval = 10000; // Every 10 seconds
// === Level Functions ===
String getTempLevel(float t) {
if (t >= 20 && t <= 30) return "Level 1";
else if (t > 30 && t <= 34) return "Level 2";
else if (t >= 16 && t < 20) return "Level 3";
else return "Out";
}
String getSalinityLevel(float s) {
if (s >= 6.5) return "High";
else if (s >= 5.5) return "Moderate";
else if (s >= 4.5) return "Low";
else return "Out";
}
String getPHLevel(float pH) {
if (pH >= 7.5) return "Alkaline";
else if (pH >= 6.5 && pH < 7.5) return "Neutral";
else return "Acidic";
}
// === Buzzer Handler ===
void handleBuzzer(String tempLevel, float salinity, float pH) {
unsigned long currentMillis = millis();
if ((tempLevel == "Level 1" || tempLevel == "Level 2" || tempLevel == "Level 3") && salinity <= 30 && pH <= 7.8) {
if (tempLevel == "Level 1" && currentMillis - lastToneTime >= 300) {
lastToneTime = currentMillis;
toneStep = (toneStep + 1) % 4;
(toneStep == 0 || toneStep == 2) ? tone(buzzerPin, 1000, 100) : noTone(buzzerPin);
} else if (tempLevel == "Level 2" && currentMillis - lastToneTime >= 500) {
lastToneTime = currentMillis;
toneStep = (toneStep + 1) % 4;
(toneStep == 0 || toneStep == 2) ? tone(buzzerPin, 1500, 200) : noTone(buzzerPin);
} else if (tempLevel == "Level 3" && currentMillis - lastToneTime >= 400) {
lastToneTime = currentMillis;
toneStep = (toneStep + 1) % 6;
(toneStep % 2 == 0) ? tone(buzzerPin, 800, 300) : tone(buzzerPin, 1200, 300);
}
} else {
noTone(buzzerPin);
}
}
// === WiFi Send Function ===
void sendToWebsite(float tempC, float salinity, float pH) {
String data = "temp=" + String(tempC, 1) + "&sal=" + String(salinity, 1) + "&ph=" + String(pH, 1);
esp8266.println("AT+CIPSTART=\"TCP\",\"fieldlink-server-using-esp8266.onrender.com\",80");
delay(2000);
String httpRequest = "GET /message?" + data + " HTTP/1.1\r\nHost: fieldlink-server-using-esp8266.onrender.com\r\nConnection: close\r\n\r\n";
esp8266.println("AT+CIPSEND=" + String(httpRequest.length()));
delay(1000);
esp8266.print(httpRequest);
delay(3000);
esp8266.println("AT+CIPCLOSE");
}
// === Setup ===
void setup() {
Serial.begin(9600);
esp8266.begin(115200);
lcd.begin(16, 2);
lcd.backlight();
pinMode(buzzerPin, OUTPUT);
// ESP8266 Init
esp8266.println("AT+RST");
delay(2000);
esp8266.println("AT+CWMODE=1");
delay(1000);
esp8266.println("AT+CWJAP=\"PLDTHOMEFIBR9d118\",\"PLDTWIFI5xuwg\"");
delay(5000);
}
// === Loop ===
void loop() {
// === Sensor Readings ===
int tempRaw = analogRead(tempPin);
float voltage = tempRaw * (5.0 / 1023.0);
float tempC = voltage * 10.0;
String tempLevel = getTempLevel(tempC);
int ecRaw = analogRead(ecPin);
float conductivity = map(ecRaw, 0, 1023, 0, 10000);
float salinity = 0.0008 * conductivity + 0.03;
String salLevel = getSalinityLevel(salinity);
int phRaw = analogRead(phPin);
float pH = map(phRaw, 0, 1023, 0, 1400) / 100.0;
String pHLevel = getPHLevel(pH);
// === Serial Output ===
Serial.print("Temp: "); Serial.print(tempC, 1); Serial.print(" C ("); Serial.print(tempLevel); Serial.print(") | ");
Serial.print("Salinity: "); Serial.print(salinity, 1); Serial.print(" ppt ("); Serial.print(salLevel); Serial.print(") | ");
Serial.print("pH: "); Serial.print(pH, 1); Serial.print(" ("); Serial.print(pHLevel); Serial.println(")");
// === LCD Output ===
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("T:"); lcd.print(tempC, 1); lcd.print("C S:"); lcd.print(salinity, 1); lcd.print(salLevel[0]);
lcd.setCursor(0, 1);
lcd.print("pH:"); lcd.print(pH, 1); lcd.print(" "); lcd.print(pHLevel.substring(0, 3));
// === Buzzer ===
handleBuzzer(tempLevel, salinity, pH);
// === WiFi Data Send ===
if (millis() - lastSendTime > sendInterval) {
sendToWebsite(tempC, salinity, pH);
lastSendTime = millis();
}
delay(200);
}Editor is loading...
Leave a Comment