Nick Elkins
Published © GPL3+

Smart Flower Pot

A smart watering system that automatically waters a plant.

IntermediateShowcase (no instructions)Over 1 day1,521
Smart Flower Pot

Things used in this project

Hardware components

Argon
Particle Argon
×1
Grove - Air quality sensor v1.3
Seeed Studio Grove - Air quality sensor v1.3
×1
Grove - Dust Sensor(PPD42NS)
Seeed Studio Grove - Dust Sensor(PPD42NS)
×1
Oled Display
×1
Water pump
×1
Gravity: Analog Capacitive Soil Moisture Sensor- Corrosion Resistant
DFRobot Gravity: Analog Capacitive Soil Moisture Sensor- Corrosion Resistant
×1
Relay Module (Generic)
×1
Gravity: I2C BME280 Environmental Sensor
DFRobot Gravity: I2C BME280 Environmental Sensor
×1

Software apps and online services

VS Code
Microsoft VS Code
Fusion
Autodesk Fusion

Hand tools and fabrication machines

Laser cutter (generic)
Laser cutter (generic)
Formlabs 3D resin printer

Story

Read more

Custom parts and enclosures

Spicket

Lid

Box

Flower pot

Schematics

Photos!

Photos

more pics

Dashboard

Inside box

Fritzing

Schematic a mess! but the main breadboard is done!

Fritzing photo

Code

Main code!

C/C++
Used in VSC code
/*
 * Project L14_04_Plantwater2
 * Description:
 * Author:
 * Date:
 */

#include "Adafruit_GFX.h"
#include "Adafruit_SSD1306.h"
#include <Adafruit_MQTT.h>
#include "Adafruit_MQTT/Adafruit_MQTT.h" 
#include "Adafruit_MQTT/Adafruit_MQTT_SPARK.h" 
#include "Adafruit_MQTT/Adafruit_MQTT.h" 
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include "Air_Quality_Sensor.h"

#define BME280_ADDRESS  (0x77)
#define OLED_RESET D4
Adafruit_SSD1306 display(OLED_RESET);
Adafruit_BME280 bme;
AirQualitySensor sensor(A2);


#define AIO_SERVER      "io.adafruit.com" 
#define AIO_SERVERPORT  1883   

TCPClient TheClient; 
Adafruit_MQTT_SPARK mqtt(&TheClient,AIO_SERVER,AIO_SERVERPORT,AIO_USERNAME,AIO_KEY); 

Adafruit_MQTT_Subscribe DOWN = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/DOWN"); 
Adafruit_MQTT_Publish UPP = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/UPP");
Adafruit_MQTT_Publish Soil = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Soil");
Adafruit_MQTT_Publish Temp = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Temp");
Adafruit_MQTT_Publish Humid = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Humid");
Adafruit_MQTT_Publish Quality = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Quality");
Adafruit_MQTT_Publish Dusty = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Dusty");

unsigned long last;
unsigned long lastTime;
unsigned long timer;
unsigned status;
float value;


int relay = A3;
int moistsense = A0;
int grovedust = A1;
int temperature;
int tempc;
int humidity;
int moist;
int qualityval; 

unsigned long duration;
unsigned long starttime;
unsigned long sample_ms = 10000;
unsigned long lowpulseoccupancy = 0;
float dusty;




void setup() {
   Serial.begin(9600);
  pinMode(relay, OUTPUT);
  pinMode(moistsense, INPUT);
  pinMode(grovedust, INPUT);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.setTextColor(WHITE);
  status = bme.begin();
  sensor.init();                                  //Seeed air quality
  display.display();
  delay(750); //wait for Serial Monitor to startup
  // if (!status) {
  //   Serial.print("issues with BME280");
  //   Serial.println(bme.sensorID(), 16);
  
  display.clearDisplay();
  display.display();
  mqtt.subscribe(&DOWN);




}


void loop() {
  MQTT_connect();
  roomcheck();
  water();
  dustsense();
  show();
  qualityval = analogRead(A2);
  if (value == 1) {
    digitalWrite(relay,HIGH);
  }
  else {
    digitalWrite(relay,LOW);
  }
  if ((millis()-last)>30000) {
      Serial.printf("Pinging MQTT \n");
      
      if(! mqtt.ping()) {
        Serial.printf("Disconnecting \n");
        mqtt.disconnect();
      }
      last = millis();
  }
 
  Adafruit_MQTT_Subscribe *subscription;
  while ((subscription = mqtt.readSubscription(4000))) {
    if (subscription == &DOWN) {
      value = atof((char *)DOWN.lastread);
     Serial.println(value);
    }
  }
    
  if((millis()-lastTime > 10000)) {
     if(mqtt.Update()) {
       
       Soil.publish(moist);
       Temp.publish(temperature);
       Humid.publish(humidity);
       Quality.publish(qualityval);
       Dusty.publish(dusty);
       
      
      Serial.println(lowpulseoccupancy);
       Serial.printf("Publishing %i \n",moist); 
       } 
     lastTime = millis();
   }
}

void MQTT_connect() {
  int8_t ret;
 
 
  if (mqtt.connected()) {
    return;
  }
 
  Serial.print("Connecting to MQTT... ");
 
  while ((ret = mqtt.connect()) != 0) { 
       Serial.println(mqtt.connectErrorString(ret));
       Serial.println("Retrying MQTT connection in 5 seconds...");
       mqtt.disconnect();
       delay(5000);  
  }
  Serial.println("MQTT Connected!");
}

void roomcheck() {
  temperature = bme.readTemperature();
  humidity = bme.readHumidity();
  moist = analogRead(moistsense);
}

void water() {
  if(moist > 2400) {
    if ((millis()-timer)>10000) {
      digitalWrite(relay,HIGH);
      delay(500);
      digitalWrite(relay,LOW);
      timer = millis();
    }
  }
}

void dustsense() {
  starttime = millis();
  duration = pulseIn(grovedust, LOW);
  lowpulseoccupancy = lowpulseoccupancy+duration;
  dusty = lowpulseoccupancy;

   if ((millis()-starttime) > sample_ms) {
     lowpulseoccupancy = 0;
   }
}

void show() {
display.clearDisplay();
display.setCursor(0,0);
display.print("Temp: ");
display.println(tempc);
display.print("Humidity: ");
display.println(humidity);
display.print("Waterlvl: ");
display.println(moist);
display.print("Airquality: ");
display.println(qualityval);
display.print("Dustvalue: ");
display.println(dusty);
display.display();
}

Credits

Nick Elkins

Nick Elkins

2 projects • 4 followers
I'm a 19 year old student at CNM, working with the IoT bootcamp. Currently full-time student, worked at Papa Johns before.

Comments