#include <WiFi.h>
#include <LiquidCrystal_I2C.h>
#include "DHTesp.h"
#include "ThingSpeak.h"
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
const int RLED_PIN = 27;
const int BLED_PIN = 14;
const int POT_PIN = 2;
//float PH;
//float Value=0;
const int DHT_PIN = 4;
const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
const int myChannelNumber =2226105 ;
const char* myApiKey = "5ZZNFKTB60NEJIV9";
const char* server = "api.thingspeak.com";
DHTesp dhtSensor;
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
WiFiClient client;
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(RLED_PIN, OUTPUT);
pinMode(RLED_PIN, OUTPUT);
pinMode(POT_PIN,INPUT);
delay(1000);
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED){
delay(1000);
Serial.println("Wifi not connected");
}
Serial.println("Wifi connected !");
Serial.println("Local IP: " + String(WiFi.localIP()));
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
}
void loop() {
//Temp
TempAndHumidity data = dhtSensor.getTempAndHumidity();
ThingSpeak.setField(1,data.temperature);
//PH value
float PHValue = map(analogRead(POT_PIN), 0, 4095, 0.0, 14.0);
//PHValue = analogRead(POT_PIN);
Serial.print("pH Value: ");
Serial.println(PHValue, 2); // Print pH value with 2 decimal places
//float voltage=Value*(3.3/4095.0);
//ph = (3.3*voltage);
//Serial.println(ph);
ThingSpeak.setField(2,PHValue);
//delay(500)
//LED
//digitalWrite(RLED_PIN, PHValue <= 6.0 && PHValue >= 8.0);
//digitalWrite(BLED_PIN, PHValue >= 6.0 && PHValue <= 8.0);
if (data.temperature >= 35 || data.temperature <= 12 || PHValue >= 6 || PHValue <= 8) {
digitalWrite(RLED_PIN, LOW);
digitalWrite(RLED_PIN, HIGH);
}else{
digitalWrite(RLED_PIN, HIGH);
digitalWrite(BLED_PIN, LOW);
}
int x = ThingSpeak.writeFields(myChannelNumber,myApiKey);
if(x == 200){
Serial.println("Data pushed successfull");
}else{
Serial.println("Push error" + String(x));
}
Serial.println("---");
//LCD
lcd.setCursor(0, 0);
lcd.print(" Temp: " + String(data.temperature, 1) + "\xDF"+"C ");
lcd.setCursor(0, 1);
lcd.print(" PH: " + String(PHValue, 1));
lcd.print("Wokwi Online IoT");
delay(10000);
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}