I continue teaching IOT with Firebase through problem solving. In this video I'll show step by step guide to create Android App using Android studio for controlling LED which is connected to Arduino NodeMCU.
I think learning through doing is the best and the problems will keep getting more complex. The code follows the video below to help.
VIDEO If you like videos like this consider donating $1 , or simply turn off AdBlocker. Either helps me to continue making tutorials
First set the internet permission for our app for which need to add this code after tag .
Manifest.xml
<manifest xmlns : android =" http :// schemas . android . com / apk / res / android "
package =" com.example.root.led_control" >
<uses-permission android : name =" android.permission.INTERNET" />
<application
android : allowBackup =" true"
android : icon =" @mipmap/ic_launcher"
android : label =" @string/app_name"
android : roundIcon =" @mipmap/ic_launcher_round"
android : supportsRtl =" true"
android : theme =" @style/AppTheme" >
<activity android : name =" .MainActivity" >
<intent-filter >
<action android : name =" android.intent.action.MAIN" />
<category android : name =" android.intent.category.LAUNCHER" />
</intent-filter >
</activity >
</application >
</manifest >
After setting the permission we need to create layout for our application so let's check out XML code.
activity_main.xml
<RelativeLayout
xmlns : android =" http :// schemas . android . com / apk / res / android "
xmlns : tools =" http :// schemas . android . com / tools "
xmlns : app =" http :// schemas . android . com / apk / res - auto "
android : layout_width =" match_parent"
android : layout_height =" match_parent"
tools : context =" com.example.root.led_control.MainActivity" >
<TextView
android : layout_width =" wrap_content"
android : layout_height =" wrap_content"
android : layout_alignParentTop =" true"
android : layout_centerHorizontal =" true"
android : layout_marginTop =" 41dp"
android : text =" IOT CONTROL LED"
android : textSize =" 30sp"
app : layout_constraintBottom_toBottomOf =" parent"
app : layout_constraintLeft_toLeftOf =" parent"
app : layout_constraintRight_toRightOf =" parent"
app : layout_constraintTop_toTopOf =" parent"
android : id =" @+id/textView" />
<Button
android : id =" @+id/on"
android : layout_width =" wrap_content"
android : layout_height =" wrap_content"
android : layout_alignParentLeft =" true"
android : layout_alignParentStart =" true"
android : layout_centerVertical =" true"
android : layout_marginLeft =" 36dp"
android : layout_marginStart =" 36dp"
android : text =" ON" />
<Button
android : id =" @+id/off"
android : layout_width =" wrap_content"
android : layout_height =" wrap_content"
android : text =" OFF"
android : layout_alignBaseline =" @+id/on"
android : layout_alignBottom =" @+id/on"
android : layout_alignRight =" @+id/textView"
android : layout_alignEnd =" @+id/textView" />
</RelativeLayout >
Now, at last here is our java code i.e Main_Activity.
Main Activity.java
package com.example.root.led_control ;
import android.os.Bundle ;
import android.support.v7.app.AppCompatActivity ;
import android.view.View ;
import android.widget.Button ;
import com.google.firebase.database.DatabaseReference ;
import com.google.firebase.database.FirebaseDatabase ;
public class MainActivity extends AppCompatActivity {
Button on;
Button off;
@Override
protected void onCreate (Bundle savedInstanceState ) {
super . onCreate(savedInstanceState);
setContentView(R . layout. activity_main);
on = (Button ) findViewById(R . id. on);
off = (Button ) findViewById(R . id. off);
on. setOnClickListener(new View .OnClickListener (){
@Override
public void onClick (View view ) {
FirebaseDatabase database = FirebaseDatabase . getInstance();
DatabaseReference myRef = database. getReference(" LED_STATUS" );
myRef. setValue(1 );
}
});
off. setOnClickListener(new View .OnClickListener (){
@Override
public void onClick (View view ) {
FirebaseDatabase database = FirebaseDatabase . getInstance();
DatabaseReference myRef = database. getReference(" LED_STATUS" );
myRef. setValue(0 );
}
});
}
}
At last we are end up with great tutorial on IOT home automation using Firebase , NodeMCU & Android App.
沒有留言:
張貼留言