#include <ESP8266WiFi.h>;
#include <WiFiClient.h>;
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <Wire.h>
#define DHTPIN 13
#define rly 15
LiquidCrystal_I2C lcd(0x3F, 16, 2);
String apiKey = "9E66C0QZECQTFFCY"; //fill in the api key from thingspeak
const char* ssid = "(1 + sqrt(5)) / 2"; //fill in your wifi name
const char* password = "1.6180339887498948482045868343656"; //fill in your wifi password
const char* server = "api.thingspeak.com";
int SensorPin = A0; //Sensor Pin Connected at A0 Pin
int Enable1 = 0;
int Enable2 = 2;
int SensorValue1 = 0;
int SensorValue2 = 0;
DHT dht(DHTPIN, DHT22);
WiFiClient client;
void setup(){
Serial.begin(115200);
delay(10);
dht.begin();
lcd.begin();
WiFi.begin(ssid, password); // Connect to WiFi network
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Serial.println("WiFi connected");
Serial.println();
pinMode(Enable1,OUTPUT);
pinMode(Enable2,OUTPUT);
pinMode(rly,OUTPUT);
}
void loop(){
//DHT22
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
delay(1000);
}
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" degrees Celcius ");
Serial.println();
Serial.print("Humidity: ");
Serial.print(h);
Serial.print("%");
Serial.println();
//Sensor
digitalWrite(Enable1, HIGH);
SensorValue1 = analogRead(SensorPin);
Serial.print("Water Quality: ");
Serial.println(SensorValue1);
digitalWrite(Enable1, LOW);
delay(500);
digitalWrite(Enable2, HIGH);
SensorValue2 = analogRead(SensorPin);
Serial.print("Light intensity: ");
Serial.println(SensorValue2);
digitalWrite(Enable2, LOW);
delay(500);
//thingspeak
if (client.connect(server,80)) { // api.thingspeak.com
String postStr = apiKey;
postStr +="&field1=";
postStr += String(t);
postStr +="&field2=";
postStr += String(h);
postStr +="&field3=";
postStr += String(SensorValue1);
//postStr +="&field7=";
postStr += "\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n\n\n\n\n\n\n");
client.print(postStr);
if(SensorValue2>500){
digitalWrite(rly,HIGH);
}else{
digitalWrite(rly,LOW);
}
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp= ");
lcd.print(t);
lcd.print(" C");
lcd.setCursor(0,1);
lcd.print("Water= ");
lcd.print(SensorValue1);
lcd.noBlink();
delay(5000);
lcd.noBlink();
delay(5000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Hum= ");
lcd.print(h);
lcd.print(" %");
lcd.setCursor(0,1);
lcd.print("Water= ");
lcd.print(SensorValue1);
delay(10000);
}
Comments
Please log in or sign up to comment.