Elmin Delibašić
Published © GPL3+

GreenHouse Monitoring System

Android application monitoring system for GreenHouse, several sensors for reading data and send to application.

IntermediateFull instructions provided10 hours34,454

Things used in this project

Story

Read more

Schematics

Scheme

Code

nodemcu.ino

Arduino
#include <PubSubClient.h>
#include <FirebaseArduino.h>
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include <dhtnew.h>

#define FIREBASE_HOST "firebase_link"
#define FIREBASE_AUTH "firebase_secret"

#define WIFI_SSID "wifi_name"
#define WIFI_PASSWORD "wifi_password"

int soilPin = 5, airPin = A0, dhtPin = 16;

DHTNEW dhtsensor(dhtPin);

void setup() {
  //Serial Begin at 9600 Baud 
  pinMode(soilPin, INPUT);
  pinMode(airPin, INPUT);
  
  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() {

int soilData = digitalRead(soilPin);
Firebase.setInt("soilData", soilData);

int airData = analogRead(airPin);
Firebase.setInt("airData", airData);

dhtsensor.read();
float t = dhtsensor.temperature;
float h = dhtsensor.humidity;
Firebase.setFloat("t", t);
Firebase.setFloat("h", h);


delay(2000);




}

MainActivity.java

Java
package com.example.elmin.plastenik;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class MainActivity extends AppCompatActivity {


    DatabaseReference dref;
    TextView text_temperature, text_humidity, text_airquality, text_soilmoisture;
    String temp, hum, air, soilmoisture;
    int col1 = Color.parseColor("#b32d00"), col2 = Color.parseColor("#000000");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        text_temperature = (TextView) findViewById(R.id.textTemp);
        text_airquality = (TextView) findViewById(R.id.textAir);
        text_humidity = (TextView) findViewById(R.id.textHum);
        text_soilmoisture = (TextView) findViewById(R.id.textSoil);



        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) {

            }
        });

        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) {

            }
        });
    }
}

activity_main.xml

XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


  <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="75dp"
      android:layout_marginTop="10dp"
      android:layout_marginLeft="20dp"
      android:layout_marginRight="20dp"
      android:background="#f3f4ee">
      <TextView
          android:layout_width="150dp"
          android:layout_height="40dp"
          android:layout_marginTop="15dp"
          android:layout_marginLeft="50dp"
          android:textSize="16dp"
          android:gravity="center"
          android:text="Temperature"
          android:textColor="#000000"
          android:alpha="0.8"/>
      <ImageView
          android:layout_width="40dp"
          android:layout_height="40dp"
          android:layout_marginLeft="5dp"
          android:layout_marginTop="15dp"
          android:src="@drawable/temperature"
          android:alpha="0.8"/>
        <TextView
            android:id="@+id/textTemp"
            android:layout_width="150dp"
            android:layout_height="50dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="200dp"
            android:gravity="center"
            android:textSize="40dp"
            android:text="N/A"
            android:textColor="#000000"
            android:alpha="0.8"
            />
  </RelativeLayout>


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="75dp"
        android:layout_marginTop="95dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"


        android:background="#f3f4ee">
        <TextView
            android:layout_width="150dp"
            android:layout_height="40dp"
            android:layout_marginTop="15dp"
            android:layout_marginLeft="50dp"
            android:textSize="16dp"
            android:gravity="center"
            android:text="Humidity"
            android:textColor="#000000"
            android:alpha="0.8"/>
        <ImageView
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="15dp"
            android:src="@drawable/humidity"
            android:alpha="0.8"/>
        <TextView
            android:id="@+id/textHum"
            android:layout_width="150dp"
            android:layout_height="50dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="200dp"
            android:gravity="center"
            android:textSize="40dp"
            android:text="N/A"
            android:textColor="#000000"
            android:alpha="0.8"
            />
    </RelativeLayout>



    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="180dp"
        android:layout_marginBottom="20dp"
        android:background="#f3f4ee"
        >
        <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="25dp"
            android:src="@drawable/soil"
            android:alpha="0.8"/>
        <TextView
            android:id="@+id/textSoil"
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="130dp"
            android:gravity="center"
            android:textAlignment="center"
            android:textSize="25dp"
            android:text="N/A"
            android:textColor="#000000"
            android:alpha="0.8"
            />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="310dp"
        android:layout_marginBottom="20dp"
        android:background="#f3f4ee"
        >
        <TextView
            android:layout_width="90dp"
            android:layout_height="40dp"
            android:layout_marginTop="30dp"
            android:layout_marginLeft="55dp"
            android:textSize="16dp"
            android:gravity="center"
            android:text="Air Quality"
            android:textColor="#000000"
            android:alpha="0.8"/>
        <ImageView
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="30dp"
            android:src="@drawable/airquality"
            android:alpha="0.8"/>
        <TextView
            android:id="@+id/textAir"
            android:layout_width="150dp"
            android:layout_height="50dp"
            android:layout_marginTop="90dp"
            android:textAlignment="center"
            android:textSize="40dp"
            android:text="N/A"
            android:textColor="#000000"
            />
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="150dp"
            android:src="@drawable/kvalitetazraka"/>
    </RelativeLayout>


</RelativeLayout>

Credits

Elmin Delibašić

Elmin Delibašić

12 projects • 81 followers
Bachelor of Electrical Engineering || Enthusiast for AI and IoT || Master Student at Faculty of Electrical Engineering

Comments