Elmin Delibašić
Published © GPL3+

Smart Plant House

NXP Rapid IoT Prototype Kit + NodeMCU + Google Firebase + Android Studio.

ExpertFull instructions providedOver 1 day745

Things used in this project

Story

Read more

Custom parts and enclosures

smart_plant_house_UeD35e48cr.atmo

smart_plant_house_firmware_Es7FHTIHcn.bin

Schematics

BLE + NodeMCU

schematic

Without docker station

Code

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"
    android:background="@drawable/bckg">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:gravity="center"
        android:background="@color/background1"
        android:alpha="0.5"></RelativeLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="Smart Plant House"
        android:textColor="#d9d9d9"
        android:textSize="30dp"
        android:gravity="center"
        android:textStyle="bold" />


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="75dp"
        android:layout_marginLeft="22dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="50dp">

<!---TEMPERATURE-->
        <RelativeLayout
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="#475385"
            android:textAlignment="center"
            android:gravity="top">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Temperature"
                android:textSize="12dp"
                android:textColor="#d9d9d9"
                android:textAlignment="center"/>

            <TextView
                android:id="@+id/text_temp"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="NaN"
                android:textSize="25dp"
                android:textColor="#d9d9d9"
                android:gravity="center"/>




        </RelativeLayout>
        <!---HUMIDITY-->
        <RelativeLayout
            android:layout_marginLeft="107dp"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="#465B76"
            android:textAlignment="center"
            android:gravity="top">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Humidity"
                android:textSize="12dp"
                android:textColor="#d9d9d9"
                android:textAlignment="center"/>

            <TextView
                android:id="@+id/text_hum"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="NaN"
                android:textColor="#d9d9d9"
                android:textSize="25dp"
                android:gravity="center"/>


        </RelativeLayout>

        <!---SOIL MOISTURE-->
        <RelativeLayout
            android:layout_marginLeft="215dp"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="#456683"
            android:textAlignment="center"
            android:gravity="top">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Soil Moisture"
                android:textSize="12dp"
                android:textColor="#d9d9d9"
                android:textAlignment="center"/>
            <TextView
                android:id="@+id/text_soil"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="NaN"
                android:textColor="#d9d9d9"
                android:textSize="25dp"
                android:gravity="center"/>


        </RelativeLayout>


        <!---AMBIENT LIGHT-->
        <RelativeLayout
            android:layout_marginTop="120dp"
            android:layout_marginLeft="215dp"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="#848448"
            android:textAlignment="center"
            android:gravity="top">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Ambient Light"
                android:textSize="12dp"
                android:textColor="#d9d9d9"
          android:textAlignment="center"/>
            <TextView
                android:id="@+id/text_light"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="NaN"
                android:textColor="#d9d9d9"
                android:textSize="25dp"
                android:gravity="center"/>


        </RelativeLayout>


        <!---AIR QUALITY-->
            <RelativeLayout
                android:layout_marginTop="120dp"
                android:layout_marginLeft="107dp"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:background="#847959"
                android:textAlignment="center"
                android:gravity="top">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Air Quality"
                    android:textSize="12dp"
                    android:textColor="#d9d9d9"
                    android:textAlignment="center"/>

                <TextView
                    android:id="@+id/text_air"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="NaN"
                    android:textColor="#d9d9d9"
                    android:textSize="25dp"
                    android:gravity="center"/>


            </RelativeLayout>

        <!---PRESSURE-->
        <RelativeLayout
            android:layout_marginTop="120dp"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="#846062"
            android:textAlignment="center"
            android:gravity="top">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Pressure"
                android:textSize="12dp"
                android:textColor="#d9d9d9"
                android:textAlignment="center"/>
            <TextView
                android:id="@+id/text_press"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="NaN"
                android:textColor="#d9d9d9"
                android:textSize="25dp"
                android:gravity="center"/>



        </RelativeLayout>

        <RelativeLayout
            android:layout_width="315dp"
            android:layout_height="80dp"
            android:layout_marginTop="260dp"
            android:background="@color/background1"
            android:alpha="0.7">
            </RelativeLayout>

<RelativeLayout
    android:layout_width="315dp"
    android:layout_height="80dp"
    android:layout_marginTop="260dp"
    >

    <TextView
        android:layout_marginLeft="5dp"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Window control:"
        android:gravity="center"
        android:textColor="#d9d9d9"
        android:textSize="25dp"/>

    <Button
        android:id="@+id/btn_windowOPEN"
        android:layout_width="70dp"
        android:layout_height="40dp"
        android:text="OPEN"
        android:layout_marginLeft="210dp"
        android:gravity="center"
        android:layout_marginTop="2dp"/>
    <Button
        android:id="@+id/btn_windowCLOSE"
        android:layout_width="70dp"
        android:layout_height="40dp"
        android:text="CLOSE"
        android:layout_marginLeft="210dp"
        android:gravity="center"
        android:layout_marginTop="38dp"/>



</RelativeLayout>

        <RelativeLayout
            android:layout_width="315dp"
            android:layout_height="50dp"
            android:layout_marginTop="370dp"
            android:background="@color/background1"
            android:alpha="0.7"
            ></RelativeLayout>
<RelativeLayout
            android:layout_width="315dp"
            android:layout_height="50dp"
            android:layout_marginTop="370dp"
            >

    <TextView
        android:layout_marginLeft="5dp"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Water Pump:"
        android:textColor="#d9d9d9"
        android:gravity="center"
        android:textSize="25dp"/>

    <Button
        android:id="@+id/btn_waterpumpOFF"
        android:layout_width="50dp"
        android:layout_height="40dp"
        android:text="OFF"
        android:layout_marginLeft="200dp"
        android:gravity="center"
        android:layout_marginTop="5dp"
        />
    <Button
        android:id="@+id/btn_waterpumpON"
        android:layout_width="50dp"
        android:layout_height="40dp"
        android:text="ON"
        android:layout_marginLeft="250dp"
        android:gravity="center"
        android:layout_marginTop="5dp"/>



</RelativeLayout>

</RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginTop="510dp"
        android:gravity="center"
        android:background="@color/background1"
        android:alpha="0.5"
        ></RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginTop="510dp"
        android:gravity="center">
        <ImageView
            android:layout_width="120dp"
            android:layout_height="60dp"
            android:src="@drawable/nxprapid"
            />
    </RelativeLayout>



   </RelativeLayout>

MainActivity.java

Java
package com.example.elmin.smartplanthouse;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Switch;
import android.widget.TextView;

import com.google.firebase.FirebaseApp;
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;
import com.google.firebase.messaging.FirebaseMessaging;

public class MainActivity extends AppCompatActivity {


    DatabaseReference dref;
    TextView text_temperature, text_humidity, text_airquality, text_soilmoisture, text_pressure, text_ambientlight;
    String temperature, humidity, airquality, soilmoisture, pressure, ambientlight;
    Button btn_on1, btn_on2, btn_off2, btn_on12;

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


        btn_on1 = (Button) findViewById(R.id.btn_windowOPEN);
        btn_on2 = (Button) findViewById(R.id.btn_waterpumpON);
        btn_on12 = (Button) findViewById(R.id.btn_windowCLOSE);
        btn_off2 = (Button) findViewById(R.id.btn_waterpumpOFF);


        text_temperature = (TextView) findViewById(R.id.text_temp);
        dref = FirebaseDatabase.getInstance().getReference();
        dref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                temperature = dataSnapshot.child("temp").getValue().toString();
                text_temperature.setText(temperature + "°C");

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

        text_humidity = (TextView) findViewById(R.id.text_hum);
        dref = FirebaseDatabase.getInstance().getReference();
        dref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                humidity = dataSnapshot.child("hum").getValue().toString();
                text_humidity.setText(humidity + "%");

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

        text_airquality = (TextView) findViewById(R.id.text_air);
        dref = FirebaseDatabase.getInstance().getReference();
        dref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                airquality = dataSnapshot.child("airquality").getValue().toString();
                text_airquality.setText(airquality + "ppb");

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });


        text_pressure = (TextView) findViewById(R.id.text_press);
        dref = FirebaseDatabase.getInstance().getReference();
        dref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                pressure = dataSnapshot.child("pressure").getValue().toString();
                text_pressure.setText(pressure + "hPa");

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

        text_soilmoisture = (TextView) findViewById(R.id.text_soil);
        dref = FirebaseDatabase.getInstance().getReference();
        dref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                soilmoisture = dataSnapshot.child("soilmoisture").getValue().toString();
                text_soilmoisture.setText(soilmoisture + "rh");

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

        text_ambientlight = (TextView) findViewById(R.id.text_light);
        dref = FirebaseDatabase.getInstance().getReference();
        dref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                ambientlight = dataSnapshot.child("ambientlight").getValue().toString();
                text_ambientlight.setText(ambientlight + "lx");

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });


        btn_on2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FirebaseDatabase database = FirebaseDatabase.getInstance();
                DatabaseReference myRef = database.getReference("S2");

                myRef.setValue(1);
            }
        });

        btn_off2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FirebaseDatabase database = FirebaseDatabase.getInstance();
                DatabaseReference myRef = database.getReference("S2");

                myRef.setValue(0);
            }
        });



        btn_on1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FirebaseDatabase database = FirebaseDatabase.getInstance();
                DatabaseReference myRef = database.getReference("S1");

                myRef.setValue(1);
            }
        });


        btn_on12.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FirebaseDatabase database = FirebaseDatabase.getInstance();
                DatabaseReference myRef = database.getReference("S1");

                myRef.setValue(2);
            }
        });

    }
}

nodemcu_code_for_ble_senddata_to_firebase.ino

Arduino
If you have docker station, you can upload this code on NodeMCU.
#include <PubSubClient.h>
#include <FirebaseArduino.h>
#include  <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include <Firebase.h>
#include <FirebaseCloudMessaging.h>
#include <FirebaseError.h>
#include <FirebaseHttpClient.h>
#include <FirebaseObject.h>



#define FIREBASE_HOST "firebase_link"
#define FIREBASE_AUTH "firebase_id"

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



void setup() {  

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

//BLE read data
if (Serial.available()) {;}
float temp = Serial.read(); 
Firebase.setFloat("temp", temp);
delay(100);

float hum = Serial.read();
Firebase.setFloat("hum", hum);
delay(100);

int pressure = Serial.read();
Firebase.setInt("pressure", pressure);
delay(100);

int ambientlight = Serial.read();
Firebase.setInt("ambientlight", ambientlight);
delay(100);

int airquality = Serial.read();
Firebase.setInt("airqualtiy", airquality);
delay(100);

int soil = Serial.read();
Firebase.setInt("soilmoisture", soil);
delay(100);

int S1 = Firebase.getInt("S1");
Serial.write(S1);
delay(100);

int S2 = Firebase.getInt("S2");
Serial.write(S2)
delay(100);

}

NodeMCU_full_with_out_docker_station.ino

Arduino
If you haven't docker station, you can upload this code on NodeMCU.
#include <PubSubClient.h>
#include <FirebaseArduino.h>
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>


#define FIREBASE_HOST "firebase_link"
#define FIREBASE_AUTH "firebase_id"

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


int IN1 = 12, IN2 = 13, IN3 = 14, IN4 = 15, water_out = 5; //16,5,4,0
#define x 5000

void setup() {  

      pinMode(water_out, OUTPUT);
      pinMode(IN1, OUTPUT);
      pinMode(IN2, OUTPUT);
      pinMode(IN3, OUTPUT);
      pinMode(IN4, OUTPUT);


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

//!!!!!!!!!!!!!!!!!!BLE read data!!!!!!!!!!!!!!!!!!!!!!
if (Serial.available()) {;}
float temp = Serial.read(); 
Firebase.setFloat("temp", temp);
delay(100);

float hum = Serial.read();
Firebase.setFloat("hum", hum);
delay(100);

int pressure = Serial.read();
Firebase.setInt("pressure", pressure);
delay(100);

int ambientlight = Serial.read();
Firebase.setInt("ambientlight", ambientlight);
delay(100);

int airquality = Serial.read();
Firebase.setInt("airqualtiy", airquality);
delay(100);

// !!!!!!!!!!!!!!!!!!!!Window control!!!!!!!!!!!!!!!!!!!!!

int window = Firebase.getInt("S1");

if (window == 1) {  
for (int i=0; i<50; i++) {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  delay(5);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  delay(5);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  delay(5);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  delay(5);
  }
  Firebase.setInt("S1", 0);}
  
if (window == 2) {

  for (int i=0; i<50; i++) {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  delay(5);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  delay(5);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  delay(5);
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  delay(5);
  }
  Firebase.setInt("S1", 0);
} else {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  }

// !!!!!!!!!!!!!!!!!!water pump!!!!!!!!!!!!!!!!!!!!!!
int water_pump = Firebase.getInt("S2");

if (water_pump > 0) {
  digitalWrite(water_out, HIGH);
} else {
  digitalWrite(water_out, LOW);
}

// !!!!!!!!!!!!!!!!!!Soil Moisture!!!!!!!!!!!!!!!!!!!
float soil = digitalRead(A0);
Firebase.setFloat("soilmoisture", soil);

}

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