Elmin D
Published © GPL3+

Weather Station and LED Control

NodeMCU + Google Firebase + Android studio.

IntermediateFull instructions provided5 hours27
Weather Station and LED Control

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
LED (generic)
LED (generic)
×2
Resistor 100 ohm
Resistor 100 ohm
×2
DHT11 Temperature & Humidity Sensor (3 pins)
DHT11 Temperature & Humidity Sensor (3 pins)
×1
Grove - Gas Sensor(MQ2)
Seeed Grove - Gas Sensor(MQ2)
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Firebase
Google Firebase
Android Studio
Android Studio

Story

Read more

Schematics

schematic

Schema zyrnywvezf

Code

project_arduino.ino

Arduino
Remember:
Insert link and secret code from your Google Firebase.
Insert name and password for your WiFi.
//#include <Adafruit_Sensor.h>
#include <PubSubClient.h>
#include <FirebaseArduino.h>
#include  <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include <dhtnew.h>
#include <Firebase.h>
#include <FirebaseCloudMessaging.h>
#include <FirebaseError.h>
#include <FirebaseHttpClient.h>
#include <FirebaseObject.h>

#define FIREBASE_HOST "link_your_firebase"
#define FIREBASE_AUTH "secret_code"


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


DHTNEW mySensor(2);


#define led1 4
#define led2 13

void setup() {
    pinMode(led1, OUTPUT);
    pinMode(led2, 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() {
//Led number 1

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

      if ( i1 == 0)
            {
               digitalWrite(led1, LOW);
            }
      else
            {
               digitalWrite(led1, HIGH);
            }

//Led number 2

  int i2 = Firebase.getInt("S2");

      if ( i2 == 0)
            {
               digitalWrite(led2, LOW);
            }
      else
            {
               digitalWrite(led2, HIGH);
             }



//Temperature and humidity
        mySensor.read();
          float t = mySensor.temperature;
          float h = mySensor.humidity;

          Firebase.setFloat("temp", t);
          Firebase.setFloat("hum", h);

         delay(1000);

 //Air quality

          float zag = analogRead(A0);

          Firebase.setFloat("zag", zag);
}

activity_main.xml

XML
Android Studio XML file
<?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">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="PROJECT BY: Elmin Delibašić"
        android:textSize="20dp"
        android:gravity="center"
        android:layout_marginTop="470dp"
        android:alpha="0.5"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Smart Weather"
        android:textSize="50dp"
        android:gravity="center"
        android:layout_marginTop="10dp"
        />

    <Button
        android:id="@+id/button_temp"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="100dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="40dp"
        android:gravity="top"
        android:text="TEMPERATURE"
        android:textStyle="bold"
        android:textSize="20dp"
        android:textAlignment="center"
        android:background="#ffc299"
        android:alpha="0.80"/>
    <Button
        android:id="@+id/button_hum"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="200dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="40dp"
        android:background="#8080ff"
        android:text="HUMIDITY"
        android:textSize="20dp"
        android:textStyle="bold"
        android:gravity="top"
        android:textAlignment="center"
        android:alpha="0.80"
        />
    <Button
        android:id="@+id/button_led"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_marginTop="300dp"
        android:layout_marginLeft="200dp"
        android:layout_marginRight="20dp"
        android:text="LED CONTROL"
        android:textStyle="bold"
        android:gravity="center"
        android:textSize="20dp"
        android:textAlignment="center"
        android:background="#ffff99"
        android:alpha="0.80"
        />
    <Button
        android:id="@+id/button_air"
        android:textAlignment="center"
        android:background="#ff8080"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_marginTop="300dp"
        android:layout_marginLeft="25dp"
        android:text="AIR QUALITY"
        android:textStyle="bold"
        android:textSize="20dp"
        android:layout_marginRight="20dp"
        android:scaleType="centerInside"
        android:gravity="top"
        android:alpha="0.80" />


    <TextView
        android:id="@+id/text_temperature"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:textSize="50dp"
        android:text="NaN"
        android:layout_marginTop="150dp"
        android:layout_marginLeft="25dp"
        android:textAlignment="center"
        />
    <TextView
        android:id="@+id/text_humidity"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:textSize="50dp"
        android:text="NaN"
        android:layout_marginTop="150dp"
        android:layout_marginLeft="200dp"
        android:textAlignment="center"/>
    <TextView
        android:id="@+id/text_airquality"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:textSize="50dp"
        android:text="NaN"
        android:layout_marginTop="350dp"
        android:layout_marginLeft="25dp"
        android:textAlignment="center"/>




</RelativeLayout>

activity_led.xml

XML
Android Studio XML file for LED CONTROL tab
<?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=".ActivityLed">


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginTop="100dp"
            android:gravity="center">

            <TextView
                android:layout_width="150dp"
                android:layout_height="60dp"
                android:text="LED 1:"
                android:textSize="35dp"
                android:textStyle="bold"
                android:textColor="@color/colorPrimary"
                android:layout_marginLeft="5dp"
                android:id="@+id/text_11"
                />


            <Button
                android:id="@+id/button_on1"
                android:layout_width="100dp"
                android:layout_height="50dp"
                android:text="ON"
                android:layout_toRightOf="@id/text_11"/>

            <Button
                android:id="@+id/button_off1"
                android:layout_width="100dp"
                android:layout_height="50dp"
                android:layout_marginLeft="10dp"
                android:layout_toRightOf="@id/button_on1"
                android:text="OFF" />

        </RelativeLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginTop="200dp"
            android:gravity="center">
            <TextView
                android:layout_width="150dp"
                android:layout_height="60dp"
                android:text="LED 2:"
                android:textSize="35dp"
                android:textStyle="bold"
                android:textColor="@color/colorPrimary"
                android:layout_marginLeft="5dp"
                android:id="@+id/text_12"
                />


            <Button
                android:id="@+id/button_on2"
                android:layout_width="100dp"
                android:layout_height="50dp"
                android:text="ON"
                android:layout_toRightOf="@id/text_12"/>

            <Button
                android:id="@+id/button_off2"
                android:layout_width="100dp"
                android:layout_height="50dp"
                android:layout_marginLeft="10dp"
                android:layout_toRightOf="@id/button_on2"
                android:text="OFF" />



    </RelativeLayout>


</RelativeLayout>

MainActivity.java

Java
package com.example.elmin.myapplication;


import android.content.Intent;
import android.media.Image;
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.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 {


    private Button btn_led;
    DatabaseReference dref;
    TextView text_temp;
    TextView text_hum;
    TextView text_air;
    String air_quality;
    String humidity;
    String temperature;

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



        btn_led = (Button) findViewById(R.id.button_led);


        btn_led.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openLed();
            }
        });


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

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

            }

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

            }
        });

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

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

            }

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

            }
        });

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

                air_quality = dataSnapshot.child("zag").getValue().toString();
                text_air.setText(air_quality);

            }

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

            }
        });


    }
    public void openLed () {

        Intent intent = new Intent(this, ActivityLed.class);
        startActivity(intent);
    }

    }

ActivityLed.java

Java
package com.example.elmin.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class ActivityLed extends AppCompatActivity {



        Button on1, off1, on2, off2;

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


            on1 = (Button) findViewById(R.id.button_on1);
            off1 = (Button) findViewById(R.id.button_off1);
            on2 = (Button) findViewById(R.id.button_on2);
            off2 = (Button) findViewById(R.id.button_off2);


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

                    myRef.setValue(1);
                }
            });

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

                    myRef.setValue(0);
                }
            });


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

                    myRef.setValue(1);
                }
            });

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

                    myRef.setValue(0);
                }
            });
        }

}

Credits

Elmin D

Elmin D

0 projects • 0 followers
I am a student at the Faculty of Electrical Engineering. I gained knowledge of hardware and programming at faculty.

Comments

Add projectSign up / Login