I continue teaching IOT with Firebase through problem solving. In this video I’ll show step by step guide to create Android App. It will be using Android studio for controlling LED which is connected to Arduino NodeMCU.
I think learning through doing is the best. The problems will keep getting more complex. The code follows the video below to help.
If you like videos like this consider donating $1, or simply turn off AdBlocker. Either helps me to continue making tutorials
Â
Firstly, 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 end up with great tutorial on IOT home automation using Firebase , NodeMCU & Android App.
where is code of ESP8266?
check out previous post
https://hackanons.com/2018/03/iot-with-firebase-control-led-using.html
I can't able to simulate output in android app
Hi Kavin
Thank you for your amazing tutorial about iot, if i want to use your tutorial for many esp8266 board what should i do ? i want to have one app and every customer by knowing something unic about their board like some serial number or MAC address or anything we can define in the board can access their specific board using firebase, i need your help please help me (my email saberisajjad14@gmail.com)
this was a really nice post. In idea I would like to put in writing like this additionally ? taking time and actual effort to make a very good article? but what can I say? I procrastinate alot and by no means seem to get something done.
kingroot apk
Thanks for sharing the details! thanks for sharing information,nice article.
i would like to more information from your side!
please added more then tips!Am working in
Excel Training In Hyderabad
what if we want to create application for controlling more than one led lights
Hi Kevin, Nice tutorial. I would like to know how to read the data from firebase when the app is loaded based on the database value? If the LED Status is 0 then need to show the ON button with LED on. OFF incase the database value is 1. How to handle this? Any help would be appreciated. Thanks in advance.
I admit, I have not been on this web page in a long time… however it was another joy to see It is such an important topic and ignored by so many, even professionals. I thank you to help making people more aware of possible issues. led work lights research by expert