Stephanie Perea
Published © Apache-2.0

Zin Home Plant Watering System

Zin Home Waterfall with Automatic Plant Watering System

BeginnerShowcase (no instructions)Over 1 day377
Zin Home Plant Watering System

Things used in this project

Hardware components

Argon
Particle Argon
×1
0.96" OLED 64x128 Display Module
ElectroPeak 0.96" OLED 64x128 Display Module
×1
Grove - Relay
Seeed Grove - Relay
×1
Grove - Air quality sensor v1.3
Seeed Grove - Air quality sensor v1.3
×1
Grove - Dust Sensor(PPD42NS)
Seeed Grove - Dust Sensor(PPD42NS)
×1
Water Pump
×1
FreshFlow II 170oz
×1
GYBMED BME 280
×1
Capacitive Soil Moisture Sensor V1.2
×1

Software apps and online services

Visual Studio Code Extension for Arduino
Microsoft Visual Studio Code Extension for Arduino
Adafruit.io
GitHum
Fusion 360
Autodesk Fusion 360

Hand tools and fabrication machines

Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
Soldering iron (generic)
Soldering iron (generic)
Volt Meter
3D Printer (generic)
3D Printer (generic)

Story

Read more

Custom parts and enclosures

Zin Home Plant Watering System

AutoDesk 360 Fusion Flower Pot

Code

Zin Home Plant Watering System

C/C++
Relaxing Zin Fountain with Automating Plant Watering System
/**
* Project PlantWatering
* Description: L14_PlantWatering Midterm Project
* Author: Stephanie Perea
* Date: 29-Apr-2021
*/

#include <math.h>
#include <Adafruit_Sensor.h>
#include <wire.h>
#include <Adafruit_BME280.h>
  Adafruit_BME280 bme;

#include <Air_Quality.h>
  Air_Quality airqualitysensor;

#include <Adafruit_MQTT.h>

#include "Adafruit_MQTT/Adafruit_MQTT.h"
#include "Adafruit_MQTT/Adafruit_MQTT_SPARK.h"
#include "Adafruit_MQTT/Adafruit_MQTT.h"

#include <Adafruit_SSD1306.h>
#define SCREEN_ADDRESS 0x3C
#define OLED_RESET D4
  Adafruit_SSD1306 display(OLED_RESET);
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

/************ Global State (you don't need to change this!) ***   ***************/ 
TCPClient TheClient;

#include "credentials.h"

//Setup the MQTT client class with the WiFi client, MQTT server and login details.
Adafruit_MQTT_SPARK mqtt (&TheClient, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

/****************************** Feeds ***************************************/ 
//Setup Feeds to publish or subscribe
//Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>

Adafruit_MQTT_Subscribe DashBoardButton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/DashBoardButton");
Adafruit_MQTT_Publish TempF = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/TempF");
Adafruit_MQTT_Publish Humidity = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Humidity");
Adafruit_MQTT_Publish Pressure = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Pressure");
Adafruit_MQTT_Publish Soil = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Soil");
Adafruit_MQTT_Publish Dust = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Dust");
Adafruit_MQTT_Publish Air = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Air");

int last, lastTime;
bool buttonState;
long unsigned int publishFreq=60000; //sample every minute
/************Declare Variables*************/
String DateTime, TimeOnly;
char currentDateTime[25], currentTime[9];

int soilPin = A1;
int soilMoisture;
int soilMoistureLevel;

int airQualityPin=A0;
int airQuality;
int current_airQuality;

int lowpulseoccupancy;
int readtemp;

int dustPin = A4;
int dustLevel;

int buttonValue; //dashboard
int MQTT;

int i;
int pumpRelayPin=A5;

float value1; //variable to hold data from adafruit.io feed
float value2; //variable to hold data to be published to adafruit.io feed

float tempC;
float tempF;
float pressure;
float humidity;

float ratio=0;//dust
float concentration=0;//dust

unsigned long pumpLastTime;
unsigned long duration;//dust
unsigned long starttime;//dust
unsigned long sampletime_ms=30000;//dust
unsigned int currentTimer; //timer
unsigned int doneTime = 5000; //timer

// Start in SEMI_AUTOMATIC Mode
//SYSTEM_MODE(SEMI_AUTOMATIC);

void setup() {  
  Time.zone(-6); //MST=-7, MDT=-6;
  Particle.syncTime(); //Sync time with Particle Cloud
  Serial.printf("Connecting to Internet\n");
  WiFi.connect();
  while(WiFi.connecting()){
  Serial.printf(".");
  delay(100);
  }
  Serial.printf("\n Connected!!!!!! \n");
  Serial.begin(9600);
  currentTimer = millis(); //timer
  pinMode(soilPin, INPUT);
  pinMode(airQuality, INPUT);
  pinMode(dustPin, INPUT);
  pinMode(pumpRelayPin,OUTPUT);
  starttime=millis();//dust
  bme.begin(0x76);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
  display.display();
  display.clearDisplay();
  display.setCursor(0,0);
  display.setRotation(0);
  display.setTextSize(1);
  display.setTextColor(WHITE);
  delay(5000);
}    
     
void loop(){
    DateTime = Time.timeStr(); // Current Date and Time from Particle Time class
    TimeOnly = DateTime.substring(11,19); //Extract the Time from the DateTime String

    //Convert String to char arrays - this is needed for formatted print
    DateTime.toCharArray(currentDateTime,25);
    TimeOnly.toCharArray(currentTime,9);
    //Print using formatted print
    //display.printf("%s\n" ,currentDateTime);
    display.printf("Time %s\n" ,currentTime);
    display.display();
    //delay(3000);

    soilMoisture=analogRead(soilPin);
      if(soilMoisture > 3000){
        display.printf("I Need Water\n");
        display.display();
        //delay(3000);
      }
      //following code allows system to water plant
      i=0;
      if(soilMoisture >3000) {
        for(i=0; i<= 5; i++){
          digitalWrite(pumpRelayPin, HIGH);
          delay(5000);
          digitalWrite(pumpRelayPin,LOW);
          delay(5000);
            while(i>5){
              digitalWrite(pumpRelayPin,LOW);
              //delay(3000);
            }
        }
      }
      if(soilMoisture>3000){
          for(i=0; i<= 5; i++){
            digitalWrite(pumpRelayPin, HIGH);
            delay(5000);
            digitalWrite(pumpRelayPin,LOW);
            delay(5000);
              while(i>5){
                digitalWrite(pumpRelayPin,LOW);
                //delay(3000);
               }
          }
        }        
  Serial.printf("soilMoisture is %i\n",soilMoisture);
    delay(1000);
  if(soilMoisture<3000){
    display.printf("\n""Im a Happy Plant\n");
    display.display();
    //delay(3000);
    //Serial.printf("The soilMoisture reading is NOT greater than 3000 - soilMoisture: %i/n", soilMoisture);
    //delay (5000); //soil delay read every 5 seconds
  } 
  
  tempF = ((bme.readTemperature()*9/5)+32);
  pressure=bme.readPressure();
  humidity = bme.readHumidity();
  display.printf("TempF = %0.2f\n",tempF);
  display.printf("Pressure = %0.2f\n",pressure);
  display.printf("Humidity= %0.2f\n",humidity);
  display.display();
  airQuality=analogRead(airQualityPin);
    if(current_airQuality<=0){
      display.println("Air Quality Low");
      display.display();
      //delay(3000);
    }
  airQuality=analogRead(airQualityPin);
    if (current_airQuality==1){
      display.println("Air Quality Moderate");
      display.display();
      //delay(3000);
    }
    if (current_airQuality==2) {
      display.println("Air Quality Good");
      display.display();
      //delay(3000);
    }
    if (current_airQuality==3){
      display.println("Fresh air");
      display.display();
      //delay(3000);
    }  
  dustLevel=analogRead(dustPin);
    duration=pulseIn(A4, LOW);
    lowpulseoccupancy=lowpulseoccupancy+duration;
      if((millis()-starttime) > sampletime_ms){
        ratio=lowpulseoccupancy/(sampletime_ms*10.0);
        concentration=1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62;
        Serial.print(lowpulseoccupancy);
        Serial.print(",");
        Serial.print(ratio);
        Serial.print(",");
        Serial.println(concentration);
        lowpulseoccupancy=0;
        starttime=millis();
    }
    if(dustLevel<10000){
      display.printf("Dust Level Low\n");
      display.display();
      //delay(3000);
    }
    if(dustLevel>20000){
      display.printf("Dust Level Moderate\n");
      display.display();
      //delay(3000);
    }
    if(dustLevel>30000){
      display.printf("Dust Level High\n");
      display.display();
      //delay(3000);
    }
      //currentTimer = millis(); //timer
      if((millis()-lastTime> publishFreq)){ //publish to MQTT feed every 1 minutes
        TempF.publish(tempF);
        Humidity.publish(humidity);
        Pressure.publish(pressure);
        Soil.publish(soilMoisture);
        Dust.publish(dustLevel);
        Air.publish(airQuality);
        lastTime=millis();
        Serial.printf("TempF = %0.2f\n",tempF);
        Serial.printf("Pressure = %0.2f\n",pressure);
        Serial.printf("Humidity= %0.2f\n",humidity);
      }
        
      
    int MQTTbegin();//dashboard
      int8_t ret; //dashboard
      if(mqtt.connected()) { //dashboard
      return; //dashboard
      Serial.print("Connecting to MQTT... "); //dashboard
      }
      while((ret = mqtt.connect()) != 0) { //dashboard
        Serial.println(mqtt.connectErrorString(ret)); //dashboard
        Serial.println("Retrying MQTT connection in 5 seconds..."); //dashboard
        mqtt.disconnect(); //dashboard
        //delay(5000); //dashboard
        Serial.println("MQTT Connected!"); //dashboard
      } 
    //Validate connected to MQTT Broker
    //MQTT_connect();
    //Ping MQTT Broker every 2 minutes to keep connection alive
    if((millis()-last)>120000){
      Serial.printf("Pinging MQTT\n");
        if(!mqtt.ping()){
          Serial.printf("Disconnecting \n");
          mqtt.disconnect();
        }
      last = millis();
    }
    //this is our 'wait for incoming subscriptio packets' busy subloop
      Adafruit_MQTT_Subscribe *subscription;
        while ((subscription = mqtt.readSubscription(3000))){
          if(subscription == &DashBoardButton){
            buttonValue = atof((char *)DashBoardButton.lastread); //dashboard
          }
        }
     
      if(buttonValue == 1){ //dashboard
        Serial.printf("\n""Button is pushed\n"); //dashboard
        
    }
    if((millis()-lastTime>30000)) {
      if(mqtt.Update()) {
    //<feedvar2>.publish(<variable>);
    //Serial.printf("Publishing %0.2f \n",<variables>);
      }
    }
    //Function to connect and reconnect as necessary to the MQTT server.
        //Should be called in the loop function and it will take care if connection.
  void MQTT_connect();{
    int8_t ret;
    //Stop if already connected.
    if (mqtt.connected()){
      return;
    }
    Serial.print("Connecting to MQTT...");
      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
    }
      Serial.println("MQTT Connected!");
  }
}

Credits

Stephanie Perea

Stephanie Perea

2 projects • 4 followers
Hello, I am retired and back in school reinventing myself. I have always enjoyed science and technology and I am in an IoT Bootcamp.

Comments

Add projectSign up / Login