Last active
May 31, 2022 04:29
-
-
Save lakshith-403/e60b89db3ddffbcb93e7f0d02532f035 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <FirebaseESP8266.h> | |
#include <ESP8266WiFi.h> | |
#include <Wire.h> | |
#include <BH1750FVI.h> | |
#include "DHT.h" | |
#define DHTPIN D1 | |
#define SOIL_DIGITAL_PIN D0 | |
#define SOIL_ANALOG_PIN A0 | |
#define RELAY_PIN D2 | |
#define DHTTYPE DHT11 | |
DHT dht(DHTPIN, DHTTYPE); | |
struct DHT11Reading{ | |
float humidity; | |
float temperature; | |
float heatIndex; | |
}; | |
struct soilReading{ | |
int reading; | |
int threshold; | |
String conclusion; | |
}; | |
// Settings | |
uint8_t ADDRESSPIN = 13; | |
BH1750FVI::eDeviceAddress_t DEVICEADDRESS = BH1750FVI::k_DevAddress_H; | |
BH1750FVI::eDeviceMode_t DEVICEMODE = BH1750FVI::k_DevModeContHighRes; | |
// Create the Lightsensor instance | |
BH1750FVI LightSensor(ADDRESSPIN, DEVICEADDRESS, DEVICEMODE); | |
/*****Change thresholds to match your needs (at what temp should fan be on)*******/ | |
const int soilThreshold = 600; | |
const int temperatureThreshold = 80; | |
bool fanStatus = false; | |
/****** CHANGE THESE *******/ | |
#define FIREBASE_HOST "irrigation-system-7aa74.firebaseio.com" | |
#define FIREBASE_AUTH "3fbE739ex93tQ1twjpT4CZR1W7fxJMaTTkPhxX5F" | |
#define WIFI_SSID "tikki tikiri tikiri liya" | |
#define WIFI_PASSWORD "kalethAranLidataGiya" | |
FirebaseData firebaseData; | |
void firebaseSetup() | |
{ | |
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | |
Serial.print("Connecting to Wi-Fi"); | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
Serial.print("."); | |
delay(300); | |
} | |
Serial.println(); | |
Serial.print("Connected with IP: "); | |
Serial.println(WiFi.localIP()); | |
Serial.println(); | |
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH); | |
Firebase.reconnectWiFi(true); | |
firebaseData.setBSSLBufferSize(1024, 1024); | |
firebaseData.setResponseSize(1024); | |
} | |
void setup() { | |
Serial.begin(115200); | |
firebaseSetup(); | |
pinMode(SOIL_DIGITAL_PIN,INPUT); | |
pinMode(RELAY_PIN,OUTPUT); | |
LightSensor.begin(); | |
Serial.println("Running..."); | |
dht.begin(); | |
} | |
DHT11Reading readDHT(){ | |
float h = dht.readHumidity(); | |
float t = dht.readTemperature(); | |
float f = dht.readTemperature(true); | |
if (isnan(h) || isnan(t) || isnan(f)) { | |
Serial.println(F("Failed to read from DHT sensor!")); | |
return {-1,-1,-1}; | |
} | |
float hif = dht.computeHeatIndex(f, h); | |
float hic = dht.computeHeatIndex(t, h, false); | |
Serial.print(F(" Humidity: ")); | |
Serial.print(h); | |
Serial.print(F("% Temperature: ")); | |
Serial.print(t); | |
Serial.print(F("C ")); | |
Serial.print(f); | |
Serial.print(F("F Heat index: ")); | |
Serial.print(hic); | |
Serial.print(F("C ")); | |
Serial.print(hif); | |
Serial.println(F("F")); | |
return((DHT11Reading){h,t,f}); | |
} | |
soilReading readSoil(){ | |
int reading = analogRead(SOIL_ANALOG_PIN); | |
Serial.print("Soil moisture reading: "); | |
Serial.println(reading); | |
return ((soilReading){reading,soilThreshold,(reading >= soilThreshold ? "Dry": "Wet")}); | |
} | |
int readLight(){ | |
uint16_t lux = LightSensor.GetLightIntensity(); | |
Serial.print("Light: "); | |
Serial.println(lux); | |
return lux; | |
} | |
void setFanStatus(bool stat){ | |
digitalWrite(RELAY_PIN,stat); | |
fanStatus = stat; | |
} | |
void updateFanStatus(DHT11Reading dhtr){ | |
if(dhtr.temperature >= temperatureThreshold) | |
setFanStatus(1); | |
else | |
setFanStatus(0); | |
} | |
bool sendDataToCloud(soilReading sr, DHT11Reading dhtr, bool mtr, int lht){ | |
Firebase.set(firebaseData,"/soilMoisture/", sr.conclusion); | |
Firebase.set(firebaseData,"/humidity/", dhtr.humidity); | |
Firebase.set(firebaseData,"/temperature/", dhtr.temperature); | |
Firebase.set(firebaseData,"/heatIndex/", dhtr.heatIndex); | |
Firebase.set(firebaseData,"/fanStatus/", (mtr?"ON":"OFF")); | |
Firebase.set(firebaseData,"/light/", lht); | |
return true; | |
} | |
void loop() { | |
delay(2000); | |
soilReading sr = readSoil(); | |
DHT11Reading dhtr = readDHT(); | |
bool mtr = fanStatus; | |
int lht = readLight(); | |
updateFanStatus(dhtr); | |
bool res = sendDataToCloud(sr,dhtr,mtr,lht); | |
Serial.println(res?"Succesfully updated the cloud database!":"Cloud Data sending failed!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment