Zaid Jaber
Published © LGPL

IoT Powerd Green House

An IoT-powered greenhouse automates temperature, humidity, and irrigation control, optimizing plant growth.

AdvancedFull instructions provided316
IoT Powerd Green House

Things used in this project

Hardware components

FireBeetle ESP32-E IoT Microcontroller with Header (Supports Wi-Fi & Bluetooth)
DFRobot FireBeetle ESP32-E IoT Microcontroller with Header (Supports Wi-Fi & Bluetooth)
×1
2.8 TFT LCD RGB ILI9341
×1
LDR module (Light Sensor)
×1
DHT22 temperature-humidity sensor
Adafruit DHT22 temperature-humidity sensor
×1
Soil Moisture Sensor Module
×1
4 Relay Module
×1
LM2596 DC to DC Step down
×1

Software apps and online services

Arduino IDE
Arduino IDE
Adafruit IO

Story

Read more

Schematics

Circuit Diagram

Code

Untitled file

Arduino
#include <WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h>
#include <Adafruit_MQTT.h>
#include <Adafruit_MQTT_Client.h>
#include "DHT.h"
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"

#define WLAN_SSID ""                  // Your SSID
#define WLAN_PASS ""                  // Your password
#define AIO_SERVER "io.adafruit.com"  //Adafruit Server
#define AIO_SERVERPORT 1883
#define AIO_USERNAME ""  // get this from you account
#define AIO_KEY ""       // get this from you account

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
Adafruit_MQTT_Publish dataT = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/IndoorTemp");
Adafruit_MQTT_Publish dataH = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/IndoorHum");
Adafruit_MQTT_Publish dataL = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Light");
Adafruit_MQTT_Publish dataP1 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/MP1");
Adafruit_MQTT_Publish dataP2 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/MP2");
Adafruit_MQTT_Subscribe Fan = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Fan");
Adafruit_MQTT_Subscribe Pump = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Pump");
const char WILL_FEED[] PROGMEM = AIO_USERNAME "/feeds/Fan";
Adafruit_MQTT_Publish lastwill = Adafruit_MQTT_Publish(&mqtt, WILL_FEED, MQTT_QOS_1);
const char WILL_FEED2[] PROGMEM = AIO_USERNAME "/feeds/Pump";
Adafruit_MQTT_Publish lastwill2 = Adafruit_MQTT_Publish(&mqtt, WILL_FEED2, MQTT_QOS_1);
//Create a wifi manager
WiFiManager manager;



#define DHTOUT 4  // Digital pin connected to the outdoor DHT sensor
#define DHTIN 2   // Digital pin connected to the indoor DHT sensor

Adafruit_ILI9341 tft = Adafruit_ILI9341(22, 13, 23, 12, 21, 19);  //(22?, DC, MOSI,CLK, RES, MISO)
#define DHTTYPE DHT22
DHT dhtout(DHTOUT, DHTTYPE);
DHT dhtin(DHTIN, DHTTYPE);

int Light, Brightness, SoilMoistureP1, moisP1, SoilMoistureP2, moisP2;
int Fanpin = 25;
int Pumppin = 26;
void MQTT_connect();

void setup() {

  pinMode(Fanpin, OUTPUT);
  pinMode(Pumppin, OUTPUT);
  digitalWrite(Fanpin, 1);
  digitalWrite(Pumppin, 1);
  Serial.begin(9600);
  Serial.println("ILI9341 Test!");
  tft.begin();
  dhtout.begin();
  dhtin.begin();
  // read diagnostics (optional but can help debug problems)
  uint8_t x = tft.readcommand8(ILI9341_RDMODE);
  Serial.print("Display Power Mode: 0x");
  Serial.println(x, HEX);
  x = tft.readcommand8(ILI9341_RDMADCTL);
  Serial.print("MADCTL Mode: 0x");
  Serial.println(x, HEX);
  x = tft.readcommand8(ILI9341_RDPIXFMT);
  Serial.print("Pixel Format: 0x");
  Serial.println(x, HEX);
  x = tft.readcommand8(ILI9341_RDIMGFMT);
  Serial.print("Image Format: 0x");
  Serial.println(x, HEX);
  x = tft.readcommand8(ILI9341_RDSELFDIAG);
  Serial.print("Self Diagnostic: 0x");
  Serial.println(x, HEX);

  tft.fillScreen(ILI9341_BLACK);
  tft.setRotation(1);
  tft.setCursor(0, 0);
  tft.setTextColor(ILI9341_YELLOW);
  tft.setTextSize(2);
  tft.println("Wifi Scanning ... ");

  manager.autoConnect("GreenHouse");

  // WiFi.begin(WLAN_SSID, WLAN_PASS);
  // read the serial port for new passwords and maintain connections
  tft.fillScreen(ILI9341_BLACK);
  tft.setRotation(1);
  tft.setCursor(0, 0);
  tft.setTextColor(ILI9341_YELLOW);
  tft.setTextSize(2);
  tft.println("Connecting to the WIFI");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    tft.setCursor(3, 0);
    tft.setTextColor(ILI9341_RED);
    tft.setTextSize(2);
    tft.print(".");
  }
  tft.fillScreen(ILI9341_BLACK);
  tft.setRotation(1);
  tft.setCursor(0, 0);
  tft.setTextColor(ILI9341_YELLOW);
  tft.setTextSize(3);
  tft.println("WIFI Connected");
  //Serial.println();

  //Serial.println(F("WiFi connected"));
  //Serial.println(F("IP address: "));
  //Serial.println(WiFi.localIP());

  mqtt.subscribe(&Fan);
  mqtt.subscribe(&Pump);
  mqtt.will(WILL_FEED, "OFF");
  mqtt.will(WILL_FEED2, "OFF");
  MQTT_connect();
  lastwill.publish("OFF");
  lastwill2.publish("OFF");
}

uint32_t x = 0;
void loop(void) {

  MQTT_connect();

  tft.fillScreen(ILI9341_BLACK);
  tft.setRotation(1);
  testText();

  int x;
  x = dhtout.readTemperature();
  // Grab the current state of the sensor
  if (!dataT.publish(x)) {
    //Serial.println(F("Failed to publish data"));
  } else {
    //Serial.println(F("data published!"));
  }
  int y;
  y = dhtout.readHumidity();
  // Grab the current state of the sensor
  if (!dataH.publish(y)) {
    //Serial.println(F("Failed to publish data"));
  } else {
    //Serial.println(F("data published!"));
  };
  BrightnessSensor();
  // Publish data
  if (!dataL.publish(Brightness)) {
    //Serial.println(F("Failed to publish data"));
  } else {
    //Serial.println(F("data published!"));
  }

  MoistureP1();
  // Publish data
  if (!dataP1.publish(SoilMoistureP1)) {
    //Serial.println(F("Failed to publish data"));
  } else {
    //Serial.println(F("data published!"));
  }
  MoistureP2();
  // Publish data
  if (!dataP2.publish(SoilMoistureP2)) {
    //Serial.println(F("Failed to publish data"));
  } else {
    //Serial.println(F("data published!"));
  }
  Adafruit_MQTT_Subscribe *subscription;
  while ((subscription = mqtt.readSubscription(30000))) {
    if (subscription == &Fan) {
      if (strcmp((char *)Fan.lastread, "ON") == 0) {
        digitalWrite(Fanpin, LOW);
      }
      if (strcmp((char *)Fan.lastread, "OFF") == 0) {
        digitalWrite(Fanpin, HIGH);
      }
    }
    if (subscription == &Pump) {
      if (strcmp((char *)Pump.lastread, "ON") == 0) {
        digitalWrite(Pumppin, LOW);
      }
      if (strcmp((char *)Pump.lastread, "OFF") == 0) {
        digitalWrite(Pumppin, HIGH);
      }
    }
  }
  //delay(30000);
}

unsigned long testText() {
  ///////////////////////////////////////Temp Sensor Outside ////////////////////////////////////////
  //  tft.fillScreen(ILI9341_BLACK);
  tft.setCursor(0, 0);
  tft.setTextColor(ILI9341_YELLOW);
  tft.setTextSize(2);
  tft.println("Outdoor Temperature:");
  tft.setTextColor(ILI9341_RED);
  tft.setTextSize(2);
  tft.println(dhtout.readTemperature());
  tft.setTextColor(ILI9341_YELLOW);
  tft.setTextSize(2);
  tft.println("Outdoor Humidity:");
  tft.setTextColor(ILI9341_RED);
  tft.setTextSize(2);
  tft.println(dhtout.readHumidity());
  ////////////////////////////////////Temp Sensor inside ////////////////////////////////////
  tft.setTextColor(ILI9341_YELLOW);
  tft.setTextSize(2);
  tft.println("Indoor Temperature:");
  tft.setTextColor(ILI9341_RED);
  tft.setTextSize(2);
  tft.println(dhtin.readTemperature());
  tft.setTextColor(ILI9341_YELLOW);
  tft.setTextSize(2);
  tft.println("Indoor Humidity:");
  tft.setTextColor(ILI9341_RED);
  tft.setTextSize(2);
  tft.println(dhtin.readHumidity());

  ///////////////////////////////Light Sensor /////////////////////////////////
  tft.setTextColor(ILI9341_YELLOW);
  tft.setTextSize(2);
  tft.println("Brightness:");
  tft.setTextColor(ILI9341_RED);
  tft.setTextSize(2);
  BrightnessSensor();
  tft.println(Brightness);  // this is the analog pin
////////////////////////MoisP1 Sensor /////////////////////////////////
  tft.setTextColor(ILI9341_YELLOW);
  tft.setTextSize(2);
  tft.println("Soil Moisture P1:");
  MoistureP1();
  tft.setTextColor(ILI9341_RED);
  tft.setTextSize(2);
  tft.println(SoilMoistureP1);  // this is the analog pin
   /////////////////////////MoisP2 Sensor //////////////////////////////
  tft.setTextColor(ILI9341_YELLOW);
  tft.setTextSize(2);
  tft.println("Soil Moisture P2:");
  MoistureP2();
  tft.setTextColor(ILI9341_RED);
  tft.setTextSize(2);
  tft.println(SoilMoistureP2);  // this is the analog pin
}
void BrightnessSensor() {
  if (analogRead(34) > 2200) {
    Light = 2200;
  } else if (analogRead(34) < 400) {
    Light = 400;
  } else {
    Light = analogRead(34);
  }
  Brightness = map(Light, 400, 2200, 100, 0);
}
void MoistureP1() {
  if (analogRead(32) > 4000) {
    moisP1 = 4000;
  } else if (analogRead(32) < 10) {
    moisP1 = 10;
  } else {
    moisP1 = analogRead(32);
  }
  SoilMoistureP1 = map(moisP1, 10, 4000, 100, 0);
}
void MoistureP2() {
  if (analogRead(35) > 4000) {
    moisP2 = 4000;
  } else if (analogRead(35) < 10) {
    moisP2 = 10;
  } else {
    moisP2 = analogRead(35);
  }
  SoilMoistureP2 = map(moisP2, 10, 4000, 100, 0);
}

void MQTT_connect() {
  int8_t ret;

  // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }

  Serial.print("Connecting to MQTT... ");

  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) {  // connect will return 0 for connected
    Serial.println(mqtt.connectErrorString(ret));
    Serial.println("Retrying MQTT connection in 5 seconds...");
    mqtt.disconnect();
    delay(5000);  // wait 5 seconds
    retries--;
    if (retries == 0) {
      // basically die and wait for WDT to reset me
      while (1)
        ;
    }
  }
  Serial.println("MQTT Connected!");
}

Credits

Zaid Jaber
4 projects • 4 followers
STEM Specialist, Maker, Innovator, Mechatronics Engineer, and Master's in Electrical Engineering. Passionate about innovation and tinkering.
Contact
Thanks to Iman Suhimat .

Comments

Please log in or sign up to comment.