This project helps you to look at your greenhouse. It consists of sensors:
- DHT11 (temperature and humidity),
- MQ-135 (Air Quality),
- Soil Moisture.
- The next few steps will explain how to make the system.
Step 1: Scheme- DHT11 sensor data pin is connected to NodeMCU via D0 pin.
- Soil Moisture sensor data pin is connected to NodeMCU via D1 pin.
- MQ-135 sensor data pin is connected to NodeMCU via A0 pin.
- VCC pin on sensors is connect to VIN pin on NodeMCU and GND pin is connect to GND pin on NodeMCU.
- The required libraries are:
Step 3: Google FirebaseGoogle Firebase serves to store data collected on the NodeMCU. These datas can be further used on websites, mobile applications and anywhere they can access the Internet. Sign in using your Google Account and follow the steps below.
After login follow the next steps:
- Click on "+ Add project"
- Fill information and click "Create"
- After loading, click "Develop" in left navigation bar. Then click "Database"
- Click "Create database" then check "Start in test mode" and click "Enable"
- Next to "Database" title in the drop-down menu, select "Realtime Database"
- Click on "Roles" tab. In code delete "false" and add "true".
- Back to "Data" tab. Copy link of your database and insert in Arduino code.
- Click on icon gear (left navigation bar) choose "Projects settings" and click on "Service accounts" choose "Database secrets".
- On right copy "Secret" code and insert in Arduino code.
Now, we've connected NodeMCU and Google Firebase.
Step 4: NodeMCU- NodeMCU is a board that has the ability to connect to the Internet. In addition, there are several digital pins and one analog pin. It is excellent for projects that need to connect to the Internet.
- The code required to connect to the Internet and connect to Google Firebase is shown below:
#include <PubSubClient.h>
#include <FirebaseArduino.h>
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#define FIREBASE_HOST "firebase_link"
#define FIREBASE_AUTH "firebase_secretcode"
#define WIFI_SSID "wifi_name"
#define WIFI_PASSWORD "wifi_password"
void setup() {
Serial.begin(9600);
WiFi.begin (WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println ("");
Serial.println ("WiFi Connected!");
Serial.println(WiFi.localIP());
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
}
void loop() {
}
Step 5: DHT11 sensor- Connecting the dhtnew.h library and pin to which the data pin of the sensor is connected is done using the following command:
int dhtPin = 0;
DHTNEW dhtsensor(dhtPin);
- Reading the temperature and humidity is done using this command:
dhtsensor.read();
float t = dhtsensor.temperature;
float h = dhtsensor.humidity;
- Finally sending data to Google Firebase is done with using this command:
Firebase.setFloat("t", t);
Firebase.setFloat("h", h);
Step 6: Soil moisture sensor- The intialization of the pin for read value of soil moisture data is done with using this command:
int soilPin = 1;
Reading data and sending to Google Firebase is done with using this commands:
int soilData = digitalRead(soilPin);
Firebase.setInt("soilData", soilData);
Step 7: MQ-135 sensor- The intialization of the pin for read value of air quality data is done with using this command:
int airPin = A0;
- Reading data and sending to Google Firebase is done with using this commands:
int airData = analogRead(airPin);
Firebase.setInt("airData", airData);
Step 8: Android application- The application was created in the android studio. The part that connects the app from Google Firebase from where the sensor data is taken from is shown below.
- For Temperature, Humidity and Air Quality:
dref = FirebaseDatabase.getInstance().getReference();
dref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
temp = dataSnapshot.child("t").getValue().toString();
text_temperature.setText(temp + "°C");
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
dref = FirebaseDatabase.getInstance().getReference();
dref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
hum = dataSnapshot.child("h").getValue().toString();
text_humidity.setText(hum + "%");
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
dref = FirebaseDatabase.getInstance().getReference();
dref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
air = dataSnapshot.child("airData").getValue().toString();
text_airquality.setText(air);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
- For Soil Moisture(if-else is used to decides whether watering is necessary or not):
dref = FirebaseDatabase.getInstance().getReference();
dref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
soilmoisture = dataSnapshot.child("soilData").getValue().toString();
int soilData = Integer.parseInt(soilmoisture);
if (soilData == 0) {
text_soilmoisture.setText("No watering required.");
text_soilmoisture.setTextColor(col2);
}
else {
text_soilmoisture.setText("Watering required!");
text_soilmoisture.setTextColor(col1);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
- Part of the code for linking text that is written in activity_main.xml (displayed in the application) and Google Firebase is in attachments with name MainActivity.java. Also the xml file is in attachments.
My GreenHouse Monitoring SystemPower for my system is power bank. Power bank + is connect to VIN on NodeMCU and - is connect to GND on NodeMCU.
Comments