本网页已闲置超过3分钟,按键盘任意键或点击空白处,即可回到网页

智能房屋植物浇水系统
发布时间:2021-05-08
分享到:
智能房屋植物浇水系统
发布时间:2021-05-08
分享到:

该智能植物浇水系统收集周围环境的数据(如空气质量、温度)、测量植物的水分含量,并通过直流电动机组件(该电动机组件的作用类似泵),使植物始终保持在最佳水合状态,保持其最鲜艳的颜色。

硬件组件:

 电路图:

代码(水系统代码):

它是Visual Studio Code上的一个代码,该代码旨在在土壤湿度降至45%以下时自动为植物浇水。

/*

 *
 * L14_04_PlantWater项目
 *说明:快速获取我们的工厂用水
 *作者:Kareem Crum
 *日期:2021年4月19日
 * /

//#include的

#include <Adafruit_MQTT.h>
#include <SPI.h>
#include <Wire.h>

#include "Adafruit_MQTT/Adafruit_MQTT.h" 
#include "Adafruit_MQTT/Adafruit_MQTT_SPARK.h" 
#include "Adafruit_MQTT/Adafruit_MQTT.h" 
#include "Adafruit_BME280.h"
#include "Adafruit_Sensor.h"
#include "Grove_Air_quality_Sensor.h"

#include "Adafruit_SSD1306.h"
#include "credentials.h"
#include "math.h"

#define SCREEN_WIDTH 128 // OLED显示宽度(以像素为单位)
#define SCREEN_HEIGHT 64 // OLED显示高度(以像素为单位)
#define OLED_RESET    D4 // 重置引脚号(如果共享Arduino重置引脚,则为-1)
#define SCREEN_ADDRESS 0x3C /// <参见数据表中的地址; 0x3D表示128x64,0x3C表示128x32


//全球状态
TCPClient TheClient;

// Adafruit BME类
Adafruit_BME280 bme;

//植物空气质量传感器等级
AirQualitySensor sensor(A2);

//Adafruit 展示类
Adafruit_SSD1306 display(OLED_RESET);

//MQTT 客户端类
Adafruit_MQTT_SPARK mqtt(&TheClient,AIO_SERVER,AIO_SERVERPORT,AIO_USERNAME,AIO_KEY); 

//Feeds
Adafruit_MQTT_Subscribe mqttSub1 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/manuelButton"); 
Adafruit_MQTT_Publish Moist = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/SoilMoisture");
Adafruit_MQTT_Publish Pressure = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Pressure");
Adafruit_MQTT_Publish Humid = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Humidity");
Adafruit_MQTT_Publish Temp = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Temperature");
Adafruit_MQTT_Publish Air = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/AirQuality");
Adafruit_MQTT_Publish Dust = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/DustSensor");

//变量
const int VALUE1 = 620;
const int VALUE2 = 310;
unsigned long lastMQTT;
unsigned long lastPub;
unsigned long dustTime;
unsigned long duration;
unsigned long waterTime;
unsigned long sampletime_ms = 30000;
unsigned long lowpulseoccupancy = 0;
int dustSensor = A0;
int soilSensor = A1;
int relay = A4;
int soilMoisturePercent; 
int soilMoistureValue; 
int current_quality =-1;
float dustSense;
float airSense;
float tempC;
float humidRH;
float pressPA;
float tempF;
float inHg;
float ratio = 0;
float concentration = 0;
bool buttonState;

SYSTEM_MODE(SEMI_AUTOMATIC);

//首次打开设备时,setup()运行一次。
void setup() 
{
  //像pinMode这样放置初始化并在此处开始功能

  pinMode(soilSensor, INPUT);
  pinMode(dustSensor, INPUT);
  pinMode(relay, OUTPUT);

  Serial.begin(9600);
  bool status;
  status = bme.begin(0x76);
  sensor.init();
  dustTime = millis();
  delay(100); //等待串行监视器启动

  Serial.printf("Connecting to Internet \n");
  WiFi.connect();
  while(WiFi.connecting()) {
    Serial.printf(".");
    delay(100);
  }
  Serial.printf("\n Connected!!!!!! \n");
  // 为开/关feed设置MQTT订阅
  mqtt.subscribe(&mqttSub1);

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.display();
  delay(2000);
  display.clearDisplay();
}

void loop()
 {

  current_quality=sensor.slope();
  if (current_quality >= 0)// 如果返回有效数据
  {
      if (current_quality==0)
          Serial.println("High pollution! Force signal active");
      else if (current_quality==1)
          Serial.println("High pollution!");
      else if (current_quality==2)
          Serial.println("Low pollution!");
      else if (current_quality ==3)
          Serial.println("Fresh air");    
  }

  duration = pulseIn(dustSensor, LOW);
  lowpulseoccupancy = lowpulseoccupancy + duration;
  if ((millis()-dustTime) >= 30000){
    ratio = lowpulseoccupancy/(30000*10.0);
    concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62;
    if(concentration > 1){
      dustSense = concentration;
    }
    Serial.printf("Concentration = %f pcs/0.01cf",dustSense);
    lowpulseoccupancy = 0;
    dustTime = millis();
  }
 

  // 每2分钟对MQTT Broker进行一次Ping操作,以保持连接状态
  if ((millis()-lastMQTT)>120000) 
  {
      Serial.printf("Pinging MQTT \n");
      if(! mqtt.ping()) {
        Serial.printf("Disconnecting \n");
        mqtt.disconnect();
      }
      lastMQTT = millis();
  }

  // 这是我们的“等待传入订阅包”繁忙子循环
 
  Adafruit_MQTT_Subscribe *subscription;
  while ((subscription = mqtt.readSubscription(1000)))
   {
    if (subscription == &mqttSub1) {
      buttonState = atoi((char *)mqttSub1.lastread);
      Serial.printf("buttonState is %i\n", buttonState);
    }   
  }

  if (buttonState == 1)
  {
      digitalWrite(relay,HIGH);
      Serial.printf("buttonState is on \n");
  }
    else
    {
      digitalWrite(relay,LOW);
      Serial.printf("buttonState is off \n");
    }

  if((millis()-lastPub > 30000)) 
  {
    if(mqtt.Update()) 
    {
      Moist.publish(soilMoisturePercent);
      Air.publish(airSense);
      Temp.publish(tempF);
      Humid.publish(humidRH);
      Dust.publish(dustSense);
      Pressure.publish(inHg);
 
    } 
    lastPub = millis();
  }
  soilMoistureValue = analogRead(soilSensor);
  Serial.printf("Soil moisture is %i\n", soilMoistureValue);
  soilMoisturePercent = map(soilMoistureValue, 1800, 3500, 100, 0);

  tempC = bme.readTemperature();
  tempF = map(tempC,0.0,100.0,32.0,212.0);
  pressPA = bme.readPressure();
  inHg = pressPA/3386.389;
  humidRH = bme.readHumidity();
  airSense = sensor.getValue();
  
  MQTT_connect();
  hydrate();
  OLEDDisplay();
  printValues();
}

//功能来连接和重新连接根据需要向MQTT服务器。
//如果在循环函数被调用,如果连接很会照顾。
void MQTT_connect() 
{
  int8_t ret;
 
  // 如连接则停止
  if (mqtt.connected()) {
    return;
  }
 
  Serial.print("Connecting to MQTT... ");
 
  while ((ret = mqtt.connect()) != 0) { // 连接将用于连接的返回0
       Serial.println(mqtt.connectErrorString(ret));
       Serial.println("Retrying MQTT connection in 5 seconds...");
       mqtt.disconnect();
       delay(5000);  // 等待5秒
  }
  Serial.println("MQTT Connected!");
}
void hydrate()
{
  if(soilMoisturePercent <= 45)
  {
    if((millis()-waterTime)>10000)
    {
      digitalWrite(relay,HIGH);
      delay(500);
      digitalWrite(relay,LOW);
      waterTime = millis();
    }
  }

}
void OLEDDisplay()
{      
    display.clearDisplay();
    display.setCursor(0,0);  //显示
    display.setRotation(2);
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.printf("Temp: %0.2f\nHumidity: %0.2f\nDust %0.2f\nPressure: %0.2f\n", tempF, humidRH, dustSense, inHg);
    display.printf("Air: %i\nMoist: %i%%\n", airSense, soilMoisturePercent);
    display.display();
}
void printValues() 
{
    Serial.printf("Temperature = %f\n", tempF);
    Serial.print(bme.readTemperature());
    Serial.println(" *F");
    
    Serial.printf("Pressure = %f\n", inHg);
    Serial.print(bme.readPressure());
    Serial.println(" hPa");
    Serial.printf("Humidity = %f\n", humidRH);
    Serial.print(bme.readHumidity());
    Serial.println(" %");

    Serial.println();
}

效果图:

操作视频:

只要按下按钮1,就可以将其打开或关闭(1,0),这将向继电器发送一条消息,要求给植物浇水!

 

加入微信技术交流群

技术交流,职业进阶

关注与非网服务号

获取电子工程师福利

加入电路城 QQ 交流群

与技术大牛交朋友

讨论